FormValidator.class.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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. $this->addElement('header', $text);
  848. }
  849. /**
  850. * @param string $name
  851. * @param string $label
  852. * @param array $attributes
  853. *
  854. * @throws Exception if the file doesn't have an id
  855. *
  856. * @return HTML_QuickForm_file
  857. */
  858. public function addFile($name, $label, $attributes = [])
  859. {
  860. $element = $this->addElement('file', $name, $label, $attributes);
  861. if (isset($attributes['crop_image'])) {
  862. $id = $element->getAttribute('id');
  863. if (empty($id)) {
  864. throw new Exception('If you use the crop functionality the element must have an id');
  865. }
  866. $this->addHtml(
  867. '
  868. <div class="form-group" id="'.$id.'-form-group" style="display: none;">
  869. <div class="col-sm-offset-2 col-sm-8">
  870. <div id="'.$id.'_crop_image" class="cropCanvas thumbnail">
  871. <img id="'.$id.'_preview_image">
  872. </div>
  873. <button class="btn btn-primary" type="button" name="cropButton" id="'.$id.'_crop_button">
  874. <em class="fa fa-crop"></em> '.get_lang('CropYourPicture').'
  875. </button>
  876. </div>
  877. </div>'
  878. );
  879. $this->addHidden($id.'_crop_result', '');
  880. $this->addHidden($id.'_crop_image_base_64', '');
  881. }
  882. return $element;
  883. }
  884. /**
  885. * @param string $snippet
  886. */
  887. public function addHtml($snippet)
  888. {
  889. $this->addElement('html', $snippet);
  890. }
  891. /**
  892. * Draws a panel of options see the course_info/infocours.php page.
  893. *
  894. * @param string $name internal name
  895. * @param string $title visible title
  896. * @param array $groupList list of group or elements
  897. */
  898. public function addPanelOption($name, $title, $groupList)
  899. {
  900. $this->addHtml('<div class="panel panel-default">');
  901. $this->addHtml(
  902. '
  903. <div class="panel-heading" role="tab" id="heading-'.$name.'-settings">
  904. <h4 class="panel-title">
  905. <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
  906. href="#collapse-'.$name.'-settings" aria-expanded="false" aria-controls="collapse-'.$name.'-settings">
  907. '
  908. );
  909. $this->addHtml($title);
  910. $this->addHtml('</a></h4></div>');
  911. $this->addHtml('<div id="collapse-'.$name.'-settings" class="panel-collapse collapse" role="tabpanel"
  912. aria-labelledby="heading-'.$name.'-settings">
  913. <div class="panel-body">
  914. ');
  915. foreach ($groupList as $groupName => $group) {
  916. // Add group array
  917. if (!empty($groupName) && is_array($group)) {
  918. $this->addGroup($group, '', $groupName);
  919. }
  920. // Add element
  921. if ($group instanceof HTML_QuickForm_element) {
  922. $this->addElement($group);
  923. }
  924. }
  925. $this->addHtml('</div></div>');
  926. $this->addHtml('</div>');
  927. }
  928. /**
  929. * Adds a HTML-editor to the form.
  930. *
  931. * @param string $name
  932. * @param string|array $label The label for the form-element
  933. * @param bool $required (optional) Is the form-element required (default=true)
  934. * @param bool $fullPage (optional) When it is true, the editor loads completed html code for a full page
  935. * @param array $config (optional) Configuration settings for the online editor
  936. */
  937. public function addHtmlEditor(
  938. $name,
  939. $label,
  940. $required = true,
  941. $fullPage = false,
  942. $config = []
  943. ) {
  944. $attributes = [];
  945. $attributes['rows'] = isset($config['rows']) ? $config['rows'] : 15;
  946. $attributes['cols'] = isset($config['cols']) ? $config['cols'] : 80;
  947. $attributes['cols-size'] = isset($config['cols-size']) ? $config['cols-size'] : [];
  948. $attributes['class'] = isset($config['class']) ? $config['class'] : [];
  949. $this->addElement('html_editor', $name, $label, $attributes, $config);
  950. $this->applyFilter($name, 'trim');
  951. if ($required) {
  952. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  953. }
  954. /** @var HtmlEditor $element */
  955. $element = $this->getElement($name);
  956. $config['style'] = isset($config['style']) ? $config['style'] : false;
  957. if ($fullPage) {
  958. $config['fullPage'] = true;
  959. // Adds editor_content.css in ckEditor
  960. $config['style'] = true;
  961. }
  962. if ($element->editor) {
  963. $element->editor->processConfig($config);
  964. }
  965. }
  966. /**
  967. * Adds a Google Maps Geolocalization field to the form.
  968. *
  969. * @param $name
  970. * @param $label
  971. * @param bool $hideGeoLocalizationDetails
  972. */
  973. public function addGeoLocationMapField($name, $label, $dataValue, $hideGeoLocalizationDetails = false)
  974. {
  975. $gMapsPlugin = GoogleMapsPlugin::create();
  976. $geolocalization = $gMapsPlugin->get('enable_api') === 'true';
  977. if ($geolocalization && $gMapsPlugin->javascriptIncluded === false) {
  978. $gmapsApiKey = $gMapsPlugin->get('api_key');
  979. $url = '//maps.googleapis.com/maps/api/js?key='.$gmapsApiKey;
  980. $this->addHtml('<script type="text/javascript" src="'.$url.'" ></script>');
  981. $gMapsPlugin->javascriptIncluded = true;
  982. }
  983. $this->addElement(
  984. 'text',
  985. $name,
  986. $label,
  987. ['id' => $name]
  988. );
  989. $this->addHidden(
  990. $name.'_coordinates',
  991. '',
  992. ['id' => $name.'_coordinates']
  993. );
  994. $this->applyFilter($name, 'stripslashes');
  995. $this->applyFilter($name, 'trim');
  996. $this->addHtml(Extrafield::getLocalizationJavascript($name, $dataValue));
  997. if ($hideGeoLocalizationDetails) {
  998. $this->addHtml('<div style="display:none">');
  999. }
  1000. $this->addHtml(
  1001. Extrafield::getLocalizationInput($name, $label)
  1002. );
  1003. if ($hideGeoLocalizationDetails) {
  1004. $this->addHtml('</div>');
  1005. }
  1006. }
  1007. /**
  1008. * @param string $name
  1009. * @param string $label
  1010. *
  1011. * @return mixed
  1012. */
  1013. public function addButtonAdvancedSettings($name, $label = '')
  1014. {
  1015. $label = !empty($label) ? $label : get_lang('AdvancedParameters');
  1016. return $this->addElement('advanced_settings', $name, $label);
  1017. }
  1018. /**
  1019. * Adds a progress loading image to the form.
  1020. */
  1021. public function addProgress($delay = 2, $label = '')
  1022. {
  1023. if (empty($label)) {
  1024. $label = get_lang('PleaseStandBy');
  1025. }
  1026. $this->with_progress_bar = true;
  1027. $id = $this->getAttribute('id');
  1028. $this->updateAttributes("onsubmit=\"javascript: addProgress('".$id."')\"");
  1029. $this->addHtml('<script language="javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/upload.js" type="text/javascript"></script>');
  1030. }
  1031. /**
  1032. * This function has been created for avoiding changes directly within QuickForm class.
  1033. * When we use it, the element is threated as 'required' to be dealt during validation.
  1034. *
  1035. * @param array $elements The array of elements
  1036. * @param string $message The message displayed
  1037. */
  1038. public function add_multiple_required_rule($elements, $message)
  1039. {
  1040. $this->_required[] = $elements[0];
  1041. $this->addRule($elements, $message, 'multiple_required');
  1042. }
  1043. /**
  1044. * Displays the form.
  1045. * If an element in the form didn't validate, an error message is showed
  1046. * asking the user to complete the form.
  1047. */
  1048. public function display()
  1049. {
  1050. echo $this->returnForm();
  1051. }
  1052. /**
  1053. * Returns the HTML code of the form.
  1054. *
  1055. * @return string $return_value HTML code of the form
  1056. */
  1057. public function returnForm()
  1058. {
  1059. $returnValue = '';
  1060. /** @var HTML_QuickForm_element $element */
  1061. foreach ($this->_elements as $element) {
  1062. $elementError = parent::getElementError($element->getName());
  1063. if (!is_null($elementError)) {
  1064. $returnValue .= Display::return_message($elementError, 'warning').'<br />';
  1065. break;
  1066. }
  1067. }
  1068. $returnValue .= parent::toHtml();
  1069. // Add div-element which is to hold the progress bar
  1070. $id = $this->getAttribute('id');
  1071. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  1072. // Deprecated
  1073. // $icon = Display::return_icon('progress_bar.gif');
  1074. // @todo improve UI
  1075. $returnValue .= '<br />
  1076. <div id="loading_div_'.$id.'" class="loading_div" style="display:none;margin-left:40%; margin-top:10px; height:50px;">
  1077. <div class="wobblebar-loader"></div>
  1078. </div>
  1079. ';
  1080. }
  1081. return $returnValue;
  1082. }
  1083. /**
  1084. * Returns the HTML code of the form.
  1085. * If an element in the form didn't validate, an error message is showed
  1086. * asking the user to complete the form.
  1087. *
  1088. * @return string $return_value HTML code of the form
  1089. *
  1090. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  1091. * @author Julio Montoya
  1092. *
  1093. * @deprecated use returnForm()
  1094. */
  1095. public function return_form()
  1096. {
  1097. return $this->returnForm();
  1098. }
  1099. /**
  1100. * @return HTML_QuickForm_Renderer_Default
  1101. */
  1102. public static function getDefaultRenderer()
  1103. {
  1104. return
  1105. isset($GLOBALS['_HTML_QuickForm_default_renderer']) ?
  1106. $GLOBALS['_HTML_QuickForm_default_renderer'] : null;
  1107. }
  1108. /**
  1109. * Adds a input of type url to the form.
  1110. *
  1111. * @param string $name The label for the form-element
  1112. * @param string $label The element name
  1113. * @param bool $required Optional. Is the form-element required (default=true)
  1114. * @param array $attributes Optional. List of attributes for the form-element
  1115. */
  1116. public function addUrl($name, $label, $required = true, $attributes = [])
  1117. {
  1118. $this->addElement('url', $name, $label, $attributes);
  1119. $this->applyFilter($name, 'trim');
  1120. $this->addRule($name, get_lang('InsertAValidUrl'), 'url');
  1121. if ($required) {
  1122. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1123. }
  1124. }
  1125. /**
  1126. * Adds a text field for letters to the form.
  1127. * A trim-filter is attached to the field.
  1128. *
  1129. * @param string $name The element name
  1130. * @param string $label The label for the form-element
  1131. * @param bool $required Optional. Is the form-element required (default=true)
  1132. * @param array $attributes Optional. List of attributes for the form-element
  1133. */
  1134. public function addTextLettersOnly(
  1135. $name,
  1136. $label,
  1137. $required = false,
  1138. $attributes = []
  1139. ) {
  1140. $attributes = array_merge(
  1141. $attributes,
  1142. [
  1143. 'pattern' => '[a-zA-ZñÑ]+',
  1144. 'title' => get_lang('OnlyLetters'),
  1145. ]
  1146. );
  1147. $this->addElement(
  1148. 'text',
  1149. $name,
  1150. [
  1151. $label,
  1152. get_lang('OnlyLetters'),
  1153. ],
  1154. $attributes
  1155. );
  1156. $this->applyFilter($name, 'trim');
  1157. if ($required) {
  1158. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1159. }
  1160. $this->addRule(
  1161. $name,
  1162. get_lang('OnlyLetters'),
  1163. 'regex',
  1164. '/^[a-zA-ZñÑ]+$/'
  1165. );
  1166. }
  1167. /**
  1168. * Adds a text field for alphanumeric characters to the form.
  1169. * A trim-filter is attached to the field.
  1170. *
  1171. * @param string $name The element name
  1172. * @param string $label The label for the form-element
  1173. * @param bool $required Optional. Is the form-element required (default=true)
  1174. * @param array $attributes Optional. List of attributes for the form-element
  1175. */
  1176. public function addTextAlphanumeric(
  1177. $name,
  1178. $label,
  1179. $required = false,
  1180. $attributes = []
  1181. ) {
  1182. $attributes = array_merge(
  1183. $attributes,
  1184. [
  1185. 'pattern' => '[a-zA-Z0-9ñÑ]+',
  1186. 'title' => get_lang('OnlyLettersAndNumbers'),
  1187. ]
  1188. );
  1189. $this->addElement(
  1190. 'text',
  1191. $name,
  1192. [
  1193. $label,
  1194. get_lang('OnlyLettersAndNumbers'),
  1195. ],
  1196. $attributes
  1197. );
  1198. $this->applyFilter($name, 'trim');
  1199. if ($required) {
  1200. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1201. }
  1202. $this->addRule(
  1203. $name,
  1204. get_lang('OnlyLettersAndNumbers'),
  1205. 'regex',
  1206. '/^[a-zA-Z0-9ÑÑ]+$/'
  1207. );
  1208. }
  1209. /**
  1210. * @param string $name
  1211. * @param $label
  1212. * @param bool $required
  1213. * @param array $attributes
  1214. * @param bool $allowNegative
  1215. * @param int $minValue
  1216. * @param null $maxValue
  1217. */
  1218. public function addFloat(
  1219. $name,
  1220. $label,
  1221. $required = false,
  1222. $attributes = [],
  1223. $allowNegative = false,
  1224. $minValue = null,
  1225. $maxValue = null
  1226. ) {
  1227. $this->addElement(
  1228. 'FloatNumber',
  1229. $name,
  1230. $label,
  1231. $attributes
  1232. );
  1233. $this->applyFilter($name, 'trim');
  1234. if ($required) {
  1235. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1236. }
  1237. // Rule allows "," and "."
  1238. /*$this->addRule(
  1239. $name,
  1240. get_lang('OnlyNumbers'),
  1241. 'regex',
  1242. '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)|(^-?\d\d*\,\d*$)|(^-?\,\d\d*$)/'
  1243. );*/
  1244. if ($allowNegative == false) {
  1245. $this->addRule(
  1246. $name,
  1247. get_lang('NegativeValue'),
  1248. 'compare',
  1249. '>=',
  1250. 'server',
  1251. false,
  1252. false,
  1253. 0
  1254. );
  1255. }
  1256. if (!is_null($minValue)) {
  1257. $this->addRule(
  1258. $name,
  1259. get_lang('UnderMin'),
  1260. 'compare',
  1261. '>=',
  1262. 'server',
  1263. false,
  1264. false,
  1265. $minValue
  1266. );
  1267. }
  1268. if (!is_null($maxValue)) {
  1269. $this->addRule(
  1270. $name,
  1271. get_lang('OverMax'),
  1272. 'compare',
  1273. '<=',
  1274. 'server',
  1275. false,
  1276. false,
  1277. $maxValue
  1278. );
  1279. }
  1280. }
  1281. /**
  1282. * Adds a text field for letters and spaces to the form.
  1283. * A trim-filter is attached to the field.
  1284. *
  1285. * @param string $name The element name
  1286. * @param string $label The label for the form-element
  1287. * @param bool $required Optional. Is the form-element required (default=true)
  1288. * @param array $attributes Optional. List of attributes for the form-element
  1289. */
  1290. public function addTextLettersAndSpaces(
  1291. $name,
  1292. $label,
  1293. $required = false,
  1294. $attributes = []
  1295. ) {
  1296. $attributes = array_merge(
  1297. $attributes,
  1298. [
  1299. 'pattern' => '[a-zA-ZñÑ\s]+',
  1300. 'title' => get_lang('OnlyLettersAndSpaces'),
  1301. ]
  1302. );
  1303. $this->addElement(
  1304. 'text',
  1305. $name,
  1306. [
  1307. $label,
  1308. get_lang('OnlyLettersAndSpaces'),
  1309. ],
  1310. $attributes
  1311. );
  1312. $this->applyFilter($name, 'trim');
  1313. if ($required) {
  1314. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1315. }
  1316. $this->addRule(
  1317. $name,
  1318. get_lang('OnlyLettersAndSpaces'),
  1319. 'regex',
  1320. '/^[a-zA-ZñÑ\s]+$/'
  1321. );
  1322. }
  1323. /**
  1324. * Adds a text field for alphanumeric and spaces characters to the form.
  1325. * A trim-filter is attached to the field.
  1326. *
  1327. * @param string $name The element name
  1328. * @param string $label The label for the form-element
  1329. * @param bool $required Optional. Is the form-element required (default=true)
  1330. * @param array $attributes Optional. List of attributes for the form-element
  1331. */
  1332. public function addTextAlphanumericAndSpaces(
  1333. $name,
  1334. $label,
  1335. $required = false,
  1336. $attributes = []
  1337. ) {
  1338. $attributes = array_merge(
  1339. $attributes,
  1340. [
  1341. 'pattern' => '[a-zA-Z0-9ñÑ\s]+',
  1342. 'title' => get_lang('OnlyLettersAndNumbersAndSpaces'),
  1343. ]
  1344. );
  1345. $this->addElement(
  1346. 'text',
  1347. $name,
  1348. [
  1349. $label,
  1350. get_lang('OnlyLettersAndNumbersAndSpaces'),
  1351. ],
  1352. $attributes
  1353. );
  1354. $this->applyFilter($name, 'trim');
  1355. if ($required) {
  1356. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1357. }
  1358. $this->addRule(
  1359. $name,
  1360. get_lang('OnlyLettersAndNumbersAndSpaces'),
  1361. 'regex',
  1362. '/^[a-zA-Z0-9ñÑ\s]+$/'
  1363. );
  1364. }
  1365. /**
  1366. * @param string $url
  1367. * @param string $urlToRedirect after upload redirect to this page
  1368. */
  1369. public function addMultipleUpload($url, $urlToRedirect = '')
  1370. {
  1371. $inputName = 'input_file_upload';
  1372. $this->addMultipleUploadJavascript($url, $inputName, $urlToRedirect);
  1373. $this->addHtml('
  1374. <div class="description-upload">
  1375. '.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'
  1376. </div>
  1377. <span class="btn btn-success fileinput-button">
  1378. <i class="glyphicon glyphicon-plus"></i>
  1379. <span>'.get_lang('AddFiles').'</span>
  1380. <!-- The file input field used as target for the file upload widget -->
  1381. <input id="'.$inputName.'" type="file" name="files[]" multiple>
  1382. </span>
  1383. <div id="dropzone">
  1384. <div class="button-load">
  1385. '.get_lang('UploadFiles').'
  1386. </div>
  1387. </div>
  1388. <br />
  1389. <!-- The global progress bar -->
  1390. <div id="progress" class="progress">
  1391. <div class="progress-bar progress-bar-success"></div>
  1392. </div>
  1393. <div id="files" class="files"></div>
  1394. ');
  1395. }
  1396. /**
  1397. * @param string $elementName
  1398. * @param string $groupName if element is inside a group
  1399. *
  1400. * @throws Exception
  1401. */
  1402. public function addPasswordRule($elementName, $groupName = '')
  1403. {
  1404. // Constant defined in old config/profile.conf.php
  1405. if (CHECK_PASS_EASY_TO_FIND === true) {
  1406. $message = get_lang('PassTooEasy').': '.api_generate_password();
  1407. if (!empty($groupName)) {
  1408. $groupObj = $this->getElement($groupName);
  1409. if ($groupObj instanceof HTML_QuickForm_group) {
  1410. $elementName = $groupObj->getElementName($elementName);
  1411. if ($elementName === false) {
  1412. throw new Exception("The $groupName doesn't have the element $elementName");
  1413. }
  1414. $this->_rules[$elementName][] = [
  1415. 'type' => 'callback',
  1416. 'format' => 'api_check_password',
  1417. 'message' => $message,
  1418. 'validation' => '',
  1419. 'reset' => false,
  1420. 'group' => $groupName,
  1421. ];
  1422. }
  1423. } else {
  1424. $this->addRule(
  1425. $elementName,
  1426. $message,
  1427. 'callback',
  1428. 'api_check_password'
  1429. );
  1430. }
  1431. }
  1432. }
  1433. /**
  1434. * Add an element with user ID and avatar to the form.
  1435. * It needs a Chamilo\UserBundle\Entity\User as value. The exported value is the Chamilo\UserBundle\Entity\User ID.
  1436. *
  1437. * @see \UserAvatar
  1438. *
  1439. * @param string $name
  1440. * @param string $label
  1441. * @param string $imageSize Optional. Small, medium or large image
  1442. * @param string $subtitle Optional. The subtitle for the field
  1443. *
  1444. * @return \UserAvatar
  1445. */
  1446. public function addUserAvatar($name, $label, $imageSize = 'small', $subtitle = '')
  1447. {
  1448. return $this->addElement('UserAvatar', $name, $label, ['image_size' => $imageSize, 'sub_title' => $subtitle]);
  1449. }
  1450. /**
  1451. * @param array $typeList
  1452. */
  1453. public function addEmailTemplate($typeList)
  1454. {
  1455. $mailManager = new MailTemplateManager();
  1456. foreach ($typeList as $type) {
  1457. $list = $mailManager->get_all(
  1458. ['where' => ['type = ? AND url_id = ?' => [$type, api_get_current_access_url_id()]]]
  1459. );
  1460. $options = [get_lang('Select')];
  1461. $name = $type;
  1462. $defaultId = '';
  1463. foreach ($list as $item) {
  1464. $options[$item['id']] = $item['name'];
  1465. $name = $item['name'];
  1466. if (empty($defaultId)) {
  1467. $defaultId = $item['default_template'] == 1 ? $item['id'] : '';
  1468. }
  1469. }
  1470. $url = api_get_path(WEB_AJAX_PATH).'mail.ajax.php?a=select_option';
  1471. $typeNoDots = 'email_template_option_'.str_replace('.tpl', '', $type);
  1472. $this->addSelect(
  1473. 'email_template_option['.$type.']',
  1474. $name,
  1475. $options,
  1476. ['id' => $typeNoDots]
  1477. );
  1478. $templateNoDots = 'email_template_'.str_replace('.tpl', '', $type);
  1479. $templateNoDotsBlock = 'email_template_block_'.str_replace('.tpl', '', $type);
  1480. $this->addHtml('<div id="'.$templateNoDotsBlock.'" style="display:none">');
  1481. $this->addTextarea(
  1482. $templateNoDots,
  1483. get_lang('Preview'),
  1484. ['disabled' => 'disabled ', 'id' => $templateNoDots, 'rows' => '5']
  1485. );
  1486. $this->addHtml('</div>');
  1487. $this->addHtml("<script>
  1488. $(function() {
  1489. var defaultValue = '$defaultId';
  1490. $('#$typeNoDots').val(defaultValue);
  1491. $('#$typeNoDots').selectpicker('render');
  1492. if (defaultValue != '') {
  1493. var selected = $('#$typeNoDots option:selected').val();
  1494. $.ajax({
  1495. url: '$url' + '&id=' + selected+ '&template_name=$type',
  1496. success: function (data) {
  1497. $('#$templateNoDots').html(data);
  1498. $('#$templateNoDotsBlock').show();
  1499. return;
  1500. },
  1501. });
  1502. }
  1503. $('#$typeNoDots').on('change', function(){
  1504. var selected = $('#$typeNoDots option:selected').val();
  1505. $.ajax({
  1506. url: '$url' + '&id=' + selected,
  1507. success: function (data) {
  1508. $('#$templateNoDots').html(data);
  1509. $('#$templateNoDotsBlock').show();
  1510. return;
  1511. },
  1512. });
  1513. });
  1514. });
  1515. </script>");
  1516. }
  1517. }
  1518. /**
  1519. * @param string $url page that will handle the upload
  1520. * @param string $inputName
  1521. * @param string $urlToRedirect
  1522. */
  1523. private function addMultipleUploadJavascript($url, $inputName, $urlToRedirect = '')
  1524. {
  1525. $redirectCondition = '';
  1526. if (!empty($urlToRedirect)) {
  1527. $redirectCondition = "window.location.replace('$urlToRedirect'); ";
  1528. }
  1529. $icon = Display::return_icon('file_txt.gif');
  1530. $this->addHtml("
  1531. <script>
  1532. $(function () {
  1533. 'use strict';
  1534. $('#".$this->getAttribute('id')."').submit(function() {
  1535. return false;
  1536. });
  1537. $('#dropzone').on('click', function() {
  1538. $('#".$inputName."').click();
  1539. });
  1540. var url = '".$url."';
  1541. var uploadButton = $('<button/>')
  1542. .addClass('btn btn-primary')
  1543. .prop('disabled', true)
  1544. .text('".addslashes(get_lang('Loading'))."')
  1545. .on('click', function () {
  1546. var \$this = $(this),
  1547. data = \$this.data();
  1548. \$this
  1549. .off('click')
  1550. .text('".addslashes(get_lang('Cancel'))."')
  1551. .on('click', function () {
  1552. \$this.remove();
  1553. data.abort();
  1554. });
  1555. data.submit().always(function () {
  1556. \$this.remove();
  1557. });
  1558. });
  1559. $('#".$inputName."').fileupload({
  1560. url: url,
  1561. dataType: 'json',
  1562. // Enable image resizing, except for Android and Opera,
  1563. // which actually support image resizing, but fail to
  1564. // send Blob objects via XHR requests:
  1565. disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
  1566. previewMaxWidth: 300,
  1567. previewMaxHeight: 169,
  1568. previewCrop: true,
  1569. dropzone: $('#dropzone'),
  1570. }).on('fileuploadadd', function (e, data) {
  1571. data.context = $('<div class=\"row\" />').appendTo('#files');
  1572. $.each(data.files, function (index, file) {
  1573. var node = $('<div class=\"col-sm-5 file_name\">').text(file.name);
  1574. node.appendTo(data.context);
  1575. });
  1576. }).on('fileuploadprocessalways', function (e, data) {
  1577. var index = data.index,
  1578. file = data.files[index],
  1579. node = $(data.context.children()[index]);
  1580. if (file.preview) {
  1581. data.context.prepend($('<div class=\"col-sm-4\">').html(file.preview));
  1582. } else {
  1583. data.context.prepend($('<div class=\"col-sm-4\">').html('".$icon."'));
  1584. }
  1585. if (index + 1 === data.files.length) {
  1586. data.context.find('button')
  1587. .text('Upload')
  1588. .prop('disabled', !!data.files.error);
  1589. }
  1590. }).on('fileuploadprogressall', function (e, data) {
  1591. var progress = parseInt(data.loaded / data.total * 100, 10);
  1592. $('#progress .progress-bar').css(
  1593. 'width',
  1594. progress + '%'
  1595. );
  1596. }).on('fileuploaddone', function (e, data) {
  1597. $.each(data.result.files, function (index, file) {
  1598. if (file.error) {
  1599. var link = $('<div>')
  1600. .attr({class : 'panel-image'}) ;
  1601. $(data.context.children()[index]).parent().wrap(link);
  1602. // Update file name with new one from Chamilo
  1603. $(data.context.children()[index]).parent().find('.file_name').html(file.name);
  1604. var message = $('<div class=\"col-sm-3\">').html(
  1605. $('<span class=\"message-image-danger\"/>').text(file.error)
  1606. );
  1607. $(data.context.children()[index]).parent().append(message);
  1608. return;
  1609. }
  1610. if (file.url) {
  1611. var link = $('<a>')
  1612. .attr({target: '_blank', class : 'panel-image'})
  1613. .prop('href', file.url);
  1614. $(data.context.children()[index]).parent().wrap(link);
  1615. }
  1616. // Update file name with new one from Chamilo
  1617. $(data.context.children()[index]).parent().find('.file_name').html(file.name);
  1618. var message = $('<div class=\"col-sm-3\">').html(
  1619. $('<span class=\"message-image-success\"/>').text('".addslashes(get_lang('UplUploadSucceeded'))."')
  1620. );
  1621. $(data.context.children()[index]).parent().append(message);
  1622. });
  1623. $('#dropzone').removeClass('hover');
  1624. ".$redirectCondition."
  1625. }).on('fileuploadfail', function (e, data) {
  1626. $.each(data.files, function (index) {
  1627. var failedMessage = '".addslashes(get_lang('UplUploadFailed'))."';
  1628. var error = $('<div class=\"col-sm-3\">').html(
  1629. $('<span class=\"alert alert-danger\"/>').text(failedMessage)
  1630. );
  1631. $(data.context.children()[index]).parent().append(error);
  1632. });
  1633. $('#dropzone').removeClass('hover');
  1634. }).prop('disabled', !$.support.fileInput).parent().addClass($.support.fileInput ? undefined : 'disabled');
  1635. $('#dropzone').on('dragover', function (e) {
  1636. // dragleave callback implementation
  1637. $('#dropzone').addClass('hover');
  1638. });
  1639. $('#dropzone').on('dragleave', function (e) {
  1640. $('#dropzone').removeClass('hover');
  1641. });
  1642. $('.fileinput-button').hide();
  1643. });
  1644. </script>");
  1645. }
  1646. }
  1647. /**
  1648. * Cleans HTML text filter.
  1649. *
  1650. * @param string $html HTML to clean
  1651. * @param int $mode (optional)
  1652. *
  1653. * @return string The cleaned HTML
  1654. */
  1655. function html_filter($html, $mode = NO_HTML)
  1656. {
  1657. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  1658. $cleaned_html = kses($html, $allowed_tags);
  1659. return $cleaned_html;
  1660. }
  1661. function html_filter_teacher($html)
  1662. {
  1663. return html_filter($html, TEACHER_HTML);
  1664. }
  1665. function html_filter_student($html)
  1666. {
  1667. return html_filter($html, STUDENT_HTML);
  1668. }
  1669. function html_filter_teacher_fullpage($html)
  1670. {
  1671. return html_filter($html, TEACHER_HTML_FULLPAGE);
  1672. }
  1673. function html_filter_student_fullpage($html)
  1674. {
  1675. return html_filter($html, STUDENT_HTML_FULLPAGE);
  1676. }
  1677. /**
  1678. * Cleans mobile phone number text.
  1679. *
  1680. * @param string $mobilePhoneNumber Mobile phone number to clean
  1681. *
  1682. * @return string The cleaned mobile phone number
  1683. */
  1684. function mobile_phone_number_filter($mobilePhoneNumber)
  1685. {
  1686. $mobilePhoneNumber = str_replace(['+', '(', ')'], '', $mobilePhoneNumber);
  1687. return ltrim($mobilePhoneNumber, '0');
  1688. }