Source for file jForms.class.php

Documentation is available at jForms.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  forms
  5. @author      Laurent Jouanneau
  6. @copyright   2006-2009 Laurent Jouanneau
  7. @link        http://www.jelix.org
  8. @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  9. */
  10.  
  11. /**
  12.  *
  13.  */
  14. require_once(JELIX_LIB_PATH.'forms/jFormsBase.class.php');
  15.  
  16. /**
  17.  * static class to manage and call a form
  18.  *
  19.  * A form is identified by a selector, and each instance of a form have a unique id (formId).
  20.  * This id can be the id of a record for example. If it is not given, the id is set to 0.
  21.  * @package     jelix
  22.  * @subpackage  forms
  23.  */
  24. class jForms {
  25.  
  26.     const ID_PARAM '__forms_id__';
  27.  
  28.     const DEFAULT_ID 0;
  29.  
  30.     const ERRDATA_INVALID 1;
  31.     const ERRDATA_REQUIRED 2;
  32.     const ERRDATA_INVALID_FILE_SIZE 3;
  33.     const ERRDATA_INVALID_FILE_TYPE 4;
  34.     const ERRDATA_FILE_UPLOAD_ERROR 5;
  35.  
  36.     /**
  37.      * pure static class, so no constructor
  38.      */
  39.     private function __construct()}
  40.  
  41.     /**
  42.      * Create a new form with empty data
  43.      *
  44.      * Call it to create a new form, before to display it.
  45.      * Data of the form are stored in the php session in a jFormsDataContainer object.
  46.      * If a form with same id exists, data are erased.
  47.      *
  48.      * @param string $formSel the selector of the xml jform file
  49.      * @param string $formId  the id of the new instance (an id of a record for example)
  50.      * @return jFormBase the object representing the form
  51.      */
  52.     public static function create($formSel$formId=null){
  53.         $sel new jSelectorForm($formSel);
  54.         // normalize the selector to avoid conflict in session
  55.         $formSel $sel->toString()
  56.         jIncluder::inc($sel);
  57.         $c $sel->getClass();
  58.         if($formId === null)
  59.             $formId self::DEFAULT_ID;
  60.         $fid is_array($formIdserialize($formId$formId;
  61.         if(!isset($_SESSION['JFORMS'][$formSel][$fid])){
  62.             $dc $_SESSION['JFORMS'][$formSel][$fid]new jFormsDataContainer($formSel$formId);
  63.             if ($formId == self::DEFAULT_ID{
  64.                 $dc->refcount 1;
  65.             }
  66.         }
  67.         else {
  68.             $dc $_SESSION['JFORMS'][$formSel][$fid];
  69.             if ($formId == self::DEFAULT_ID
  70.                 $dc->refcount++;
  71.         }
  72.         $form new $c($formSel$dctrue);
  73.         return $form;
  74.     }
  75.  
  76.     /**
  77.      * get an existing instance of a form
  78.      *
  79.      * In your controller, call it before to re-display a form with existing data.
  80.      *
  81.      * @param string $formSel the selector of the xml jform file
  82.      * @param string $formId  the id of the form (if you use multiple instance of a form)
  83.      * @return jFormBase the object representing the form. Return null if there isn't an existing form
  84.      */
  85.     static public function get($formSel$formId=null){
  86.         global $gJCoord;
  87.         if($formId === null)
  88.             $formIdself::DEFAULT_ID;
  89.         $fid is_array($formIdserialize($formId$formId;
  90.  
  91.         $sel new jSelectorForm($formSel);
  92.         // normalize the selector to avoid conflict in session
  93.         $formSel $sel->toString();
  94.  
  95.         if(!isset($_SESSION['JFORMS'][$formSel][$fid])){
  96.             return null;
  97.         }
  98.  
  99.         jIncluder::inc($sel);
  100.         $c $sel->getClass();
  101.         $form new $c($formSel$_SESSION['JFORMS'][$formSel][$fid],false);
  102.  
  103.         return $form;
  104.     }
  105.  
  106.     /**
  107.      * get an existing instance of a form, and fill it with data provided by the request
  108.      *
  109.      * use it in the action called to submit a webform.
  110.      *
  111.      * @param string $formSel the selector of the xml jform file
  112.      * @param string $formId  the id of the form (if you use multiple instance of a form)
  113.      * @return jFormBase the object representing the form. Return null if there isn't an existing form
  114.      */
  115.     static public function fill($formSel,$formId=null){
  116.         $form self::get($formSel,$formId);
  117.         if($form)
  118.             $form->initFromRequest();
  119.         return $form;
  120.     }
  121.  
  122.     /**
  123.      * destroy a form in the session
  124.      *
  125.      * use it after saving data of a form, and if you don't want to re-display the form.
  126.      *
  127.      * @param string $formSel the selector of the xml jform file
  128.      * @param string $formId  the id of the form (if you use multiple instance of a form)
  129.      */
  130.     static public function destroy($formSel$formId=null){
  131.         global $gJCoord;
  132.         if($formId === null)  $formId self::DEFAULT_ID;
  133.         if(is_array($formId)) $formId serialize($formId);
  134.         
  135.         // normalize the selector to avoid conflict in session
  136.         $sel new jSelectorForm($formSel);
  137.         $formSel $sel->toString();
  138.  
  139.         if(isset($_SESSION['JFORMS'][$formSel][$formId])){
  140.             if ($formId == self::DEFAULT_ID{
  141.                 if((--$_SESSION['JFORMS'][$formSel][$formId]->refcount0{
  142.                   $_SESSION['JFORMS'][$formSel][$formId]->clear();
  143.                     return;
  144.                 }
  145.             }
  146.             unset($_SESSION['JFORMS'][$formSel][$formId]);
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * destroy all form which are too old and unused
  152.      * @param integer $life the number of second of a life of a form
  153.      */
  154.     static public function clean($formSel=''$life=86400{
  155.         if(!isset($_SESSION['JFORMS'])) return;
  156.         if($formSel==''{
  157.             $t time();
  158.             foreach($_SESSION['JFORMS'as $sel=>$f{
  159.                 // don't call clean itself, see bug #1154
  160.                 foreach($_SESSION['JFORMS'][$selas $id=>$cont{
  161.                     if($t-$cont->updatetime $life)
  162.                         unset($_SESSION['JFORMS'][$sel][$id]);
  163.                 }
  164.             }
  165.         else {
  166.             // normalize the selector to avoid conflict in session
  167.             $sel new jSelectorForm($formSel);
  168.             $formSel $sel->toString();
  169.             
  170.             if(isset($_SESSION['JFORMS'][$formSel])) {
  171.                 $t time();
  172.                 foreach($_SESSION['JFORMS'][$formSelas $id=>$cont{
  173.                     if($t-$cont->updatetime $life)
  174.                         unset($_SESSION['JFORMS'][$formSel][$id]);
  175.                 }
  176.             }
  177.         }
  178.     }
  179. }

Documentation generated on Mon, 19 Sep 2011 14:12:36 +0200 by phpDocumentor 1.4.3