DataObjects/Core_event_audit.php
[Pman.Core] / DataObjects / Core_event_audit.php
1 <?php
2 /**
3  * Table Definition for mtrack_change_audit
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_event_audit extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_event_audit';             // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $event_id;                       // int(11)  not_null multiple_key
15     public $name;                       // string(128)  
16     public $old_audit_id;                        // int(11)  blob
17     public $newvalue;                           // blob(65535)  blob
18
19     
20     /* the code above is auto generated do not remove the tag below */
21     ###END_AUTOCODE
22     
23     /**
24      * get the value, 
25      *
26      */
27     
28     function value($event)
29     {
30         $x = DB_DataObject::factory($event->on_table);
31         $ar = $x->links();
32         // is the name a link..
33         if (!isset($ar[$this->name])) {
34             return $this->value;
35         }
36         if (empty($this->value) ) {
37             return '';
38         }
39         // lr = ProjecT:id
40         // get the current value of that...
41         $lr = explode(':', $ar[$this->name]);
42         $x = DB_DataObject::factory($lr[0]);
43         if (!method_exists($x, 'toEventString')) {
44             return $lr[0] .':'. $this->value;
45         }
46         $x->get($this->value);
47         
48         return $x->toEventString(); // big assumption..        
49     }
50     function oldvalue($cg)
51     {
52         //var_dump($cg->ontable);
53         $x = DB_DataObject::factory($cg->ontable);
54         
55         $ar = $x->links();
56         if ( !isset($ar[$this->field()])) {
57             return $this->oldvalue;
58         }
59         // lr = ProjecT:id
60         if (empty($this->oldvalue)) {
61             return '';
62         }
63         $lr = explode(':', $ar[$this->field()]);
64         $x = DB_DataObject::factory($lr[0]);
65         $x->get($this->oldvalue);
66         return isset($x->name) ? $x->name : $this->oldvalue; 
67         
68     }
69     
70     function toAuditString($change)
71     {
72         $field = $this->field();
73         switch($field) {
74             case 'id':
75             case 'created_id':
76             
77                 return false; //??? ignore?
78             case '@components':
79                 return false;
80             //  $old = array();
81             //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
82              /*   if (!strlen($id)) continue;
83                 $c = get_component($id);
84                 $old[$id] = $c->name;
85               }
86               $value = $T->getComponents();
87               $field = 'Component';
88               break;
89              */
90             case '@milestones':
91                 return false;
92             //  $old = array();
93             //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
94               /*  if (!strlen($id)) continue;
95                 $m = get_milestone($id);
96                 $old[$id] = $m->name;
97               }
98               $value = array();
99               $value = $T->getMilestones();
100               $field = 'Milestone';
101               break;
102               */
103             case '@keywords':
104                 return false;
105             
106             default:
107                 $oldvalue = $this->oldvalue($change);
108                 $value = $this->value($change);
109                 
110                 $field = preg_replace('/_id$/', '', $this->field());
111                 $field = ucfirst(str_replace('_', ' ', $field));
112                 
113                 if ( ($oldvalue == $value)  ||
114                     (!strlen($oldvalue) && !strlen($value))) {
115                     return false;
116                 }
117                 $lb = strpos($oldvalue,"\n") > -1 || strpos($value,"\n") > -1 ? "\n\n" : '';
118                 $lbs = $lb == '' ? '' : "\n\n---\n\n";
119                 if (!strlen($oldvalue)) {
120                     return " * Set {$field} to: {$lbs}{$value}{$lbs}";
121                 }
122                        
123                 if (!strlen($value)) {
124                     return  " * Removed {$lb}{$field} - was: {$lbs}{$oldvalue}";
125                     
126                 }
127                 
128                 return " * Changed {$field} from : {$lbs}{$oldvalue} {$lbs} to {$lbs}{$value}{$lbs}";
129                 
130         }
131     }
132     
133     function toJSONArray($change)
134     {
135         $ret=  $this->toArray();
136         // now add the value strings..
137         
138         $field = preg_replace('/_id$/', '', $this->field());
139         $field = ucfirst(str_replace('_', ' ', $field));
140                 
141         $ret['field_str'] = $field;
142
143         $ret['oldvalue_str'] = $this->oldvalue($change);
144         $ret['value_str'] = $this->value($change);
145         return $ret;
146         
147     }
148     
149     
150     
151 }