Source for file jFormsDatasource.class.php

Documentation is available at jFormsDatasource.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  forms
  5. @author      Laurent Jouanneau
  6. @contributor Dominique Papin, Julien Issler
  7. @copyright   2006-2010 Laurent Jouanneau
  8. @copyright   2008 Dominique Papin
  9. @copyright   2010 Julien Issler
  10. @link        http://www.jelix.org
  11. @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  12. */
  13. /**
  14.  * Interface for objects which provides a source of data to fill some controls in a form,
  15.  * like menulist, listbox etc...
  16.  * @package     jelix
  17.  * @subpackage  forms
  18.  */
  19. interface jIFormsDatasource {
  20.     /**
  21.      * load and returns data to fill a control. The returned array should be
  22.      * an associative array  key => label
  23.      * @param jFormsBase $form  the form
  24.      * @return array the data
  25.      */
  26.     public function getData($form);
  27.  
  28.     /**
  29.      * Return the label corresponding to the given key
  30.      * if the class implements also jIFormsDatasource2,
  31.      * you must not call getLabel but getLabel2
  32.      * @param string $key the key
  33.      * @return string the label
  34.      */
  35.     public function getLabel($key);
  36. }
  37.  
  38. /**
  39.  * Interface for objects which provides a source of data to fill some controls in a form,
  40.  * like menulist, listbox etc...
  41.  * @package     jelix
  42.  * @subpackage  forms
  43.  */
  44. interface jIFormsDatasource2 extends jIFormsDatasource {
  45.     /**
  46.      * Says if data are grouped, ie, if getData() returns a simple array
  47.      * value=>label (false) or if it returns an array of simple arrays
  48.      * array('group label'=>array(value=>label,)) (true)
  49.      * @return boolean 
  50.      */
  51.     public function hasGroupedData();
  52.  
  53.     /**
  54.      * set a parameter indicating how data are grouped
  55.      * @param string $group the group parameter
  56.      */
  57.     public function setGroupBy($group);
  58.  
  59.     /**
  60.      * Return the label corresponding to the given key.
  61.      * It replace getLabel so it should be called instead of getLabel.
  62.      * @param string $key the key
  63.      * @param jFormsBase $form  the form
  64.      * @return string the label
  65.      */
  66.     public function getLabel2($key$form);
  67.  
  68. }
  69.  
  70.  
  71. /**
  72.  * A datasource which is based on static values.
  73.  * @package     jelix
  74.  * @subpackage  forms
  75.  */
  76. class jFormsStaticDatasource implements jIFormsDatasource2 {
  77.     /**
  78.      * associative array which contains keys and labels
  79.      * @var array 
  80.      */
  81.     public $data = array();
  82.     
  83.     protected $grouped = false;
  84.  
  85.     public function getData($form){
  86.         return $this->data;
  87.     }
  88.  
  89.     public function getLabel2($key$form{
  90.         return $this->getLabel($key);
  91.     }
  92.  
  93.     public function getLabel($key){
  94.         if ($this->grouped{
  95.             foreach ($this->data as $group=>$data){
  96.                 if(isset($data[$key]))
  97.                     return $data[$key];
  98.             }
  99.         }
  100.         elseif(isset($this->data[$key]))
  101.             return $this->data[$key];
  102.         return null;
  103.     }
  104.  
  105.     public function hasGroupedData({
  106.         return $this->grouped;
  107.     }
  108.  
  109.     public function setGroupBy($group{
  110.         $this->grouped = $group;
  111.     }
  112. }
  113.  
  114.  
  115. /**
  116.  * A datasource which is based on a dao
  117.  * @package     jelix
  118.  * @subpackage  forms
  119.  */
  120. class jFormsDaoDatasource implements jIFormsDatasource2 {
  121.  
  122.     protected $selector;
  123.     protected $method;
  124.     protected $labelProperty = array();
  125.     protected $labelSeparator;
  126.     public $labelMethod = 'get';
  127.     protected $keyProperty;
  128.     protected $profile;
  129.  
  130.     protected $criteria = null;
  131.     protected $criteriaFrom = null;
  132.  
  133.     protected $dao = null;
  134.  
  135.     protected $groupeBy = '';
  136.  
  137.     function __construct ($selector ,$method $label$key$profile=''$criteria=null$criteriaFrom=null$labelSeparator=''){
  138.         $this->selector  = $selector;
  139.         $this->profile = $profile;
  140.         $this->method = $method ;
  141.         $this->labelProperty = preg_split('/[\s,]+/',$label);
  142.         $this->labelSeparator = $labelSeparator;
  143.         if $criteria !== null )
  144.             $this->criteria = preg_split('/[\s,]+/',$criteria;
  145.         if $criteriaFrom !== null )
  146.             $this->criteriaFrom = preg_split('/[\s,]+/',$criteriaFrom;
  147.  
  148.         if($key == ''){
  149.             $rec jDao::createRecord($this->selector$this->profile);
  150.             $pfields $rec->getPrimaryKeyNames();
  151.             $key $pfields[0];
  152.         }
  153.         $this->keyProperty = $key;
  154.     }
  155.  
  156.     public function getData($form){
  157.         if($this->dao === null)
  158.             $this->dao = jDao::get($this->selector$this->profile);
  159.         if($this->criteria !== null{
  160.             $found call_user_func_arrayarray($this->dao$this->method)$this->criteria);
  161.         else if ($this->criteriaFrom !== null{
  162.             $args array(;
  163.             foreach(array)$this->criteriaFrom as $criteria {
  164.               array_push$args$form->getData($criteria) ) ;
  165.             }
  166.             $found call_user_func_arrayarray($this->dao$this->method)$args);
  167.         else {
  168.             $found $this->dao->{$this->method}();
  169.         }
  170.  
  171.         $result array();
  172.  
  173.         foreach($found as $obj){
  174.             $label $this->buildLabel($obj);
  175.             $value $obj->{$this->keyProperty};
  176.             if ($this->groupeBy{
  177.                 $group = (string)$obj->{$this->groupeBy};
  178.                 if (!isset($result[$group]))
  179.                     $result[$grouparray();
  180.                 $result[$group][$value$label;
  181.             }
  182.             else {
  183.                 $result[$value$label;
  184.             }
  185.         }
  186.         return $result;
  187.     }
  188.  
  189.     public function getLabel($key{
  190.         throw new Exception("should not be called");
  191.     }
  192.  
  193.     public function getLabel2($key$form){
  194.         if($this->dao === null)
  195.             $this->dao = jDao::get($this->selector$this->profile);
  196.  
  197.         $method $this->labelMethod;
  198.  
  199.  
  200.         if ($this->criteria !== null || $this->criteriaFrom !== null{
  201.             $countPKeys count($this->dao->getPrimaryKeyNames());
  202.             if ($this->criteria !== null{
  203.                 $values $this->criteria;
  204.                 array_unshift($values$key);                
  205.             }
  206.             else if ($this->criteriaFrom !== null{
  207.                 $values array($key);
  208.                 foreach(array)$this->criteriaFrom as $criteria {
  209.                     array_push($values$form->getData($criteria));
  210.                 }
  211.             }
  212.  
  213.             if ($method == 'get'{
  214.                 // in the case where the number of criterias doesn't correspond
  215.                 // to the number of field of the primary key, we give only
  216.                 // the expected number of values. So the retrieved record
  217.                 // won't correspond to the criterias. However, in some case,
  218.                 // it could make sens.
  219.                 // for example, the dependence could be just a filter...
  220.                 while (count($values!= $countPKeys{
  221.                     array_pop($values);
  222.                 }
  223.             }
  224.             $rec call_user_func_arrayarray($this->dao$method)$values);
  225.  
  226.         }
  227.         else {
  228.             $rec $this->dao->{$method}($key);
  229.         }
  230.         if ($rec{
  231.             return $this->buildLabel($rec);
  232.         }
  233.         else {
  234.             return null;
  235.         }
  236.     }
  237.  
  238.     protected function buildLabel($rec{
  239.         $label '' ;
  240.         foreach(array)$this->labelProperty as $property {
  241.             if ((string)$rec->{$property!== '')
  242.                 $label .= $rec->{$property}.$this->labelSeparator;
  243.         }
  244.         if ($this->labelSeparator != '')
  245.             $label substr($label0-strlen($this->labelSeparator));
  246.         return $label ;
  247.     }
  248.  
  249.     public function getDependentControls({
  250.         return $this->criteriaFrom;
  251.     }
  252.  
  253.     public function hasGroupedData({
  254.         return $this->groupeBy;
  255.     }
  256.  
  257.     public function setGroupBy($group{
  258.         $this->groupeBy $group;
  259.     }
  260. }

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