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