DataObjects/Events.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     
28     
29     
30     //  ------------ROO HOOKS------------------------------------
31     function applyFilters($q, $au)
32     {
33         if (!empty($q['query']['from'])) {
34             $dt = date('Y-m-d' , strtotime($q['query']['from']));
35             $this->whereAdd(" Events.event_when >=  '$dt' ");
36         }
37         if (!empty($q['query']['to'])) {
38             $dt = date('Y-m-d' , strtotime($q['query']['to']));
39             $this->whereAdd(" Events.event_when <=  '$dt' ");
40         }
41         /*
42         if (!empty($q['query']['grouped']) && $q['query']['grouped'] == 'gr') {
43             // grouped..
44             DB_DataObject::Debuglevel(1);
45             $this->groupBy('on_id');
46             $this->selectAdd('
47                 (SELECT count(id) FROM core_event_audit WHERE event_id = Events.id) as changed
48                 ');
49         }
50         */
51         
52         if (!$au->hasPerm("Admin.Admin_Tab", 'S')) {
53             //DB_DataObject::DebugLevel(1);
54             // they can only view their changes..
55             $this->person_id = $au->id;
56             
57         }
58         
59         if (!empty($q['query']['viewtype']) && $q['query']['viewtype'] == 'summary') {
60             $this->selectAdd('COUNT(on_id) as no_changed');
61             $this->groupBy('on_table, action');
62             
63             
64         }
65         
66         
67         
68             
69     }
70     /**
71      * check who is trying to access this. false == access denied..
72      * @return {boolean} true if access is allowed.
73      */
74     function checkPerm($lvl, $au) 
75     {
76         if ($lvl == 'S') {
77             return true;
78         }
79         // listing is controleed by applyfilters..
80         return $au->hasPerm("Admin.Admin_Tab", 'S');
81     }
82     /**
83      * object :
84      * return the object that this relates to.
85      * 
86      * @return {DB_DataObject} related object
87      */
88     function object()
89     {
90         $o = DB_DataObject::factory($this->on_table);
91         $o->get($this->on_id);
92         return $o;
93         
94     }
95     
96     
97     /**
98      * init:
99      * Initialize an event - ready to insert..
100      * 
101      * @param {String} action  - group/name of event
102      * @param {DataObject|false} obj - dataobject action occured on.
103      * @param {String} any remarks 
104      */
105     
106     function init($act, $obj, $remarks)
107     {
108         $ff = HTML_FlexyFramework::get();
109         $pg = $ff->page;
110         $au = $pg->getAuthUser();
111         
112         if ($ff->cli && empty($au) && isset($obj->person_id)) {
113             $au = DB_DataObject::Factory('Person'); // not always a person..
114             $au->get($obj->person_id);
115         } 
116          
117          
118          
119         $this->person_name = $au && !empty($au->name) ? $au->name : '';
120         $this->person_id = $au ? $au->id : '';
121         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
122         $this->action = $act;
123         $this->on_table = $obj ? $obj->tableName() : '';
124         $pk = $obj ? $obj->keys()  : false;
125         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
126         $rem  = array();
127         // should this really go in remarks? - 
128         if ($obj && method_exists($obj,'toEventString')) {
129             $rem[] = $obj->toEventString() ;
130         }
131         $rem[] = $remarks;
132         $this->remarks = implode(' : ', $rem);
133     }
134     
135     /**
136      * Generate an audit for this field.
137      *
138      * @param {DB_DataObject} new data
139      * @param {DB_DataObject} old data
140      * 
141      * @return {int} number of entries logged.
142      */
143     
144     function audit($new, $old = false)
145     {
146         if ($old == $new) {
147             return 0; // they are the same...
148         }
149          
150         $ret = 0;
151         foreach(array_keys($new->table()) as $k) {
152             // should we JSON serialize this?
153             $n = empty($new->$k) ? '' : $new->$k;
154             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
155             if ($n == $o) {
156                 continue;
157             }
158             $this->auditField($k, $o, $n, $old);
159             $ret++;
160         }
161         return $ret;
162     }
163     /**
164      * Record an audited change, in theory so we can audit data that is not just
165      * database Fields...
166      *
167      * @param {string} $name    table field anme
168      * @param {mixed} $ov  old value
169      * @param {mixed} $onv  new value
170      * @param {mixed} $old  old object (false if we are creating..)
171      */
172     function auditField($name, $ov, $nv, $old=false )
173     {
174         $x = DB_DataObject::factory('core_event_audit');
175         $x->setFrom(array(
176             'event_id' => $this->id,
177             'name' => $name,
178             'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
179             'newvalue' => $nv
180
181         ));
182         $x->insert();
183     
184     }
185 }