dokeos
[ class tree: dokeos ] [ index: dokeos ] [ all elements ]

Source for file FormValidator.class.php

Documentation is available at FormValidator.class.php

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

Documentation generated on Thu, 12 Jun 2008 13:30:37 -0500 by phpDocumentor 1.4.1