FormValidator.class.php 57 KB

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