FormValidator.class.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class FormValidator
  5. * create/manipulate/validate user input.
  6. */
  7. class FormValidator extends HTML_QuickForm
  8. {
  9. const LAYOUT_HORIZONTAL = 'horizontal';
  10. const LAYOUT_INLINE = 'inline';
  11. const LAYOUT_BOX = 'box';
  12. const LAYOUT_BOX_NO_LABEL = 'box-no-label';
  13. public $with_progress_bar = false;
  14. private $layout;
  15. /**
  16. * Constructor.
  17. *
  18. * @param string $name Name of the form
  19. * @param string $method (optional) Method ('post' (default) or 'get')
  20. * @param string $action (optional) Action (default is $PHP_SELF)
  21. * @param string $target (optional) Form's target defaults to '_self'
  22. * @param mixed $attributes (optional) Extra attributes for <form> tag
  23. * @param string $layout
  24. * @param bool $trackSubmit (optional) Whether to track if the form was
  25. * submitted by adding a special hidden field (default = true)
  26. */
  27. public function __construct(
  28. $name,
  29. $method = 'post',
  30. $action = '',
  31. $target = '',
  32. $attributes = [],
  33. $layout = self::LAYOUT_HORIZONTAL,
  34. $trackSubmit = true
  35. ) {
  36. // Default form class.
  37. if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) {
  38. $attributes['class'] = 'form-horizontal';
  39. }
  40. if (isset($attributes['class']) && strpos($attributes['class'], 'form-search') !== false) {
  41. $layout = 'inline';
  42. }
  43. $this->setLayout($layout);
  44. switch ($layout) {
  45. case self::LAYOUT_HORIZONTAL:
  46. $attributes['class'] = 'form-horizontal';
  47. break;
  48. case self::LAYOUT_INLINE:
  49. case self::LAYOUT_BOX:
  50. $attributes['class'] = 'form-inline';
  51. break;
  52. }
  53. parent::__construct($name, $method, $action, $target, $attributes, $trackSubmit);
  54. // Modify the default templates
  55. $renderer = &$this->defaultRenderer();
  56. // Form template
  57. $formTemplate = $this->getFormTemplate();
  58. $renderer->setFormTemplate($formTemplate);
  59. // Element template
  60. if (isset($attributes['class']) && $attributes['class'] == 'form-inline') {
  61. $elementTemplate = ' {label} {element} ';
  62. $renderer->setElementTemplate($elementTemplate);
  63. } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') {
  64. $elementTemplate = ' {label} {element} ';
  65. $renderer->setElementTemplate($elementTemplate);
  66. } else {
  67. $renderer->setElementTemplate($this->getDefaultElementTemplate());
  68. // Display a gray div in the buttons
  69. $templateSimple = '<div class="form-actions">{label} {element}</div>';
  70. $renderer->setElementTemplate($templateSimple, 'submit_in_actions');
  71. //Display a gray div in the buttons + makes the button available when scrolling
  72. $templateBottom = '<div class="form-actions bottom_actions bg-form">{label} {element}</div>';
  73. $renderer->setElementTemplate($templateBottom, 'submit_fixed_in_bottom');
  74. //When you want to group buttons use something like this
  75. /* $group = array();
  76. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  77. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  78. $form->addGroup($group, 'buttons_in_action');
  79. */
  80. $renderer->setElementTemplate($templateSimple, 'buttons_in_action');
  81. $templateSimpleRight = '<div class="form-actions"> <div class="pull-right">{label} {element}</div></div>';
  82. $renderer->setElementTemplate($templateSimpleRight, 'buttons_in_action_right');
  83. }
  84. //Set Header template
  85. $renderer->setHeaderTemplate('<legend>{header}</legend>');
  86. //Set required field template
  87. $this->setRequiredNote(
  88. '<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>'
  89. );
  90. $noteTemplate = <<<EOT
  91. <div class="form-group">
  92. <div class="col-sm-offset-2 col-sm-10">{requiredNote}</div>
  93. </div>
  94. EOT;
  95. $renderer->setRequiredNoteTemplate($noteTemplate);
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function getFormTemplate()
  101. {
  102. return '<form{attributes}>
  103. <fieldset>
  104. {content}
  105. </fieldset>
  106. {hidden}
  107. </form>';
  108. }
  109. /**
  110. * @todo this function should be added in the element class
  111. *
  112. * @return string
  113. */
  114. public function getDefaultElementTemplate()
  115. {
  116. return '
  117. <div class="form-group {error_class}">
  118. <label {label-for} class="col-sm-2 control-label {extra_label_class}" >
  119. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  120. {label}
  121. </label>
  122. <div class="col-sm-8">
  123. {icon}
  124. {element}
  125. <!-- BEGIN label_2 -->
  126. <p class="help-block">{label_2}</p>
  127. <!-- END label_2 -->
  128. <!-- BEGIN error -->
  129. <span class="help-inline help-block">{error}</span>
  130. <!-- END error -->
  131. </div>
  132. <div class="col-sm-2">
  133. <!-- BEGIN label_3 -->
  134. {label_3}
  135. <!-- END label_3 -->
  136. </div>
  137. </div>';
  138. }
  139. /**
  140. * @return string
  141. */
  142. public function getLayout()
  143. {
  144. return $this->layout;
  145. }
  146. /**
  147. * @param string $layout
  148. */
  149. public function setLayout($layout)
  150. {
  151. $this->layout = $layout;
  152. }
  153. /**
  154. * Adds a text field to the form.
  155. * A trim-filter is attached to the field.
  156. *
  157. * @param string|array $label The label for the form-element
  158. * @param string $name The element name
  159. * @param bool $required (optional) Is the form-element required (default=true)
  160. * @param array $attributes (optional) List of attributes for the form-element
  161. *
  162. * @return HTML_QuickForm_text
  163. */
  164. public function addText($name, $label, $required = true, $attributes = [], $createElement = false)
  165. {
  166. if ($createElement) {
  167. $element = $this->createElement('text', $name, $label, $attributes);
  168. } else {
  169. $element = $this->addElement('text', $name, $label, $attributes);
  170. }
  171. $this->applyFilter($name, 'trim');
  172. if ($required) {
  173. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  174. }
  175. return $element;
  176. }
  177. /**
  178. * Add hidden course params.
  179. */
  180. public function addCourseHiddenParams()
  181. {
  182. $this->addHidden('cidReq', api_get_course_id());
  183. $this->addHidden('id_session', api_get_session_id());
  184. }
  185. /**
  186. * The "date_range_picker" element creates 2 hidden fields
  187. * "elementName" + "_start" and "elementName" + "_end"
  188. * For example if the name is "range", you will have 2 new fields
  189. * when executing $form->getSubmitValues()
  190. * "range_start" and "range_end".
  191. *
  192. * @param string $name
  193. * @param string $label
  194. * @param bool $required
  195. * @param array $attributes
  196. */
  197. public function addDateRangePicker($name, $label, $required = true, $attributes = [])
  198. {
  199. $this->addElement('date_range_picker', $name, $label, $attributes);
  200. $this->addElement('hidden', $name.'_start');
  201. $this->addElement('hidden', $name.'_end');
  202. if ($required) {
  203. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  204. }
  205. }
  206. /**
  207. * @param string $name
  208. * @param string $label
  209. * @param array $attributes
  210. *
  211. * @return mixed
  212. */
  213. public function addDatePicker($name, $label, $attributes = [])
  214. {
  215. return $this->addElement('DatePicker', $name, $label, $attributes);
  216. }
  217. /**
  218. * @param string $name
  219. * @param string $label
  220. * @param array $attributes
  221. *
  222. * @return mixed
  223. */
  224. public function addSelectLanguage($name, $label, $options = [], $attributes = [])
  225. {
  226. return $this->addElement('SelectLanguage', $name, $label, $options, $attributes);
  227. }
  228. /**
  229. * @param string $name
  230. * @param string $label
  231. * @param array $options
  232. * @param array $attributes
  233. *
  234. * @throws Exception
  235. *
  236. * @return HTML_QuickForm_element
  237. */
  238. public function addSelectAjax($name, $label, $options = [], $attributes = [])
  239. {
  240. if (!isset($attributes['url'])) {
  241. throw new \Exception('select_ajax needs an URL');
  242. }
  243. return $this->addElement(
  244. 'select_ajax',
  245. $name,
  246. $label,
  247. $options,
  248. $attributes
  249. );
  250. }
  251. /**
  252. * @param string $name
  253. * @param string|array $label
  254. * @param array $attributes
  255. *
  256. * @return DateTimePicker
  257. */
  258. public function addDateTimePicker($name, $label, $attributes = [])
  259. {
  260. return $this->addElement('DateTimePicker', $name, $label, $attributes);
  261. }
  262. /**
  263. * @param string $name
  264. * @param string|array $label
  265. * @param array $attributes
  266. *
  267. * @return DateTimeRangePicker
  268. */
  269. public function addDateTimeRangePicker($name, $label, $attributes = [])
  270. {
  271. return $this->addElement('DateTimeRangePicker', $name, $label, $attributes);
  272. }
  273. /**
  274. * @param string $name
  275. * @param string $value
  276. * @param array $attributes
  277. */
  278. public function addHidden($name, $value, $attributes = [])
  279. {
  280. $this->addElement('hidden', $name, $value, $attributes);
  281. }
  282. /**
  283. * @param string $name
  284. * @param string|array $label
  285. * @param array $attributes
  286. * @param bool $required
  287. *
  288. * @return HTML_QuickForm_textarea
  289. */
  290. public function addTextarea($name, $label, $attributes = [], $required = false)
  291. {
  292. $element = $this->addElement('textarea', $name, $label, $attributes);
  293. if ($required) {
  294. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  295. }
  296. return $element;
  297. }
  298. /**
  299. * @param string $name
  300. * @param string $label
  301. * @param string $icon font-awesome
  302. * @param string $style default|primary|success|info|warning|danger|link
  303. * @param string $size large|default|small|extra-small
  304. * @param string $class Example plus is transformed to icon fa fa-plus
  305. * @param array $attributes
  306. * @param bool $createElement
  307. *
  308. * @return HTML_QuickForm_button
  309. */
  310. public function addButton(
  311. $name,
  312. $label,
  313. $icon = 'check',
  314. $style = 'default',
  315. $size = 'default',
  316. $class = null,
  317. $attributes = [],
  318. $createElement = false
  319. ) {
  320. if ($createElement) {
  321. return $this->createElement(
  322. 'button',
  323. $name,
  324. $label,
  325. $icon,
  326. $style,
  327. $size,
  328. $class,
  329. $attributes
  330. );
  331. }
  332. return $this->addElement(
  333. 'button',
  334. $name,
  335. $label,
  336. $icon,
  337. $style,
  338. $size,
  339. $class,
  340. $attributes
  341. );
  342. }
  343. /**
  344. * Returns a button with the primary color and a check mark.
  345. *
  346. * @param string $label Text appearing on the button
  347. * @param string $name Element name (for form treatment purposes)
  348. * @param bool $createElement Whether to use the create or add method
  349. *
  350. * @return HTML_QuickForm_button
  351. */
  352. public function addButtonSave($label, $name = 'submit', $createElement = false)
  353. {
  354. return $this->addButton(
  355. $name,
  356. $label,
  357. 'check',
  358. 'primary',
  359. null,
  360. null,
  361. [],
  362. $createElement
  363. );
  364. }
  365. /**
  366. * Returns a cancel button.
  367. *
  368. * @param string $label Text appearing on the button
  369. * @param string $name Element name (for form treatment purposes)
  370. * @param bool $createElement Whether to use the create or add method
  371. *
  372. * @return HTML_QuickForm_button
  373. */
  374. public function addButtonCancel($label, $name = 'submit', $createElement = false)
  375. {
  376. return $this->addButton(
  377. $name,
  378. $label,
  379. 'times',
  380. 'danger',
  381. null,
  382. null,
  383. [],
  384. $createElement
  385. );
  386. }
  387. /**
  388. * Returns a button with the primary color and a "plus" icon.
  389. *
  390. * @param string $label Text appearing on the button
  391. * @param string $name Element name (for form treatment purposes)
  392. * @param bool $createElement Whether to use the create or add method
  393. * @param array $attributes Additional attributes
  394. *
  395. * @return HTML_QuickForm_button
  396. */
  397. public function addButtonCreate($label, $name = 'submit', $createElement = false, $attributes = [])
  398. {
  399. return $this->addButton(
  400. $name,
  401. $label,
  402. 'plus',
  403. 'primary',
  404. null,
  405. null,
  406. $attributes,
  407. $createElement
  408. );
  409. }
  410. /**
  411. * Returns a button with the primary color and a pencil icon.
  412. *
  413. * @param string $label Text appearing on the button
  414. * @param string $name Element name (for form treatment purposes)
  415. * @param bool $createElement Whether to use the create or add method
  416. *
  417. * @return HTML_QuickForm_button
  418. */
  419. public function addButtonUpdate($label, $name = 'submit', $createElement = false)
  420. {
  421. return $this->addButton(
  422. $name,
  423. $label,
  424. 'pencil',
  425. 'primary',
  426. null,
  427. null,
  428. [],
  429. $createElement
  430. );
  431. }
  432. /**
  433. * Returns a button with the danger color and a trash icon.
  434. *
  435. * @param string $label Text appearing on the button
  436. * @param string $name Element name (for form treatment purposes)
  437. * @param bool $createElement Whether to use the create or add method
  438. *
  439. * @return HTML_QuickForm_button
  440. */
  441. public function addButtonDelete($label, $name = 'submit', $createElement = false)
  442. {
  443. return $this->addButton(
  444. $name,
  445. $label,
  446. 'trash',
  447. 'danger',
  448. null,
  449. null,
  450. [],
  451. $createElement
  452. );
  453. }
  454. /**
  455. * Returns a move style button.
  456. *
  457. * @param string $label Text appearing on the button
  458. * @param string $name Element name (for form treatment purposes)
  459. * @param bool $createElement Whether to use the create or add method
  460. *
  461. * @return HTML_QuickForm_button
  462. */
  463. public function addButtonMove($label, $name = 'submit', $createElement = false)
  464. {
  465. return $this->addButton(
  466. $name,
  467. $label,
  468. 'arrow-circle-right',
  469. 'primary',
  470. null,
  471. null,
  472. [],
  473. $createElement
  474. );
  475. }
  476. /**
  477. * Returns a button with the primary color and a paper-plane icon.
  478. *
  479. * @param string $label Text appearing on the button
  480. * @param string $name Element name (for form treatment purposes)
  481. * @param bool $createElement Whether to use the create or add method
  482. * @param array $attributes
  483. *
  484. * @return HTML_QuickForm_button
  485. */
  486. public function addButtonSend($label, $name = 'submit', $createElement = false, $attributes = [])
  487. {
  488. return $this->addButton(
  489. $name,
  490. $label,
  491. 'paper-plane',
  492. 'primary',
  493. null,
  494. null,
  495. $attributes,
  496. $createElement
  497. );
  498. }
  499. /**
  500. * Returns a button with the default (grey?) color and a magnifier icon.
  501. *
  502. * @param string $label Text appearing on the button
  503. * @param string $name Element name (for form treatment purposes)
  504. *
  505. * @return HTML_QuickForm_button
  506. */
  507. public function addButtonSearch($label = null, $name = 'submit')
  508. {
  509. if (empty($label)) {
  510. $label = get_lang('Search');
  511. }
  512. return $this->addButton($name, $label, 'search', 'default');
  513. }
  514. /**
  515. * Returns a button with the primary color and a right-pointing arrow icon.
  516. *
  517. * @param string $label Text appearing on the button
  518. * @param string $name Element name (for form treatment purposes)
  519. * @param array $attributes Additional attributes
  520. *
  521. * @return HTML_QuickForm_button
  522. */
  523. public function addButtonNext($label, $name = 'submit', $attributes = [])
  524. {
  525. return $this->addButton(
  526. $name,
  527. $label,
  528. 'arrow-right',
  529. 'primary',
  530. null,
  531. null,
  532. $attributes
  533. );
  534. }
  535. /**
  536. * Returns a button with the primary color and a check mark icon.
  537. *
  538. * @param string $label Text appearing on the button
  539. * @param string $name Element name (for form treatment purposes)
  540. * @param bool $createElement Whether to use the create or add method
  541. *
  542. * @return HTML_QuickForm_button
  543. */
  544. public function addButtonImport($label, $name = 'submit', $createElement = false)
  545. {
  546. return $this->addButton(
  547. $name,
  548. $label,
  549. 'check',
  550. 'primary',
  551. null,
  552. null,
  553. [],
  554. $createElement
  555. );
  556. }
  557. /**
  558. * Returns a button with the primary color and a check-mark icon.
  559. *
  560. * @param string $label Text appearing on the button
  561. * @param string $name Element name (for form treatment purposes)
  562. * @param bool $createElement Whether to use the create or add method
  563. *
  564. * @return HTML_QuickForm_button
  565. */
  566. public function addButtonExport($label, $name = 'submit', $createElement = false)
  567. {
  568. return $this->addButton(
  569. $name,
  570. $label,
  571. 'check',
  572. 'primary',
  573. null,
  574. null,
  575. [],
  576. $createElement
  577. );
  578. }
  579. /**
  580. * Shortcut to filter button.
  581. *
  582. * @param string $label Text appearing on the button
  583. * @param string $name Element name (for form treatment purposes)
  584. * @param bool $createElement Whether to use the create or add method
  585. *
  586. * @return HTML_QuickForm_button
  587. */
  588. public function addButtonFilter($label, $name = 'submit', $createElement = false)
  589. {
  590. return $this->addButton(
  591. $name,
  592. $label,
  593. 'filter',
  594. 'primary',
  595. null,
  596. null,
  597. [],
  598. $createElement
  599. );
  600. }
  601. /**
  602. * Shortcut to reset button.
  603. *
  604. * @param string $label Text appearing on the button
  605. * @param string $name Element name (for form treatment purposes)
  606. * @param bool $createElement Whether to use the create or add method
  607. *
  608. * @return HTML_QuickForm_button
  609. */
  610. public function addButtonReset($label, $name = 'reset', $createElement = false)
  611. {
  612. $icon = 'eraser';
  613. $style = 'default';
  614. $size = 'default';
  615. $class = null;
  616. $attributes = [];
  617. if ($createElement) {
  618. return $this->createElement(
  619. 'reset',
  620. $name,
  621. $label,
  622. $icon,
  623. $style,
  624. $size,
  625. $class,
  626. $attributes
  627. );
  628. }
  629. return $this->addElement(
  630. 'reset',
  631. $name,
  632. $label,
  633. $icon,
  634. $style,
  635. $size,
  636. $class,
  637. $attributes
  638. );
  639. }
  640. /**
  641. * Returns a button with the primary color and an upload icon.
  642. *
  643. * @param string $label Text appearing on the button
  644. * @param string $name Element name (for form treatment purposes)
  645. * @param bool $createElement Whether to use the create or add method
  646. *
  647. * @return HTML_QuickForm_button
  648. */
  649. public function addButtonUpload($label, $name = 'submit', $createElement = false)
  650. {
  651. return $this->addButton(
  652. $name,
  653. $label,
  654. 'upload',
  655. 'primary',
  656. null,
  657. null,
  658. [],
  659. $createElement
  660. );
  661. }
  662. /**
  663. * Returns a button with the primary color and a download icon.
  664. *
  665. * @param string $label Text appearing on the button
  666. * @param string $name Element name (for form treatment purposes)
  667. * @param bool $createElement Whether to use the create or add method
  668. *
  669. * @return HTML_QuickForm_button
  670. */
  671. public function addButtonDownload($label, $name = 'submit', $createElement = false)
  672. {
  673. return $this->addButton(
  674. $name,
  675. $label,
  676. 'download',
  677. 'primary',
  678. null,
  679. null,
  680. [],
  681. $createElement
  682. );
  683. }
  684. /**
  685. * Returns a button with the primary color and a magnifier icon.
  686. *
  687. * @param string $label Text appearing on the button
  688. * @param string $name Element name (for form treatment purposes)
  689. * @param bool $createElement Whether to use the create or add method
  690. *
  691. * @return HTML_QuickForm_button
  692. */
  693. public function addButtonPreview($label, $name = 'submit', $createElement = false)
  694. {
  695. return $this->addButton(
  696. $name,
  697. $label,
  698. 'search',
  699. 'primary',
  700. null,
  701. null,
  702. [],
  703. $createElement
  704. );
  705. }
  706. /**
  707. * Returns a button with the primary color and a copy (double sheet) icon.
  708. *
  709. * @param string $label Text appearing on the button
  710. * @param string $name Element name (for form treatment purposes)
  711. * @param bool $createElement Whether to use the create or add method
  712. *
  713. * @return HTML_QuickForm_button
  714. */
  715. public function addButtonCopy($label, $name = 'submit', $createElement = false)
  716. {
  717. return $this->addButton(
  718. $name,
  719. $label,
  720. 'copy',
  721. 'primary',
  722. null,
  723. null,
  724. [],
  725. $createElement
  726. );
  727. }
  728. /**
  729. * @param string $name
  730. * @param string $label
  731. * @param string $text
  732. * @param array $attributes
  733. *
  734. * @return HTML_QuickForm_checkbox
  735. */
  736. public function addCheckBox($name, $label, $text = '', $attributes = [])
  737. {
  738. return $this->addElement('checkbox', $name, $label, $text, $attributes);
  739. }
  740. /**
  741. * @param string $name
  742. * @param string $label
  743. * @param array $options
  744. * @param array $attributes
  745. *
  746. * @return HTML_QuickForm_group
  747. */
  748. public function addCheckBoxGroup($name, $label, $options = [], $attributes = [])
  749. {
  750. $group = [];
  751. foreach ($options as $value => $text) {
  752. $attributes['value'] = $value;
  753. $group[] = $this->createElement(
  754. 'checkbox',
  755. $value,
  756. null,
  757. $text,
  758. $attributes
  759. );
  760. }
  761. return $this->addGroup($group, $name, $label);
  762. }
  763. /**
  764. * @param string $name
  765. * @param string $label
  766. * @param array $options
  767. * @param array $attributes
  768. *
  769. * @return HTML_QuickForm_group
  770. */
  771. public function addRadio($name, $label, $options = [], $attributes = [])
  772. {
  773. $group = [];
  774. foreach ($options as $key => $value) {
  775. $group[] = $this->createElement('radio', null, null, $value, $key, $attributes);
  776. }
  777. return $this->addGroup($group, $name, $label);
  778. }
  779. /**
  780. * @param string $name
  781. * @param string $label
  782. * @param array $options
  783. * @param array $attributes
  784. *
  785. * @return HTML_QuickForm_select
  786. */
  787. public function addSelect($name, $label, $options = [], $attributes = [])
  788. {
  789. return $this->addElement('select', $name, $label, $options, $attributes);
  790. }
  791. /**
  792. * @param $name
  793. * @param $label
  794. * @param $collection
  795. * @param array $attributes
  796. * @param bool $addNoneOption
  797. * @param string $textCallable set a function getStringValue() by default __toString()
  798. *
  799. * @return HTML_QuickForm_element
  800. */
  801. public function addSelectFromCollection(
  802. $name,
  803. $label,
  804. $collection,
  805. $attributes = [],
  806. $addNoneOption = false,
  807. $textCallable = ''
  808. ) {
  809. $options = [];
  810. if ($addNoneOption) {
  811. $options[0] = get_lang('None');
  812. }
  813. if (!empty($collection)) {
  814. foreach ($collection as $item) {
  815. $text = $item;
  816. if (!empty($textCallable)) {
  817. $text = $item->$textCallable();
  818. }
  819. $options[$item->getId()] = $text;
  820. }
  821. }
  822. return $this->addElement('select', $name, $label, $options, $attributes);
  823. }
  824. /**
  825. * @param string $label
  826. * @param string $text
  827. * @param bool $createElement
  828. *
  829. * @return HTML_QuickForm_Element
  830. */
  831. public function addLabel($label, $text, $createElement = false)
  832. {
  833. if ($createElement) {
  834. return $this->createElement(
  835. 'label',
  836. $label,
  837. $text
  838. );
  839. }
  840. return $this->addElement('label', $label, $text);
  841. }
  842. /**
  843. * @param string $text
  844. */
  845. public function addHeader($text)
  846. {
  847. if (!empty($text)) {
  848. $this->addElement('header', $text);
  849. }
  850. }
  851. /**
  852. * @param string $name
  853. * @param string $label
  854. * @param array $attributes
  855. *
  856. * @throws Exception if the file doesn't have an id
  857. *
  858. * @return HTML_QuickForm_file
  859. */
  860. public function addFile($name, $label, $attributes = [])
  861. {
  862. $element = $this->addElement('file', $name, $label, $attributes);
  863. if (isset($attributes['crop_image'])) {
  864. $id = $element->getAttribute('id');
  865. if (empty($id)) {
  866. throw new Exception('If you use the crop functionality the element must have an id');
  867. }
  868. $this->addHtml(
  869. '
  870. <div class="form-group" id="'.$id.'-form-group" style="display: none;">
  871. <div class="col-sm-offset-2 col-sm-8">
  872. <div id="'.$id.'_crop_image" class="cropCanvas thumbnail">
  873. <img id="'.$id.'_preview_image">
  874. </div>
  875. <button class="btn btn-primary" type="button" name="cropButton" id="'.$id.'_crop_button">
  876. <em class="fa fa-crop"></em> '.get_lang('CropYourPicture').'
  877. </button>
  878. </div>
  879. </div>'
  880. );
  881. $this->addHidden($id.'_crop_result', '');
  882. $this->addHidden($id.'_crop_image_base_64', '');
  883. }
  884. return $element;
  885. }
  886. /**
  887. * @param string $snippet
  888. */
  889. public function addHtml($snippet)
  890. {
  891. $this->addElement('html', $snippet);
  892. }
  893. /**
  894. * Draws a panel of options see the course_info/infocours.php page.
  895. *
  896. * @param string $name internal name
  897. * @param string $title visible title
  898. * @param array $groupList list of group or elements
  899. */
  900. public function addPanelOption($name, $title, $groupList)
  901. {
  902. $this->addHtml('<div class="panel panel-default">');
  903. $this->addHtml(
  904. '
  905. <div class="panel-heading" role="tab" id="heading-'.$name.'-settings">
  906. <h4 class="panel-title">
  907. <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
  908. href="#collapse-'.$name.'-settings" aria-expanded="false" aria-controls="collapse-'.$name.'-settings">
  909. '
  910. );
  911. $this->addHtml($title);
  912. $this->addHtml('</a></h4></div>');
  913. $this->addHtml('<div id="collapse-'.$name.'-settings" class="panel-collapse collapse" role="tabpanel"
  914. aria-labelledby="heading-'.$name.'-settings">
  915. <div class="panel-body">
  916. ');
  917. foreach ($groupList as $groupName => $group) {
  918. // Add group array
  919. if (!empty($groupName) && is_array($group)) {
  920. $this->addGroup($group, '', $groupName);
  921. }
  922. // Add element
  923. if ($group instanceof HTML_QuickForm_element) {
  924. $this->addElement($group);
  925. }
  926. }
  927. $this->addHtml('</div></div>');
  928. $this->addHtml('</div>');
  929. }
  930. /**
  931. * Adds a HTML-editor to the form.
  932. *
  933. * @param string $name
  934. * @param string|array $label The label for the form-element
  935. * @param bool $required (optional) Is the form-element required (default=true)
  936. * @param bool $fullPage (optional) When it is true, the editor loads completed html code for a full page
  937. * @param array $config (optional) Configuration settings for the online editor
  938. */
  939. public function addHtmlEditor(
  940. $name,
  941. $label,
  942. $required = true,
  943. $fullPage = false,
  944. $config = []
  945. ) {
  946. $attributes = [];
  947. $attributes['rows'] = isset($config['rows']) ? $config['rows'] : 15;
  948. $attributes['cols'] = isset($config['cols']) ? $config['cols'] : 80;
  949. $attributes['cols-size'] = isset($config['cols-size']) ? $config['cols-size'] : [];
  950. $attributes['class'] = isset($config['class']) ? $config['class'] : [];
  951. $attributes['id'] = isset($config['id']) ? $config['id'] : '';
  952. if (empty($attributes['id'])) {
  953. $attributes['id'] = $name;
  954. }
  955. $this->addElement('html_editor', $name, $label, $attributes, $config);
  956. $this->applyFilter($name, 'trim');
  957. if ($required) {
  958. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  959. }
  960. /** @var HtmlEditor $element */
  961. $element = $this->getElement($name);
  962. $config['style'] = isset($config['style']) ? $config['style'] : false;
  963. if ($fullPage) {
  964. $config['fullPage'] = true;
  965. // Adds editor_content.css in ckEditor
  966. $config['style'] = true;
  967. }
  968. if ($element->editor) {
  969. $element->editor->processConfig($config);
  970. }
  971. }
  972. /**
  973. * Adds a Google Maps Geolocalization field to the form.
  974. *
  975. * @param $name
  976. * @param $label
  977. * @param bool $hideGeoLocalizationDetails
  978. */
  979. public function addGeoLocationMapField($name, $label, $dataValue, $hideGeoLocalizationDetails = false)
  980. {
  981. $gMapsPlugin = GoogleMapsPlugin::create();
  982. $geolocalization = $gMapsPlugin->get('enable_api') === 'true';
  983. if ($geolocalization && $gMapsPlugin->javascriptIncluded === false) {
  984. $gmapsApiKey = $gMapsPlugin->get('api_key');
  985. $url = '//maps.googleapis.com/maps/api/js?key='.$gmapsApiKey;
  986. $this->addHtml('<script type="text/javascript" src="'.$url.'" ></script>');
  987. $gMapsPlugin->javascriptIncluded = true;
  988. }
  989. $this->addElement(
  990. 'text',
  991. $name,
  992. $label,
  993. ['id' => $name]
  994. );
  995. $this->addHidden(
  996. $name.'_coordinates',
  997. '',
  998. ['id' => $name.'_coordinates']
  999. );
  1000. $this->applyFilter($name, 'stripslashes');
  1001. $this->applyFilter($name, 'trim');
  1002. $this->addHtml(Extrafield::getLocalizationJavascript($name, $dataValue));
  1003. if ($hideGeoLocalizationDetails) {
  1004. $this->addHtml('<div style="display:none">');
  1005. }
  1006. $this->addHtml(
  1007. Extrafield::getLocalizationInput($name, $label)
  1008. );
  1009. if ($hideGeoLocalizationDetails) {
  1010. $this->addHtml('</div>');
  1011. }
  1012. }
  1013. /**
  1014. * @param string $name
  1015. * @param string $label
  1016. *
  1017. * @return mixed
  1018. */
  1019. public function addButtonAdvancedSettings($name, $label = '')
  1020. {
  1021. $label = !empty($label) ? $label : get_lang('AdvancedParameters');
  1022. return $this->addElement('advanced_settings', $name, $label);
  1023. }
  1024. /**
  1025. * Adds a progress loading image to the form.
  1026. */
  1027. public function addProgress($delay = 2, $label = '')
  1028. {
  1029. if (empty($label)) {
  1030. $label = get_lang('PleaseStandBy');
  1031. }
  1032. $this->with_progress_bar = true;
  1033. $id = $this->getAttribute('id');
  1034. $this->updateAttributes("onsubmit=\"javascript: addProgress('".$id."')\"");
  1035. $this->addHtml('<script language="javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/upload.js" type="text/javascript"></script>');
  1036. }
  1037. /**
  1038. * This function has been created for avoiding changes directly within QuickForm class.
  1039. * When we use it, the element is threated as 'required' to be dealt during validation.
  1040. *
  1041. * @param array $elements The array of elements
  1042. * @param string $message The message displayed
  1043. */
  1044. public function add_multiple_required_rule($elements, $message)
  1045. {
  1046. $this->_required[] = $elements[0];
  1047. $this->addRule($elements, $message, 'multiple_required');
  1048. }
  1049. /**
  1050. * Displays the form.
  1051. * If an element in the form didn't validate, an error message is showed
  1052. * asking the user to complete the form.
  1053. */
  1054. public function display()
  1055. {
  1056. echo $this->returnForm();
  1057. }
  1058. /**
  1059. * Returns the HTML code of the form.
  1060. *
  1061. * @return string $return_value HTML code of the form
  1062. */
  1063. public function returnForm()
  1064. {
  1065. $returnValue = '';
  1066. /** @var HTML_QuickForm_element $element */
  1067. foreach ($this->_elements as $element) {
  1068. $elementError = parent::getElementError($element->getName());
  1069. if (!is_null($elementError)) {
  1070. $returnValue .= Display::return_message($elementError, 'warning').'<br />';
  1071. break;
  1072. }
  1073. }
  1074. $returnValue .= parent::toHtml();
  1075. // Add div-element which is to hold the progress bar
  1076. $id = $this->getAttribute('id');
  1077. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  1078. // Deprecated
  1079. // $icon = Display::return_icon('progress_bar.gif');
  1080. // @todo improve UI
  1081. $returnValue .= '<br />
  1082. <div id="loading_div_'.$id.'" class="loading_div" style="display:none;margin-left:40%; margin-top:10px; height:50px;">
  1083. <div class="wobblebar-loader"></div>
  1084. </div>
  1085. ';
  1086. }
  1087. return $returnValue;
  1088. }
  1089. /**
  1090. * Returns the HTML code of the form.
  1091. * If an element in the form didn't validate, an error message is showed
  1092. * asking the user to complete the form.
  1093. *
  1094. * @return string $return_value HTML code of the form
  1095. *
  1096. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  1097. * @author Julio Montoya
  1098. *
  1099. * @deprecated use returnForm()
  1100. */
  1101. public function return_form()
  1102. {
  1103. return $this->returnForm();
  1104. }
  1105. /**
  1106. * @return HTML_QuickForm_Renderer_Default
  1107. */
  1108. public static function getDefaultRenderer()
  1109. {
  1110. return
  1111. isset($GLOBALS['_HTML_QuickForm_default_renderer']) ?
  1112. $GLOBALS['_HTML_QuickForm_default_renderer'] : null;
  1113. }
  1114. /**
  1115. * Adds a input of type url to the form.
  1116. *
  1117. * @param string $name The label for the form-element
  1118. * @param string $label The element name
  1119. * @param bool $required Optional. Is the form-element required (default=true)
  1120. * @param array $attributes Optional. List of attributes for the form-element
  1121. */
  1122. public function addUrl($name, $label, $required = true, $attributes = [])
  1123. {
  1124. $this->addElement('url', $name, $label, $attributes);
  1125. $this->applyFilter($name, 'trim');
  1126. $this->addRule($name, get_lang('InsertAValidUrl'), 'url');
  1127. if ($required) {
  1128. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1129. }
  1130. }
  1131. /**
  1132. * Adds a text field for letters to the form.
  1133. * A trim-filter is attached to the field.
  1134. *
  1135. * @param string $name The element name
  1136. * @param string $label The label for the form-element
  1137. * @param bool $required Optional. Is the form-element required (default=true)
  1138. * @param array $attributes Optional. List of attributes for the form-element
  1139. */
  1140. public function addTextLettersOnly(
  1141. $name,
  1142. $label,
  1143. $required = false,
  1144. $attributes = []
  1145. ) {
  1146. $attributes = array_merge(
  1147. $attributes,
  1148. [
  1149. 'pattern' => '[a-zA-ZñÑ]+',
  1150. 'title' => get_lang('OnlyLetters'),
  1151. ]
  1152. );
  1153. $this->addElement(
  1154. 'text',
  1155. $name,
  1156. [
  1157. $label,
  1158. get_lang('OnlyLetters'),
  1159. ],
  1160. $attributes
  1161. );
  1162. $this->applyFilter($name, 'trim');
  1163. if ($required) {
  1164. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1165. }
  1166. $this->addRule(
  1167. $name,
  1168. get_lang('OnlyLetters'),
  1169. 'regex',
  1170. '/^[a-zA-ZñÑ]+$/'
  1171. );
  1172. }
  1173. /**
  1174. * Adds a text field for alphanumeric characters to the form.
  1175. * A trim-filter is attached to the field.
  1176. *
  1177. * @param string $name The element name
  1178. * @param string $label The label for the form-element
  1179. * @param bool $required Optional. Is the form-element required (default=true)
  1180. * @param array $attributes Optional. List of attributes for the form-element
  1181. */
  1182. public function addTextAlphanumeric(
  1183. $name,
  1184. $label,
  1185. $required = false,
  1186. $attributes = []
  1187. ) {
  1188. $attributes = array_merge(
  1189. $attributes,
  1190. [
  1191. 'pattern' => '[a-zA-Z0-9ñÑ]+',
  1192. 'title' => get_lang('OnlyLettersAndNumbers'),
  1193. ]
  1194. );
  1195. $this->addElement(
  1196. 'text',
  1197. $name,
  1198. [
  1199. $label,
  1200. get_lang('OnlyLettersAndNumbers'),
  1201. ],
  1202. $attributes
  1203. );
  1204. $this->applyFilter($name, 'trim');
  1205. if ($required) {
  1206. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1207. }
  1208. $this->addRule(
  1209. $name,
  1210. get_lang('OnlyLettersAndNumbers'),
  1211. 'regex',
  1212. '/^[a-zA-Z0-9ÑÑ]+$/'
  1213. );
  1214. }
  1215. /**
  1216. * @param string $name
  1217. * @param $label
  1218. * @param bool $required
  1219. * @param array $attributes
  1220. * @param bool $allowNegative
  1221. * @param int $minValue
  1222. * @param null $maxValue
  1223. */
  1224. public function addFloat(
  1225. $name,
  1226. $label,
  1227. $required = false,
  1228. $attributes = [],
  1229. $allowNegative = false,
  1230. $minValue = null,
  1231. $maxValue = null
  1232. ) {
  1233. $this->addElement(
  1234. 'FloatNumber',
  1235. $name,
  1236. $label,
  1237. $attributes
  1238. );
  1239. $this->applyFilter($name, 'trim');
  1240. if ($required) {
  1241. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1242. }
  1243. // Rule allows "," and "."
  1244. /*$this->addRule(
  1245. $name,
  1246. get_lang('OnlyNumbers'),
  1247. 'regex',
  1248. '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)|(^-?\d\d*\,\d*$)|(^-?\,\d\d*$)/'
  1249. );*/
  1250. if ($allowNegative == false) {
  1251. $this->addRule(
  1252. $name,
  1253. get_lang('NegativeValue'),
  1254. 'compare',
  1255. '>=',
  1256. 'server',
  1257. false,
  1258. false,
  1259. 0
  1260. );
  1261. }
  1262. if (!is_null($minValue)) {
  1263. $this->addRule(
  1264. $name,
  1265. get_lang('UnderMin'),
  1266. 'compare',
  1267. '>=',
  1268. 'server',
  1269. false,
  1270. false,
  1271. $minValue
  1272. );
  1273. }
  1274. if (!is_null($maxValue)) {
  1275. $this->addRule(
  1276. $name,
  1277. get_lang('OverMax'),
  1278. 'compare',
  1279. '<=',
  1280. 'server',
  1281. false,
  1282. false,
  1283. $maxValue
  1284. );
  1285. }
  1286. }
  1287. /**
  1288. * Adds a text field for letters and spaces to the form.
  1289. * A trim-filter is attached to the field.
  1290. *
  1291. * @param string $name The element name
  1292. * @param string $label The label for the form-element
  1293. * @param bool $required Optional. Is the form-element required (default=true)
  1294. * @param array $attributes Optional. List of attributes for the form-element
  1295. */
  1296. public function addTextLettersAndSpaces(
  1297. $name,
  1298. $label,
  1299. $required = false,
  1300. $attributes = []
  1301. ) {
  1302. $attributes = array_merge(
  1303. $attributes,
  1304. [
  1305. 'pattern' => '[a-zA-ZñÑ\s]+',
  1306. 'title' => get_lang('OnlyLettersAndSpaces'),
  1307. ]
  1308. );
  1309. $this->addElement(
  1310. 'text',
  1311. $name,
  1312. [
  1313. $label,
  1314. get_lang('OnlyLettersAndSpaces'),
  1315. ],
  1316. $attributes
  1317. );
  1318. $this->applyFilter($name, 'trim');
  1319. if ($required) {
  1320. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1321. }
  1322. $this->addRule(
  1323. $name,
  1324. get_lang('OnlyLettersAndSpaces'),
  1325. 'regex',
  1326. '/^[a-zA-ZñÑ\s]+$/'
  1327. );
  1328. }
  1329. /**
  1330. * Adds a text field for alphanumeric and spaces characters to the form.
  1331. * A trim-filter is attached to the field.
  1332. *
  1333. * @param string $name The element name
  1334. * @param string $label The label for the form-element
  1335. * @param bool $required Optional. Is the form-element required (default=true)
  1336. * @param array $attributes Optional. List of attributes for the form-element
  1337. */
  1338. public function addTextAlphanumericAndSpaces(
  1339. $name,
  1340. $label,
  1341. $required = false,
  1342. $attributes = []
  1343. ) {
  1344. $attributes = array_merge(
  1345. $attributes,
  1346. [
  1347. 'pattern' => '[a-zA-Z0-9ñÑ\s]+',
  1348. 'title' => get_lang('OnlyLettersAndNumbersAndSpaces'),
  1349. ]
  1350. );
  1351. $this->addElement(
  1352. 'text',
  1353. $name,
  1354. [
  1355. $label,
  1356. get_lang('OnlyLettersAndNumbersAndSpaces'),
  1357. ],
  1358. $attributes
  1359. );
  1360. $this->applyFilter($name, 'trim');
  1361. if ($required) {
  1362. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1363. }
  1364. $this->addRule(
  1365. $name,
  1366. get_lang('OnlyLettersAndNumbersAndSpaces'),
  1367. 'regex',
  1368. '/^[a-zA-Z0-9ñÑ\s]+$/'
  1369. );
  1370. }
  1371. /**
  1372. * @param string $url
  1373. * @param string $urlToRedirect after upload redirect to this page
  1374. */
  1375. public function addMultipleUpload($url, $urlToRedirect = '')
  1376. {
  1377. $inputName = 'input_file_upload';
  1378. $this->addMultipleUploadJavascript($url, $inputName, $urlToRedirect);
  1379. $this->addHtml('
  1380. <div class="description-upload">
  1381. '.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'
  1382. </div>
  1383. <span class="btn btn-success fileinput-button">
  1384. <i class="glyphicon glyphicon-plus"></i>
  1385. <span>'.get_lang('AddFiles').'</span>
  1386. <!-- The file input field used as target for the file upload widget -->
  1387. <input id="'.$inputName.'" type="file" name="files[]" multiple>
  1388. </span>
  1389. <div id="dropzone">
  1390. <div class="button-load">
  1391. '.get_lang('UploadFiles').'
  1392. </div>
  1393. </div>
  1394. <br />
  1395. <!-- The global progress bar -->
  1396. <div id="progress" class="progress">
  1397. <div class="progress-bar progress-bar-success"></div>
  1398. </div>
  1399. <div id="files" class="files"></div>
  1400. ');
  1401. }
  1402. /**
  1403. * @param string $elementName
  1404. * @param string $groupName if element is inside a group
  1405. *
  1406. * @throws Exception
  1407. */
  1408. public function addPasswordRule($elementName, $groupName = '')
  1409. {
  1410. // Constant defined in old config/profile.conf.php
  1411. if (CHECK_PASS_EASY_TO_FIND === true) {
  1412. $message = get_lang('PassTooEasy').': '.api_generate_password();
  1413. if (!empty($groupName)) {
  1414. $groupObj = $this->getElement($groupName);
  1415. if ($groupObj instanceof HTML_QuickForm_group) {
  1416. $elementName = $groupObj->getElementName($elementName);
  1417. if ($elementName === false) {
  1418. throw new Exception("The $groupName doesn't have the element $elementName");
  1419. }
  1420. $this->_rules[$elementName][] = [
  1421. 'type' => 'callback',
  1422. 'format' => 'api_check_password',
  1423. 'message' => $message,
  1424. 'validation' => '',
  1425. 'reset' => false,
  1426. 'group' => $groupName,
  1427. ];
  1428. }
  1429. } else {
  1430. $this->addRule(
  1431. $elementName,
  1432. $message,
  1433. 'callback',
  1434. 'api_check_password'
  1435. );
  1436. }
  1437. }
  1438. }
  1439. /**
  1440. * Add an element with user ID and avatar to the form.
  1441. * It needs a Chamilo\UserBundle\Entity\User as value. The exported value is the Chamilo\UserBundle\Entity\User ID.
  1442. *
  1443. * @see \UserAvatar
  1444. *
  1445. * @param string $name
  1446. * @param string $label
  1447. * @param string $imageSize Optional. Small, medium or large image
  1448. * @param string $subtitle Optional. The subtitle for the field
  1449. *
  1450. * @return \UserAvatar
  1451. */
  1452. public function addUserAvatar($name, $label, $imageSize = 'small', $subtitle = '')
  1453. {
  1454. return $this->addElement('UserAvatar', $name, $label, ['image_size' => $imageSize, 'sub_title' => $subtitle]);
  1455. }
  1456. /**
  1457. * @param array $typeList
  1458. */
  1459. public function addEmailTemplate($typeList)
  1460. {
  1461. $mailManager = new MailTemplateManager();
  1462. foreach ($typeList as $type) {
  1463. $list = $mailManager->get_all(
  1464. ['where' => ['type = ? AND url_id = ?' => [$type, api_get_current_access_url_id()]]]
  1465. );
  1466. $options = [get_lang('Select')];
  1467. $name = $type;
  1468. $defaultId = '';
  1469. foreach ($list as $item) {
  1470. $options[$item['id']] = $item['name'];
  1471. $name = $item['name'];
  1472. if (empty($defaultId)) {
  1473. $defaultId = $item['default_template'] == 1 ? $item['id'] : '';
  1474. }
  1475. }
  1476. $url = api_get_path(WEB_AJAX_PATH).'mail.ajax.php?a=select_option';
  1477. $typeNoDots = 'email_template_option_'.str_replace('.tpl', '', $type);
  1478. $this->addSelect(
  1479. 'email_template_option['.$type.']',
  1480. $name,
  1481. $options,
  1482. ['id' => $typeNoDots]
  1483. );
  1484. $templateNoDots = 'email_template_'.str_replace('.tpl', '', $type);
  1485. $templateNoDotsBlock = 'email_template_block_'.str_replace('.tpl', '', $type);
  1486. $this->addHtml('<div id="'.$templateNoDotsBlock.'" style="display:none">');
  1487. $this->addTextarea(
  1488. $templateNoDots,
  1489. get_lang('Preview'),
  1490. ['disabled' => 'disabled ', 'id' => $templateNoDots, 'rows' => '5']
  1491. );
  1492. $this->addHtml('</div>');
  1493. $this->addHtml("<script>
  1494. $(function() {
  1495. var defaultValue = '$defaultId';
  1496. $('#$typeNoDots').val(defaultValue);
  1497. $('#$typeNoDots').selectpicker('render');
  1498. if (defaultValue != '') {
  1499. var selected = $('#$typeNoDots option:selected').val();
  1500. $.ajax({
  1501. url: '$url' + '&id=' + selected+ '&template_name=$type',
  1502. success: function (data) {
  1503. $('#$templateNoDots').html(data);
  1504. $('#$templateNoDotsBlock').show();
  1505. return;
  1506. },
  1507. });
  1508. }
  1509. $('#$typeNoDots').on('change', function(){
  1510. var selected = $('#$typeNoDots option:selected').val();
  1511. $.ajax({
  1512. url: '$url' + '&id=' + selected,
  1513. success: function (data) {
  1514. $('#$templateNoDots').html(data);
  1515. $('#$templateNoDotsBlock').show();
  1516. return;
  1517. },
  1518. });
  1519. });
  1520. });
  1521. </script>");
  1522. }
  1523. }
  1524. /**
  1525. * @param string $url page that will handle the upload
  1526. * @param string $inputName
  1527. * @param string $urlToRedirect
  1528. */
  1529. private function addMultipleUploadJavascript($url, $inputName, $urlToRedirect = '')
  1530. {
  1531. $redirectCondition = '';
  1532. if (!empty($urlToRedirect)) {
  1533. $redirectCondition = "window.location.replace('$urlToRedirect'); ";
  1534. }
  1535. $icon = Display::return_icon('file_txt.gif');
  1536. $this->addHtml("
  1537. <script>
  1538. $(function () {
  1539. 'use strict';
  1540. $('#".$this->getAttribute('id')."').submit(function() {
  1541. return false;
  1542. });
  1543. $('#dropzone').on('click', function() {
  1544. $('#".$inputName."').click();
  1545. });
  1546. var url = '".$url."';
  1547. var uploadButton = $('<button/>')
  1548. .addClass('btn btn-primary')
  1549. .prop('disabled', true)
  1550. .text('".addslashes(get_lang('Loading'))."')
  1551. .on('click', function () {
  1552. var \$this = $(this),
  1553. data = \$this.data();
  1554. \$this
  1555. .off('click')
  1556. .text('".addslashes(get_lang('Cancel'))."')
  1557. .on('click', function () {
  1558. \$this.remove();
  1559. data.abort();
  1560. });
  1561. data.submit().always(function () {
  1562. \$this.remove();
  1563. });
  1564. });
  1565. $('#".$inputName."').fileupload({
  1566. url: url,
  1567. dataType: 'json',
  1568. // Enable image resizing, except for Android and Opera,
  1569. // which actually support image resizing, but fail to
  1570. // send Blob objects via XHR requests:
  1571. disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
  1572. previewMaxWidth: 300,
  1573. previewMaxHeight: 169,
  1574. previewCrop: true,
  1575. dropzone: $('#dropzone'),
  1576. }).on('fileuploadadd', function (e, data) {
  1577. data.context = $('<div class=\"row\" />').appendTo('#files');
  1578. $.each(data.files, function (index, file) {
  1579. var node = $('<div class=\"col-sm-5 file_name\">').text(file.name);
  1580. node.appendTo(data.context);
  1581. });
  1582. }).on('fileuploadprocessalways', function (e, data) {
  1583. var index = data.index,
  1584. file = data.files[index],
  1585. node = $(data.context.children()[index]);
  1586. if (file.preview) {
  1587. data.context.prepend($('<div class=\"col-sm-4\">').html(file.preview));
  1588. } else {
  1589. data.context.prepend($('<div class=\"col-sm-4\">').html('".$icon."'));
  1590. }
  1591. if (index + 1 === data.files.length) {
  1592. data.context.find('button')
  1593. .text('Upload')
  1594. .prop('disabled', !!data.files.error);
  1595. }
  1596. }).on('fileuploadprogressall', function (e, data) {
  1597. var progress = parseInt(data.loaded / data.total * 100, 10);
  1598. $('#progress .progress-bar').css(
  1599. 'width',
  1600. progress + '%'
  1601. );
  1602. }).on('fileuploaddone', function (e, data) {
  1603. $.each(data.result.files, function (index, file) {
  1604. if (file.error) {
  1605. var link = $('<div>')
  1606. .attr({class : 'panel-image'}) ;
  1607. $(data.context.children()[index]).parent().wrap(link);
  1608. // Update file name with new one from Chamilo
  1609. $(data.context.children()[index]).parent().find('.file_name').html(file.name);
  1610. var message = $('<div class=\"col-sm-3\">').html(
  1611. $('<span class=\"message-image-danger\"/>').text(file.error)
  1612. );
  1613. $(data.context.children()[index]).parent().append(message);
  1614. return;
  1615. }
  1616. if (file.url) {
  1617. var link = $('<a>')
  1618. .attr({target: '_blank', class : 'panel-image'})
  1619. .prop('href', file.url);
  1620. $(data.context.children()[index]).parent().wrap(link);
  1621. }
  1622. // Update file name with new one from Chamilo
  1623. $(data.context.children()[index]).parent().find('.file_name').html(file.name);
  1624. var message = $('<div class=\"col-sm-3\">').html(
  1625. $('<span class=\"message-image-success\"/>').text('".addslashes(get_lang('UplUploadSucceeded'))."')
  1626. );
  1627. $(data.context.children()[index]).parent().append(message);
  1628. });
  1629. $('#dropzone').removeClass('hover');
  1630. ".$redirectCondition."
  1631. }).on('fileuploadfail', function (e, data) {
  1632. $.each(data.files, function (index) {
  1633. var failedMessage = '".addslashes(get_lang('UplUploadFailed'))."';
  1634. var error = $('<div class=\"col-sm-3\">').html(
  1635. $('<span class=\"alert alert-danger\"/>').text(failedMessage)
  1636. );
  1637. $(data.context.children()[index]).parent().append(error);
  1638. });
  1639. $('#dropzone').removeClass('hover');
  1640. }).prop('disabled', !$.support.fileInput).parent().addClass($.support.fileInput ? undefined : 'disabled');
  1641. $('#dropzone').on('dragover', function (e) {
  1642. // dragleave callback implementation
  1643. $('#dropzone').addClass('hover');
  1644. });
  1645. $('#dropzone').on('dragleave', function (e) {
  1646. $('#dropzone').removeClass('hover');
  1647. });
  1648. $('.fileinput-button').hide();
  1649. });
  1650. </script>");
  1651. }
  1652. }
  1653. /**
  1654. * Cleans HTML text filter.
  1655. *
  1656. * @param string $html HTML to clean
  1657. * @param int $mode (optional)
  1658. *
  1659. * @return string The cleaned HTML
  1660. */
  1661. function html_filter($html, $mode = NO_HTML)
  1662. {
  1663. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  1664. $cleaned_html = kses($html, $allowed_tags);
  1665. return $cleaned_html;
  1666. }
  1667. function html_filter_teacher($html)
  1668. {
  1669. return html_filter($html, TEACHER_HTML);
  1670. }
  1671. function html_filter_student($html)
  1672. {
  1673. return html_filter($html, STUDENT_HTML);
  1674. }
  1675. function html_filter_teacher_fullpage($html)
  1676. {
  1677. return html_filter($html, TEACHER_HTML_FULLPAGE);
  1678. }
  1679. function html_filter_student_fullpage($html)
  1680. {
  1681. return html_filter($html, STUDENT_HTML_FULLPAGE);
  1682. }
  1683. /**
  1684. * Cleans mobile phone number text.
  1685. *
  1686. * @param string $mobilePhoneNumber Mobile phone number to clean
  1687. *
  1688. * @return string The cleaned mobile phone number
  1689. */
  1690. function mobile_phone_number_filter($mobilePhoneNumber)
  1691. {
  1692. $mobilePhoneNumber = str_replace(['+', '(', ')'], '', $mobilePhoneNumber);
  1693. return ltrim($mobilePhoneNumber, '0');
  1694. }