PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4/lib/Controller/Data/Array.php

https://github.com/mahimarathore/mahi
PHP | 197 lines | 143 code | 12 blank | 42 comment | 26 complexity | 33b2d80f6fab33663e907ec3e328ec8e MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /*
  3. * Implementation of array access controller for Agile Toolkit Models.
  4. *
  5. * $m=$this->add('Model');
  6. * $m->addField('test');
  7. * $m->table='test_table';
  8. *
  9. * $storage=array();
  10. *
  11. * $m->setSource('Array', $storage);
  12. * $m['test']=123;
  13. * $m->save(1);
  14. *
  15. * $m['test']=321;
  16. * $m->save(2);
  17. *
  18. * $m->load(1);
  19. * echo $m['test'];
  20. *//*
  21. ==ATK4===================================================
  22. This file is part of Agile Toolkit 4
  23. http://agiletoolkit.org/
  24. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  25. Distributed under Affero General Public License v3 and
  26. commercial license.
  27. See LICENSE or LICENSE_COM for more information
  28. =====================================================ATK4=*/
  29. class Controller_Data_Array extends Controller_Data {
  30. /* By default new records are added with the ID being sequential. If you set this to false, then model IDs will be assigned using unique identifiers */
  31. public $sequential_id=true;
  32. /* If your model is using id_field and the record with key==id was not found, controller will scan array for a matching record based on the field.
  33. * When you are using data blob from external source which does not have key associations, you should keep this "true". If you save records using
  34. * save() only, it will maintain keys automatically and you can set this to false for extra speed.
  35. *
  36. * This setting have no effect on models without id_field property, as those would always rely on keys */
  37. public $search_on_load=true;
  38. function setSource($model,$data=undefined){
  39. if(!$data || $data===undefined)$data=array();
  40. parent::setSource($model,$data);
  41. if(!$model->hasElement($model->id_field))$model->addField($model->id_field)->system(true);
  42. return $this;
  43. }
  44. function getBy($model,$field,$cond=undefined,$value=undefined){
  45. $t =& $model->_table[$this->short_name];
  46. foreach($t as $row){
  47. if($row[$field]==$value){
  48. return $row;
  49. }
  50. }
  51. }
  52. function tryLoadBy($model,$field,$cond=undefined,$value=undefined){
  53. if($value===undefined){
  54. $value=$cond;
  55. $cond='=';
  56. }
  57. foreach($model->_table[$this->short_name] as $row){
  58. if($row[$field]==$value){
  59. $model->data=$row;
  60. $model->dirty=array();
  61. $model->id=$row[$model->id_field];
  62. return $this;
  63. }
  64. }
  65. return $this;
  66. }
  67. function tryLoadAny($model){
  68. if(!is_array($model->_table[$this->short_name]))return null;
  69. reset($model->_table[$this->short_name]);
  70. list($id,$row)=each($model->_table[$this->short_name]);
  71. $model->data=$row;
  72. $model->dirty=array();
  73. $model->id=$model->id_field?$row[$model->id_field]:$id;
  74. return $this;
  75. }
  76. function loadBy($model,$field,$cond=undefined,$value=undefined){
  77. if($value===undefined){
  78. $value=$cond;
  79. $cond='=';
  80. }
  81. $this->tryLoadBy($model,$field,$cond,$value);
  82. if(!$model->loaded())throw $this->exception('Unable to load data')
  83. ->addMoreInfo('field',$field)
  84. ->addMoreInfo('condition',$cond)
  85. ->addMoreInfo('value',$value);
  86. return $this;
  87. }
  88. function tryLoad($model,$id){
  89. if(is_object($id))return;
  90. if(@$model->id_field){
  91. if( !isset($model->_table[$this->short_name][$id]) || $model->_table[$this->short_name][$id][$model->id_field]!=$id){
  92. return $this->tryLoadBy($model,$model->id_field,$id);
  93. }
  94. // ID key exists and it points to record with a matching id_field. Lucky! Can save some time loading it.
  95. }
  96. if(!isset($model->_table[$this->short_name][$id]))return $this;
  97. $model->data=$model->_table[$this->short_name][$id];
  98. $model->dirty=array();
  99. $model->id=$id;
  100. return $this;
  101. }
  102. function load($model,$id=null){
  103. $this->tryLoad($model,$id);
  104. if(!$model->loaded())throw $this->exception('Unable to load data')
  105. ->addMoreInfo('id',$id);
  106. return $this;
  107. }
  108. function save($model,$id=null){
  109. if(is_null($id)){
  110. if($this->sequential_id){
  111. // Imants: This fail if array is not sorted in ascending order by its keys
  112. //end($model->_table[$this->short_name]);
  113. //list($id)=each($model->_table[$this->short_name]);
  114. //$id++;
  115. if(!empty($model->_table[$this->short_name])) {
  116. $id = max(array_keys($model->_table[$this->short_name])) + 1;
  117. } else {
  118. $id = 1;
  119. }
  120. }else{
  121. $id=uniqid();
  122. }
  123. if($model->id_field){
  124. $model->data[$model->id_field]=$id;
  125. }
  126. $model->_table[$this->short_name][$id]=$model->data;
  127. }else{
  128. $model->_table[$this->short_name][$id]=$model->data;
  129. }
  130. return $id;
  131. }
  132. function delete($model,$id=null){
  133. unset($model->_table[$this->short_name][$id?:$model->id]);
  134. return $this;
  135. }
  136. function deleteAll($model){
  137. $model->_table[$this->short_name]=array();
  138. return $this;
  139. }
  140. function getRows($model){
  141. return $model->_table[$this->short_name];
  142. }
  143. function setOrder($model,$field,$desc=false){
  144. if (is_bool($desc)) {
  145. $desc=$desc?'desc':'';
  146. } elseif (strtolower($desc)==='asc') {
  147. $desc='';
  148. } elseif ($desc && strtolower($desc)!='desc') {
  149. throw $this->exception('Incorrect ordering keyword')
  150. ->addMoreInfo('order by', $desc);
  151. }
  152. // this physically change order of array elements, so be aware of that !
  153. uasort($model->_table[$this->short_name], function($a,$b)use($field,$desc){
  154. $r = strtolower($a[$field]) < strtolower($b[$field]) ? -1 : 1;
  155. return $desc==='desc' ? -$r : $r;
  156. });
  157. }
  158. function setLimit($model,$count,$offset=0){
  159. // TODO: splice
  160. }
  161. function rewind($model){
  162. reset($model->_table[$this->short_name]);
  163. list($model->id,$model->data)=each($model->_table[$this->short_name]);
  164. if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
  165. return $model->data;
  166. }
  167. function next($model){
  168. list($model->id,$model->data)=each($model->_table[$this->short_name]);
  169. if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
  170. $model->set("id", $model->id); // romans, revise please - otherwise, array based source not working properly
  171. return $model;
  172. }
  173. function setAssoc($data){
  174. $this->array_data=array();
  175. foreach($data as $id=>$name){
  176. $this->array_data[]=array('id'=>$id,'name'=>$name);
  177. }
  178. return $this;
  179. }
  180. function getActualFields(){
  181. return array();
  182. }
  183. }