root/trunk/owa_entity.php

Revision 501, 7.5 kB (checked in by padams, 5 months ago)

adding OFC, sparklines, revamped metrics, revamped db, etc.

Line 
1<?php
2
3//
4// Open Web Analytics - An Open Source Web Analytics Framework
5//
6// Copyright 2006 Peter Adams. All rights reserved.
7//
8// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// $Id$
17//
18
19if (!class_exists('owa_dbColumn')):
20        require_once(OWA_BASE_CLASS_DIR.'column.php');
21endif;
22
23/**
24 * Abstract Entity Class
25 *
26 * @author      Peter Adams <peter@openwebanalytics.com>
27 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
28 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
29 * @category    owa
30 * @package     owa
31 * @version             $Revision$           
32 * @since               owa 1.0.0
33 */
34
35class owa_entity {
36       
37        function owa_entity() {
38               
39                $vars = $this->getColumns();
40               
41                foreach ($vars as $k => $v) {
42                       
43                        $this->$v = new owa_dbColumn($this->$v);
44                        $this->$v->set('name', $v);
45                }
46               
47                return;
48        }
49       
50        function _getProperties() {
51               
52                $vars = get_object_vars($this);
53               
54                $properties = array();
55               
56                foreach ($vars as $k => $v) {
57                       
58                        $properties[$k] = $v->value;
59                       
60                }
61               
62                return $properties;     
63        }
64       
65        function getColumns($return_as_string = false, $as_namespace = '', $table_namespace = false) {
66               
67                $all_cols = get_object_vars($this);
68                $table = $this->getTableName();
69                $new_cols = array();
70               
71                if (!empty($table_namespace)): 
72                        $ns = $table.'.';
73                endif;
74                               
75                foreach ($all_cols as $k => $v) {
76                       
77                        if (!empty($as_namespace)):     
78                                $as =  ' AS '.$as_namespace.$k;
79                        endif;
80                       
81                        $new_cols[] = $ns.$k.$as;
82                }
83               
84                // add implode as string here
85               
86                if ($return_as_string == true):
87                        $new_cols = implode(', ', $new_cols);   
88                endif;
89               
90                //print_r($new_cols);
91                return $new_cols; 
92               
93        }
94       
95        function getColumnsSql($as_namespace = '', $table_namespace = true) {
96       
97                return $this->getColumns(true, $as_namespace, $table_namespace);
98               
99        }
100       
101        /**
102         * Sets object attributes
103         *
104         * @param unknown_type $array
105         */
106        function setProperties($array) {
107               
108                $properties = $this->getColumns();
109               
110                foreach ($properties as $k => $v) {
111                               
112                                if (!empty($array[$v])):
113                                        $this->$v->value = $array[$v];
114                                        //print $this->getTableName().$v.':'.$this->$v->value;
115                                endif;
116                               
117                        }
118               
119                return;
120        }
121       
122        function setGuid($string) {
123               
124                return owa_lib::setStringGuid($string);
125               
126        }
127       
128        function set($name, $value) {
129               
130                return $this->$name->value = $value;
131        }
132       
133       
134        function setValues($values) {
135               
136                $properties = array_keys(get_object_vars($this));
137               
138                foreach ($properties as $k => $v) {
139                               
140                                $this->$v->value = $values[$v];
141               
142                        }
143               
144                return;
145               
146        }
147       
148        function get($name) {
149               
150                return $this->$name->value;
151        }
152       
153        function getTableOptions() {
154       
155                return array('table_type' => 'disk');
156       
157        }
158       
159        /**
160         * Persist new object
161         *
162         */ 
163        function create() {     
164               
165                $db = owa_coreAPI::dbSingleton();
166                $config = owa_coreAPI::configSingleton();
167                $cache = owa_coreAPI::cacheSingleton();
168               
169                $all_cols = $this->getColumns();
170               
171                $db->insertInto(get_class($this));
172               
173                // Control loop
174                foreach ($all_cols as $k => $v){
175               
176                        // drop column is it is marked as auto-incement as DB will take care of that.
177                        if ($this->$v->auto_increment == true):
178                                ;
179                        else:
180                                $db->set($v, $this->get($v));
181                        endif;
182                               
183                }
184       
185                // Persist object
186                $status = $db->executeQuery();
187               
188                // Add to Cache
189                if ($status == true):
190                        if ($config->get('base', 'cache_objects') == true):
191                                $cache->set(get_class($this), 'id'.$this->id->value, $this);
192                        endif;
193                endif;
194               
195                return $status;
196               
197        }
198       
199        /**
200         * Update all properties of an Existing object
201         *
202         */
203        function update($where = '') { 
204               
205                $db = owa_coreAPI::dbSingleton();
206                $config = owa_coreAPI::configSingleton();
207                $cache = owa_coreAPI::cacheSingleton();
208               
209                $db->updateTable(get_class($this));
210               
211                // get column list
212                $all_cols = $this->getColumns();
213               
214                // Control loop
215                foreach ($all_cols as $k => $v){
216               
217                        // drop column is it is marked as auto-incement as DB will take care of that.
218                        if (!empty($this->$v->value)):
219                                $db->set($v, $this->get($v));
220                        endif;
221                               
222                }
223               
224                if(empty($where)):
225                        $id = $this->get('id');
226                        $db->where('id', $id);
227                       
228                else:
229                        $db->where($where, $this->get($where));
230                endif;
231               
232                // Persist object
233                $status = $db->executeQuery();
234
235                // Add to Cache
236                if ($status == true):
237                        if ($config->get('base', 'cache_objects') == true):
238                                $cache->replace(get_class($this), 'id'.$this->id->value, $this);
239                        endif;
240                endif;
241               
242                return $status;
243               
244        }
245       
246        /**
247         * Update named list of properties of an existing object
248         *
249         * @param array $named_properties
250         * @param array $where
251         * @return boolean
252         */
253        function partialUpdate($named_properties, $where) {
254               
255                $db = owa_coreAPI::dbSingleton();
256                $config = owa_coreAPI::configSingleton();
257                $cache = owa_coreAPI::cacheSingleton();
258               
259                $db->updateTable(get_class($this));
260               
261                foreach ($named_properties as $v) {
262                       
263                        if (!empty($this->$v->value)):
264                                $db->set($v, $this->get($v));
265                        endif;
266                       
267                }
268               
269                if(empty($where)):
270                        $db->where('id', $this->get('id'));
271                else:
272                        $db->where($where, $this->get($where));
273                endif;
274               
275                // Persist object
276                $status = $db->executeQuery();
277               
278               
279                // Add to Cache
280                if ($status == true):
281                        if ($config->get('base', 'cache_objects') == true):
282                                $cache->set(get_class($this), 'id'.$this->id->value, $this);
283                        endif;
284                endif;
285               
286                return $status;
287               
288        }
289       
290       
291        /**
292         * Delete Object
293         *
294         */
295        function delete($value = '', $col = 'id') {     
296               
297                $db = owa_coreAPI::dbSingleton();
298                $config = owa_coreAPI::configSingleton();
299                $cache = owa_coreAPI::cacheSingleton();
300               
301                $db->deleteFrom(get_class($this));
302               
303                if (empty($value)):
304                        $value = $this->get('id');
305                endif;
306               
307                $db->where($col, $value);       
308
309                $status = $db->executeQuery();
310       
311                // Add to Cache
312                if ($status == true):
313                        if ($config->get('base', 'cache_objects') == true):
314                                $cache->remove(get_class($this), 'id'.$this->id->value);
315                        endif;
316                endif;
317               
318                return $status;
319               
320        }
321       
322        function load($value, $col = 'id') {
323               
324                return $this->getByColumn($col, $value);
325               
326        }
327       
328        function getByPk($col, $value) {
329               
330                return $this->getByColumn($col, $value);
331               
332        }
333       
334        function getByColumn($col, $value) {
335               
336                $db = owa_coreAPI::dbSingleton();
337                $config = owa_coreAPI::configSingleton();
338                $cache = owa_coreAPI::cacheSingleton();
339               
340                $cache_obj = '';
341               
342                if ($config->get('base', 'cache_objects') == true):
343                        $cache_obj = $cache->get(get_class($this), $col.$value);
344                endif;
345                       
346                if (!empty($cache_obj)):
347               
348                        $cache_obj_properties = $cache_obj->_getProperties();
349                        $this->setProperties($cache_obj_properties);
350                                       
351                else:
352                       
353                        $db->selectFrom(get_class($this));
354                        $db->selectColumn('*');
355                        $db->where($col, $value);
356                        $properties = $db->getOneRow();
357                       
358                        if (!empty($properties)):
359                                       
360                                $this->setProperties($properties);
361                               
362                                if ($config->get('base', 'cache_objects') == true):
363                                        $cache->set(get_class($this), 'id'.$this->id->value, $this);   
364                                endif; 
365                                       
366                        endif;
367                       
368                endif;
369               
370                return; 
371        }
372
373        function getTableName() {
374                return get_class($this);
375        }
376
377       
378}
379
380?>
Note: See TracBrowser for help on using the browser.