3 * Table Definition for Events
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Events extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
24 /* the code above is auto generated do not remove the tag below */
30 // ------------ROO HOOKS------------------------------------
31 function applyFilters($q, $au)
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' ");
38 if (!empty($q['query']['to'])) {
39 $dt = date('Y-m-d' , strtotime($q['query']['to']));
40 $this->whereAdd(" {$tn}.event_when <= '$dt' ");
43 if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
45 DB_DataObject::Debuglevel(1);
46 $this->groupBy('on_id');
48 (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
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;
59 // _join = tablename,tablename...
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..
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')) {
77 $jtn = $x->tableName();
78 $jk = array_shift($x->keys());
81 LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
82 AND on_table = '{$jtn}'
84 $keys = array_keys($x->table());
85 if (isset($q['_join_cols']) && in_array($q['_join_cols'], $keys)) {
86 // DB_DataObject::DebugLevel(1);
90 distinct(join_on_id_{$jtn}.{$q['_join_cols']} )
91 as on_id_{$q['_join_cols']} ,
92 MAX(events.event_when) as event_when
95 $this->groupBy("on_id_{$q['_join_cols']} ");
96 $this->orderBy('event_when DESC');
97 $this->whereAdd("{$q['_join_cols']} IS NOT NULL");
98 // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
100 $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
114 * check who is trying to access this. false == access denied..
115 * @return {boolean} true if access is allowed.
117 function checkPerm($lvl, $au)
122 // listing is controleed by applyfilters..
123 return $au->hasPerm("Admin.Admin_Tab", 'S');
127 * return the object that this relates to.
129 * @return {DB_DataObject} related object
133 $o = DB_DataObject::factory($this->on_table);
134 $o->get($this->on_id);
142 * Initialize an event - ready to insert..
144 * @param {String} action - group/name of event
145 * @param {DataObject|false} obj - dataobject action occured on.
146 * @param {String} any remarks
149 function init($act, $obj, $remarks)
151 $ff = HTML_FlexyFramework::get();
153 $au = $pg->getAuthUser();
155 if ($ff->cli && empty($au) && isset($obj->person_id)) {
156 $au = DB_DataObject::Factory('Person'); // not always a person..
157 $au->get($obj->person_id);
162 $this->person_name = $au && !empty($au->name) ? $au->name : '';
163 $this->person_id = $au ? $au->id : '';
164 $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
165 $this->action = $act;
166 $this->on_table = $obj ? $obj->tableName() : '';
167 $pk = $obj ? $obj->keys() : false;
168 $this->on_id = $obj && $pk ? $obj->{$pk[0]}: 0;
170 // should this really go in remarks? -
171 if ($obj && method_exists($obj,'toEventString')) {
172 $rem[] = $obj->toEventString() ;
175 $this->remarks = implode(' : ', $rem);
179 * Generate an audit for this field.
181 * @param {DB_DataObject} new data
182 * @param {DB_DataObject} old data
184 * @return {int} number of entries logged.
187 function audit($new, $old = false)
190 return 0; // they are the same...
194 foreach(array_keys($new->table()) as $k) {
195 // should we JSON serialize this?
196 $n = empty($new->$k) ? '' : $new->$k;
197 $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
201 $this->auditField($k, $o, $n, $old);
207 * Record an audited change, in theory so we can audit data that is not just
210 * @param {string} $name table field anme
211 * @param {mixed} $ov old value
212 * @param {mixed} $onv new value
213 * @param {mixed} $old old object (false if we are creating..)
215 function auditField($name, $ov, $nv, $old=false )
217 $x = DB_DataObject::factory('core_event_audit');
219 'event_id' => $this->id,
221 'old_audit_id' => $old ? $x->findLast($this, $name) : 0,