FormValidator.class.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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. * @param string $name Name of the form
  18. * @param string $method (optional Method ('post' (default) or 'get')
  19. * @param string $action (optional Action (default is $PHP_SELF)
  20. * @param string $target (optional Form's target defaults to '_self'
  21. * @param mixed $attributes (optional) Extra attributes for <form> tag
  22. * @param string $layout
  23. * @param bool $trackSubmit (optional) Whether to track if the form was
  24. * submitted by adding a special hidden field (default = true)
  25. */
  26. public function __construct(
  27. $name,
  28. $method = 'post',
  29. $action = '',
  30. $target = '',
  31. $attributes = array(),
  32. $layout = self::LAYOUT_HORIZONTAL,
  33. $trackSubmit = true
  34. ) {
  35. // Default form class.
  36. if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) {
  37. $attributes['class'] = 'form-horizontal';
  38. }
  39. if (isset($attributes['class']) && strpos($attributes['class'], 'form-search') !== false) {
  40. $layout = 'inline';
  41. }
  42. $this->setLayout($layout);
  43. switch ($layout) {
  44. case self::LAYOUT_HORIZONTAL:
  45. $attributes['class'] = 'form-horizontal';
  46. break;
  47. case self::LAYOUT_INLINE:
  48. case self::LAYOUT_BOX:
  49. $attributes['class'] = 'form-inline';
  50. break;
  51. }
  52. parent::__construct($name, $method, $action, $target, $attributes, $trackSubmit);
  53. // Modify the default templates
  54. $renderer = & $this->defaultRenderer();
  55. // Form template
  56. $formTemplate = $this->getFormTemplate();
  57. $renderer->setFormTemplate($formTemplate);
  58. // Element template
  59. if (isset($attributes['class']) && $attributes['class'] == 'form-inline') {
  60. $elementTemplate = ' {label} {element} ';
  61. $renderer->setElementTemplate($elementTemplate);
  62. } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') {
  63. $elementTemplate = ' {label} {element} ';
  64. $renderer->setElementTemplate($elementTemplate);
  65. } else {
  66. $renderer->setElementTemplate($this->getDefaultElementTemplate());
  67. // Display a gray div in the buttons
  68. $templateSimple = '<div class="form-actions">{label} {element}</div>';
  69. $renderer->setElementTemplate($templateSimple, 'submit_in_actions');
  70. //Display a gray div in the buttons + makes the button available when scrolling
  71. $templateBottom = '<div class="form-actions bottom_actions bg-form">{label} {element}</div>';
  72. $renderer->setElementTemplate($templateBottom, 'submit_fixed_in_bottom');
  73. //When you want to group buttons use something like this
  74. /* $group = array();
  75. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  76. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  77. $form->addGroup($group, 'buttons_in_action');
  78. */
  79. $renderer->setElementTemplate($templateSimple, 'buttons_in_action');
  80. $templateSimpleRight = '<div class="form-actions"> <div class="pull-right">{label} {element}</div></div>';
  81. $renderer->setElementTemplate($templateSimpleRight, 'buttons_in_action_right');
  82. }
  83. //Set Header template
  84. $renderer->setHeaderTemplate('<legend>{header}</legend>');
  85. //Set required field template
  86. $this->setRequiredNote('<span class="form_required">*</span> <small>' . get_lang('ThisFieldIsRequired') . '</small>');
  87. $noteTemplate = <<<EOT
  88. <div class="form-group">
  89. <div class="col-sm-offset-2 col-sm-10">{requiredNote}</div>
  90. </div>
  91. EOT;
  92. $renderer->setRequiredNoteTemplate($noteTemplate);
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function getFormTemplate()
  98. {
  99. return '<form{attributes}>
  100. <fieldset>
  101. {content}
  102. <div class="clear"></div>
  103. </fieldset>
  104. {hidden}
  105. </form>';
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getDefaultElementTemplate()
  111. {
  112. return '
  113. <div class="form-group {error_class}">
  114. <label {label-for} class="col-sm-2 control-label" >
  115. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  116. {label}
  117. </label>
  118. <div class="col-sm-8">
  119. {icon}
  120. {element}
  121. <!-- BEGIN label_2 -->
  122. <p class="help-block">{label_2}</p>
  123. <!-- END label_2 -->
  124. <!-- BEGIN error -->
  125. <span class="help-inline">{error}</span>
  126. <!-- END error -->
  127. </div>
  128. <div class="col-sm-2">
  129. <!-- BEGIN label_3 -->
  130. {label_3}
  131. <!-- END label_3 -->
  132. </div>
  133. </div>';
  134. }
  135. /**
  136. * @return string
  137. */
  138. public function getLayout()
  139. {
  140. return $this->layout;
  141. }
  142. /**
  143. * @param string $layout
  144. */
  145. public function setLayout($layout)
  146. {
  147. $this->layout = $layout;
  148. }
  149. /**
  150. * Adds a text field to the form.
  151. * A trim-filter is attached to the field.
  152. * @param string $label The label for the form-element
  153. * @param string $name The element name
  154. * @param bool $required (optional) Is the form-element required (default=true)
  155. * @param array $attributes (optional) List of attributes for the form-element
  156. */
  157. public function addText($name, $label, $required = true, $attributes = array())
  158. {
  159. $this->addElement('text', $name, $label, $attributes);
  160. $this->applyFilter($name, 'trim');
  161. if ($required) {
  162. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  163. }
  164. }
  165. /**
  166. * The "date_range_picker" element creates 2 hidden fields
  167. * "elementName" + "_start" and "elementName" + "_end"
  168. * For example if the name is "range", you will have 2 new fields
  169. * when executing $form->getSubmitValues()
  170. * "range_start" and "range_end"
  171. *
  172. * @param string $name
  173. * @param string $label
  174. * @param bool $required
  175. * @param array $attributes
  176. */
  177. public function addDateRangePicker($name, $label, $required = true, $attributes = array())
  178. {
  179. $this->addElement('date_range_picker', $name, $label, $attributes);
  180. $this->addElement('hidden', $name.'_start');
  181. $this->addElement('hidden', $name.'_end');
  182. if ($required) {
  183. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  184. }
  185. }
  186. /**
  187. * @param string $name
  188. * @param string $label
  189. * @param array $attributes
  190. *
  191. * @return mixed
  192. */
  193. public function addDatePicker($name, $label, $attributes = [])
  194. {
  195. return $this->addElement('DatePicker', $name, $label, $attributes);
  196. }
  197. /**
  198. * @param string $name
  199. * @param string $label
  200. * @param array $attributes
  201. *
  202. * @return mixed
  203. */
  204. public function addDateTimePicker($name, $label, $attributes = [])
  205. {
  206. return $this->addElement('DateTimePicker', $name, $label, $attributes);
  207. }
  208. /**
  209. * @param string $name
  210. * @param string $value
  211. */
  212. public function addHidden($name, $value)
  213. {
  214. $this->addElement('hidden', $name, $value);
  215. }
  216. /**
  217. * @param string $name
  218. * @param string $label
  219. * @param array $attributes
  220. *
  221. * @return HTML_QuickForm_textarea
  222. */
  223. public function addTextarea($name, $label, $attributes = array())
  224. {
  225. return $this->addElement('textarea', $name, $label, $attributes);
  226. }
  227. /**
  228. * @param string $name
  229. * @param string $label
  230. * @param string $icon font-awesome
  231. * @param string $style default|primary|success|info|warning|danger|link
  232. * @param string $size large|default|small|extra-small
  233. * @param string $class Example plus is transformed to icon fa fa-plus
  234. * @param array $attributes
  235. *
  236. * @return HTML_QuickForm_button
  237. */
  238. public function addButton(
  239. $name,
  240. $label,
  241. $icon = 'check',
  242. $style = 'default',
  243. $size = 'default',
  244. $class = null,
  245. $attributes = array(),
  246. $createElement = false
  247. ) {
  248. if ($createElement) {
  249. return $this->createElement(
  250. 'button',
  251. $name,
  252. $label,
  253. $icon,
  254. $style,
  255. $size,
  256. $class,
  257. $attributes
  258. );
  259. }
  260. return $this->addElement(
  261. 'button',
  262. $name,
  263. $label,
  264. $icon,
  265. $style,
  266. $size,
  267. $class,
  268. $attributes
  269. );
  270. }
  271. /**
  272. * Returns a button with the primary color and a check mark
  273. * @param string $label Text appearing on the button
  274. * @param string $name Element name (for form treatment purposes)
  275. * @param bool $createElement Whether to use the create or add method
  276. *
  277. * @return HTML_QuickForm_button
  278. */
  279. public function addButtonSave($label, $name = 'submit', $createElement = false)
  280. {
  281. return $this->addButton(
  282. $name,
  283. $label,
  284. 'check',
  285. 'primary',
  286. null,
  287. null,
  288. array(),
  289. $createElement
  290. );
  291. }
  292. /**
  293. * Returns a cancel button
  294. * @param string $label Text appearing on the button
  295. * @param string $name Element name (for form treatment purposes)
  296. * @param bool $createElement Whether to use the create or add method
  297. *
  298. * @return HTML_QuickForm_button
  299. */
  300. public function addButtonCancel($label, $name = 'submit', $createElement = false)
  301. {
  302. return $this->addButton(
  303. $name,
  304. $label,
  305. 'times',
  306. 'btn',
  307. null,
  308. null,
  309. array(),
  310. $createElement
  311. );
  312. }
  313. /**
  314. * Returns a button with the primary color and a "plus" icon
  315. * @param string $label Text appearing on the button
  316. * @param string $name Element name (for form treatment purposes)
  317. * @param bool $createElement Whether to use the create or add method
  318. * @param array $attributes Additional attributes
  319. *
  320. * @return HTML_QuickForm_button
  321. */
  322. public function addButtonCreate($label, $name = 'submit', $createElement = false, $attributes = array())
  323. {
  324. return $this->addButton(
  325. $name,
  326. $label,
  327. 'plus',
  328. 'primary',
  329. null,
  330. null,
  331. $attributes,
  332. $createElement
  333. );
  334. }
  335. /**
  336. * Returns a button with the primary color and a pencil icon
  337. * @param string $label Text appearing on the button
  338. * @param string $name Element name (for form treatment purposes)
  339. * @param bool $createElement Whether to use the create or add method
  340. * @return HTML_QuickForm_button
  341. */
  342. public function addButtonUpdate($label, $name = 'submit', $createElement = false)
  343. {
  344. return $this->addButton(
  345. $name,
  346. $label,
  347. 'pencil',
  348. 'primary',
  349. null,
  350. null,
  351. array(),
  352. $createElement
  353. );
  354. }
  355. /**
  356. * Returns a button with the danger color and a trash icon
  357. * @param string $label Text appearing on the button
  358. * @param string $name Element name (for form treatment purposes)
  359. * @param bool $createElement Whether to use the create or add method
  360. *
  361. * @return HTML_QuickForm_button
  362. */
  363. public function addButtonDelete($label, $name = 'submit', $createElement = false)
  364. {
  365. return $this->addButton(
  366. $name,
  367. $label,
  368. 'trash',
  369. 'danger',
  370. null,
  371. null,
  372. array(),
  373. $createElement
  374. );
  375. }
  376. /**
  377. * Returns a button with the primary color and a paper-plane icon
  378. * @param string $label Text appearing on the button
  379. * @param string $name Element name (for form treatment purposes)
  380. * @param bool $createElement Whether to use the create or add method
  381. *
  382. * @return HTML_QuickForm_button
  383. */
  384. public function addButtonSend($label, $name = 'submit', $createElement = false)
  385. {
  386. return $this->addButton(
  387. $name,
  388. $label,
  389. 'paper-plane',
  390. 'primary',
  391. null,
  392. null,
  393. array(),
  394. $createElement
  395. );
  396. }
  397. /**
  398. * Returns a button with the default (grey?) color and a magnifier icon
  399. * @param string $label Text appearing on the button
  400. * @param string $name Element name (for form treatment purposes)
  401. *
  402. * @return HTML_QuickForm_button
  403. */
  404. public function addButtonSearch($label = null, $name = 'submit')
  405. {
  406. if (empty($label)) {
  407. $label = get_lang('Search');
  408. }
  409. return $this->addButton($name, $label, 'search', 'default');
  410. }
  411. /**
  412. * Returns a button with the primary color and a right-pointing arrow icon
  413. * @param string $label Text appearing on the button
  414. * @param string $name Element name (for form treatment purposes)
  415. * @param array $attributes Additional attributes
  416. * @return HTML_QuickForm_button
  417. */
  418. public function addButtonNext($label, $name = 'submit',$attributes = array())
  419. {
  420. return $this->addButton($name, $label, 'arrow-right', 'primary', null, null, $attributes);
  421. }
  422. /**
  423. * Returns a button with the primary color and a check mark icon
  424. * @param string $label Text appearing on the button
  425. * @param string $name Element name (for form treatment purposes)
  426. * @param bool $createElement Whether to use the create or add method
  427. * @return HTML_QuickForm_button
  428. */
  429. public function addButtonImport($label, $name = 'submit', $createElement = false)
  430. {
  431. return $this->addButton(
  432. $name,
  433. $label,
  434. 'check',
  435. 'primary',
  436. null,
  437. null,
  438. array(),
  439. $createElement
  440. );
  441. }
  442. /**
  443. * Returns a button with the primary color and a check-mark icon
  444. * @param string $label Text appearing on the button
  445. * @param string $name Element name (for form treatment purposes)
  446. * @param bool $createElement Whether to use the create or add method
  447. * @return HTML_QuickForm_button
  448. */
  449. public function addButtonExport($label, $name = 'submit', $createElement = false)
  450. {
  451. return $this->addButton(
  452. $name,
  453. $label,
  454. 'check',
  455. 'primary',
  456. null,
  457. null,
  458. array(),
  459. $createElement
  460. );
  461. }
  462. /**
  463. * Shortcut to filter button
  464. * @param string $label Text appearing on the button
  465. * @param string $name Element name (for form treatment purposes)
  466. * @param bool $createElement Whether to use the create or add method
  467. * @return HTML_QuickForm_button
  468. */
  469. public function addButtonFilter($label, $name = 'submit', $createElement = false)
  470. {
  471. return $this->addButton(
  472. $name,
  473. $label,
  474. 'filter',
  475. 'primary',
  476. null,
  477. null,
  478. array(),
  479. $createElement
  480. );
  481. }
  482. /**
  483. * Shortcut to reset button
  484. * @param string $label Text appearing on the button
  485. * @param string $name Element name (for form treatment purposes)
  486. * @param bool $createElement Whether to use the create or add method
  487. * @return HTML_QuickForm_button
  488. */
  489. public function addButtonReset($label, $name = 'reset', $createElement = false)
  490. {
  491. $icon = 'eraser';
  492. $style = 'default';
  493. $size = 'default';
  494. $class = null;
  495. $attributes = array();
  496. if ($createElement) {
  497. return $this->createElement(
  498. 'reset',
  499. $name,
  500. $label,
  501. $icon,
  502. $style,
  503. $size,
  504. $class,
  505. $attributes
  506. );
  507. }
  508. return $this->addElement(
  509. 'reset',
  510. $name,
  511. $label,
  512. $icon,
  513. $style,
  514. $size,
  515. $class,
  516. $attributes
  517. );
  518. }
  519. /**
  520. * Returns a button with the primary color and an upload icon
  521. * @param string $label Text appearing on the button
  522. * @param string $name Element name (for form treatment purposes)
  523. * @param bool $createElement Whether to use the create or add method
  524. *
  525. * @return HTML_QuickForm_button
  526. */
  527. public function addButtonUpload($label, $name = 'submit', $createElement = false)
  528. {
  529. return $this->addButton(
  530. $name,
  531. $label,
  532. 'upload',
  533. 'primary',
  534. null,
  535. null,
  536. array(),
  537. $createElement
  538. );
  539. }
  540. /**
  541. * Returns a button with the primary color and a download icon
  542. * @param string $label Text appearing on the button
  543. * @param string $name Element name (for form treatment purposes)
  544. * @param bool $createElement Whether to use the create or add method
  545. *
  546. * @return HTML_QuickForm_button
  547. */
  548. public function addButtonDownload($label, $name = 'submit', $createElement = false)
  549. {
  550. return $this->addButton(
  551. $name,
  552. $label,
  553. 'download',
  554. 'primary',
  555. null,
  556. null,
  557. array(),
  558. $createElement
  559. );
  560. }
  561. /**
  562. * Returns a button with the primary color and a magnifier icon
  563. * @param string $label Text appearing on the button
  564. * @param string $name Element name (for form treatment purposes)
  565. * @param bool $createElement Whether to use the create or add method
  566. *
  567. * @return HTML_QuickForm_button
  568. */
  569. public function addButtonPreview($label, $name = 'submit', $createElement = false)
  570. {
  571. return $this->addButton(
  572. $name,
  573. $label,
  574. 'search',
  575. 'primary',
  576. null,
  577. null,
  578. array(),
  579. $createElement
  580. );
  581. }
  582. /**
  583. * Returns a button with the primary color and a copy (double sheet) icon
  584. * @param string $label Text appearing on the button
  585. * @param string $name Element name (for form treatment purposes)
  586. * @param bool $createElement Whether to use the create or add method
  587. *
  588. * @return HTML_QuickForm_button
  589. */
  590. public function addButtonCopy($label, $name = 'submit', $createElement = false)
  591. {
  592. return $this->addButton(
  593. $name,
  594. $label,
  595. 'copy',
  596. 'primary',
  597. null,
  598. null,
  599. array(),
  600. $createElement
  601. );
  602. }
  603. /**
  604. * @param string $name
  605. * @param string $label
  606. * @param string $text
  607. * @param array $attributes
  608. *
  609. * @return HTML_QuickForm_checkbox
  610. */
  611. public function addCheckBox($name, $label, $text = '', $attributes = array())
  612. {
  613. return $this->addElement('checkbox', $name, $label, $text, $attributes);
  614. }
  615. /**
  616. * @param string $name
  617. * @param string $label
  618. * @param array $options
  619. * @param array $attributes
  620. *
  621. * @return HTML_QuickForm_group
  622. */
  623. public function addCheckBoxGroup($name, $label, $options = array(), $attributes = array())
  624. {
  625. $group = array();
  626. foreach ($options as $value => $text) {
  627. $attributes['value'] = $value;
  628. $group[] = $this->createElement('checkbox', $value, null, $text, $attributes);
  629. }
  630. return $this->addGroup($group, $name, $label);
  631. }
  632. /**
  633. * @param string $name
  634. * @param string $label
  635. * @param array $options
  636. * @param array $attributes
  637. *
  638. * @return HTML_QuickForm_radio
  639. */
  640. public function addRadio($name, $label, $options = array(), $attributes = array())
  641. {
  642. $group = array();
  643. foreach ($options as $key => $value) {
  644. $group[] = $this->createElement('radio', null, null, $value, $key, $attributes);
  645. }
  646. return $this->addGroup($group, $name, $label);
  647. }
  648. /**
  649. * @param string $name
  650. * @param string $label
  651. * @param array $options
  652. * @param array $attributes
  653. *
  654. * @return HTML_QuickForm_select
  655. */
  656. public function addSelect($name, $label, $options = array(), $attributes = array())
  657. {
  658. return $this->addElement('select', $name, $label, $options, $attributes);
  659. }
  660. /**
  661. * @param string $label
  662. * @param string $text
  663. *
  664. * @return HTML_QuickForm_label
  665. */
  666. public function addLabel($label, $text)
  667. {
  668. return $this->addElement('label', $label, $text);
  669. }
  670. /**
  671. * @param string $text
  672. */
  673. public function addHeader($text)
  674. {
  675. $this->addElement('header', $text);
  676. }
  677. /**
  678. * @param string $name
  679. * @param string $label
  680. * @param array $attributes
  681. */
  682. public function addFile($name, $label, $attributes = array())
  683. {
  684. $this->addElement('file', $name, $label, $attributes);
  685. }
  686. /**
  687. * @param string $snippet
  688. */
  689. public function addHtml($snippet)
  690. {
  691. $this->addElement('html', $snippet);
  692. }
  693. /**
  694. * Adds a HTML-editor to the form
  695. * @param string $name
  696. * @param string $label The label for the form-element
  697. * @param bool $required (optional) Is the form-element required (default=true)
  698. * @param bool $fullPage (optional) When it is true, the editor loads completed html code for a full page.
  699. * @param array $config (optional) Configuration settings for the online editor.
  700. * @param bool $style
  701. */
  702. public function addHtmlEditor($name, $label, $required = true, $fullPage = false, $config = array(), $style = false)
  703. {
  704. $config['rows'] = isset($config['rows']) ? $config['rows'] : 15;
  705. $config['cols'] = isset($config['cols']) ? $config['cols'] : 80;
  706. $this->addElement('html_editor', $name, $label, $config, $style);
  707. $this->applyFilter($name, 'trim');
  708. if ($required) {
  709. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  710. }
  711. /** @var HtmlEditor $element */
  712. $element = $this->getElement($name);
  713. if ($style) {
  714. $config['style'] = true;
  715. }
  716. if ($fullPage) {
  717. $config['fullPage'] = true;
  718. }
  719. if ($element->editor) {
  720. $element->editor->processConfig($config);
  721. }
  722. }
  723. /**
  724. * @param string $name
  725. * @param string $label
  726. *
  727. * @return mixed
  728. */
  729. public function addButtonAdvancedSettings($name, $label = '')
  730. {
  731. $label = !empty($label) ? $label : get_lang('AdvancedParameters');
  732. return $this->addElement('advanced_settings', $name, $label);
  733. }
  734. /**
  735. * Adds a progress bar to the form.
  736. *
  737. * Once the user submits the form, a progress bar (animated gif) is
  738. * displayed. The progress bar will disappear once the page has been
  739. * reloaded.
  740. *
  741. * @param int $delay (optional) The number of seconds between the moment the user
  742. * @param string $label (optional) Custom label to be shown
  743. *
  744. * submits the form and the start of the progress bar.
  745. * @deprecated ?
  746. */
  747. public function add_progress_bar($delay = 2, $label = '')
  748. {
  749. if (empty($label)) {
  750. $label = get_lang('PleaseStandBy');
  751. }
  752. $this->with_progress_bar = true;
  753. $this->updateAttributes("onsubmit=\"javascript: myUpload.start('dynamic_div','" . api_get_path(WEB_IMG_PATH) . "progress_bar.gif','" . $label . "','" . $this->getAttribute('id') . "')\"");
  754. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  755. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  756. }
  757. /**
  758. * Uses new functions (php 5.2) for displaying real upload progress.
  759. * @param string $upload_id The value of the field UPLOAD_IDENTIFIER, the second parameter (XXX) of the $form->addElement('file', XXX) sentence
  760. * @param string $element_after The first element of the form (to place at first UPLOAD_IDENTIFIER)
  761. * @param int $delay (optional) The frequency of the xajax call
  762. * @param bool $wait_after_upload (optional)
  763. */
  764. public function add_real_progress_bar($upload_id, $element_after, $delay = 2, $wait_after_upload = false)
  765. {
  766. if (!function_exists('uploadprogress_get_info')) {
  767. $this->add_progress_bar($delay);
  768. return;
  769. }
  770. $xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH) . 'upload.xajax.php');
  771. $xajax_upload->registerFunction('updateProgress');
  772. // IMPORTANT : must be the first element of the form
  773. $el = $this->insertElementBefore(FormValidator::createElement('html', '<input type="hidden" name="UPLOAD_IDENTIFIER" value="' . $upload_id . '" />'), $element_after);
  774. $this->addElement('html', '<br />');
  775. // Add div-element where the progress bar is to be displayed
  776. $this->addElement('html', '
  777. <div id="dynamic_div_container" style="display:none">
  778. <div id="dynamic_div_label">' . get_lang('UploadFile') . '</div>
  779. <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif);">
  780. <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_step.gif);background-repeat:repeat-x;background-position:center;"></div>
  781. </div>
  782. </div>');
  783. if ($wait_after_upload) {
  784. $this->addElement('html', '
  785. <div id="dynamic_div_waiter_container" style="display:none">
  786. <div id="dynamic_div_waiter_label">
  787. ' . get_lang('SlideshowConversion') . '
  788. </div>
  789. <div id="dynamic_div_waiter_frame">
  790. <img src="' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif" />
  791. </div>
  792. </div>
  793. ');
  794. }
  795. // Get the xajax code
  796. $this->addElement('html', $xajax_upload->getJavascript(api_get_path(WEB_LIBRARY_PATH) . 'xajax'));
  797. // Get the upload code
  798. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  799. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  800. if (!$wait_after_upload) {
  801. $wait_after_upload = 0;
  802. }
  803. // Add the upload event
  804. $this->updateAttributes("onsubmit=\"javascript: myUpload.startRealUpload('dynamic_div','" . $upload_id . "','" . $this->getAttribute('id') . "'," . $wait_after_upload . ")\"");
  805. }
  806. /**
  807. * This function has been created for avoiding changes directly within QuickForm class.
  808. * When we use it, the element is threated as 'required' to be dealt during validation.
  809. * @param array $element The array of elements
  810. * @param string $message The message displayed
  811. */
  812. public function add_multiple_required_rule($elements, $message)
  813. {
  814. $this->_required[] = $elements[0];
  815. $this->addRule($elements, $message, 'multiple_required');
  816. }
  817. /**
  818. * Displays the form.
  819. * If an element in the form didn't validate, an error message is showed
  820. * asking the user to complete the form.
  821. */
  822. public function display()
  823. {
  824. echo $this->returnForm();
  825. }
  826. /**
  827. * Returns the HTML code of the form.
  828. * @return string $return_value HTML code of the form
  829. */
  830. public function returnForm()
  831. {
  832. $error = false;
  833. /** @var HTML_QuickForm_element $element */
  834. foreach ($this->_elements as $element) {
  835. if (!is_null(parent::getElementError($element->getName()))) {
  836. $error = true;
  837. break;
  838. }
  839. }
  840. $returnValue = '';
  841. $js = null;
  842. if ($error) {
  843. $returnValue = Display::return_message(
  844. get_lang('FormHasErrorsPleaseComplete'),
  845. 'warning'
  846. );
  847. }
  848. $returnValue .= $js;
  849. $returnValue .= parent::toHtml();
  850. // Add div-element which is to hold the progress bar
  851. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  852. $returnValue .= '<div id="dynamic_div" style="display:block; margin-left:40%; margin-top:10px; height:50px;"></div>';
  853. }
  854. return $returnValue;
  855. }
  856. /**
  857. * Returns the HTML code of the form.
  858. * If an element in the form didn't validate, an error message is showed
  859. * asking the user to complete the form.
  860. *
  861. * @return string $return_value HTML code of the form
  862. *
  863. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  864. * @author Julio Montoya
  865. * @deprecated use returnForm()
  866. */
  867. public function return_form()
  868. {
  869. return $this->returnForm();
  870. }
  871. /**
  872. * Create a form validator based on an array of form data:
  873. *
  874. * array(
  875. * 'name' => 'zombie_report_parameters', //optional
  876. * 'method' => 'GET', //optional
  877. * 'items' => array(
  878. * array(
  879. * 'name' => 'ceiling',
  880. * 'label' => 'Ceiling', //optional
  881. * 'type' => 'date',
  882. * 'default' => date() //optional
  883. * ),
  884. * array(
  885. * 'name' => 'active_only',
  886. * 'label' => 'ActiveOnly',
  887. * 'type' => 'checkbox',
  888. * 'default' => true
  889. * ),
  890. * array(
  891. * 'name' => 'submit_button',
  892. * 'type' => 'style_submit_button',
  893. * 'value' => get_lang('Search'),
  894. * 'attributes' => array('class' => 'search')
  895. * )
  896. * )
  897. * );
  898. *
  899. * @param array $form_data
  900. * @deprecated use normal FormValidator construct
  901. *
  902. * @return FormValidator
  903. */
  904. public static function create($form_data)
  905. {
  906. if (empty($form_data)) {
  907. return null;
  908. }
  909. $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
  910. $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
  911. $form_action = isset($form_data['action']) ? $form_data['action'] : '';
  912. $form_target = isset($form_data['target']) ? $form_data['target'] : '';
  913. $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
  914. $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
  915. $reset = null;
  916. $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
  917. $defaults = array();
  918. foreach ($form_data['items'] as $item) {
  919. $name = $item['name'];
  920. $type = isset($item['type']) ? $item['type'] : 'text';
  921. $label = isset($item['label']) ? $item['label'] : '';
  922. if ($type == 'wysiwyg') {
  923. $element = $result->addHtmlEditor($name, $label);
  924. } else {
  925. $element = $result->addElement($type, $name, $label);
  926. }
  927. if (isset($item['attributes'])) {
  928. $attributes = $item['attributes'];
  929. $element->setAttributes($attributes);
  930. }
  931. if (isset($item['value'])) {
  932. $value = $item['value'];
  933. $element->setValue($value);
  934. }
  935. if (isset($item['default'])) {
  936. $defaults[$name] = $item['default'];
  937. }
  938. if (isset($item['rules'])) {
  939. $rules = $item['rules'];
  940. foreach ($rules as $rule) {
  941. $message = $rule['message'];
  942. $type = $rule['type'];
  943. $format = isset($rule['format']) ? $rule['format'] : null;
  944. $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
  945. $force = isset($rule['force']) ? $rule['force'] : false;
  946. $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
  947. }
  948. }
  949. }
  950. $result->setDefaults($defaults);
  951. return $result;
  952. }
  953. /**
  954. * @return HTML_QuickForm_Renderer_Default
  955. */
  956. public static function getDefaultRenderer()
  957. {
  958. return
  959. isset($GLOBALS['_HTML_QuickForm_default_renderer']) ?
  960. $GLOBALS['_HTML_QuickForm_default_renderer'] : null;
  961. }
  962. /**
  963. * Adds a input of type url to the form.
  964. * @param type $name The label for the form-element
  965. * @param type $label The element name
  966. * @param type $required Optional. Is the form-element required (default=true)
  967. * @param type $attributes Optional. List of attributes for the form-element
  968. */
  969. public function addUrl($name, $label, $required = true, $attributes = array())
  970. {
  971. $this->addElement('url', $name, $label, $attributes);
  972. $this->applyFilter($name, 'trim');
  973. $this->addRule($name, get_lang('InsertAValidUrl'), 'url');
  974. if ($required) {
  975. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  976. }
  977. }
  978. /**
  979. * Adds a text field for letters to the form.
  980. * A trim-filter is attached to the field.
  981. * @param string $name The element name
  982. * @param string $label The label for the form-element
  983. * @param bool $required Optional. Is the form-element required (default=true)
  984. * @param array $attributes Optional. List of attributes for the form-element
  985. */
  986. public function addTextLettersOnly(
  987. $name,
  988. $label,
  989. $required = false,
  990. $attributes = []
  991. )
  992. {
  993. $attributes = array_merge(
  994. $attributes,
  995. [
  996. 'pattern' => '[a-zA-ZñÑ]+',
  997. 'title' => get_lang('OnlyLetters')
  998. ]
  999. );
  1000. $this->addElement(
  1001. 'text',
  1002. $name,
  1003. [
  1004. $label,
  1005. get_lang('OnlyLetters')
  1006. ],
  1007. $attributes
  1008. );
  1009. $this->applyFilter($name, 'trim');
  1010. if ($required) {
  1011. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1012. }
  1013. $this->addRule(
  1014. $name,
  1015. get_lang('OnlyLetters'),
  1016. 'regex',
  1017. '/^[a-zA-ZñÑ]+$/'
  1018. );
  1019. }
  1020. /**
  1021. * Adds a text field for alphanumeric characters to the form.
  1022. * A trim-filter is attached to the field.
  1023. * @param string $name The element name
  1024. * @param string $label The label for the form-element
  1025. * @param bool $required Optional. Is the form-element required (default=true)
  1026. * @param array $attributes Optional. List of attributes for the form-element
  1027. */
  1028. public function addTextAlphanumeric(
  1029. $name,
  1030. $label,
  1031. $required = false,
  1032. $attributes = []
  1033. )
  1034. {
  1035. $attributes = array_merge(
  1036. $attributes,
  1037. [
  1038. 'pattern' => '[a-zA-Z0-9ñÑ]+',
  1039. 'title' => get_lang('OnlyLettersAndNumbers')
  1040. ]
  1041. );
  1042. $this->addElement(
  1043. 'text',
  1044. $name,
  1045. [
  1046. $label,
  1047. get_lang('OnlyLettersAndNumbers')
  1048. ],
  1049. $attributes
  1050. );
  1051. $this->applyFilter($name, 'trim');
  1052. if ($required) {
  1053. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1054. }
  1055. $this->addRule(
  1056. $name,
  1057. get_lang('OnlyLettersAndNumbers'),
  1058. 'regex',
  1059. '/^[a-zA-Z0-9ÑÑ]+$/'
  1060. );
  1061. }
  1062. /**
  1063. * Adds a text field for letters and spaces to the form.
  1064. * A trim-filter is attached to the field.
  1065. * @param string $name The element name
  1066. * @param string $label The label for the form-element
  1067. * @param bool $required Optional. Is the form-element required (default=true)
  1068. * @param array $attributes Optional. List of attributes for the form-element
  1069. */
  1070. public function addTextLettersAndSpaces(
  1071. $name,
  1072. $label,
  1073. $required = false,
  1074. $attributes = []
  1075. )
  1076. {
  1077. $attributes = array_merge(
  1078. $attributes,
  1079. [
  1080. 'pattern' => '[a-zA-ZñÑ\s]+',
  1081. 'title' => get_lang('OnlyLettersAndSpaces')
  1082. ]
  1083. );
  1084. $this->addElement(
  1085. 'text',
  1086. $name,
  1087. [
  1088. $label,
  1089. get_lang('OnlyLettersAndSpaces')
  1090. ],
  1091. $attributes
  1092. );
  1093. $this->applyFilter($name, 'trim');
  1094. if ($required) {
  1095. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1096. }
  1097. $this->addRule(
  1098. $name,
  1099. get_lang('OnlyLettersAndSpaces'),
  1100. 'regex',
  1101. '/^[a-zA-ZñÑ\s]+$/'
  1102. );
  1103. }
  1104. /**
  1105. * Adds a text field for alphanumeric and spaces characters to the form.
  1106. * A trim-filter is attached to the field.
  1107. * @param string $name The element name
  1108. * @param string $label The label for the form-element
  1109. * @param bool $required Optional. Is the form-element required (default=true)
  1110. * @param array $attributes Optional. List of attributes for the form-element
  1111. */
  1112. public function addTextAlphanumericAndSpaces(
  1113. $name,
  1114. $label,
  1115. $required = false,
  1116. $attributes = []
  1117. )
  1118. {
  1119. $attributes = array_merge(
  1120. $attributes,
  1121. [
  1122. 'pattern' => '[a-zA-Z0-9ñÑ\s]+',
  1123. 'title' => get_lang('OnlyLettersAndNumbersAndSpaces')
  1124. ]
  1125. );
  1126. $this->addElement(
  1127. 'text',
  1128. $name,
  1129. [
  1130. $label,
  1131. get_lang('OnlyLettersAndNumbersAndSpaces')
  1132. ],
  1133. $attributes
  1134. );
  1135. $this->applyFilter($name, 'trim');
  1136. if ($required) {
  1137. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1138. }
  1139. $this->addRule(
  1140. $name,
  1141. get_lang('OnlyLettersAndNumbersAndSpaces'),
  1142. 'regex',
  1143. '/^[a-zA-Z0-9ñÑ\s]+$/'
  1144. );
  1145. }
  1146. }
  1147. /**
  1148. * Cleans HTML text filter
  1149. * @param string $html HTML to clean
  1150. * @param int $mode (optional)
  1151. * @return string The cleaned HTML
  1152. */
  1153. function html_filter($html, $mode = NO_HTML)
  1154. {
  1155. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  1156. $cleaned_html = kses($html, $allowed_tags);
  1157. return $cleaned_html;
  1158. }
  1159. function html_filter_teacher($html)
  1160. {
  1161. return html_filter($html, TEACHER_HTML);
  1162. }
  1163. function html_filter_student($html)
  1164. {
  1165. return html_filter($html, STUDENT_HTML);
  1166. }
  1167. function html_filter_teacher_fullpage($html)
  1168. {
  1169. return html_filter($html, TEACHER_HTML_FULLPAGE);
  1170. }
  1171. function html_filter_student_fullpage($html)
  1172. {
  1173. return html_filter($html, STUDENT_HTML_FULLPAGE);
  1174. }
  1175. /**
  1176. * Cleans mobile phone number text
  1177. * @param string $mobilePhoneNumber Mobile phone number to clean
  1178. * @return string The cleaned mobile phone number
  1179. */
  1180. function mobile_phone_number_filter($mobilePhoneNumber)
  1181. {
  1182. $mobilePhoneNumber = str_replace(array('+', '(', ')'), '', $mobilePhoneNumber);
  1183. return ltrim($mobilePhoneNumber,'0');
  1184. }