DataObjects/Events.php
[Pman.Core] / DataObjects / Events.php
1 <?php
2 /**
3  * Table Definition for Events
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Events extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'Events';                          // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $person_name;                     // string(128)  
15     public $event_when;                      // datetime(19)  binary
16     public $action;                          // string(32)  
17     public $ipaddr;                          // string(16)  
18     public $on_id;                           // int(11)  
19     public $on_table;                        // string(64)  
20     public $person_id;                       // int(11)  
21     public $remarks;                         // blob(65535)  blob
22
23     
24     /* the code above is auto generated do not remove the tag below */
25     ###END_AUTOCODE
26     
27     
28     
29     
30     //  ------------ROO HOOKS------------------------------------
31     function applyFilters($q, $au)
32     {
33         $tn = $this->tableName();
34         if (!empty($q['query']['from'])) {
35             $dt = date('Y-m-d' , strtotime($q['query']['from']));
36             $this->whereAdd(" {$tn}.event_when >=  '$dt' ");
37         }
38         if (!empty($q['query']['to'])) {
39             $dt = date('Y-m-d' , strtotime($q['query']['to']));
40             $this->whereAdd(" {$tn}.event_when <=  '$dt' ");
41         }
42         /*
43         if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
44             // grouped..
45             DB_DataObject::Debuglevel(1);
46             $this->groupBy('on_id');
47             $this->selectAdd('
48                 (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
49                 ');
50         }
51         */
52         
53         if (!$au->hasPerm("Admin.Admin_Tab", 'S')) {
54             //DB_DataObject::DebugLevel(1);
55             // they can only view their changes..
56             $this->person_id = $au->id;
57             
58         }
59         // _join = tablename,tablename...
60         
61         /// on_table=cohead
62         //   &_join=cohead
63         //   &_join_cols=cohead_number
64         //    &_columns=on_id_cohead_number,event_when << this is ignored at present.
65         // max(event_when) is not supported... by any query yet..
66         
67         
68         if (isset($q['_join'])) {
69             //DB_DataObject::DebugLevel(1);
70             $joins = explode(',',$q['_join']);
71             
72             $this->selectAdd(); // ???
73             
74             foreach($joins as $t) {
75                 $t = preg_replace('/[^a-z_]+/', '', $t); // protection.
76                 $x = DB_DataObject::Factory($t);
77                 if (!is_a($x,'DB_DataObject')) {
78                     continue;
79                 }
80                 $jtn = $x->tableName();
81                 $jk = array_shift($x->keys());
82                 $this->_join .= "
83                 
84                     LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
85                         AND on_table = '{$jtn}'
86                 ";
87                 $keys = array_keys($x->table());
88                 if (isset($q['_join_cols'])) {
89                     $jcs = explode(',',$q['_join_cols'] );
90                     DB_DataObject::DebugLevel(1);
91                     
92                     foreach($jcs as $jc) { 
93                         if (! in_array($jc, $keys)) {
94                             continue;
95                         }
96                     
97                         
98                        
99                         $this->selectAdd( " distinct(join_on_id_{$jtn}.{$q['_join_cols']}  ) as on_id_{$q['_join_cols']} ");
100                         $this->groupBy("on_id_{$q['_join_cols']} ");
101                         $this->whereAdd("{$q['_join_cols']} IS NOT NULL");
102                     }
103                     $this->selectAdd( "MAX(events.event_when) as event_when");
104                     $this->orderBy('event_when DESC');
105                    // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
106                 } else { 
107                     $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
108                 }
109             }
110                 
111                 
112             
113             
114         }
115         
116         
117         
118             
119     }
120     /**
121      * check who is trying to access this. false == access denied..
122      * @return {boolean} true if access is allowed.
123      */
124     function checkPerm($lvl, $au) 
125     {
126         if ($lvl == 'S') {
127             return true;
128         }
129         // listing is controleed by applyfilters..
130         return $au->hasPerm("Admin.Admin_Tab", 'S');
131     }
132     /**
133      * object :
134      * return the object that this relates to.
135      * 
136      * @return {DB_DataObject} related object
137      */
138     function object()
139     {
140         $o = DB_DataObject::factory($this->on_table);
141         $o->get($this->on_id);
142         return $o;
143         
144     }
145     
146     
147     /**
148      * init:
149      * Initialize an event - ready to insert..
150      * 
151      * @param {String} action  - group/name of event
152      * @param {DataObject|false} obj - dataobject action occured on.
153      * @param {String} any remarks 
154      */
155     
156     function init($act, $obj, $remarks)
157     {
158         $ff = HTML_FlexyFramework::get();
159         $pg = $ff->page;
160         $au = $pg->getAuthUser();
161         
162         if ($ff->cli && empty($au) && isset($obj->person_id)) {
163             $au = DB_DataObject::Factory('Person'); // not always a person..
164             $au->get($obj->person_id);
165         } 
166          
167          
168          
169         $this->person_name = $au && !empty($au->name) ? $au->name : '';
170         $this->person_id = $au ? $au->id : '';
171         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
172         $this->action = $act;
173         $this->on_table = $obj ? $obj->tableName() : '';
174         $pk = $obj ? $obj->keys()  : false;
175         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
176         $rem  = array();
177         // should this really go in remarks? - 
178         if ($obj && method_exists($obj,'toEventString')) {
179             $rem[] = $obj->toEventString() ;
180         }
181         $rem[] = $remarks;
182         $this->remarks = implode(' : ', $rem);
183     }
184     
185     /**
186      * Generate an audit for this field.
187      *
188      * @param {DB_DataObject} new data
189      * @param {DB_DataObject} old data
190      * 
191      * @return {int} number of entries logged.
192      */
193     
194     function audit($new, $old = false)
195     {
196         if ($old == $new) {
197             return 0; // they are the same...
198         }
199          
200         $ret = 0;
201         foreach(array_keys($new->table()) as $k) {
202             // should we JSON serialize this?
203             $n = empty($new->$k) ? '' : $new->$k;
204             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
205             if ($n == $o) {
206                 continue;
207             }
208             $this->auditField($k, $o, $n, $old);
209             $ret++;
210         }
211         return $ret;
212     }
213     /**
214      * Record an audited change, in theory so we can audit data that is not just
215      * database Fields...
216      *
217      * @param {string} $name    table field anme
218      * @param {mixed} $ov  old value
219      * @param {mixed} $onv  new value
220      * @param {mixed} $old  old object (false if we are creating..)
221      */
222     function auditField($name, $ov, $nv, $old=false )
223     {
224         $x = DB_DataObject::factory('core_event_audit');
225         $x->setFrom(array(
226             'event_id' => $this->id,
227             'name' => $name,
228             'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
229             'newvalue' => $nv
230
231         ));
232         $x->insert();
233     
234     }
235 }