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)
38 $tn = $this->tableName();
39 // if not empty on_table
40 if(!empty($q['person_table'])){
41 $jt = DB_DataObject::factory($q['person_table']);
43 $this->_join = "LEFT JOIN {$jt->tableName()} AS join_person_id_id ON (join_person_id_id.id=Events.person_id)";
47 $this->selectAs($jt, 'person_id_%s', 'join_person_id_id');
49 if (method_exists($jt,'nameColumn')) {
50 $this->selectAdd("join_person_id_id.{$jt->nameColumn()} as person_id_name");
55 $jt = DB_DataObject::factory('Person');
57 person_table = '{$jt->tableName()}'
60 OR person_table IS NULL"
61 ); // default to our standard.. - unless otherwise requested..
65 if (!empty($q['query']['from'])) {
66 $dt = date('Y-m-d' , strtotime($q['query']['from']));
67 $this->whereAdd(" {$tn}.event_when >= '$dt' ");
69 if (!empty($q['query']['to'])) {
70 $dt = date('Y-m-d' , strtotime($q['query']['to']));
71 $this->whereAdd(" {$tn}.event_when <= '$dt' ");
74 if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
76 DB_DataObject::Debuglevel(1);
77 $this->groupBy('on_id');
79 (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
84 if (!$au->hasPerm("Admin.Admin_Tab", 'S')) {
85 //DB_DataObject::DebugLevel(1);
86 // they can only view their changes..
87 $this->person_id = $au->id;
90 // _join = tablename,tablename...
94 // &_join_cols=cohead_number
95 // &_columns=on_id_cohead_number,event_when << this is ignored at present.
96 // max(event_when) is not supported... by any query yet..
98 if (isset($q['on_table']) && !strlen($q['on_table'])) {
99 // empty ontable queries.. these are valid..
100 $this->whereAdd("$tn.on_table = ''");
105 if (isset($q['query']['person_sum'])) {
106 //DB_DataObject::debugLevel(1);
107 $this->_extra_cols = array('qty' );
108 $this->selectAdd("count($tn.id) as qty");
109 $this->selectAdd("count( distinct $tn.on_id) as uqty");
110 $this->whereAdd('LENGTH(join_person_id_id.name) > 0 ');
111 $this->groupBy('person_id,join_person_id_id.name,join_person_id_id.email');
113 if (isset($q['query']['table_sum'])) {
114 //DB_DataObject::debugLevel(1);
115 $this->_extra_cols = array('qty' , 'uqty');
116 $this->selectAdd("count($tn.id) as qty");
117 $this->selectAdd("count( distinct $tn.on_table, $tn.on_id) as uqty");
119 $this->groupBy('on_table');
121 if (isset($q['query']['day_sum'])) {
122 //DB_DataObject::debugLevel(1);
123 $this->_extra_cols = array('qty' , 'uqty');
124 $this->selectAdd("DATE_FORMAT(event_when, '%Y-%m-%d') as on_day");
125 $this->selectAdd("count($tn.id) as qty");
126 $this->selectAdd("count( distinct $tn.on_id) as uqty");
128 $this->groupBy('on_day');
131 if (isset($q['_join'])) {
132 //DB_DataObject::DebugLevel(1);
133 $joins = explode(',',$q['_join']);
135 $this->selectAdd(); // ???
138 foreach($joins as $t) {
139 $t = preg_replace('/[^a-z_]+/', '', $t); // protection.
140 $x = DB_DataObject::Factory($t);
141 if (!is_a($x,'DB_DataObject')) {
144 $jtn = $x->tableName();
145 $jk = array_shift($x->keys());
148 LEFT JOIN {$jtn} as join_on_id_{$jtn} ON {$tn}.on_id = join_on_id_{$jtn}.{$jk}
149 AND on_table = '{$jtn}'
151 $keys = array_keys($x->table());
152 if (isset($q['_join_cols'])) {
153 $jcs = explode(',',$q['_join_cols'] );
154 //DB_DataObject::DebugLevel(1);
156 foreach($jcs as $jc) {
157 if (! in_array($jc, $keys)) {
163 $this->selectAdd( " join_on_id_{$jtn}.{$jc} as on_id_{$jc} ");
165 $this->selectAdd( " distinct(join_on_id_{$jtn}.{$jc} ) as on_id_{$jc} ");
168 $this->groupBy("on_id_{$jc} ");
169 $this->whereAdd("join_on_id_{$jtn}.{$jc} IS NOT NULL");
171 $this->selectAdd( "MAX(events.event_when) as event_when");
172 $this->orderBy('event_when DESC');
173 // $this->selectAs(array($q['_join_cols']) , 'on_id_%s', "join_on_id_{$jtn}");
175 $this->selectAs($x, 'on_id_%s', "join_on_id_{$jtn}");
182 if (isset($q['_related_on_id']) && isset($q['_related_on_table'])) {
183 // example: sales order - has invoices,
184 ///DB_DataObject::DebugLevel(1);
185 $ev =$this->factory('Events');
187 'on_id' => $q['_related_on_id'],
188 'on_table' => $q['_related_on_table'],
190 $obj = $ev->object();
193 $roo->jerr("ontable is invalid");
195 if (!method_exists($obj,'relatedWhere')) {
196 $roo->jerr( $q['_related_on_table'] . " Does not have method relatedWhere :" .
197 implode(',', get_class_methods($obj)));
199 if ($obj && method_exists($obj,'relatedWhere')) {
200 $ar = $obj->relatedWhere();
201 $tn = $this->tableName();
204 $w[] = "( {$tn}.on_table = '" .
205 $this->escape($q['_related_on_table']) .
206 "' AND {$tn}.on_id = ". ((int) $q['_related_on_id']) .
210 foreach($ar as $k=>$v) {
214 $w[] = "( {$tn}.on_table = '$k' AND {$tn}.on_id IN (". implode(',', $v). "))";
217 $this->whereAdd(implode(' OR ' , $w));
225 // since roo does not support autojoin yet..
226 if (!isset($q['_distinct'])) {
227 //$this->autoJoinExtra();
230 if(!empty($q['query']['action'])) {
231 $act = $this->escape($q['query']['action']);
232 $this->whereAdd("Events.action LIKE '%{$act}%'");
235 if(!empty($q['query']['on_table'])) {
236 $tnb = $this->escape($q['query']['on_table']);
237 $this->whereAdd("Events.on_table LIKE '%{$tnb}%'");
244 * check who is trying to access this. false == access denied..
245 * @return {boolean} true if access is allowed.
247 function checkPerm($lvl, $au)
252 // listing is controleed by applyfilters..
253 return $au->hasPerm("Admin.Admin_Tab", 'S');
257 * return the object that this relates to.
259 * @return {DB_DataObject} related object
263 $o = DB_DataObject::factory($this->on_table);
264 $o->get($this->on_id);
272 * Initialize an event - ready to insert..
274 * @param {String} action - group/name of event
275 * @param {DataObject|false} obj - dataobject action occured on.
276 * @param {String} any remarks
279 function init($act, $obj, $remarks)
281 $ff = HTML_FlexyFramework::get();
283 $au = $pg->getAuthUser();
285 if ($ff->cli) { // && empty($au) && isset($obj->person_id)) {
287 // $au = DB_DataObject::Factory('Person'); // not always a person..
288 // $au->get($obj->person_id);
291 $this->person_name = $au && !empty($au->name) ? $au->name : '';
292 if (isset($au->id) && empty($au->id)) {
293 // not authenticated - and a standard id based object
294 $this->person_id = 0;
296 $this->person_id = $au ? (!empty($au->id) ? $au->id : $au->pid()) : -1;
298 $this->person_table = $au ? $au->tableName() : '';
299 $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
300 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
301 $this->ipaddr = $_SERVER['HTTP_X_FORWARDED_FOR'];
304 $this->action = $act;
305 $this->on_table = $obj ? $obj->tableName() : '';
306 $pk = $obj ? $obj->keys() : false;
307 $this->on_id = $obj && $pk ? $obj->{$pk[0]}: 0;
309 // should this really go in remarks? -
310 if ($obj && method_exists($obj,'toEventString')) {
311 $rem[] = $obj->toEventString() ;
314 $this->remarks = implode(' : ', $rem);
318 * Generate an audit for this field.
320 * @param {DB_DataObject} new data
321 * @param {DB_DataObject} old data
323 * @return {int} number of entries logged.
326 function audit($new, $old = false)
329 return 0; // they are the same...
333 foreach(array_keys($new->table()) as $k) {
334 // should we JSON serialize this?
335 $n = empty($new->$k) ? '' : $new->$k;
336 $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
340 $this->auditField($k, $o, $n, $old);
346 * Record an audited change, in theory so we can audit data that is not just
349 * @param {string} $name table field anme
350 * @param {mixed} $ov old value
351 * @param {mixed} $onv new value
352 * @param {mixed} $old old object (false if we are creating..)
354 function auditField($name, $ov, $nv, $old=false )
357 if (is_object($nv)) {
362 $x = DB_DataObject::factory('core_event_audit');
364 'event_id' => $this->id,
366 'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
376 function onInsert($request,$roo)
378 $this->writeEventLog();
381 function writeEventLog()
383 $ff = HTML_FlexyFramework::get();
384 if (empty($ff->Pman['event_log_dir'])) {
388 // add user (eg. www-data or local user if not..)
389 if (function_exists('posix_getpwuid')) {
390 $uinfo = posix_getpwuid( posix_getuid () );
392 $user = $uinfo['name'];
394 $user = getenv('USERNAME'); // windows.
397 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/'). $this->id . ".json";
398 if (!file_exists(dirname($file))) {
399 mkdir(dirname($file),0700,true);
402 // Remove all the password from logs...
403 $p = empty($_POST) ? array() : $_POST;
404 foreach(array('passwd', 'password','passwd1', 'passwd2','password1', 'password2') as $rm) {
405 if (isset($p[$rm])) {
415 foreach ($_FILES as $k=>$f){
416 // does not handle any other file[] arrary very well..
417 if (empty($f['tmp_name']) || !file_exists($f['tmp_name'])) {
424 $files[$k]['tmp_name'] = $this->id . '-'. $i;
425 $nf = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/'). $files[$k]['tmp_name'];
426 if (!copy($f['tmp_name'], $nf)) {
427 print_r("failed to copy {$f['tmp_name']}...\n");
431 file_put_contents($file, json_encode(array(
432 'REQUEST_URI' => empty($_SERVER['REQUEST_URI']) ? 'cli' : $_SERVER['REQUEST_URI'],
433 'HTTP_USER_AGENT' => empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'],
434 'GET' => empty($_GET) ? array() : $_GET,