FormValidator.class.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. </fieldset>
  103. {hidden}
  104. </form>';
  105. }
  106. /**
  107. * @return string
  108. */
  109. public function getDefaultElementTemplate()
  110. {
  111. return '
  112. <div class="form-group {error_class}">
  113. <label {label-for} class="col-sm-2 control-label {extra_label_class}" >
  114. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  115. {label}
  116. </label>
  117. <div class="col-sm-8">
  118. {icon}
  119. {element}
  120. <!-- BEGIN label_2 -->
  121. <p class="help-block">{label_2}</p>
  122. <!-- END label_2 -->
  123. <!-- BEGIN error -->
  124. <span class="help-inline">{error}</span>
  125. <!-- END error -->
  126. </div>
  127. <div class="col-sm-2">
  128. <!-- BEGIN label_3 -->
  129. {label_3}
  130. <!-- END label_3 -->
  131. </div>
  132. </div>';
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function getLayout()
  138. {
  139. return $this->layout;
  140. }
  141. /**
  142. * @param string $layout
  143. */
  144. public function setLayout($layout)
  145. {
  146. $this->layout = $layout;
  147. }
  148. /**
  149. * Adds a text field to the form.
  150. * A trim-filter is attached to the field.
  151. * @param string $label The label for the form-element
  152. * @param string $name The element name
  153. * @param bool $required (optional) Is the form-element required (default=true)
  154. * @param array $attributes (optional) List of attributes for the form-element
  155. */
  156. public function addText($name, $label, $required = true, $attributes = array())
  157. {
  158. $this->addElement('text', $name, $label, $attributes);
  159. $this->applyFilter($name, 'trim');
  160. if ($required) {
  161. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  162. }
  163. }
  164. /**
  165. * The "date_range_picker" element creates 2 hidden fields
  166. * "elementName" + "_start" and "elementName" + "_end"
  167. * For example if the name is "range", you will have 2 new fields
  168. * when executing $form->getSubmitValues()
  169. * "range_start" and "range_end"
  170. *
  171. * @param string $name
  172. * @param string $label
  173. * @param bool $required
  174. * @param array $attributes
  175. */
  176. public function addDateRangePicker($name, $label, $required = true, $attributes = array())
  177. {
  178. $this->addElement('date_range_picker', $name, $label, $attributes);
  179. $this->addElement('hidden', $name.'_start');
  180. $this->addElement('hidden', $name.'_end');
  181. if ($required) {
  182. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  183. }
  184. }
  185. /**
  186. * @param string $name
  187. * @param string $label
  188. * @param array $attributes
  189. *
  190. * @return mixed
  191. */
  192. public function addDatePicker($name, $label, $attributes = [])
  193. {
  194. return $this->addElement('DatePicker', $name, $label, $attributes);
  195. }
  196. /**
  197. * @param string $name
  198. * @param string $label
  199. * @param array $attributes
  200. *
  201. * @return mixed
  202. */
  203. public function addSelectLanguage($name, $label, $options = [], $attributes = [])
  204. {
  205. return $this->addElement('SelectLanguage', $name, $label, $options, $attributes);
  206. }
  207. /**
  208. * @param $name
  209. * @param $label
  210. * @param array $options
  211. * @param array $attributes
  212. * @throws
  213. */
  214. public function addSelectAjax($name, $label, $options = [], $attributes = [])
  215. {
  216. if (!isset($attributes['url'])) {
  217. throw new \Exception('select_ajax needs an URL');
  218. }
  219. $this->addElement(
  220. 'select_ajax',
  221. $name,
  222. $label,
  223. $options,
  224. $attributes
  225. );
  226. }
  227. /**
  228. * @param string $name
  229. * @param string $label
  230. * @param array $attributes
  231. *
  232. * @return mixed
  233. */
  234. public function addDateTimePicker($name, $label, $attributes = [])
  235. {
  236. return $this->addElement('DateTimePicker', $name, $label, $attributes);
  237. }
  238. /**
  239. * @param string $name
  240. * @param string $value
  241. */
  242. public function addHidden($name, $value)
  243. {
  244. $this->addElement('hidden', $name, $value);
  245. }
  246. /**
  247. * @param string $name
  248. * @param string $label
  249. * @param array $attributes
  250. *
  251. * @return HTML_QuickForm_textarea
  252. */
  253. public function addTextarea($name, $label, $attributes = array())
  254. {
  255. return $this->addElement('textarea', $name, $label, $attributes);
  256. }
  257. /**
  258. * @param string $name
  259. * @param string $label
  260. * @param string $icon font-awesome
  261. * @param string $style default|primary|success|info|warning|danger|link
  262. * @param string $size large|default|small|extra-small
  263. * @param string $class Example plus is transformed to icon fa fa-plus
  264. * @param array $attributes
  265. *
  266. * @return HTML_QuickForm_button
  267. */
  268. public function addButton(
  269. $name,
  270. $label,
  271. $icon = 'check',
  272. $style = 'default',
  273. $size = 'default',
  274. $class = null,
  275. $attributes = array(),
  276. $createElement = false
  277. ) {
  278. if ($createElement) {
  279. return $this->createElement(
  280. 'button',
  281. $name,
  282. $label,
  283. $icon,
  284. $style,
  285. $size,
  286. $class,
  287. $attributes
  288. );
  289. }
  290. return $this->addElement(
  291. 'button',
  292. $name,
  293. $label,
  294. $icon,
  295. $style,
  296. $size,
  297. $class,
  298. $attributes
  299. );
  300. }
  301. /**
  302. * Returns a button with the primary color and a check mark
  303. * @param string $label Text appearing on the button
  304. * @param string $name Element name (for form treatment purposes)
  305. * @param bool $createElement Whether to use the create or add method
  306. *
  307. * @return HTML_QuickForm_button
  308. */
  309. public function addButtonSave($label, $name = 'submit', $createElement = false)
  310. {
  311. return $this->addButton(
  312. $name,
  313. $label,
  314. 'check',
  315. 'primary',
  316. null,
  317. null,
  318. array(),
  319. $createElement
  320. );
  321. }
  322. /**
  323. * Returns a cancel button
  324. * @param string $label Text appearing on the button
  325. * @param string $name Element name (for form treatment purposes)
  326. * @param bool $createElement Whether to use the create or add method
  327. *
  328. * @return HTML_QuickForm_button
  329. */
  330. public function addButtonCancel($label, $name = 'submit', $createElement = false)
  331. {
  332. return $this->addButton(
  333. $name,
  334. $label,
  335. 'times',
  336. 'danger',
  337. null,
  338. null,
  339. array(),
  340. $createElement
  341. );
  342. }
  343. /**
  344. * Returns a button with the primary color and a "plus" icon
  345. * @param string $label Text appearing on the button
  346. * @param string $name Element name (for form treatment purposes)
  347. * @param bool $createElement Whether to use the create or add method
  348. * @param array $attributes Additional attributes
  349. *
  350. * @return HTML_QuickForm_button
  351. */
  352. public function addButtonCreate($label, $name = 'submit', $createElement = false, $attributes = array())
  353. {
  354. return $this->addButton(
  355. $name,
  356. $label,
  357. 'plus',
  358. 'primary',
  359. null,
  360. null,
  361. $attributes,
  362. $createElement
  363. );
  364. }
  365. /**
  366. * Returns a button with the primary color and a pencil icon
  367. * @param string $label Text appearing on the button
  368. * @param string $name Element name (for form treatment purposes)
  369. * @param bool $createElement Whether to use the create or add method
  370. * @return HTML_QuickForm_button
  371. */
  372. public function addButtonUpdate($label, $name = 'submit', $createElement = false)
  373. {
  374. return $this->addButton(
  375. $name,
  376. $label,
  377. 'pencil',
  378. 'primary',
  379. null,
  380. null,
  381. array(),
  382. $createElement
  383. );
  384. }
  385. /**
  386. * Returns a button with the danger color and a trash icon
  387. * @param string $label Text appearing on the button
  388. * @param string $name Element name (for form treatment purposes)
  389. * @param bool $createElement Whether to use the create or add method
  390. *
  391. * @return HTML_QuickForm_button
  392. */
  393. public function addButtonDelete($label, $name = 'submit', $createElement = false)
  394. {
  395. return $this->addButton(
  396. $name,
  397. $label,
  398. 'trash',
  399. 'danger',
  400. null,
  401. null,
  402. array(),
  403. $createElement
  404. );
  405. }
  406. /**
  407. * Returns a button with the primary color and a paper-plane icon
  408. * @param string $label Text appearing on the button
  409. * @param string $name Element name (for form treatment purposes)
  410. * @param bool $createElement Whether to use the create or add method
  411. *
  412. * @return HTML_QuickForm_button
  413. */
  414. public function addButtonSend($label, $name = 'submit', $createElement = false, $attributes = array())
  415. {
  416. return $this->addButton(
  417. $name,
  418. $label,
  419. 'paper-plane',
  420. 'primary',
  421. null,
  422. null,
  423. $attributes,
  424. $createElement
  425. );
  426. }
  427. /**
  428. * Returns a button with the default (grey?) color and a magnifier icon
  429. * @param string $label Text appearing on the button
  430. * @param string $name Element name (for form treatment purposes)
  431. *
  432. * @return HTML_QuickForm_button
  433. */
  434. public function addButtonSearch($label = null, $name = 'submit')
  435. {
  436. if (empty($label)) {
  437. $label = get_lang('Search');
  438. }
  439. return $this->addButton($name, $label, 'search', 'default');
  440. }
  441. /**
  442. * Returns a button with the primary color and a right-pointing arrow icon
  443. * @param string $label Text appearing on the button
  444. * @param string $name Element name (for form treatment purposes)
  445. * @param array $attributes Additional attributes
  446. * @return HTML_QuickForm_button
  447. */
  448. public function addButtonNext($label, $name = 'submit', $attributes = array())
  449. {
  450. return $this->addButton($name, $label, 'arrow-right', 'primary', null, null, $attributes);
  451. }
  452. /**
  453. * Returns a button with the primary color and a check mark icon
  454. * @param string $label Text appearing on the button
  455. * @param string $name Element name (for form treatment purposes)
  456. * @param bool $createElement Whether to use the create or add method
  457. * @return HTML_QuickForm_button
  458. */
  459. public function addButtonImport($label, $name = 'submit', $createElement = false)
  460. {
  461. return $this->addButton(
  462. $name,
  463. $label,
  464. 'check',
  465. 'primary',
  466. null,
  467. null,
  468. array(),
  469. $createElement
  470. );
  471. }
  472. /**
  473. * Returns a button with the primary color and a check-mark icon
  474. * @param string $label Text appearing on the button
  475. * @param string $name Element name (for form treatment purposes)
  476. * @param bool $createElement Whether to use the create or add method
  477. * @return HTML_QuickForm_button
  478. */
  479. public function addButtonExport($label, $name = 'submit', $createElement = false)
  480. {
  481. return $this->addButton(
  482. $name,
  483. $label,
  484. 'check',
  485. 'primary',
  486. null,
  487. null,
  488. array(),
  489. $createElement
  490. );
  491. }
  492. /**
  493. * Shortcut to filter button
  494. * @param string $label Text appearing on the button
  495. * @param string $name Element name (for form treatment purposes)
  496. * @param bool $createElement Whether to use the create or add method
  497. * @return HTML_QuickForm_button
  498. */
  499. public function addButtonFilter($label, $name = 'submit', $createElement = false)
  500. {
  501. return $this->addButton(
  502. $name,
  503. $label,
  504. 'filter',
  505. 'primary',
  506. null,
  507. null,
  508. array(),
  509. $createElement
  510. );
  511. }
  512. /**
  513. * Shortcut to reset button
  514. * @param string $label Text appearing on the button
  515. * @param string $name Element name (for form treatment purposes)
  516. * @param bool $createElement Whether to use the create or add method
  517. * @return HTML_QuickForm_button
  518. */
  519. public function addButtonReset($label, $name = 'reset', $createElement = false)
  520. {
  521. $icon = 'eraser';
  522. $style = 'default';
  523. $size = 'default';
  524. $class = null;
  525. $attributes = array();
  526. if ($createElement) {
  527. return $this->createElement(
  528. 'reset',
  529. $name,
  530. $label,
  531. $icon,
  532. $style,
  533. $size,
  534. $class,
  535. $attributes
  536. );
  537. }
  538. return $this->addElement(
  539. 'reset',
  540. $name,
  541. $label,
  542. $icon,
  543. $style,
  544. $size,
  545. $class,
  546. $attributes
  547. );
  548. }
  549. /**
  550. * Returns a button with the primary color and an upload icon
  551. * @param string $label Text appearing on the button
  552. * @param string $name Element name (for form treatment purposes)
  553. * @param bool $createElement Whether to use the create or add method
  554. *
  555. * @return HTML_QuickForm_button
  556. */
  557. public function addButtonUpload($label, $name = 'submit', $createElement = false)
  558. {
  559. return $this->addButton(
  560. $name,
  561. $label,
  562. 'upload',
  563. 'primary',
  564. null,
  565. null,
  566. array(),
  567. $createElement
  568. );
  569. }
  570. /**
  571. * Returns a button with the primary color and a download icon
  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 addButtonDownload($label, $name = 'submit', $createElement = false)
  579. {
  580. return $this->addButton(
  581. $name,
  582. $label,
  583. 'download',
  584. 'primary',
  585. null,
  586. null,
  587. array(),
  588. $createElement
  589. );
  590. }
  591. /**
  592. * Returns a button with the primary color and a magnifier icon
  593. * @param string $label Text appearing on the button
  594. * @param string $name Element name (for form treatment purposes)
  595. * @param bool $createElement Whether to use the create or add method
  596. *
  597. * @return HTML_QuickForm_button
  598. */
  599. public function addButtonPreview($label, $name = 'submit', $createElement = false)
  600. {
  601. return $this->addButton(
  602. $name,
  603. $label,
  604. 'search',
  605. 'primary',
  606. null,
  607. null,
  608. array(),
  609. $createElement
  610. );
  611. }
  612. /**
  613. * Returns a button with the primary color and a copy (double sheet) icon
  614. * @param string $label Text appearing on the button
  615. * @param string $name Element name (for form treatment purposes)
  616. * @param bool $createElement Whether to use the create or add method
  617. *
  618. * @return HTML_QuickForm_button
  619. */
  620. public function addButtonCopy($label, $name = 'submit', $createElement = false)
  621. {
  622. return $this->addButton(
  623. $name,
  624. $label,
  625. 'copy',
  626. 'primary',
  627. null,
  628. null,
  629. array(),
  630. $createElement
  631. );
  632. }
  633. /**
  634. * @param string $name
  635. * @param string $label
  636. * @param string $text
  637. * @param array $attributes
  638. *
  639. * @return HTML_QuickForm_checkbox
  640. */
  641. public function addCheckBox($name, $label, $text = '', $attributes = array())
  642. {
  643. return $this->addElement('checkbox', $name, $label, $text, $attributes);
  644. }
  645. /**
  646. * @param string $name
  647. * @param string $label
  648. * @param array $options
  649. * @param array $attributes
  650. *
  651. * @return HTML_QuickForm_group
  652. */
  653. public function addCheckBoxGroup($name, $label, $options = array(), $attributes = array())
  654. {
  655. $group = array();
  656. foreach ($options as $value => $text) {
  657. $attributes['value'] = $value;
  658. $group[] = $this->createElement('checkbox', $value, null, $text, $attributes);
  659. }
  660. return $this->addGroup($group, $name, $label);
  661. }
  662. /**
  663. * @param string $name
  664. * @param string $label
  665. * @param array $options
  666. * @param array $attributes
  667. *
  668. * @return HTML_QuickForm_radio
  669. */
  670. public function addRadio($name, $label, $options = array(), $attributes = array())
  671. {
  672. $group = array();
  673. foreach ($options as $key => $value) {
  674. $group[] = $this->createElement('radio', null, null, $value, $key, $attributes);
  675. }
  676. return $this->addGroup($group, $name, $label);
  677. }
  678. /**
  679. * @param string $name
  680. * @param string $label
  681. * @param array $options
  682. * @param array $attributes
  683. *
  684. * @return HTML_QuickForm_select
  685. */
  686. public function addSelect($name, $label, $options = array(), $attributes = array())
  687. {
  688. return $this->addElement('select', $name, $label, $options, $attributes);
  689. }
  690. /**
  691. * @param $name
  692. * @param $label
  693. * @param $collection
  694. * @param array $attributes
  695. * @param bool $addNoneOption
  696. * @param string $textCallable set a function getStringValue() by default __toString()
  697. *
  698. * @return HTML_QuickForm_element
  699. */
  700. public function addSelectFromCollection(
  701. $name,
  702. $label,
  703. $collection,
  704. $attributes = array(),
  705. $addNoneOption = false,
  706. $textCallable = ''
  707. ) {
  708. $options = [];
  709. if ($addNoneOption) {
  710. $options[0] = get_lang('None');
  711. }
  712. if (!empty($collection)) {
  713. foreach ($collection as $item) {
  714. $text = $item;
  715. if (!empty($textCallable)) {
  716. $text = $item->$textCallable();
  717. }
  718. $options[$item->getId()] = $text;
  719. }
  720. }
  721. return $this->addElement('select', $name, $label, $options, $attributes);
  722. }
  723. /**
  724. * @param string $label
  725. * @param string $text
  726. *
  727. * @return HTML_QuickForm_label
  728. */
  729. public function addLabel($label, $text)
  730. {
  731. return $this->addElement('label', $label, $text);
  732. }
  733. /**
  734. * @param string $text
  735. */
  736. public function addHeader($text)
  737. {
  738. $this->addElement('header', $text);
  739. }
  740. /**
  741. * @param string $name
  742. * @param string $label
  743. * @param array $attributes
  744. */
  745. public function addFile($name, $label, $attributes = array())
  746. {
  747. $this->addElement('file', $name, $label, $attributes);
  748. }
  749. /**
  750. * @param string $snippet
  751. */
  752. public function addHtml($snippet)
  753. {
  754. $this->addElement('html', $snippet);
  755. }
  756. /**
  757. * Adds a HTML-editor to the form
  758. * @param string $name
  759. * @param string $label The label for the form-element
  760. * @param bool $required (optional) Is the form-element required (default=true)
  761. * @param bool $fullPage (optional) When it is true, the editor loads completed html code for a full page.
  762. * @param array $config (optional) Configuration settings for the online editor.
  763. * @param bool $style
  764. */
  765. public function addHtmlEditor($name, $label, $required = true, $fullPage = false, $config = array(), $style = false)
  766. {
  767. $config['rows'] = isset($config['rows']) ? $config['rows'] : 15;
  768. $config['cols'] = isset($config['cols']) ? $config['cols'] : 80;
  769. $this->addElement('html_editor', $name, $label, $config, $style);
  770. $this->applyFilter($name, 'trim');
  771. if ($required) {
  772. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  773. }
  774. /** @var HtmlEditor $element */
  775. $element = $this->getElement($name);
  776. if ($style) {
  777. $config['style'] = true;
  778. }
  779. if ($fullPage) {
  780. $config['fullPage'] = true;
  781. }
  782. if ($element->editor) {
  783. $element->editor->processConfig($config);
  784. }
  785. }
  786. /**
  787. * @param string $name
  788. * @param string $label
  789. *
  790. * @return mixed
  791. */
  792. public function addButtonAdvancedSettings($name, $label = '')
  793. {
  794. $label = !empty($label) ? $label : get_lang('AdvancedParameters');
  795. return $this->addElement('advanced_settings', $name, $label);
  796. }
  797. /**
  798. * Adds a progress bar to the form.
  799. *
  800. * Once the user submits the form, a progress bar (animated gif) is
  801. * displayed. The progress bar will disappear once the page has been
  802. * reloaded.
  803. *
  804. * @param int $delay (optional) The number of seconds between the moment the user
  805. * @param string $label (optional) Custom label to be shown
  806. *
  807. * submits the form and the start of the progress bar.
  808. * @deprecated ?
  809. */
  810. public function add_progress_bar($delay = 2, $label = '')
  811. {
  812. if (empty($label)) {
  813. $label = get_lang('PleaseStandBy');
  814. }
  815. $this->with_progress_bar = true;
  816. $this->updateAttributes("onsubmit=\"javascript: myUpload.start('dynamic_div','".Display::returnIconPath('progress_bar.gif')."','" . $label . "','" . $this->getAttribute('id') . "')\"");
  817. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  818. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  819. }
  820. /**
  821. * Uses new functions (php 5.2) for displaying real upload progress.
  822. * @param string $upload_id The value of the field UPLOAD_IDENTIFIER, the second parameter (XXX) of the $form->addElement('file', XXX) sentence
  823. * @param string $element_after The first element of the form (to place at first UPLOAD_IDENTIFIER)
  824. * @param int $delay (optional) The frequency of the xajax call
  825. * @param bool $wait_after_upload (optional)
  826. */
  827. public function add_real_progress_bar($upload_id, $element_after, $delay = 2, $wait_after_upload = false)
  828. {
  829. if (!function_exists('uploadprogress_get_info')) {
  830. $this->add_progress_bar($delay);
  831. return;
  832. }
  833. $xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH) . 'upload.xajax.php');
  834. $xajax_upload->registerFunction('updateProgress');
  835. // IMPORTANT : must be the first element of the form
  836. $el = $this->insertElementBefore(FormValidator::createElement('html', '<input type="hidden" name="UPLOAD_IDENTIFIER" value="' . $upload_id . '" />'), $element_after);
  837. $this->addElement('html', '<br />');
  838. // Add div-element where the progress bar is to be displayed
  839. $this->addElement(
  840. 'html',
  841. '<div id="dynamic_div_container" style="display:none">
  842. <div id="dynamic_div_label">' . get_lang('UploadFile') . '</div>
  843. <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url(' . Display::returnIconPath('real_upload_frame.gif').');">
  844. <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url(' . Display::returnIconPath('real_upload_step.gif').');background-repeat:repeat-x;background-position:center;"></div>
  845. </div>
  846. </div>'
  847. );
  848. if ($wait_after_upload) {
  849. $this->addElement('html', '
  850. <div id="dynamic_div_waiter_container" style="display:none">
  851. <div id="dynamic_div_waiter_label">
  852. ' . get_lang('SlideshowConversion') . '
  853. </div>
  854. <div id="dynamic_div_waiter_frame">
  855. '.Display::return_icon('real_upload_frame.gif').'
  856. </div>
  857. </div>
  858. ');
  859. }
  860. // Get the xajax code
  861. $this->addElement('html', $xajax_upload->getJavascript(api_get_path(WEB_LIBRARY_PATH) . 'xajax'));
  862. // Get the upload code
  863. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  864. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  865. if (!$wait_after_upload) {
  866. $wait_after_upload = 0;
  867. }
  868. // Add the upload event
  869. $this->updateAttributes("onsubmit=\"javascript: myUpload.startRealUpload('dynamic_div','" . $upload_id . "','" . $this->getAttribute('id') . "'," . $wait_after_upload . ")\"");
  870. }
  871. /**
  872. * This function has been created for avoiding changes directly within QuickForm class.
  873. * When we use it, the element is threated as 'required' to be dealt during validation.
  874. * @param array $element The array of elements
  875. * @param string $message The message displayed
  876. */
  877. public function add_multiple_required_rule($elements, $message)
  878. {
  879. $this->_required[] = $elements[0];
  880. $this->addRule($elements, $message, 'multiple_required');
  881. }
  882. /**
  883. * Displays the form.
  884. * If an element in the form didn't validate, an error message is showed
  885. * asking the user to complete the form.
  886. */
  887. public function display()
  888. {
  889. echo $this->returnForm();
  890. }
  891. /**
  892. * Returns the HTML code of the form.
  893. * @return string $return_value HTML code of the form
  894. */
  895. public function returnForm()
  896. {
  897. $returnValue = '';
  898. /** @var HTML_QuickForm_element $element */
  899. foreach ($this->_elements as $element) {
  900. $elementError = parent::getElementError($element->getName());
  901. if (!is_null($elementError)) {
  902. $returnValue .= Display::return_message($elementError, 'warning').'<br />';
  903. break;
  904. }
  905. }
  906. $returnValue .= parent::toHtml();
  907. // Add div-element which is to hold the progress bar
  908. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  909. $returnValue .= '<div id="dynamic_div" style="display:block; margin-left:40%; margin-top:10px; height:50px;"></div>';
  910. }
  911. return $returnValue;
  912. }
  913. /**
  914. * Returns the HTML code of the form.
  915. * If an element in the form didn't validate, an error message is showed
  916. * asking the user to complete the form.
  917. *
  918. * @return string $return_value HTML code of the form
  919. *
  920. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  921. * @author Julio Montoya
  922. * @deprecated use returnForm()
  923. */
  924. public function return_form()
  925. {
  926. return $this->returnForm();
  927. }
  928. /**
  929. * Create a form validator based on an array of form data:
  930. *
  931. * array(
  932. * 'name' => 'zombie_report_parameters', //optional
  933. * 'method' => 'GET', //optional
  934. * 'items' => array(
  935. * array(
  936. * 'name' => 'ceiling',
  937. * 'label' => 'Ceiling', //optional
  938. * 'type' => 'date',
  939. * 'default' => date() //optional
  940. * ),
  941. * array(
  942. * 'name' => 'active_only',
  943. * 'label' => 'ActiveOnly',
  944. * 'type' => 'checkbox',
  945. * 'default' => true
  946. * ),
  947. * array(
  948. * 'name' => 'submit_button',
  949. * 'type' => 'style_submit_button',
  950. * 'value' => get_lang('Search'),
  951. * 'attributes' => array('class' => 'search')
  952. * )
  953. * )
  954. * );
  955. *
  956. * @param array $form_data
  957. * @deprecated use normal FormValidator construct
  958. *
  959. * @return FormValidator
  960. */
  961. public static function create($form_data)
  962. {
  963. if (empty($form_data)) {
  964. return null;
  965. }
  966. $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
  967. $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
  968. $form_action = isset($form_data['action']) ? $form_data['action'] : '';
  969. $form_target = isset($form_data['target']) ? $form_data['target'] : '';
  970. $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
  971. $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
  972. $reset = null;
  973. $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
  974. $defaults = array();
  975. foreach ($form_data['items'] as $item) {
  976. $name = $item['name'];
  977. $type = isset($item['type']) ? $item['type'] : 'text';
  978. $label = isset($item['label']) ? $item['label'] : '';
  979. if ($type == 'wysiwyg') {
  980. $element = $result->addHtmlEditor($name, $label);
  981. } else {
  982. $element = $result->addElement($type, $name, $label);
  983. }
  984. if (isset($item['attributes'])) {
  985. $attributes = $item['attributes'];
  986. $element->setAttributes($attributes);
  987. }
  988. if (isset($item['value'])) {
  989. $value = $item['value'];
  990. $element->setValue($value);
  991. }
  992. if (isset($item['default'])) {
  993. $defaults[$name] = $item['default'];
  994. }
  995. if (isset($item['rules'])) {
  996. $rules = $item['rules'];
  997. foreach ($rules as $rule) {
  998. $message = $rule['message'];
  999. $type = $rule['type'];
  1000. $format = isset($rule['format']) ? $rule['format'] : null;
  1001. $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
  1002. $force = isset($rule['force']) ? $rule['force'] : false;
  1003. $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
  1004. }
  1005. }
  1006. }
  1007. $result->setDefaults($defaults);
  1008. return $result;
  1009. }
  1010. /**
  1011. * @return HTML_QuickForm_Renderer_Default
  1012. */
  1013. public static function getDefaultRenderer()
  1014. {
  1015. return
  1016. isset($GLOBALS['_HTML_QuickForm_default_renderer']) ?
  1017. $GLOBALS['_HTML_QuickForm_default_renderer'] : null;
  1018. }
  1019. /**
  1020. * Adds a input of type url to the form.
  1021. * @param type $name The label for the form-element
  1022. * @param type $label The element name
  1023. * @param type $required Optional. Is the form-element required (default=true)
  1024. * @param type $attributes Optional. List of attributes for the form-element
  1025. */
  1026. public function addUrl($name, $label, $required = true, $attributes = array())
  1027. {
  1028. $this->addElement('url', $name, $label, $attributes);
  1029. $this->applyFilter($name, 'trim');
  1030. $this->addRule($name, get_lang('InsertAValidUrl'), 'url');
  1031. if ($required) {
  1032. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1033. }
  1034. }
  1035. /**
  1036. * Adds a text field for letters to the form.
  1037. * A trim-filter is attached to the field.
  1038. * @param string $name The element name
  1039. * @param string $label The label for the form-element
  1040. * @param bool $required Optional. Is the form-element required (default=true)
  1041. * @param array $attributes Optional. List of attributes for the form-element
  1042. */
  1043. public function addTextLettersOnly(
  1044. $name,
  1045. $label,
  1046. $required = false,
  1047. $attributes = []
  1048. ) {
  1049. $attributes = array_merge(
  1050. $attributes,
  1051. [
  1052. 'pattern' => '[a-zA-ZñÑ]+',
  1053. 'title' => get_lang('OnlyLetters')
  1054. ]
  1055. );
  1056. $this->addElement(
  1057. 'text',
  1058. $name,
  1059. [
  1060. $label,
  1061. get_lang('OnlyLetters')
  1062. ],
  1063. $attributes
  1064. );
  1065. $this->applyFilter($name, 'trim');
  1066. if ($required) {
  1067. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1068. }
  1069. $this->addRule(
  1070. $name,
  1071. get_lang('OnlyLetters'),
  1072. 'regex',
  1073. '/^[a-zA-ZñÑ]+$/'
  1074. );
  1075. }
  1076. /**
  1077. * Adds a text field for alphanumeric characters to the form.
  1078. * A trim-filter is attached to the field.
  1079. * @param string $name The element name
  1080. * @param string $label The label for the form-element
  1081. * @param bool $required Optional. Is the form-element required (default=true)
  1082. * @param array $attributes Optional. List of attributes for the form-element
  1083. */
  1084. public function addTextAlphanumeric(
  1085. $name,
  1086. $label,
  1087. $required = false,
  1088. $attributes = []
  1089. ) {
  1090. $attributes = array_merge(
  1091. $attributes,
  1092. [
  1093. 'pattern' => '[a-zA-Z0-9ñÑ]+',
  1094. 'title' => get_lang('OnlyLettersAndNumbers')
  1095. ]
  1096. );
  1097. $this->addElement(
  1098. 'text',
  1099. $name,
  1100. [
  1101. $label,
  1102. get_lang('OnlyLettersAndNumbers')
  1103. ],
  1104. $attributes
  1105. );
  1106. $this->applyFilter($name, 'trim');
  1107. if ($required) {
  1108. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1109. }
  1110. $this->addRule(
  1111. $name,
  1112. get_lang('OnlyLettersAndNumbers'),
  1113. 'regex',
  1114. '/^[a-zA-Z0-9ÑÑ]+$/'
  1115. );
  1116. }
  1117. /**
  1118. * Adds a text field for letters and spaces to the form.
  1119. * A trim-filter is attached to the field.
  1120. * @param string $name The element name
  1121. * @param string $label The label for the form-element
  1122. * @param bool $required Optional. Is the form-element required (default=true)
  1123. * @param array $attributes Optional. List of attributes for the form-element
  1124. */
  1125. public function addTextLettersAndSpaces(
  1126. $name,
  1127. $label,
  1128. $required = false,
  1129. $attributes = []
  1130. ) {
  1131. $attributes = array_merge(
  1132. $attributes,
  1133. [
  1134. 'pattern' => '[a-zA-ZñÑ\s]+',
  1135. 'title' => get_lang('OnlyLettersAndSpaces')
  1136. ]
  1137. );
  1138. $this->addElement(
  1139. 'text',
  1140. $name,
  1141. [
  1142. $label,
  1143. get_lang('OnlyLettersAndSpaces')
  1144. ],
  1145. $attributes
  1146. );
  1147. $this->applyFilter($name, 'trim');
  1148. if ($required) {
  1149. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  1150. }
  1151. $this->addRule(
  1152. $name,
  1153. get_lang('OnlyLettersAndSpaces'),
  1154. 'regex',
  1155. '/^[a-zA-ZñÑ\s]+$/'
  1156. );
  1157. }
  1158. /**
  1159. * Adds a text field for alphanumeric and spaces characters to the form.
  1160. * A trim-filter is attached to the field.
  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 addTextAlphanumericAndSpaces(
  1167. $name,
  1168. $label,
  1169. $required = false,
  1170. $attributes = []
  1171. ) {
  1172. $attributes = array_merge(
  1173. $attributes,
  1174. [
  1175. 'pattern' => '[a-zA-Z0-9ñÑ\s]+',
  1176. 'title' => get_lang('OnlyLettersAndNumbersAndSpaces')
  1177. ]
  1178. );
  1179. $this->addElement(
  1180. 'text',
  1181. $name,
  1182. [
  1183. $label,
  1184. get_lang('OnlyLettersAndNumbersAndSpaces')
  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('OnlyLettersAndNumbersAndSpaces'),
  1195. 'regex',
  1196. '/^[a-zA-Z0-9ñÑ\s]+$/'
  1197. );
  1198. }
  1199. /**
  1200. * @param string $url
  1201. */
  1202. public function addMultipleUpload($url)
  1203. {
  1204. $inputName = 'input_file_upload';
  1205. $this->addMultipleUploadJavascript($url, $inputName);
  1206. $this->addHtml('
  1207. <div class="description-upload">'.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'</div>
  1208. <span class="btn btn-success fileinput-button">
  1209. <i class="glyphicon glyphicon-plus"></i>
  1210. <span>'.get_lang('AddFiles').'</span>
  1211. <!-- The file input field used as target for the file upload widget -->
  1212. <input id="'.$inputName.'" type="file" name="files[]" multiple>
  1213. </span>
  1214. <br />
  1215. <br />
  1216. <div id="dropzone">
  1217. <div class="button-load">
  1218. '.get_lang('UploadFiles').'
  1219. </div>
  1220. </div>
  1221. <br />
  1222. <!-- The global progress bar -->
  1223. <div id="progress" class="progress">
  1224. <div class="progress-bar progress-bar-success"></div>
  1225. </div>
  1226. <div id="files" class="files"></div>
  1227. ');
  1228. }
  1229. /**
  1230. *
  1231. * @param string $url page that will handle the upload
  1232. * @param string $inputName
  1233. */
  1234. private function addMultipleUploadJavascript($url, $inputName)
  1235. {
  1236. $this->addHtml("
  1237. <script>
  1238. $(function () {
  1239. 'use strict';
  1240. $('#".$this->getAttribute('id')."').submit(function(){
  1241. return false;
  1242. });
  1243. $('#dropzone').on('click', function() {
  1244. $('#".$inputName."').click();
  1245. });
  1246. var url = '".$url."';
  1247. var uploadButton = $('<button/>')
  1248. .addClass('btn btn-primary')
  1249. .prop('disabled', true)
  1250. .text('".get_lang('Loading')."')
  1251. .on('click', function () {
  1252. var \$this = $(this),
  1253. data = \$this.data();
  1254. \$this
  1255. .off('click')
  1256. .text('".get_lang('Cancel')."')
  1257. .on('click', function () {
  1258. \$this.remove();
  1259. data.abort();
  1260. });
  1261. data.submit().always(function () {
  1262. \$this.remove();
  1263. });
  1264. });
  1265. $('#".$inputName."').fileupload({
  1266. url: url,
  1267. dataType: 'json',
  1268. autoUpload: true,
  1269. // Enable image resizing, except for Android and Opera,
  1270. // which actually support image resizing, but fail to
  1271. // send Blob objects via XHR requests:
  1272. disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent),
  1273. previewMaxWidth: 100,
  1274. previewMaxHeight: 100,
  1275. previewCrop: true,
  1276. dropzone: $('#dropzone')
  1277. }).on('fileuploadadd', function (e, data) {
  1278. data.context = $('<div/>').appendTo('#files');
  1279. $.each(data.files, function (index, file) {
  1280. var node = $('<p/>').append($('<span/>').text(file.name));
  1281. /*if (!index) {
  1282. node
  1283. .append('<br>')
  1284. .append(uploadButton.clone(true).data(data));
  1285. }*/
  1286. node.appendTo(data.context);
  1287. }
  1288. );
  1289. }).on('fileuploadprocessalways', function (e, data) {
  1290. var index = data.index,
  1291. file = data.files[index],
  1292. node = $(data.context.children()[index]);
  1293. if (file.preview) {
  1294. node
  1295. .prepend('<br>')
  1296. .prepend(file.preview);
  1297. }
  1298. if (file.error) {
  1299. node
  1300. .append('<br>')
  1301. .append($('<span class=\"text-danger\"/>').text(file.error));
  1302. }
  1303. if (index + 1 === data.files.length) {
  1304. data.context.find('button')
  1305. .text('Upload')
  1306. .prop('disabled', !!data.files.error);
  1307. }
  1308. }).on('fileuploadprogressall', function (e, data) {
  1309. var progress = parseInt(data.loaded / data.total * 100, 10);
  1310. $('#progress .progress-bar').css(
  1311. 'width',
  1312. progress + '%'
  1313. );
  1314. }).on('fileuploaddone', function (e, data) {
  1315. $.each(data.result.files, function (index, file) {
  1316. if (file.url) {
  1317. var link = $('<a>')
  1318. .attr('target', '_blank')
  1319. .prop('href', file.url);
  1320. $(data.context.children()[index]).wrap(link);
  1321. } else if (file.error) {
  1322. var error = $('<span class=\"text-danger\"/>').text(file.error);
  1323. $(data.context.children()[index])
  1324. .append('<br>')
  1325. .append(error);
  1326. }
  1327. });
  1328. }).on('fileuploadfail', function (e, data) {
  1329. $.each(data.files, function (index) {
  1330. var error = $('<span class=\"text-danger\"/>').text('".get_lang('Failed')."');
  1331. $(data.context.children()[index])
  1332. .append('<br>')
  1333. .append(error);
  1334. });
  1335. }).prop('disabled', !$.support.fileInput)
  1336. .parent().addClass($.support.fileInput ? undefined : 'disabled');
  1337. $('.fileinput-button').hide();
  1338. });
  1339. </script>"
  1340. );
  1341. }
  1342. }
  1343. /**
  1344. * Cleans HTML text filter
  1345. * @param string $html HTML to clean
  1346. * @param int $mode (optional)
  1347. * @return string The cleaned HTML
  1348. */
  1349. function html_filter($html, $mode = NO_HTML)
  1350. {
  1351. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  1352. $cleaned_html = kses($html, $allowed_tags);
  1353. return $cleaned_html;
  1354. }
  1355. function html_filter_teacher($html)
  1356. {
  1357. return html_filter($html, TEACHER_HTML);
  1358. }
  1359. function html_filter_student($html)
  1360. {
  1361. return html_filter($html, STUDENT_HTML);
  1362. }
  1363. function html_filter_teacher_fullpage($html)
  1364. {
  1365. return html_filter($html, TEACHER_HTML_FULLPAGE);
  1366. }
  1367. function html_filter_student_fullpage($html)
  1368. {
  1369. return html_filter($html, STUDENT_HTML_FULLPAGE);
  1370. }
  1371. /**
  1372. * Cleans mobile phone number text
  1373. * @param string $mobilePhoneNumber Mobile phone number to clean
  1374. * @return string The cleaned mobile phone number
  1375. */
  1376. function mobile_phone_number_filter($mobilePhoneNumber)
  1377. {
  1378. $mobilePhoneNumber = str_replace(array('+', '(', ')'), '', $mobilePhoneNumber);
  1379. return ltrim($mobilePhoneNumber, '0');
  1380. }