DataObjects/pman.links.ini
[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     }
53     /**
54      * check who is trying to access this. false == access denied..
55      */
56     function checkPerm($lvl, $au) 
57     {
58         return $lvl == 'S' && $au->hasPerm("Admin.Admin_Tab", $lvl);
59     } 
60     /**
61      * init:
62      * Initialize an event - ready to insert..
63      * 
64      * @param {String} action  - group/name of event
65      * @param {DataObject|false} obj - dataobject action occured on.
66      * @param {String} any remarks 
67      */
68     
69     function init($act, $obj, $remarks)
70     {
71         $pg = HTML_FlexyFramework::get()->page;
72         $au = $pg->getAuthUser();
73          
74         $this->person_name = $au && !empty($au->name) ? $au->name : '';
75         $this->person_id = $au ? $au->id : '';
76         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
77         $this->action = $act;
78         $this->on_table = $obj ? $obj->tableName() : '';
79         $pk = $obj ? $obj->keys()  : false;
80         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
81         $rem  = array();
82         // should this really go in remarks? - 
83         if ($obj && method_exists($obj,'toEventString')) {
84             $rem[] = $obj->toEventString() ;
85         }
86         $rem[] = $remarks;
87         $this->remarks = implode(' : ', $rem);
88     }
89     
90     /**
91      * Generate an audit for this field.
92      *
93      * @param {DB_DataObject} new data
94      * @param {DB_DataObject} old data
95      * 
96      * @return {int} number of entries logged.
97      */
98     
99     function audit($new, $old = false)
100     {
101         if ($old == $new) {
102             return 0; // they are the same...
103         }
104          
105         $ret = 0;
106         foreach(array_keys($new->table()) as $k) {
107             // should we JSON serialize this?
108             $n = empty($new->$k) ? '' : $new->$k;
109             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
110             if ($n == $o) {
111                 continue;
112             }
113             $this->auditField($k, $o, $n, $old);
114             $ret++;
115         }
116         return $ret;
117     }
118     /**
119      * Record an audited change, in theory so we can audit data that is not just
120      * database Fields...
121      *
122      * @param {string} $name    table field anme
123      * @param {mixed} $ov  old value
124      * @param {mixed} $onv  new value
125      * @param {mixed} $old  old object (false if we are creating..)
126      */
127     function auditField($name, $ov, $nv, $old=false )
128     {
129         $x = DB_DataObject::factory('core_event_audit');
130         $x->setFrom(array(
131             'event_id' => $this->id,
132             'name' => $name,
133             'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
134             'newvalue' => $nv
135
136         ));
137         $x->insert();
138     
139     }
140 }