DataObjects/Core_event_audit.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      * check who is trying to access this. false == access denied..
28      */
29     function checkPerm($lvl, $au) 
30     {
31         return $lvl == 'S' && $au->hasPerm("Admin.Admin_Tab", $lvl);
32     } 
33     /**
34      * init:
35      * Initialize an event - ready to insert..
36      * 
37      * @param {String} action  - group/name of event
38      * @param {DataObject|false} obj - dataobject action occured on.
39      * @param {String} any remarks 
40      */
41     
42     function init($act, $obj, $remarks)
43     {
44         $pg = HTML_FlexyFramework::get()->page;
45         $au = $pg->getAuthUser();
46          
47         $this->person_name = $au && !empty($au->name) ? $au->name : '';
48         $this->person_id = $au ? $au->id : '';
49         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
50         $this->action = $act;
51         $this->on_table = $obj ? $obj->tableName() : '';
52         $pk = $obj ? $obj->keys()  : false;
53         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
54         $rem  = array();
55         // should this really go in remarks? - 
56         if ($obj && method_exists($obj,'toEventString')) {
57             $rem[] = $obj->toEventString() ;
58         }
59         $rem[] = $remarks;
60         $this->remarks = implode(' : ', $rem);
61     }
62     
63     /**
64      * Generate an audit for this field.
65      *
66      * @param {DB_DataObject} new data
67      * @param {DB_DataObject} old data
68      * 
69      * @return {int} number of entries logged.
70      */
71     
72     function audit($new, $old = false)
73     {
74         if ($old == $new) {
75             return 0; // they are the same...
76         }
77          
78         $ret = 0;
79         foreach(array_keys($new->table()) as $k) {
80             // should we JSON serialize this?
81             $n = empty($new->$k) ? '' : $new->$k;
82             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
83             if ($n == $o) {
84                 continue;
85             }
86             $x = DB_DataObject::factory('core_event_audit');
87             $x->setFrom(array(
88                 'event_id' => $this->id,
89                 'name' => $k,
90                 'old_audit_id' => $old ? $x->findLast($this, $k) : 0,
91                 'newvalue' => $n
92
93             ));
94             $x->insert();
95             $ret++;
96         }
97         return $ret;
98     }
99      
100 }