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             //DB_DataObject::debugLevel(1);
61             $this->selectAdd();
62             $this->selectAdd("
63                     COUNT(on_id) as id,
64                     DATE_FORMAT(event_when, '%Y-%m-%d') as event_when,
65                     on_table,
66                     action,
67                     join_person_id_id.name as person_id_name,
68                     join_person_id_id.email as person_id_email,
69                     '' as remarks
70                 ") ;
71             $this->groupBy('event_when, on_table, action, person_id_name, person_id_email');
72             
73             
74         }
75         
76         
77         
78             
79     }
80     /**
81      * check who is trying to access this. false == access denied..
82      * @return {boolean} true if access is allowed.
83      */
84     function checkPerm($lvl, $au) 
85     {
86         if ($lvl == 'S') {
87             return true;
88         }
89         // listing is controleed by applyfilters..
90         return $au->hasPerm("Admin.Admin_Tab", 'S');
91     }
92     /**
93      * object :
94      * return the object that this relates to.
95      * 
96      * @return {DB_DataObject} related object
97      */
98     function object()
99     {
100         $o = DB_DataObject::factory($this->on_table);
101         $o->get($this->on_id);
102         return $o;
103         
104     }
105     
106     
107     /**
108      * init:
109      * Initialize an event - ready to insert..
110      * 
111      * @param {String} action  - group/name of event
112      * @param {DataObject|false} obj - dataobject action occured on.
113      * @param {String} any remarks 
114      */
115     
116     function init($act, $obj, $remarks)
117     {
118         $ff = HTML_FlexyFramework::get();
119         $pg = $ff->page;
120         $au = $pg->getAuthUser();
121         
122         if ($ff->cli && empty($au) && isset($obj->person_id)) {
123             $au = DB_DataObject::Factory('Person'); // not always a person..
124             $au->get($obj->person_id);
125         } 
126          
127          
128          
129         $this->person_name = $au && !empty($au->name) ? $au->name : '';
130         $this->person_id = $au ? $au->id : '';
131         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
132         $this->action = $act;
133         $this->on_table = $obj ? $obj->tableName() : '';
134         $pk = $obj ? $obj->keys()  : false;
135         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
136         $rem  = array();
137         // should this really go in remarks? - 
138         if ($obj && method_exists($obj,'toEventString')) {
139             $rem[] = $obj->toEventString() ;
140         }
141         $rem[] = $remarks;
142         $this->remarks = implode(' : ', $rem);
143     }
144     
145     /**
146      * Generate an audit for this field.
147      *
148      * @param {DB_DataObject} new data
149      * @param {DB_DataObject} old data
150      * 
151      * @return {int} number of entries logged.
152      */
153     
154     function audit($new, $old = false)
155     {
156         if ($old == $new) {
157             return 0; // they are the same...
158         }
159          
160         $ret = 0;
161         foreach(array_keys($new->table()) as $k) {
162             // should we JSON serialize this?
163             $n = empty($new->$k) ? '' : $new->$k;
164             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
165             if ($n == $o) {
166                 continue;
167             }
168             $this->auditField($k, $o, $n, $old);
169             $ret++;
170         }
171         return $ret;
172     }
173     /**
174      * Record an audited change, in theory so we can audit data that is not just
175      * database Fields...
176      *
177      * @param {string} $name    table field anme
178      * @param {mixed} $ov  old value
179      * @param {mixed} $onv  new value
180      * @param {mixed} $old  old object (false if we are creating..)
181      */
182     function auditField($name, $ov, $nv, $old=false )
183     {
184         $x = DB_DataObject::factory('core_event_audit');
185         $x->setFrom(array(
186             'event_id' => $this->id,
187             'name' => $name,
188             'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
189             'newvalue' => $nv
190
191         ));
192         $x->insert();
193     
194     }
195 }