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-2007 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.      * @param string $key the key
  31.      * @return string the label
  32.      */
  33.     public function getLabel($key);
  34. }
  35.  
  36. /**
  37.  * old interface which have been renamed to jIFormsDatasource.
  38.  * use jIFormsDatasource instead
  39.  * @package     jelix
  40.  * @subpackage  forms
  41.  * @deprecated since 1.1
  42.  */
  43. interface jIFormDatasource extends jIFormsDatasource {
  44. }
  45.  
  46. /**
  47.  * A datasource which is based on static values.
  48.  * @package     jelix
  49.  * @subpackage  forms
  50.  */
  51. class jFormsStaticDatasource implements jIFormsDatasource {
  52.     /**
  53.      * associative array which contains keys and labels
  54.      * @var array 
  55.      */
  56.     public $data = array();
  57.  
  58.     public function getData($form){
  59.         return $this->data;
  60.     }
  61.  
  62.     public function getLabel($key){
  63.         if(isset($this->data[$key]))
  64.             return $this->data[$key];
  65.         else
  66.             return null;
  67.     }
  68. }
  69.  
  70.  
  71. /**
  72.  * A datasource which is based on a dao
  73.  * @package     jelix
  74.  * @subpackage  forms
  75.  */
  76. class jFormsDaoDatasource implements jIFormsDatasource {
  77.  
  78.     protected $selector;
  79.     protected $method;
  80.     protected $labelProperty = array();
  81.     protected $labelSeparator;
  82.     protected $keyProperty;
  83.     protected $profile;
  84.  
  85.     protected $criteria = null;
  86.     protected $criteriaFrom = null;
  87.  
  88.     protected $dao = null;
  89.  
  90.     function __construct ($selector ,$method , $label, $key, $profile='', $criteria=null, $criteriaFrom=null, $labelSeparator=''){
  91.         $this->selector  = $selector;
  92.         $this->profile = $profile;
  93.         $this->method = $method ;
  94.         $this->labelProperty = preg_split('/[\s,]+/',$label);
  95.         $this->labelSeparator = $labelSeparator;
  96.         if ( $criteria !== null )
  97.             $this->criteria = preg_split('/[\s,]+/',$criteria) ;
  98.         if ( $criteriaFrom !== null )
  99.             $this->criteriaFrom = preg_split('/[\s,]+/',$criteriaFrom) ;
  100.  
  101.         if($key == ''){
  102.             $rec = jDao::createRecord($this->selector, $this->profile);
  103.             $pfields = $rec->getPrimaryKeyNames();
  104.             $key = $pfields[0];
  105.         }
  106.         $this->keyProperty = $key;
  107.     }
  108.  
  109.     public function getData($form){
  110.         if($this->dao === null)
  111.             $this->dao = jDao::get($this->selector, $this->profile);
  112.         if($this->criteria !== null) {
  113.             $found = call_user_func_array( array($this->dao, $this->method), $this->criteria);
  114.         } else if ($this->criteriaFrom !== null) {
  115.             $args = array() ;
  116.             foreach( (array)$this->criteriaFrom as $criteria ) {
  117.               array_push( $args, $form->getData($criteria) ) ;
  118.             }
  119.             $found = call_user_func_array( array($this->dao, $this->method), $args);
  120.         } else {
  121.             $found = $this->dao->{$this->method}();
  122.         }
  123.         $result=array();
  124.         foreach($found as $obj){
  125.             $label = '' ;
  126.             foreach( (array)$this->labelProperty as $property ) {
  127.                 if ((string)$obj->{$property} !== '')
  128.                     $label .= $obj->{$property}.$this->labelSeparator;
  129.             }
  130.             if ($this->labelSeparator != '')
  131.                 $label = substr($label, 0, -strlen($this->labelSeparator));
  132.             $result[$obj->{$this->keyProperty}] = $label ;
  133.         }
  134.         return $result;
  135.     }
  136.  
  137.     public function getLabel($key){
  138.         if($this->dao === null) $this->dao = jDao::get($this->selector, $this->profile);
  139.         $rec = $this->dao->get($key);
  140.         if ($rec) {
  141.             $label = '' ;
  142.             foreach( (array)$this->labelProperty as $property ) {
  143.                 if ((string)$rec->{$property} !== '')
  144.                     $label .= $rec->{$property}.$this->labelSeparator;
  145.             }
  146.             if ($this->labelSeparator != '')
  147.                 $label = substr($label, 0, -strlen($this->labelSeparator));
  148.             return $label ;
  149.         }
  150.         else
  151.             return null;
  152.     }
  153. }

Documentation generated on Thu, 22 Mar 2012 22:16:07 +0100 by phpDocumentor 1.4.3