FormValidator.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Objects of this class can be used to create/manipulate/validate user input.
  5. */
  6. class FormValidator extends HTML_QuickForm
  7. {
  8. public $with_progress_bar = false;
  9. /**
  10. * Constructor
  11. * @param string $form_name Name of the form
  12. * @param string $method (optional Method ('post' (default) or 'get')
  13. * @param string $action (optional Action (default is $PHP_SELF)
  14. * @param string $target (optional Form's target defaults to '_self'
  15. * @param mixed $attributes (optional) Extra attributes for <form> tag
  16. * @param bool $track_submit (optional) Whether to track if the form was
  17. * submitted by adding a special hidden field (default = true)
  18. */
  19. public function __construct(
  20. $form_name = null,
  21. $method = 'post',
  22. $action = '',
  23. $target = '',
  24. $attributes = null,
  25. $track_submit = true
  26. ) {
  27. // Default form class
  28. if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) {
  29. $attributes['class'] = 'form-horizontal';
  30. }
  31. // Fixing form search
  32. if (is_array($attributes) && isset($attributes['class'])) {
  33. //if (strpos($attributes['class'], 'form-search')) {
  34. if ($attributes['class'] == 'form-search') {
  35. // $attributes['class'] = str_replace('form-search', 'form-inline', $attributes['class']);
  36. $attributes['class'] = 'form-inline';
  37. }
  38. }
  39. // Allow form with no names
  40. if (empty($form_name)) {
  41. $form_name = uniqid();
  42. }
  43. parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
  44. // Load some custom elements and rules
  45. $dir = api_get_path(LIBRARY_PATH).'formvalidator/';
  46. $this->registerElementType('html_editor', $dir . 'Element/html_editor.php', 'HTML_QuickForm_html_editor');
  47. $this->registerElementType('date_range_picker', $dir . 'Element/DateRangePicker.php', 'DateRangePicker');
  48. $this->registerElementType('date_time_picker', $dir . 'Element/DateTimePicker.php', 'DateTimePicker');
  49. $this->registerElementType('date_picker', $dir . 'Element/DatePicker.php', 'DatePicker');
  50. $this->registerElementType('datepicker', $dir . 'Element/datepicker.php', 'HTML_QuickForm_datepicker');
  51. $this->registerElementType('datepickerdate', $dir . 'Element/datepickerdate.php', 'HTML_QuickForm_datepickerdate');
  52. $this->registerElementType('receivers', $dir . 'Element/receivers.php', 'HTML_QuickForm_receivers');
  53. $this->registerElementType('select_language', $dir . 'Element/select_language.php', 'HTML_QuickForm_Select_Language');
  54. $this->registerElementType('select_ajax', $dir . 'Element/select_ajax.php', 'HTML_QuickForm_Select_Ajax');
  55. $this->registerElementType('select_theme', $dir . 'Element/select_theme.php', 'HTML_QuickForm_Select_Theme');
  56. $this->registerElementType('style_submit_button', $dir . 'Element/style_submit_button.php', 'HTML_QuickForm_stylesubmitbutton');
  57. $this->registerElementType('style_reset_button', $dir . 'Element/style_reset_button.php', 'HTML_QuickForm_styleresetbutton');
  58. $this->registerElementType('button', $dir . 'Element/style_submit_button.php', 'HTML_QuickForm_stylesubmitbutton');
  59. $this->registerElementType('captcha', 'HTML/QuickForm/CAPTCHA.php', 'HTML_QuickForm_CAPTCHA');
  60. $this->registerElementType('CAPTCHA_Image', 'HTML/QuickForm/CAPTCHA/Image.php', 'HTML_QuickForm_CAPTCHA_Image');
  61. $this->registerRule('date', null, 'HTML_QuickForm_Rule_Date', $dir . 'Rule/Date.php');
  62. $this->registerRule('datetime', null, 'DateTimeRule', $dir . 'Rule/DateTimeRule.php');
  63. $this->registerRule('date_compare', null, 'HTML_QuickForm_Rule_DateCompare', $dir . 'Rule/DateCompare.php');
  64. $this->registerRule('html', null, 'HTML_QuickForm_Rule_HTML', $dir . 'Rule/HTML.php');
  65. $this->registerRule('username_available', null, 'HTML_QuickForm_Rule_UsernameAvailable', $dir . 'Rule/UsernameAvailable.php');
  66. $this->registerRule('username', null, 'HTML_QuickForm_Rule_Username', $dir . 'Rule/Username.php');
  67. $this->registerRule('filetype', null, 'HTML_QuickForm_Rule_Filetype', $dir . 'Rule/Filetype.php');
  68. $this->registerRule('multiple_required', 'required', 'HTML_QuickForm_Rule_MultipleRequired', $dir . 'Rule/MultipleRequired.php');
  69. $this->registerRule('url', null, 'HTML_QuickForm_Rule_Url', $dir . 'Rule/Url.php');
  70. $this->registerRule('compare_fields', null, 'HTML_QuickForm_Compare_Fields', $dir . 'Rule/CompareFields.php');
  71. $this->registerRule('compare_datetime_text', null, 'HTML_QuickForm_Rule_CompareDateTimeText', $dir . 'Rule/CompareDateTimeText.php');
  72. $this->registerRule('CAPTCHA', 'rule', 'HTML_QuickForm_Rule_CAPTCHA', 'HTML/QuickForm/Rule/CAPTCHA.php');
  73. // Modify the default templates
  74. /** @var HTML_QuickForm_Renderer_Default $renderer */
  75. $renderer = & $this->defaultRenderer();
  76. // Form template
  77. $renderer->setFormTemplate($this->getFormTemplate());
  78. // Element template
  79. if (isset($attributes['class']) && $attributes['class'] == 'well form-inline') {
  80. $element_template = ' {label} {element} ';
  81. $renderer->setElementTemplate($element_template);
  82. } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') {
  83. $element_template = ' {label} {element} ';
  84. $renderer->setElementTemplate($element_template);
  85. } else {
  86. if (is_array($attributes) && isset($attributes['class']) && $attributes['class'] == 'form-inline') {
  87. $element_template = $this->getDefaultInlineElementTemplate();
  88. } else {
  89. $element_template = $this->getDefaultElementTemplate();
  90. }
  91. $renderer->setElementTemplate($element_template);
  92. // Display a gray div in the buttons
  93. $button_element_template_simple = '<div class="form-actions">{label} {element}</div>';
  94. $renderer->setElementTemplate($button_element_template_simple, 'submit_in_actions');
  95. //Display a gray div in the buttons + makes the button available when scrolling
  96. $button_element_template_in_bottom = '<div class="form-actions bottom_actions">{label} {element}</div>';
  97. $renderer->setElementTemplate($button_element_template_in_bottom, 'submit_fixed_in_bottom');
  98. $renderer->setElementTemplate($button_element_template_simple, 'buttons_in_action');
  99. $button_element_template_simple_right = '<div class="form-actions"> <div class="pull-right">{label} {element}</div></div>';
  100. $renderer->setElementTemplate($button_element_template_simple_right, 'buttons_in_action_right');
  101. }
  102. // Set Header template
  103. $renderer->setHeaderTemplate('<h2>{header}</h2>');
  104. //Set required field template
  105. HTML_QuickForm::setRequiredNote('<span class="form_required">*</span> <small>' . get_lang('ThisFieldIsRequired') . '</small>');
  106. $required_note_template = <<<EOT
  107. <div class="form-group">
  108. <div class="col-sm-2">{requiredNote}</div>
  109. </div>
  110. EOT;
  111. $renderer->setRequiredNoteTemplate($required_note_template);
  112. }
  113. /**
  114. * @return string
  115. */
  116. private function getFormTemplate()
  117. {
  118. return '<form{attributes}>
  119. <fieldset>{content}</fieldset>
  120. {hidden}
  121. </form>';
  122. }
  123. /**
  124. * @return string
  125. */
  126. private function getDefaultElementTemplate()
  127. {
  128. return '
  129. <div class="form-group {error_class}">
  130. <label class="col-sm-2 control-label">
  131. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  132. {label}
  133. </label>
  134. <div class="col-sm-10">
  135. {element}
  136. <!-- BEGIN label_3 -->
  137. {label_3}
  138. <!-- END label_3 -->
  139. <!-- BEGIN label_2 -->
  140. <span class="help-block">
  141. {label_2}
  142. </span>
  143. <!-- END label_2 -->
  144. <!-- BEGIN error -->
  145. <span class=" col-sm-2 help-block">{error}</span>
  146. <!-- END error -->
  147. </div>
  148. </div>';
  149. }
  150. /**
  151. * @return string
  152. */
  153. private function getDefaultInlineElementTemplate()
  154. {
  155. return '
  156. <div class="form-group {error_class}">
  157. <label class="sr-only">
  158. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  159. {label}
  160. </label>
  161. {element}
  162. <!-- BEGIN label_3 -->
  163. {label_3}
  164. <!-- END label_3 -->
  165. <!-- BEGIN label_2 -->
  166. <span class="help-block">
  167. {label_2}
  168. </span>
  169. <!-- END label_2 -->
  170. <!-- BEGIN error -->
  171. <span class=" col-sm-2 help-block">{error}</span>
  172. <!-- END error -->
  173. </div>';
  174. }
  175. /**
  176. * Adds a text field to the form.
  177. * A trim-filter is attached to the field.
  178. * @param string $label The label for the form-element
  179. * @param string $name The element name
  180. * @param boolean $required (optional) Is the form-element required (default=true)
  181. * @param array $attributes (optional) List of attributes for the form-element
  182. */
  183. public function add_textfield($name, $label, $required = true, $attributes = array())
  184. {
  185. $this->addElement('text', $name, $label, $attributes);
  186. $this->applyFilter($name, 'trim');
  187. if ($required) {
  188. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  189. }
  190. }
  191. /**
  192. * date_range_picker element creates 2 hidden fields
  193. * elementName + "_start" elementName "_end"
  194. * @param string $name
  195. * @param string $label
  196. * @param bool $required
  197. * @param array $attributes
  198. */
  199. public function addDateRangePicker($name, $label, $required = true, $attributes = array())
  200. {
  201. $this->addElement('date_range_picker', $name, $label, $attributes);
  202. $this->addElement('hidden', $name.'_start');
  203. $this->addElement('hidden', $name.'_end');
  204. if ($required) {
  205. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  206. }
  207. }
  208. /**
  209. * @param string $name
  210. * @param string $value
  211. */
  212. function add_hidden($name, $value)
  213. {
  214. $this->addElement('hidden', $name, $value);
  215. }
  216. public function add_textarea($name, $label, $attributes = array())
  217. {
  218. $this->addElement('textarea', $name, $label, $attributes);
  219. }
  220. public function add_button($name, $label, $attributes = array())
  221. {
  222. $this->addElement('button', $name, $label, $attributes);
  223. }
  224. public function add_checkbox($name, $label, $trailer = '', $attributes = array())
  225. {
  226. $this->addElement('checkbox', $name, $label, $trailer, $attributes);
  227. }
  228. public function add_radio($name, $label, $options = '')
  229. {
  230. $group = array();
  231. foreach ($options as $key => $value) {
  232. $group[] = $this->createElement('radio', null, null, $value, $key);
  233. }
  234. $this->addGroup($group, $name, $label);
  235. }
  236. public function add_select($name, $label, $options = '', $attributes = array())
  237. {
  238. $this->addElement('select', $name, $label, $options, $attributes);
  239. }
  240. public function add_label($label, $text)
  241. {
  242. $this->addElement('label', $label, $text);
  243. }
  244. public function add_header($text)
  245. {
  246. $this->addElement('header', $text);
  247. }
  248. public function add_file($name, $label, $attributes = array())
  249. {
  250. $this->addElement('file', $name, $label, $attributes);
  251. }
  252. public function add_html($snippet)
  253. {
  254. $this->addElement('html', $snippet);
  255. }
  256. /**
  257. * Adds a HTML-editor to the form to fill in a title.
  258. * A trim-filter is attached to the field.
  259. * A HTML-filter is attached to the field (cleans HTML)
  260. * A rule is attached to check for unwanted HTML
  261. * @param string $name
  262. * @param string $label The label for the form-element
  263. * @param boolean $required (optional) Is the form-element required (default=true)
  264. * @param boolean $full_page (optional) When it is true, the editor loads completed html code for a full page.
  265. * @param array $config (optional) Configuration settings for the online editor.
  266. *
  267. */
  268. public function add_html_editor($name, $label, $required = true, $full_page = false, $config = null)
  269. {
  270. $this->addElement('html_editor', $name, $label, 'rows="15" cols="80"', $config);
  271. $this->applyFilter($name, 'trim');
  272. if ($required) {
  273. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  274. }
  275. /** @var HTML_QuickForm_html_editor $element */
  276. $element = $this->getElement($name);
  277. $element->editor->processConfig($config);
  278. }
  279. /**
  280. * Adds a date picker element to the form
  281. * A rule is added to check if the date is a valid one
  282. * @param string $label The label for the form-element
  283. * @param string $name The element name
  284. */
  285. public function add_datepicker($name, $label)
  286. {
  287. $this->addElement('datepicker', $name, $label, array('form_name' => $this->getAttribute('name')));
  288. $this->_elements[$this->_elementIndex[$name]]->setLocalOption('minYear', 1900); // TODO: Now - 9 years
  289. $this->addRule($name, get_lang('InvalidDate'), 'date');
  290. }
  291. /**
  292. * Adds a date picker date element to the form
  293. * A rule is added to check if the date is a valid one
  294. * @param string $label The label for the form-element
  295. * @param string $name The element name
  296. * @deprecated
  297. */
  298. public function add_datepickerdate($name, $label)
  299. {
  300. $this->addElement('datepickerdate', $name, $label, array('form_name' => $this->getAttribute('name')));
  301. $this->_elements[$this->_elementIndex[$name]]->setLocalOption('minYear', 1900); // TODO: Now - 9 years
  302. $this->addRule($name, get_lang('InvalidDate'), 'date');
  303. }
  304. /**
  305. * Adds a timewindow element to the form.
  306. * 2 datepicker elements are added and a rule to check if the first date is
  307. * before the second one.
  308. * @param string $label The label for the form-element
  309. * @param string $name The element name
  310. * @deprecated
  311. */
  312. public function add_timewindow($name_1, $name_2, $label_1, $label_2)
  313. {
  314. $this->add_datepicker($name_1, $label_1);
  315. $this->add_datepicker($name_2, $label_2);
  316. $this->addRule(array($name_1, $name_2), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  317. }
  318. /**
  319. * Adds a progress bar to the form.
  320. *
  321. * Once the user submits the form, a progress bar (animated gif) is
  322. * displayed. The progress bar will disappear once the page has been
  323. * reloaded.
  324. *
  325. * @param int $delay (optional) The number of seconds between the moment the user
  326. * @param string $label (optional) Custom label to be shown
  327. * submits the form and the start of the progress bar.
  328. */
  329. public function add_progress_bar($delay = 2, $label = '')
  330. {
  331. if (empty($label)) {
  332. $label = get_lang('PleaseStandBy');
  333. }
  334. $this->with_progress_bar = true;
  335. $this->updateAttributes("onsubmit=\"javascript: myUpload.start('dynamic_div','" . api_get_path(WEB_IMG_PATH) . "progress_bar.gif','" . $label . "','" . $this->getAttribute('id') . "')\"");
  336. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  337. }
  338. /**
  339. * Uses new functions (php 5.2) for displaying real upload progress.
  340. * @param string $upload_id The value of the field UPLOAD_IDENTIFIER, the second parameter (XXX) of the $form->addElement('file', XXX) sentence
  341. * @param string $element_after The first element of the form (to place at first UPLOAD_IDENTIFIER)
  342. * @param int $delay (optional) The frequency of the xajax call
  343. * @param bool $wait_after_upload (optional)
  344. */
  345. public function add_real_progress_bar($upload_id, $element_after, $delay = 2, $wait_after_upload = false)
  346. {
  347. if (!function_exists('uploadprogress_get_info')) {
  348. $this->add_progress_bar($delay);
  349. return;
  350. }
  351. if (!class_exists('xajax')) {
  352. require_once api_get_path(LIBRARY_PATH) . 'xajax/xajax.inc.php';
  353. }
  354. $xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH).'upload.xajax.php');
  355. $xajax_upload->registerFunction('updateProgress');
  356. // IMPORTANT : must be the first element of the form
  357. $el = $this->insertElementBefore(FormValidator::createElement('html', '<input type="hidden" name="UPLOAD_IDENTIFIER" value="' . $upload_id . '" />'), $element_after);
  358. $this->addElement('html', '<br />');
  359. // Add div-element where the progress bar is to be displayed
  360. $this->addElement('html', '
  361. <div id="dynamic_div_container" style="display:none">
  362. <div id="dynamic_div_label">' . get_lang('UploadFile') . '</div>
  363. <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif);">
  364. <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_step.gif);background-repeat:repeat-x;background-position:center;"></div>
  365. </div>
  366. </div>');
  367. if ($wait_after_upload) {
  368. $this->addElement('html', '
  369. <div id="dynamic_div_waiter_container" style="display:none">
  370. <div id="dynamic_div_waiter_label">
  371. ' . get_lang('SlideshowConversion') . '
  372. </div>
  373. <div id="dynamic_div_waiter_frame">
  374. <img src="' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif" />
  375. </div>
  376. </div>
  377. ');
  378. }
  379. // Get the xajax code
  380. $this->addElement('html', $xajax_upload->getJavascript(api_get_path(WEB_LIBRARY_PATH).'xajax'));
  381. // Get the upload code
  382. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  383. if (!$wait_after_upload) {
  384. $wait_after_upload = 0;
  385. }
  386. // Add the upload event
  387. $this->updateAttributes("onsubmit=\"javascript: myUpload.startRealUpload('dynamic_div','" . $upload_id . "','" . $this->getAttribute('id') . "'," . $wait_after_upload . ")\"");
  388. }
  389. /**
  390. * This function has been created for avoiding changes directly within QuickForm class.
  391. * When we use it, the element is threated as 'required' to be dealt during validation.
  392. * @param array $elements The array of elements
  393. * @param string $message The message displayed
  394. */
  395. public function add_multiple_required_rule($elements, $message)
  396. {
  397. $this->_required[] = $elements[0];
  398. $this->addRule($elements, $message, 'multiple_required');
  399. }
  400. /**
  401. * Displays the form.
  402. * If an element in the form didn't validate, an error message is showed
  403. * asking the user to complete the form.
  404. */
  405. public function display()
  406. {
  407. echo $this->return_form();
  408. }
  409. /**
  410. * Returns the HTML code of the form.
  411. * If an element in the form didn't validate, an error message is showed
  412. * asking the user to complete the form.
  413. *
  414. * @return string $return_value HTML code of the form
  415. *
  416. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  417. */
  418. public function return_form()
  419. {
  420. $error = false;
  421. $addDateLibraries = false;
  422. $dateElementTypes = array('date_range_picker', 'date_time_picker', 'date_picker', 'datepicker', 'datetimepicker');
  423. /** @var HTML_QuickForm_element $element */
  424. foreach ($this->_elements as $element) {
  425. if (in_array($element->getType(), $dateElementTypes)) {
  426. $addDateLibraries = true;
  427. }
  428. if (!is_null(parent::getElementError($element->getName()))) {
  429. $error = true;
  430. break;
  431. }
  432. }
  433. $return_value = '';
  434. $js = null;
  435. if ($addDateLibraries) {
  436. $js = api_get_js('jquery-ui/jquery-ui-i18n.min.js');
  437. $js .= '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'datetimepicker/jquery-ui-timepicker-addon.js" type="text/javascript"></script>';
  438. $js .= '<link href="'.api_get_path(WEB_LIBRARY_JS_PATH).'datetimepicker/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css" />';
  439. $js .= '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'daterange/moment.min.js" type="text/javascript"></script>';
  440. $js .= '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'daterange/daterangepicker.js" type="text/javascript"></script>';
  441. $js .= '<link href="'.api_get_path(WEB_LIBRARY_JS_PATH).'daterange/daterangepicker-bs2.css" rel="stylesheet" type="text/css" />';
  442. $isocode = api_get_language_isocode();
  443. if ($isocode != 'en') {
  444. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/i18n/jquery-ui-timepicker-'.$isocode.'.js" type="text/javascript"></script>';
  445. $js .= '<script>
  446. $(function(){
  447. $.datepicker.setDefaults($.datepicker.regional["'.$isocode.'"]);
  448. moment.lang("'.$isocode.'");
  449. });
  450. </script>';
  451. }
  452. }
  453. if ($error) {
  454. $return_value = Display::return_message(get_lang('FormHasErrorsPleaseComplete'), 'warning');
  455. }
  456. $return_value .= $js;
  457. $return_value .= parent::toHtml();
  458. // Add div-element which is to hold the progress bar
  459. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  460. $return_value .= '<div id="dynamic_div" style="display:block; margin-left:40%; margin-top:10px; height:50px;"></div>';
  461. }
  462. return $return_value;
  463. }
  464. /**
  465. * @param string $name
  466. * @param string $label
  467. * @param array $values
  468. * @param array $attributes
  469. */
  470. public function addDoubleMultipleSelect($name, $label, $values, $attributes = array())
  471. {
  472. $group = $this->addElement('advmultiselect', $name, $label, $values, $attributes);
  473. $group->setElementTemplate('
  474. {javascript}
  475. <table{class}>
  476. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  477. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  478. <tr>
  479. <td valign="top">{unselected}</td>
  480. <td align="center">{add}<br /><br />{remove}</td>
  481. <td valign="top">{selected}</td>
  482. </tr>
  483. </table>
  484. ');
  485. $group->setButtonAttributes('add', array('class' => 'btn arrowr'));
  486. $group->setButtonAttributes('remove', array('class' => 'btn arrowl'));
  487. }
  488. /**
  489. * @return string
  490. */
  491. public function getDoubleMultipleSelectTemplate()
  492. {
  493. return '
  494. <div class="form-group">
  495. <label class="col-sm-2 control-label">
  496. <!-- BEGIN required -->
  497. <span class="form_required">*</span> <!-- END required -->
  498. {label}
  499. </label>
  500. <div class="col-sm-10">
  501. <table cellpadding="0" cellspacing="0">
  502. <tr>
  503. <!-- BEGIN error -->
  504. <span class="form_error">{error}</span>
  505. <br /><!-- END error -->
  506. <td>{element}</td>
  507. </tr>
  508. </table>
  509. </div>
  510. </div>';
  511. }
  512. /**
  513. * Create a form validator based on an array of form data:
  514. *
  515. * array(
  516. * 'name' => 'zombie_report_parameters', //optional
  517. * 'method' => 'GET', //optional
  518. * 'items' => array(
  519. * array(
  520. * 'name' => 'ceiling',
  521. * 'label' => 'Ceiling', //optional
  522. * 'type' => 'date',
  523. * 'default' => date() //optional
  524. * ),
  525. * array(
  526. * 'name' => 'active_only',
  527. * 'label' => 'ActiveOnly',
  528. * 'type' => 'checkbox',
  529. * 'default' => true
  530. * ),
  531. * array(
  532. * 'name' => 'submit_button',
  533. * 'type' => 'style_submit_button',
  534. * 'value' => get_lang('Search'),
  535. * 'attributes' => array('class' => 'search')
  536. * )
  537. * )
  538. * );
  539. *
  540. * @param array form_data
  541. * @return FormValidator
  542. */
  543. public static function create($form_data)
  544. {
  545. if (empty($form_data)) {
  546. return null;
  547. }
  548. $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
  549. $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
  550. $form_action = isset($form_data['action']) ? $form_data['action'] : '';
  551. $form_target = isset($form_data['target']) ? $form_data['target'] : '';
  552. $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
  553. $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
  554. $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
  555. $defaults = array();
  556. foreach ($form_data['items'] as $item) {
  557. $name = $item['name'];
  558. $type = isset($item['type']) ? $item['type'] : 'text';
  559. $label = isset($item['label']) ? $item['label'] : '';
  560. if ($type == 'wysiwyg') {
  561. $element = $result->add_html_editor($name, $label);
  562. } else {
  563. $element = $result->addElement($type, $name, $label);
  564. }
  565. if (isset($item['attributes'])) {
  566. $attributes = $item['attributes'];
  567. $element->setAttributes($attributes);
  568. }
  569. if (isset($item['value'])) {
  570. $value = $item['value'];
  571. $element->setValue($value);
  572. }
  573. if (isset($item['default'])) {
  574. $defaults[$name] = $item['default'];
  575. }
  576. if (isset($item['rules'])) {
  577. $rules = $item['rules'];
  578. foreach ($rules as $rule) {
  579. $message = $rule['message'];
  580. $type = $rule['type'];
  581. $format = isset($rule['format']) ? $rule['format'] : null;
  582. $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
  583. $force = isset($rule['force']) ? $rule['force'] : false;
  584. $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
  585. }
  586. }
  587. }
  588. $result->setDefaults($defaults);
  589. return $result;
  590. }
  591. }
  592. // Used when applying filters
  593. /**
  594. * Cleans HTML text
  595. * @param string $html HTML to clean
  596. * @param int $mode (optional)
  597. * @return string The cleaned HTML
  598. */
  599. function html_filter($html, $mode = NO_HTML)
  600. {
  601. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  602. $cleaned_html = kses($html, $allowed_tags);
  603. return $cleaned_html;
  604. }
  605. function html_filter_teacher($html)
  606. {
  607. return html_filter($html, TEACHER_HTML);
  608. }
  609. function html_filter_student($html)
  610. {
  611. return html_filter($html, STUDENT_HTML);
  612. }
  613. function html_filter_teacher_fullpage($html)
  614. {
  615. return html_filter($html, TEACHER_HTML_FULLPAGE);
  616. }
  617. function html_filter_student_fullpage($html)
  618. {
  619. return html_filter($html, STUDENT_HTML_FULLPAGE);
  620. }