FormValidator.class.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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. }