FormValidator.class.php 56 KB

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