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
27 public $person_table; // string(64)
29 /* the code above is auto generated do not remove the tag below */
35 // ------------ROO HOOKS------------------------------------
36 function applyFilters($q, $au ,$roo)
39 $tn = $this->tableName();
41 // if not empty on_table
42 if(!empty($q['person_table'])){
43 $jt = DB_DataObject::factory($q['person_table']);
45 $this->_join = "LEFT JOIN {$jt->tableName()} AS join_person_id_id ON (join_person_id_id.id=Events.person_id)";
49 $this->selectAs($jt, 'person_id_%s', 'join_person_id_id');
54 if (!empty($q['query']['from'])) {
55 $dt = date('Y-m-d' , strtotime($q['query']['from']));
56 $this->whereAdd(" {$tn}.event_when >= '$dt' ");
58 if (!empty($q['query']['to'])) {
59 $dt = date('Y-m-d' , strtotime($q['query']['to']));
60 $this->whereAdd(" {$tn}.event_when <= '$dt' ");
63 if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
65 DB_DataObject::Debuglevel(1);
66 $this->groupBy('on_id');
68 (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
73 if (!$au->hasPerm("Admin.Admin_Tab", 'S')) {
74 //DB_DataObject::DebugLevel(1);
75 // they can only view their changes..
76 $this->person_id = $au->id;
79 // _join = tablename,tablename...
83 // &_join_cols=cohead_number
84 // &_columns=on_id_cohead_number,event_when << this is ignored at present.
85 // max(event_when) is not supported... by any query yet..
87 if (isset($q['on_table']) && !strlen($q['on_table'])) {
88 // empty ontable queries.. these are valid..
89 $this->whereAdd("$tn.on_table = ''");
94 if (isset($q['query']['person_sum'])) {
95 //DB_DataObject::debugLevel(1);
96 $this->_extra_cols = array('qty' );
97 $this->selectAdd("count($tn.id) as qty");
98 $this->selectAdd("count( distinct $tn.on_id) as uqty");
99 $this->whereAdd('LENGTH(join_person_id_id.name) > 0 ');
100 $this->groupBy('person_id,join_person_id_id.name,join_person_id_id.email');
102 if (isset($q['query']['table_sum'])) {
103 //DB_DataObject::debugLevel(1);
104 $this->_extra_cols = array('qty' , 'uqty');
105 $this->selectAdd("count($tn.id) as qty");
106 $this->selectAdd("count( distinct $tn.on_table, $tn.on_id) as uqty");
108 $this->groupBy('on_table');
110 if (isset($q['query']['day_sum'])) {
111 //DB_DataObject::debugLevel(1);
112 $this->_extra_cols = array('qty' , 'uqty');
113 $this->selectAdd("DATE_FORMAT(event_when, '%Y-%m-%d') as on_day");
114 $this->selectAdd("count($tn.id) as qty");
115 $this->selectAdd("count( distinct $tn.on_id) as uqty");
117 $this->groupBy('on_day');
120 if (isset($q['_join'])) {
121 //DB_DataObject::DebugLevel(1);
122 $joins = explode(',',$q['_join']);
124 $this->selectAdd(); // ???
127 foreach($joins as $t) {
128 $t = preg_replace('/[^a-z_]+/', '', $t); // protection.
129 $x = DB_DataObject::Factory($t);
130 if (!is_a($x,'DB_DataObject')) {
133 $jtn = $x->tableName();
134 $jk = array_shift($x->keys());
137 LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
138 AND on_table = '{$jtn}'
140 $keys = array_keys($x->table());
141 if (isset($q['_join_cols'])) {
142 $jcs = explode(',',$q['_join_cols'] );
143 //DB_DataObject::DebugLevel(1);
145 foreach($jcs as $jc) {
146 if (! in_array($jc, $keys)) {
152 $this->selectAdd( " join_on_id_{$jtn}.{$jc} as on_id_{$jc} ");
154 $this->selectAdd( " distinct(join_on_id_{$jtn}.{$jc} ) as on_id_{$jc} ");
157 $this->groupBy("on_id_{$jc} ");
158 $this->whereAdd("join_on_id_{$jtn}.{$jc} IS NOT NULL");
160 $this->selectAdd( "MAX(events.event_when) as event_when");
161 $this->orderBy('event_when DESC');
162 // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
164 $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
173 if (isset($q['_related_on_id']) && isset($q['_related_on_table'])) {
174 // example: sales order - has invoices,
175 ///DB_DataObject::DebugLevel(1);
176 $ev =$this->factory('Events');
178 'on_id' => $q['_related_on_id'],
179 'on_table' => $q['_related_on_table'],
181 $obj = $ev->object();
184 $roo->jerr("ontable is invalid");
186 if (!method_exists($obj,'relatedWhere')) {
187 $roo->jerr( $q['_related_on_table'] . " Does not have method relatedWhere :" .
188 implode(',', get_class_methods($obj)));
190 if ($obj && method_exists($obj,'relatedWhere')) {
191 $ar = $obj->relatedWhere();
192 $tn = $this->tableName();
195 $w[] = "( {$tn}.on_table = '" .
196 $this->escape($q['_related_on_table']) .
197 "' AND {$tn}.on_id = ". ((int) $q['_related_on_id']) .
201 foreach($ar as $k=>$v) {
205 $w[] = "( {$tn}.on_table = '$k' AND {$tn}.on_id IN (". implode(',', $v). "))";
208 $this->whereAdd(implode(' OR ' , $w));
216 // since roo does not support autojoin yet..
217 if (!isset($q['_distinct'])) {
218 //$this->autoJoinExtra();
229 * check who is trying to access this. false == access denied..
230 * @return {boolean} true if access is allowed.
232 function checkPerm($lvl, $au)
237 // listing is controleed by applyfilters..
238 return $au->hasPerm("Admin.Admin_Tab", 'S');
242 * return the object that this relates to.
244 * @return {DB_DataObject} related object
248 $o = DB_DataObject::factory($this->on_table);
249 $o->get($this->on_id);
257 * Initialize an event - ready to insert..
259 * @param {String} action - group/name of event
260 * @param {DataObject|false} obj - dataobject action occured on.
261 * @param {String} any remarks
264 function init($act, $obj, $remarks)
266 $ff = HTML_FlexyFramework::get();
268 $au = $pg->getAuthUser();
270 if ($ff->cli) { // && empty($au) && isset($obj->person_id)) {
272 // $au = DB_DataObject::Factory('Person'); // not always a person..
273 // $au->get($obj->person_id);
276 $this->person_name = $au && !empty($au->name) ? $au->name : '';
277 $this->person_id = $au ? $au->id : -1;
278 $this->person_table = $au ? $au->tableName() : '';
279 $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
280 $this->action = $act;
281 $this->on_table = $obj ? $obj->tableName() : '';
282 $pk = $obj ? $obj->keys() : false;
283 $this->on_id = $obj && $pk ? $obj->{$pk[0]}: 0;
285 // should this really go in remarks? -
286 if ($obj && method_exists($obj,'toEventString')) {
287 $rem[] = $obj->toEventString() ;
290 $this->remarks = implode(' : ', $rem);
294 * Generate an audit for this field.
296 * @param {DB_DataObject} new data
297 * @param {DB_DataObject} old data
299 * @return {int} number of entries logged.
302 function audit($new, $old = false)
305 return 0; // they are the same...
309 foreach(array_keys($new->table()) as $k) {
310 // should we JSON serialize this?
311 $n = empty($new->$k) ? '' : $new->$k;
312 $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
316 $this->auditField($k, $o, $n, $old);
322 * Record an audited change, in theory so we can audit data that is not just
325 * @param {string} $name table field anme
326 * @param {mixed} $ov old value
327 * @param {mixed} $onv new value
328 * @param {mixed} $old old object (false if we are creating..)
330 function auditField($name, $ov, $nv, $old=false )
333 if (is_object($nv)) {
338 $x = DB_DataObject::factory('core_event_audit');
340 'event_id' => $this->id,
342 'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
352 function onInsert($request,$roo)
354 $this->writeEventLog();
357 function writeEventLog()
359 $ff = HTML_FlexyFramework::get();
360 if (empty($ff->Pman['event_log_dir'])) {
364 // add user (eg. www-data or local user if not..)
365 if (function_exists('posix_getpwuid')) {
366 $uinfo = posix_getpwuid( posix_getuid () );
368 $user = $uinfo['name'];
370 $user = getenv('USERNAME'); // windows.
373 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/'). $this->id . ".json";
374 if (!file_exists(dirname($file))) {
375 mkdir(dirname($file),0700,true);
378 // Remove all the password from logs...
379 $p = empty($_POST) ? array() : $_POST;
380 foreach(array('passwd', 'password', 'passwd2', 'password2') as $rm) {
381 if (isset($p[$rm])) {
382 $p['passwd'] = '******';
387 foreach ($_FILES as $k=>$f){
388 if (empty($f['tmp_name']) || !file_exists($f['tmp_name'])) {
393 $files[$k]['tmp_name'] = $this->id . '.file_'. $i.'.jpg';
394 $nf = $ff->Pman['event_log_dir']. '/'. $this->id . ".file_$i.jpg";
395 if (!copy($f['tmp_name'], $nf)) {
396 print_r("failed to copy {$f['tmp_name']}...\n");
400 file_put_contents($file, json_encode(array(
401 'REQUEST_URI' => empty($_SERVER['REQUEST_URI']) ? 'cli' : $_SERVER['REQUEST_URI'],
402 'GET' => empty($_GET) ? array() : $_GET,