FormValidator.class.php 28 KB

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