PageRenderTime 184ms CodeModel.GetById 101ms app.highlight 10ms RepoModel.GetById 65ms 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
  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
 25   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
 26   Distributed under Affero General Public License v3 and
 27   commercial license.
 28
 29   See LICENSE or LICENSE_COM for more information
 30 =====================================================ATK4=*/
 31
 32class Controller_Data_Array extends Controller_Data {
 33
 34    /* 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 */
 35    public $sequential_id=true;
 36
 37    /* 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. 
 38     * 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
 39     * save() only, it will maintain keys automatically and you can set this to false for extra speed.
 40     *
 41     * This setting have no effect on models without id_field property, as those would always rely on keys */
 42    public $search_on_load=true;
 43
 44    function setSource($model,$data=undefined){
 45        if(!$data || $data===undefined)$data=array();
 46        parent::setSource($model,$data);
 47
 48        if(!$model->hasElement($model->id_field))$model->addField($model->id_field)->system(true);
 49
 50
 51        return $this;
 52    }
 53    function getBy($model,$field,$cond=undefined,$value=undefined){
 54        $t =& $model->_table[$this->short_name];
 55        foreach($t as $row){
 56            if($row[$field]==$value){
 57                return $row;
 58            }
 59        }
 60    }
 61    function tryLoadBy($model,$field,$cond=undefined,$value=undefined){
 62        if($value===undefined){
 63            $value=$cond;
 64            $cond='=';
 65        }
 66        foreach($model->_table[$this->short_name] as $row){
 67            if($row[$field]==$value){
 68                $model->data=$row;
 69                $model->dirty=array();
 70                $model->id=$row[$model->id_field];
 71                return $this;
 72            }
 73        }
 74        return $this;
 75    }
 76    function tryLoadAny($model){
 77        if(!is_array($model->_table[$this->short_name]))return null;
 78        reset($model->_table[$this->short_name]);
 79        list($id,$row)=each($model->_table[$this->short_name]);
 80
 81        $model->data=$row;
 82        $model->dirty=array();
 83        $model->id=$model->id_field?$row[$model->id_field]:$id;
 84
 85        return $this;
 86    }
 87    function loadBy($model,$field,$cond=undefined,$value=undefined){
 88        if($value===undefined){
 89            $value=$cond;
 90            $cond='=';
 91        }
 92        $this->tryLoadBy($model,$field,$cond,$value);
 93        if(!$model->loaded())throw $this->exception('Unable to load data')
 94            ->addMoreInfo('field',$field)
 95            ->addMoreInfo('condition',$cond)
 96            ->addMoreInfo('value',$value);
 97        return $this;
 98    }
 99    function tryLoad($model,$id){
100        if(is_object($id))return;
101
102        if(@$model->id_field){
103            if( !isset($model->_table[$this->short_name][$id]) || $model->_table[$this->short_name][$id][$model->id_field]!=$id){
104                return $this->tryLoadBy($model,$model->id_field,$id);
105            }
106            // ID key exists and it points to record with a matching id_field. Lucky! Can save some time loading it.
107        }
108        if(!isset($model->_table[$this->short_name][$id]))return $this;
109        $model->data=$model->_table[$this->short_name][$id];
110        $model->dirty=array();
111        $model->id=$id;
112        return $this;
113    }
114    function load($model,$id=null){
115        $this->tryLoad($model,$id);
116        if(!$model->loaded())throw $this->exception('Unable to load data')
117            ->addMoreInfo('id',$id);
118        return $this;
119    }
120    function save($model,$id=null){
121        if(is_null($id)){
122            if($this->sequential_id){
123                // Imants: This fail if array is not sorted in ascending order by its keys
124                //end($model->_table[$this->short_name]);
125                //list($id)=each($model->_table[$this->short_name]);
126                //$id++;
127                if(!empty($model->_table[$this->short_name])) {
128                    $id = max(array_keys($model->_table[$this->short_name])) + 1;
129                } else {
130                    $id = 1;
131                }
132            }else{
133                $id=uniqid();
134            }
135            if($model->id_field){
136                $model->data[$model->id_field]=$id;
137            }
138            $model->_table[$this->short_name][$id]=$model->data;
139        }else{
140            $model->_table[$this->short_name][$id]=$model->data;
141        }
142        return $id;
143    }
144    function delete($model,$id=null){
145        unset($model->_table[$this->short_name][$id?:$model->id]);
146        return $this;
147    }
148    function deleteAll($model){
149        $model->_table[$this->short_name]=array();
150        return $this;
151    }
152    function getRows($model){
153        return $model->_table[$this->short_name];
154    }
155    function setOrder($model,$field,$desc=false){
156        if (is_bool($desc)) {
157            $desc=$desc?'desc':'';
158        } elseif (strtolower($desc)==='asc') {
159            $desc='';
160        } elseif ($desc && strtolower($desc)!='desc') {
161            throw $this->exception('Incorrect ordering keyword')
162                ->addMoreInfo('order by', $desc);
163        }
164        // this physically change order of array elements, so be aware of that !
165        uasort($model->_table[$this->short_name], function($a,$b)use($field,$desc){
166            $r = strtolower($a[$field]) < strtolower($b[$field]) ? -1 : 1;
167            return $desc==='desc' ? -$r : $r;
168        });
169    }
170    function setLimit($model,$count,$offset=0){
171        // TODO: splice
172    }
173
174    function rewind($model){
175        reset($model->_table[$this->short_name]);
176
177        list($model->id,$model->data)=each($model->_table[$this->short_name]);
178        if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
179        return $model->data;
180    }
181    function next($model){
182        list($model->id,$model->data)=each($model->_table[$this->short_name]);
183        if(@$model->id_field && isset($model->data[$model->id_field]))$model->id=$model->data[$model->id_field];
184        $model->set("id", $model->id); // romans, revise please - otherwise, array based source not working properly
185        return $model;
186    }
187    function setAssoc($data){
188        $this->array_data=array();
189        foreach($data as $id=>$name){
190            $this->array_data[]=array('id'=>$id,'name'=>$name);
191        }
192        return $this;
193    }
194    function getActualFields(){
195        return array();
196    }
197}