FormValidator.class.php 57 KB

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