Source for file jFormsControl.class.php

Documentation is available at jFormsControl.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  forms
  5. @author      Laurent Jouanneau
  6. @contributor Loic Mathaud, Dominique Papin
  7. @copyright   2006-2007 Laurent Jouanneau, 2007-2008 Dominique Papin
  8. @copyright   2007 Loic Mathaud
  9. @link        http://www.jelix.org
  10. @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  11. */
  12.  
  13. /**
  14.  * base class for all jforms control
  15.  * @package     jelix
  16.  * @subpackage  forms
  17.  */
  18. abstract class jFormsControl {
  19.     public $type = null;
  20.     public $ref='';
  21.     public $datatype;
  22.     public $required = false;
  23.     public $readonly = false;
  24.     public $label='';
  25.     public $defaultValue='';
  26.     public $hasHelp = false;
  27.     public $hint='';
  28.     public $alertInvalid='';
  29.     public $alertRequired='';
  30.  
  31.     function __construct($ref){
  32.         $this->ref = $ref;
  33.         $this->datatype = new jDatatypeString();
  34.     }
  35.  
  36.     function isContainer(){
  37.         return false;
  38.     }
  39.  
  40.     function check($value$form){
  41.         if(trim($value== ''{
  42.             if($this->required)
  43.                 return jForms::ERRDATA_REQUIRED;
  44.         }elseif(!$this->datatype->check($value)){
  45.             return jForms::ERRDATA_INVALID;
  46.         }
  47.         return null;
  48.     }
  49.  
  50.     function getDisplayValue($value){
  51.         return $value;
  52.     }
  53. }
  54.  
  55. /**
  56.  * bas class for controls which uses a datasource to fill their contents.
  57.  * @package     jelix
  58.  * @subpackage  forms
  59.  */
  60. abstract class jFormsControlDatasource extends jFormsControl {
  61.  
  62.     public $type="datasource";
  63.  
  64.     /**
  65.      * @var jIFormDatasource 
  66.      */
  67.     public $datasource;
  68.     public $defaultValue=array();
  69.  
  70.     function getDisplayValue($value){
  71.         if(is_array($value)){
  72.             $labels array();
  73.             foreach($value as $val){
  74.                 $labels[$val]=$this->datasource->getLabel($val);
  75.             }
  76.             return $labels;
  77.         }else{
  78.             return $this->datasource->getLabel($value);
  79.         }
  80.     }
  81. }
  82.  
  83. /**
  84.  *
  85.  * @package     jelix
  86.  * @subpackage  forms
  87.  */
  88. class jFormsControlInput extends jFormsControl {
  89.     public $type='input';
  90.     public $size=0;
  91. }
  92.  
  93. /**
  94.  *
  95.  * @package     jelix
  96.  * @subpackage  forms
  97.  */
  98. class jFormsControlCheckboxes extends jFormsControlDatasource {
  99.     public $type="checkboxes";
  100.  
  101.     function isContainer(){
  102.         return true;
  103.     }
  104.  
  105.     function check($value$form){
  106.         if(is_array($value)){
  107.             if(count($value== && $this->required){
  108.                 return jForms::ERRDATA_REQUIRED;
  109.             }else{
  110.                 foreach($value as $v){
  111.                     if(!$this->datatype->check($v)){
  112.                         return jForms::ERRDATA_INVALID;
  113.                     }
  114.                 }
  115.             }
  116.         }else{
  117.             if(trim($value== ''){
  118.                 if($this->required)
  119.                     return jForms::ERRDATA_REQUIRED;
  120.             }else{
  121.                 return jForms::ERRDATA_INVALID;
  122.             }
  123.         }
  124.         return null;
  125.     }
  126. }
  127.  
  128. /**
  129.  *
  130.  * @package     jelix
  131.  * @subpackage  forms
  132.  */
  133. class jFormsControlRadiobuttons extends jFormsControlDatasource {
  134.     public $type="radiobuttons";
  135.     public $defaultValue='';
  136. }
  137.  
  138. /**
  139.  *
  140.  * @package     jelix
  141.  * @subpackage  forms
  142.  */
  143. class jFormsControlListbox extends jFormsControlDatasource {
  144.     public $type="listbox";
  145.     public $multiple = false;
  146.     public $size = 4;
  147.  
  148.     function isContainer(){
  149.         return $this->multiple;
  150.     }
  151.  
  152.     function check($value$form){
  153.         if(is_array($value)){
  154.             if(!$this->multiple){
  155.                 return jForms::ERRDATA_INVALID;
  156.             }
  157.             if(count($value== && $this->required){
  158.                 return jForms::ERRDATA_REQUIRED;
  159.             }else{
  160.                 foreach($value as $v){
  161.                     if(!$this->datatype->check($v)){
  162.                         return jForms::ERRDATA_INVALID;
  163.                     }
  164.                 }
  165.             }
  166.         }else{
  167.             if(trim($value== '' && $this->required){
  168.                 return jForms::ERRDATA_REQUIRED;
  169.             }elseif(!$this->datatype->check($value)){
  170.                 return jForms::ERRDATA_INVALID;
  171.             }
  172.         }
  173.         return null;
  174.     }
  175. }
  176.  
  177. /**
  178.  *
  179.  * @package     jelix
  180.  * @subpackage  forms
  181.  */
  182. class jFormsControlMenulist extends jFormsControlDatasource {
  183.     public $type="menulist";
  184.     public $defaultValue='';
  185. }
  186.  
  187. /**
  188.  *
  189.  * @package     jelix
  190.  * @subpackage  forms
  191.  */
  192. class jFormsControlTextarea extends jFormsControl {
  193.     public $type='textarea';
  194.     public $rows=5;
  195.     public $cols=40;
  196. }
  197.  
  198. /**
  199.  *
  200.  * @package     jelix
  201.  * @subpackage  forms
  202.  */
  203. class jFormsControlSecret extends jFormsControl {
  204.     public $type='secret';
  205.     public $size=0;
  206. }
  207.  
  208. /**
  209.  *
  210.  * @package     jelix
  211.  * @subpackage  forms
  212.  */
  213. class jFormsControlSecretConfirm extends jFormsControl {
  214.     public $type='secretconfirm';
  215.     public $size=0;
  216.     public $primarySecret='';
  217.     function check($value$form){
  218.         if($value != $form->getData($this->primarySecret))
  219.             return jForms::ERRDATA_INVALID;
  220.         return null;
  221.     }
  222. }
  223.  
  224. /**
  225.  *
  226.  * @package     jelix
  227.  * @subpackage  forms
  228.  */
  229. class jFormsControlCheckbox extends jFormsControl {
  230.     public $type='checkbox';
  231.     public $defaultValue='0';
  232.     public $valueOnCheck='1';
  233.     public $valueOnUncheck='0';
  234.  
  235.     function check($value$form){
  236.         if($value != $this->valueOnCheck && $value != $this->valueOnUncheck)
  237.             return jForms::ERRDATA_INVALID;
  238.         return null;
  239.     }
  240. }
  241.  
  242. /**
  243.  *
  244.  * @package     jelix
  245.  * @subpackage  forms
  246.  */
  247. class jFormsControlOutput extends jFormsControl {
  248.     public $type='output';
  249.  
  250.     public function check($value$form){
  251.         return null;
  252.     }
  253. }
  254.  
  255. /**
  256.  *
  257.  * @package     jelix
  258.  * @subpackage  forms
  259.  */
  260. class jFormsControlUpload extends jFormsControl {
  261.     public $type='upload';
  262.     public $mimetype=array();
  263.     public $maxsize=0;
  264.  
  265.     public $fileInfo = array();
  266.  
  267.     function check($value$form){
  268.         if(isset($_FILES[$this->ref]))
  269.             $this->fileInfo = $_FILES[$this->ref];
  270.         else
  271.             $this->fileInfo = array('name'=>'','type'=>'','size'=>0,'tmp_name'=>'''error'=>UPLOAD_ERR_NO_FILE);
  272.  
  273.         if($this->fileInfo['error'== UPLOAD_ERR_NO_FILE{
  274.             if($this->required)
  275.                 return jForms::ERRDATA_REQUIRED;
  276.         }else{
  277.             if($this->fileInfo['error'!= UPLOAD_ERR_OK || !is_uploaded_file($this->fileInfo['tmp_name']))
  278.                 return jForms::ERRDATA_INVALID;
  279.  
  280.             if($this->maxsize && $this->fileInfo['size'$this->maxsize)
  281.                 return jForms::ERRDATA_INVALID;
  282.  
  283.             if(count($this->mimetype)){
  284.                 if($this->fileInfo['type']==''){
  285.                     $this->fileInfo['type'mime_content_type($this->fileInfo['tmp_name']);
  286.                 }
  287.                 if(!in_array($this->fileInfo['type']$this->mimetype))
  288.                     return jForms::ERRDATA_INVALID;
  289.             }
  290.         }
  291.         return null;
  292.     }
  293. }
  294.  
  295. /**
  296.  *
  297.  * @package     jelix
  298.  * @subpackage  forms
  299.  */
  300. class jFormsControlSubmit extends jFormsControlDatasource {
  301.     public $type='submit';
  302.     public $standalone = true;
  303.     public function check($value$form){
  304.         return null;
  305.     }
  306. }
  307.  
  308. class jFormsControlReset extends jFormsControl {
  309.     public $type='reset';
  310.     public function check($value$form){
  311.         return null;
  312.     }
  313. }
  314.  
  315. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:27 +0200 by phpDocumentor 1.4.3