FormValidator.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos S.A.
  6. Copyright (c) Bart Mollet, Hogeschool Gent
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. require_once ('HTML/QuickForm.php');
  19. require_once ('HTML/QuickForm/advmultiselect.php');
  20. /**
  21. * Filter
  22. */
  23. define('NO_HTML', 1);
  24. define('STUDENT_HTML', 2);
  25. define('TEACHER_HTML', 3);
  26. define('STUDENT_HTML_FULLPAGE',4);
  27. define('TEACHER_HTML_FULLPAGE',5);
  28. /**
  29. * Objects of this class can be used to create/manipulate/validate user input.
  30. */
  31. class FormValidator extends HTML_QuickForm
  32. {
  33. var $with_progress_bar=false;
  34. /**
  35. * Constructor
  36. * @param string $form_name Name of the form
  37. * @param string $method Method ('post' (default) or 'get')
  38. * @param string $action Action (default is $PHP_SELF)
  39. * @param string $target Form's target defaults to '_self'
  40. * @param mixed $attributes (optional)Extra attributes for <form> tag
  41. * @param bool $trackSubmit (optional)Whether to track if the form was
  42. * submitted by adding a special hidden field (default = true)
  43. */
  44. function FormValidator($form_name, $method = 'post', $action = '', $target = '', $attributes = null, $trackSubmit = true)
  45. {
  46. $this->HTML_QuickForm($form_name, $method,$action, $target, $attributes, $trackSubmit);
  47. // Load some custom elements and rules
  48. $dir = dirname(__FILE__).'/';
  49. $this->registerElementType('html_editor', $dir.'Element/html_editor.php', 'HTML_QuickForm_html_editor');
  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_theme', $dir.'Element/select_theme.php', 'HTML_QuickForm_Select_Theme');
  55. $this->registerElementType('style_button', $dir.'Element/style_button.php', 'HTML_QuickForm_stylebutton');
  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->registerRule('date', null, 'HTML_QuickForm_Rule_Date', $dir.'Rule/Date.php');
  59. $this->registerRule('date_compare', null, 'HTML_QuickForm_Rule_DateCompare', $dir.'Rule/DateCompare.php');
  60. $this->registerRule('html',null,'HTML_QuickForm_Rule_HTML',$dir.'Rule/HTML.php');
  61. $this->registerRule('username_available',null,'HTML_QuickForm_Rule_UsernameAvailable',$dir.'Rule/UsernameAvailable.php');
  62. $this->registerRule('username',null,'HTML_QuickForm_Rule_Username',$dir.'Rule/Username.php');
  63. $this->registerRule('filetype',null,'HTML_QuickForm_Rule_Filetype',$dir.'Rule/Filetype.php');
  64. $this->registerRule('multiple_required','required','HTML_QuickForm_Rule_MultipleRequired',$dir.'Rule/MultipleRequired.php');
  65. // Modify the default templates
  66. $renderer = & $this->defaultRenderer();
  67. $form_template = <<<EOT
  68. <form {attributes}>
  69. {content}
  70. <div class="clear">
  71. &nbsp;
  72. </div>
  73. </form>
  74. EOT;
  75. $renderer->setFormTemplate($form_template);
  76. $element_template = <<<EOT
  77. <div class="row">
  78. <div class="label">
  79. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  80. </div>
  81. <div class="formw">
  82. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}
  83. </div>
  84. </div>
  85. EOT;
  86. $renderer->setElementTemplate($element_template);
  87. $header_template = <<<EOT
  88. <div class="row">
  89. <div class="form_header">{header}</div>
  90. </div>
  91. EOT;
  92. $renderer->setHeaderTemplate($header_template);
  93. HTML_QuickForm :: setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  94. $required_note_template = <<<EOT
  95. <div class="row">
  96. <div class="label"></div>
  97. <div class="formw">{requiredNote}</div>
  98. </div>
  99. EOT;
  100. $renderer->setRequiredNoteTemplate($required_note_template);
  101. }
  102. /**
  103. * Add a textfield to the form.
  104. * A trim-filter is attached to the field.
  105. * @param string $label The label for the form-element
  106. * @param string $name The element name
  107. * @param boolean $required Is the form-element required (default=true)
  108. * @param array $attributes Optional list of attributes for the form-element
  109. */
  110. function add_textfield( $name, $label,$required = true, $attributes = array())
  111. {
  112. $this->addElement('text',$name,$label,$attributes);
  113. $this->applyFilter($name,'trim');
  114. if($required)
  115. {
  116. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  117. }
  118. }
  119. /**
  120. * Add a HTML-editor to the form to fill in a title.
  121. * A trim-filter is attached to the field.
  122. * A HTML-filter is attached to the field (cleans HTML)
  123. * A rule is attached to check for unwanted HTML
  124. * @param string $label The label for the form-element
  125. * @param string $name The element name
  126. * @param boolean $required Is the form-element required (default=true)
  127. */
  128. function add_html_editor($name, $label, $required = true, $full_page = false)
  129. {
  130. $this->addElement('html_editor',$name,$label,'rows="15" cols="80"');
  131. $this->applyFilter($name,'trim');
  132. $html_type = STUDENT_HTML;
  133. if(!empty($_SESSION['status']))
  134. {
  135. $html_type = $_SESSION['status'] == COURSEMANAGER ? TEACHER_HTML : STUDENT_HTML;
  136. }
  137. if($full_page)
  138. {
  139. $html_type = $_SESSION['status'] == COURSEMANAGER ? TEACHER_HTML_FULLPAGE : STUDENT_HTML_FULLPAGE;
  140. //First *filter* the HTML (markup, indenting, ...)
  141. //$this->applyFilter($name,'html_filter_teacher_fullpage');
  142. }
  143. else
  144. {
  145. //First *filter* the HTML (markup, indenting, ...)
  146. //$this->applyFilter($name,'html_filter_teacher');
  147. }
  148. if($required)
  149. {
  150. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  151. }
  152. if($full_page)
  153. {
  154. $el = $this->getElement($name);
  155. $el->fullPage = true;
  156. }
  157. //Add rule to check not-allowed HTML
  158. //$this->addRule($name,get_lang('SomeHTMLNotAllowed'),'html',$html_type);
  159. }
  160. /**
  161. * Add a datepicker element to the form
  162. * A rule is added to check if the date is a valid one
  163. * @param string $label The label for the form-element
  164. * @param string $name The element name
  165. */
  166. function add_datepicker($name,$label)
  167. {
  168. $this->addElement('datepicker', $name, $label, array ('form_name' => $this->getAttribute('name')));
  169. $this->addRule($name, get_lang('InvalidDate'), 'date');
  170. }
  171. /**
  172. * Add a datepickerdate element to the form
  173. * A rule is added to check if the date is a valid one
  174. * @param string $label The label for the form-element
  175. * @param string $name The element name
  176. */
  177. function add_datepickerdate($name,$label)
  178. {
  179. $this->addElement('datepickerdate', $name, $label, array ('form_name' => $this->getAttribute('name')));
  180. $this->addRule($name, get_lang('InvalidDate'), 'date');
  181. }
  182. /**
  183. * Add a timewindow element to the form.
  184. * 2 datepicker elements are added and a rule to check if the first date is
  185. * before the second one.
  186. * @param string $label The label for the form-element
  187. * @param string $name The element name
  188. */
  189. function add_timewindow($name_1, $name_2, $label_1,$label_2)
  190. {
  191. $this->add_datepicker($name_1, $label_1);
  192. $this->add_datepicker( $name_2, $label_2);
  193. $this->addRule(array ($name_1, $name_2), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  194. }
  195. /**
  196. * Add a button to the form to add resources.
  197. */
  198. function add_resource_button()
  199. {
  200. $group[] = $this->createElement('static','add_resource_img',null,'<img src="'.api_get_path(WEB_CODE_PATH).'img/attachment.gif" alt="'.get_lang('Attachment').'"/>');
  201. $group[] = $this->createElement('submit','add_resource',get_lang('Attachment'),'class="link_alike"');
  202. $this->addGroup($group);
  203. }
  204. /**
  205. * Adds a progress bar to the form.
  206. *
  207. * Once the user submits the form, a progress bar (animated gif) is
  208. * displayed. The progress bar will disappear once the page has been
  209. * reloaded.
  210. *
  211. * @param int $delay The number of seconds between the moment the user
  212. * submits the form and the start of the progress bar.
  213. */
  214. function add_progress_bar($delay = 2, $label='')
  215. {
  216. if(empty($label))
  217. {
  218. $label = get_lang('PleaseStandBy');
  219. }
  220. $this->with_progress_bar = true;
  221. $this->updateAttributes("onsubmit=\"myUpload.start('dynamic_div','".api_get_path(WEB_CODE_PATH)."img/progress_bar.gif','".$label."','".$this->getAttribute('id')."')\"");
  222. $this->addElement('html','<script language="javascript" src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>');
  223. $this->addElement('html','<script type="text/javascript">var myUpload = new upload('.(abs(intval($delay))*1000).');</script>');
  224. }
  225. /**
  226. * Use the new functions (php 5.2) allowing to display a real upload progress.
  227. * @param upload_id the value of the field UPLOAD_IDENTIFIER
  228. * @param elementAfter the first element of the form (to place at first UPLOAD_IDENTIFIER
  229. * @param delay the frequency of the xajax call
  230. * @param waitAfterUpload
  231. */
  232. function add_real_progress_bar($upload_id, $elementAfter, $delay=2,$waitAfterUpload=false)
  233. {
  234. if(!function_exists('uploadprogress_get_info'))
  235. {
  236. $this -> add_progress_bar($delay);
  237. return;
  238. }
  239. if(!class_exists('xajax')) {
  240. require_once api_get_path(LIBRARY_PATH).'xajax/xajax.inc.php';
  241. }
  242. $xajax_upload = new xajax(api_get_path(WEB_CODE_PATH).'inc/lib/upload.xajax.php');
  243. $xajax_upload -> registerFunction ('updateProgress');
  244. // IMPORTANT : must be the first element of the form
  245. $el = $this->insertElementBefore(FormValidator::createElement('html','<input type="hidden" name="UPLOAD_IDENTIFIER" value="'.$upload_id.'" />'), $elementAfter);
  246. $this->addElement('html','<br />');
  247. // add the div where the progress bar will be displayed
  248. $this->addElement('html','
  249. <div id="dynamic_div_container" style="display:none">
  250. <div id="dynamic_div_label">'.get_lang('UploadFile').'</div>
  251. <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url('.api_get_path(REL_PATH).'main/img/real_upload_frame.gif);">
  252. <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url('.api_get_path(REL_PATH).'main/img/real_upload_step.gif);background-repeat:repeat-x;background-position:center;"></div>
  253. </div>
  254. </div>');
  255. if($waitAfterUpload){
  256. $this->addElement('html','
  257. <div id="dynamic_div_waiter_container" style="display:none">
  258. <div id="dynamic_div_waiter_label">
  259. '.get_lang('SlideshowConversion').'
  260. </div>
  261. <div id="dynamic_div_waiter_frame">
  262. <img src="'.api_get_path(WEB_CODE_PATH).'img/real_upload_frame.gif" />
  263. </div>
  264. </div>
  265. ');
  266. }
  267. // get the xajax code
  268. $this->addElement('html',$xajax_upload -> getJavascript(api_get_path(WEB_CODE_PATH).'inc/lib/xajax'));
  269. // get the upload code
  270. $this->addElement('html','<script language="javascript" src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>');
  271. $this->addElement('html','<script type="text/javascript">var myUpload = new upload('.(abs(intval($delay))*1000).');</script>');
  272. if(!$waitAfterUpload)
  273. {
  274. $waitAfterUpload = 0;
  275. }
  276. // add the upload event
  277. $this->updateAttributes("onsubmit=\"myUpload.startRealUpload('dynamic_div','".$upload_id."','".$this->getAttribute('id')."',".$waitAfterUpload.")\"");
  278. }
  279. /**
  280. * This function avoid to change directly QuickForm class.
  281. * When we use it, the element is threated as 'required' to be dealt during validation
  282. * @param array $element the array of elements
  283. * @param string $message the message displayed
  284. */
  285. function add_multiple_required_rule($elements, $message)
  286. {
  287. $this->_required[] = $elements[0];
  288. $this -> addRule ($elements , $message , 'multiple_required');
  289. }
  290. /**
  291. * Display the form.
  292. * If an element in the form didn't validate, an error message is showed
  293. * asking the user to complete the form.
  294. */
  295. function display()
  296. {
  297. echo $this->return_form();
  298. }
  299. /**
  300. * Returns the HTML code of the form.
  301. * If an element in the form didn't validate, an error message is showed
  302. * asking the user to complete the form.
  303. *
  304. * @return string $return_value HTML code of the form
  305. *
  306. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  307. * @version Dokeos 1.8, august 2006
  308. */
  309. function return_form()
  310. {
  311. $error = false;
  312. foreach($this->_elements as $index => $element)
  313. {
  314. if( !is_null(parent::getElementError($element->getName())) )
  315. {
  316. $error = true;
  317. break;
  318. }
  319. }
  320. if($error)
  321. {
  322. Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  323. }
  324. $return_value = parent::toHtml();
  325. // Add the div which will hold the progress bar
  326. if(isset($this->with_progress_bar) && $this->with_progress_bar)
  327. {
  328. $return_value .= '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>';
  329. }
  330. return $return_value;
  331. }
  332. }
  333. /**
  334. * Clean HTML
  335. * @param string HTML to clean
  336. * @param int $mode
  337. * @return string The cleaned HTML
  338. */
  339. function html_filter($html, $mode = NO_HTML)
  340. {
  341. require_once(dirname(__FILE__).'/Rule/HTML.php');
  342. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  343. $cleaned_html = kses($html,$allowed_tags);
  344. return $cleaned_html;
  345. }
  346. function html_filter_teacher($html)
  347. {
  348. return html_filter($html,TEACHER_HTML);
  349. }
  350. function html_filter_student($html)
  351. {
  352. return html_filter($html,STUDENT_HTML);
  353. }
  354. function html_filter_teacher_fullpage($html)
  355. {
  356. return html_filter($html,TEACHER_HTML_FULLPAGE);
  357. }
  358. function html_filter_student_fullpage($html)
  359. {
  360. return html_filter($html,STUDENT_HTML_FULLPAGE);
  361. }
  362. ?>