/lib/jelix/forms/jFormsDatasource.class.php

https://github.com/BorisMorel/havefnubb · PHP · 164 lines · 152 code · 0 blank · 12 comment · 19 complexity · 35a7cd33ca1a84a32f7bea7d52ec06d6 MD5 · raw file

  1. <?php
  2. /* comments & extra-whitespaces have been removed by jBuildTools*/
  3. /**
  4. * @package jelix
  5. * @subpackage forms
  6. * @author Laurent Jouanneau
  7. * @contributor Dominique Papin, Julien Issler
  8. * @copyright 2006-2010 Laurent Jouanneau
  9. * @copyright 2008 Dominique Papin
  10. * @copyright 2010 Julien Issler
  11. * @link http://www.jelix.org
  12. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  13. */
  14. interface jIFormsDatasource{
  15. public function getData($form);
  16. public function getLabel($key);
  17. }
  18. interface jIFormsDatasource2 extends jIFormsDatasource{
  19. public function hasGroupedData();
  20. public function setGroupBy($group);
  21. public function getLabel2($key,$form);
  22. }
  23. class jFormsStaticDatasource implements jIFormsDatasource2{
  24. public $data=array();
  25. protected $grouped=false;
  26. public function getData($form){
  27. return $this->data;
  28. }
  29. public function getLabel2($key,$form){
  30. return $this->getLabel($key);
  31. }
  32. public function getLabel($key){
  33. if($this->grouped){
  34. foreach($this->data as $group=>$data){
  35. if(isset($data[$key]))
  36. return $data[$key];
  37. }
  38. }
  39. elseif(isset($this->data[$key]))
  40. return $this->data[$key];
  41. return null;
  42. }
  43. public function hasGroupedData(){
  44. return $this->grouped;
  45. }
  46. public function setGroupBy($group){
  47. $this->grouped=$group;
  48. }
  49. }
  50. class jFormsDaoDatasource implements jIFormsDatasource2{
  51. protected $selector;
  52. protected $method;
  53. protected $labelProperty=array();
  54. protected $labelSeparator;
  55. public $labelMethod='get';
  56. protected $keyProperty;
  57. protected $profile;
  58. protected $criteria=null;
  59. protected $criteriaFrom=null;
  60. protected $dao=null;
  61. protected $groupeBy='';
  62. function __construct($selector,$method,$label,$key,$profile='',$criteria=null,$criteriaFrom=null,$labelSeparator=''){
  63. $this->selector=$selector;
  64. $this->profile=$profile;
  65. $this->method=$method;
  66. $this->labelProperty=preg_split('/[\s,]+/',$label);
  67. $this->labelSeparator=$labelSeparator;
  68. if($criteria!==null)
  69. $this->criteria=preg_split('/[\s,]+/',$criteria);
  70. if($criteriaFrom!==null)
  71. $this->criteriaFrom=preg_split('/[\s,]+/',$criteriaFrom);
  72. if($key==''){
  73. $rec=jDao::createRecord($this->selector,$this->profile);
  74. $pfields=$rec->getPrimaryKeyNames();
  75. $key=$pfields[0];
  76. }
  77. $this->keyProperty=$key;
  78. }
  79. public function getData($form){
  80. if($this->dao===null)
  81. $this->dao=jDao::get($this->selector,$this->profile);
  82. if($this->criteria!==null){
  83. $found=call_user_func_array(array($this->dao,$this->method),$this->criteria);
  84. }else if($this->criteriaFrom!==null){
  85. $args=array();
  86. foreach((array)$this->criteriaFrom as $criteria){
  87. array_push($args,$form->getData($criteria));
  88. }
  89. $found=call_user_func_array(array($this->dao,$this->method),$args);
  90. }else{
  91. $found=$this->dao->{$this->method}();
  92. }
  93. $result=array();
  94. foreach($found as $obj){
  95. $label=$this->buildLabel($obj);
  96. $value=$obj->{$this->keyProperty};
  97. if($this->groupeBy){
  98. $group=(string)$obj->{$this->groupeBy};
  99. if(!isset($result[$group]))
  100. $result[$group]=array();
  101. $result[$group][$value]=$label;
  102. }
  103. else{
  104. $result[$value]=$label;
  105. }
  106. }
  107. return $result;
  108. }
  109. public function getLabel($key){
  110. throw new Exception("should not be called");
  111. }
  112. public function getLabel2($key,$form){
  113. if($this->dao===null)
  114. $this->dao=jDao::get($this->selector,$this->profile);
  115. $method=$this->labelMethod;
  116. if($this->criteria!==null||$this->criteriaFrom!==null){
  117. $countPKeys=count($this->dao->getPrimaryKeyNames());
  118. if($this->criteria!==null){
  119. $values=$this->criteria;
  120. array_unshift($values,$key);
  121. }
  122. else if($this->criteriaFrom!==null){
  123. $values=array($key);
  124. foreach((array)$this->criteriaFrom as $criteria){
  125. array_push($values,$form->getData($criteria));
  126. }
  127. }
  128. if($method=='get'){
  129. while(count($values)!=$countPKeys){
  130. array_pop($values);
  131. }
  132. }
  133. $rec=call_user_func_array(array($this->dao,$method),$values);
  134. }
  135. else{
  136. $rec=$this->dao->{$method}($key);
  137. }
  138. if($rec){
  139. return $this->buildLabel($rec);
  140. }
  141. else{
  142. return null;
  143. }
  144. }
  145. protected function buildLabel($rec){
  146. $label='';
  147. foreach((array)$this->labelProperty as $property){
  148. if((string)$rec->{$property}!=='')
  149. $label.=$rec->{$property}.$this->labelSeparator;
  150. }
  151. if($this->labelSeparator!='')
  152. $label=substr($label,0,-strlen($this->labelSeparator));
  153. return $label;
  154. }
  155. public function getDependentControls(){
  156. return $this->criteriaFrom;
  157. }
  158. public function hasGroupedData(){
  159. return $this->groupeBy;
  160. }
  161. public function setGroupBy($group){
  162. $this->groupeBy=$group;
  163. }
  164. }