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...
66         
67         
68         if (isset($q['_join'])) {
69             //DB_DataObject::DebugLevel(1);
70             $joins = explode(',',$q['_join']);
71             foreach($joins as $t) {
72                 $t = preg_replace('/[^a-z_]+/', '', $t); // protection.
73                 $x = DB_DataObject::Factory($t);
74                 if (!is_a($x,'DB_DataObject')) {
75                     continue;
76                 }
77                 $jtn = $x->tableName();
78                 $jk = array_shift($x->keys());
79                 $this->_join .= "
80                 
81                     LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
82                         AND on_table = '{$jtn}'
83                 ";
84                 $keys = array_keys($x->table());
85                 if (isset($q['_join_cols']) && in_array($q['_join_cols'], $keys)) {
86                     DB_DataObject::DebugLevel(1);
87                      $this->selectAdd();
88                    
89                     $this->selectAdd( "
90                                 distinct(join_on_id_{$jtn}.{$q['_join_cols']}  )
91                                 as on_id_{$q['_join_cols']} ,
92                                 MAX(events.event_when) as event_when
93                                 
94                                 ");
95                     $this->groupBy("on_id_{$q['_join_cols']} ");
96                     $this->orderBy('event_when DESC');
97                    // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
98                 } else { 
99                     $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
100                 }
101             }
102                 
103                 
104             
105             
106         }
107         
108         
109         
110             
111     }
112     /**
113      * check who is trying to access this. false == access denied..
114      * @return {boolean} true if access is allowed.
115      */
116     function checkPerm($lvl, $au) 
117     {
118         if ($lvl == 'S') {
119             return true;
120         }
121         // listing is controleed by applyfilters..
122         return $au->hasPerm("Admin.Admin_Tab", 'S');
123     }
124     /**
125      * object :
126      * return the object that this relates to.
127      * 
128      * @return {DB_DataObject} related object
129      */
130     function object()
131     {
132         $o = DB_DataObject::factory($this->on_table);
133         $o->get($this->on_id);
134         return $o;
135         
136     }
137     
138     
139     /**
140      * init:
141      * Initialize an event - ready to insert..
142      * 
143      * @param {String} action  - group/name of event
144      * @param {DataObject|false} obj - dataobject action occured on.
145      * @param {String} any remarks 
146      */
147     
148     function init($act, $obj, $remarks)
149     {
150         $ff = HTML_FlexyFramework::get();
151         $pg = $ff->page;
152         $au = $pg->getAuthUser();
153         
154         if ($ff->cli && empty($au) && isset($obj->person_id)) {
155             $au = DB_DataObject::Factory('Person'); // not always a person..
156             $au->get($obj->person_id);
157         } 
158          
159          
160          
161         $this->person_name = $au && !empty($au->name) ? $au->name : '';
162         $this->person_id = $au ? $au->id : '';
163         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
164         $this->action = $act;
165         $this->on_table = $obj ? $obj->tableName() : '';
166         $pk = $obj ? $obj->keys()  : false;
167         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
168         $rem  = array();
169         // should this really go in remarks? - 
170         if ($obj && method_exists($obj,'toEventString')) {
171             $rem[] = $obj->toEventString() ;
172         }
173         $rem[] = $remarks;
174         $this->remarks = implode(' : ', $rem);
175     }
176     
177     /**
178      * Generate an audit for this field.
179      *
180      * @param {DB_DataObject} new data
181      * @param {DB_DataObject} old data
182      * 
183      * @return {int} number of entries logged.
184      */
185     
186     function audit($new, $old = false)
187     {
188         if ($old == $new) {
189             return 0; // they are the same...
190         }
191          
192         $ret = 0;
193         foreach(array_keys($new->table()) as $k) {
194             // should we JSON serialize this?
195             $n = empty($new->$k) ? '' : $new->$k;
196             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
197             if ($n == $o) {
198                 continue;
199             }
200             $this->auditField($k, $o, $n, $old);
201             $ret++;
202         }
203         return $ret;
204     }
205     /**
206      * Record an audited change, in theory so we can audit data that is not just
207      * database Fields...
208      *
209      * @param {string} $name    table field anme
210      * @param {mixed} $ov  old value
211      * @param {mixed} $onv  new value
212      * @param {mixed} $old  old object (false if we are creating..)
213      */
214     function auditField($name, $ov, $nv, $old=false )
215     {
216         $x = DB_DataObject::factory('core_event_audit');
217         $x->setFrom(array(
218             'event_id' => $this->id,
219             'name' => $name,
220             'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
221             'newvalue' => $nv
222
223         ));
224         $x->insert();
225     
226     }
227 }