FormValidator.class.php 58 KB

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