3 * Table Definition for Events
5 * objects can implement relatedWhere(), which should return
6 * 'tablename' => array of ids
10 require_once 'DB/DataObject.php';
12 class Pman_Core_DataObjects_Events extends DB_DataObject
15 /* the code below is auto generated do not remove the above tag */
17 public $__table = 'Events'; // table name
18 public $id; // int(11) not_null primary_key auto_increment
19 public $person_name; // string(128)
20 public $event_when; // datetime(19) binary
21 public $action; // string(32)
22 public $ipaddr; // string(16)
23 public $on_id; // int(11)
24 public $on_table; // string(64)
25 public $person_id; // int(11)
26 public $remarks; // blob(65535) blob
29 /* the code above is auto generated do not remove the tag below */
35 // ------------ROO HOOKS------------------------------------
36 function applyFilters($q, $au)
38 $tn = $this->tableName();
39 if (!empty($q['query']['from'])) {
40 $dt = date('Y-m-d' , strtotime($q['query']['from']));
41 $this->whereAdd(" {$tn}.event_when >= '$dt' ");
43 if (!empty($q['query']['to'])) {
44 $dt = date('Y-m-d' , strtotime($q['query']['to']));
45 $this->whereAdd(" {$tn}.event_when <= '$dt' ");
48 if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
50 DB_DataObject::Debuglevel(1);
51 $this->groupBy('on_id');
53 (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
58 if (!$au->hasPerm("Admin.Admin_Tab", 'S')) {
59 //DB_DataObject::DebugLevel(1);
60 // they can only view their changes..
61 $this->person_id = $au->id;
64 // _join = tablename,tablename...
68 // &_join_cols=cohead_number
69 // &_columns=on_id_cohead_number,event_when << this is ignored at present.
70 // max(event_when) is not supported... by any query yet..
72 if (isset($q['query']['person_sum'])) {
73 //DB_DataObject::debugLevel(1);
74 $this->_extra_cols = array('qty' );
75 $this->selectAdd("count($tn.id) as qty");
76 $this->whereAdd('LENGTH(person_id_name) > 0 ');
77 $this->groupBy('person_id');
79 if (isset($q['query']['table_sum'])) {
80 //DB_DataObject::debugLevel(1);
81 $this->_extra_cols = array('qty' , 'uqty');
82 $this->selectAdd("count($tn.id) as qty");
83 $this->selectAdd("count( distinct $tn.on_id) as uqty");
85 $this->groupBy('on_table');
89 if (isset($q['_join'])) {
90 //DB_DataObject::DebugLevel(1);
91 $joins = explode(',',$q['_join']);
93 $this->selectAdd(); // ???
96 foreach($joins as $t) {
97 $t = preg_replace('/[^a-z_]+/', '', $t); // protection.
98 $x = DB_DataObject::Factory($t);
99 if (!is_a($x,'DB_DataObject')) {
102 $jtn = $x->tableName();
103 $jk = array_shift($x->keys());
106 LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
107 AND on_table = '{$jtn}'
109 $keys = array_keys($x->table());
110 if (isset($q['_join_cols'])) {
111 $jcs = explode(',',$q['_join_cols'] );
112 //DB_DataObject::DebugLevel(1);
114 foreach($jcs as $jc) {
115 if (! in_array($jc, $keys)) {
121 $this->selectAdd( " join_on_id_{$jtn}.{$jc} as on_id_{$jc} ");
123 $this->selectAdd( " distinct(join_on_id_{$jtn}.{$jc} ) as on_id_{$jc} ");
126 $this->groupBy("on_id_{$jc} ");
127 $this->whereAdd("join_on_id_{$jtn}.{$jc} IS NOT NULL");
129 $this->selectAdd( "MAX(events.event_when) as event_when");
130 $this->orderBy('event_when DESC');
131 // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
133 $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
142 if (isset($q['_related_on_id']) && isset($q['_related_on_table'])) {
143 // example: sales order - has invoices,
144 $ev =$this->factory('Events');
146 'on_id' => $q['_related_on_id'],
147 'on_table' => $q['_related_on_table'],
149 $obj = $ev->object();
151 if ($obj && method_exists($obj,'relatedWhere')) {
152 $ar = $obj->relatedWhere();
153 $tn = $this->tableName();
156 $w[] = "( {$tn}.on_table = '" .
157 $this->escape($q['_related_on_table']) .
158 "' AND {$tn}.on_id = ". ((int) $q['_related_on_id']) .
162 foreach($ar as $k=>$v) {
166 $w[] = "( {$tn}.on_table = '$k' AND {$tn}.on_id IN (". implode(',', $v). "))";
169 $this->whereAdd(implode(' OR ' , $w));
185 * check who is trying to access this. false == access denied..
186 * @return {boolean} true if access is allowed.
188 function checkPerm($lvl, $au)
193 // listing is controleed by applyfilters..
194 return $au->hasPerm("Admin.Admin_Tab", 'S');
198 * return the object that this relates to.
200 * @return {DB_DataObject} related object
204 $o = DB_DataObject::factory($this->on_table);
205 $o->get($this->on_id);
213 * Initialize an event - ready to insert..
215 * @param {String} action - group/name of event
216 * @param {DataObject|false} obj - dataobject action occured on.
217 * @param {String} any remarks
220 function init($act, $obj, $remarks)
222 $ff = HTML_FlexyFramework::get();
224 $au = $pg->getAuthUser();
226 if ($ff->cli && empty($au) && isset($obj->person_id)) {
227 $au = DB_DataObject::Factory('Person'); // not always a person..
228 $au->get($obj->person_id);
233 $this->person_name = $au && !empty($au->name) ? $au->name : '';
234 $this->person_id = $au ? $au->id : '';
235 $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
236 $this->action = $act;
237 $this->on_table = $obj ? $obj->tableName() : '';
238 $pk = $obj ? $obj->keys() : false;
239 $this->on_id = $obj && $pk ? $obj->{$pk[0]}: 0;
241 // should this really go in remarks? -
242 if ($obj && method_exists($obj,'toEventString')) {
243 $rem[] = $obj->toEventString() ;
246 $this->remarks = implode(' : ', $rem);
250 * Generate an audit for this field.
252 * @param {DB_DataObject} new data
253 * @param {DB_DataObject} old data
255 * @return {int} number of entries logged.
258 function audit($new, $old = false)
261 return 0; // they are the same...
265 foreach(array_keys($new->table()) as $k) {
266 // should we JSON serialize this?
267 $n = empty($new->$k) ? '' : $new->$k;
268 $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
272 $this->auditField($k, $o, $n, $old);
278 * Record an audited change, in theory so we can audit data that is not just
281 * @param {string} $name table field anme
282 * @param {mixed} $ov old value
283 * @param {mixed} $onv new value
284 * @param {mixed} $old old object (false if we are creating..)
286 function auditField($name, $ov, $nv, $old=false )
288 $x = DB_DataObject::factory('core_event_audit');
290 'event_id' => $this->id,
292 'old_audit_id' => $old ? $x->findLast($this, $name) : 0,