FormValidator.class.php 49 KB

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