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         
51         
52     }
53     function oldvalue($cg)
54     {
55         //var_dump($cg->ontable);
56         $x = DB_DataObject::factory($cg->ontable);
57         
58         $ar = $x->links();
59         if ( !isset($ar[$this->field()])) {
60             return $this->oldvalue;
61         }
62         // lr = ProjecT:id
63         if (empty($this->oldvalue)) {
64             return '';
65         }
66         $lr = explode(':', $ar[$this->field()]);
67         $x = DB_DataObject::factory($lr[0]);
68         $x->get($this->oldvalue);
69         return isset($x->name) ? $x->name : $this->oldvalue; 
70         
71     }
72     
73     function toAuditString($change)
74     {
75         $field = $this->field();
76         switch($field) {
77             case 'id':
78             case 'created_id':
79             
80                 return false; //??? ignore?
81             case '@components':
82                 return false;
83             //  $old = array();
84             //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
85              /*   if (!strlen($id)) continue;
86                 $c = get_component($id);
87                 $old[$id] = $c->name;
88               }
89               $value = $T->getComponents();
90               $field = 'Component';
91               break;
92              */
93             case '@milestones':
94                 return false;
95             //  $old = array();
96             //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
97               /*  if (!strlen($id)) continue;
98                 $m = get_milestone($id);
99                 $old[$id] = $m->name;
100               }
101               $value = array();
102               $value = $T->getMilestones();
103               $field = 'Milestone';
104               break;
105               */
106             case '@keywords':
107                 return false;
108             
109             default:
110                 $oldvalue = $this->oldvalue($change);
111                 $value = $this->value($change);
112                 
113                 $field = preg_replace('/_id$/', '', $this->field());
114                 $field = ucfirst(str_replace('_', ' ', $field));
115                 
116                 if ( ($oldvalue == $value)  ||
117                     (!strlen($oldvalue) && !strlen($value))) {
118                     return false;
119                 }
120                 $lb = strpos($oldvalue,"\n") > -1 || strpos($value,"\n") > -1 ? "\n\n" : '';
121                 $lbs = $lb == '' ? '' : "\n\n---\n\n";
122                 if (!strlen($oldvalue)) {
123                     return " * Set {$field} to: {$lbs}{$value}{$lbs}";
124                 }
125                        
126                 if (!strlen($value)) {
127                     return  " * Removed {$lb}{$field} - was: {$lbs}{$oldvalue}";
128                     
129                 }
130                 
131                 return " * Changed {$field} from : {$lbs}{$oldvalue} {$lbs} to {$lbs}{$value}{$lbs}";
132                 
133         }
134     }
135     
136     function toJSONArray($change)
137     {
138         $ret=  $this->toArray();
139         // now add the value strings..
140         
141         $field = preg_replace('/_id$/', '', $this->field());
142         $field = ucfirst(str_replace('_', ' ', $field));
143                 
144         $ret['field_str'] = $field;
145
146         $ret['oldvalue_str'] = $this->oldvalue($change);
147         $ret['value_str'] = $this->value($change);
148         return $ret;
149         
150     }
151     
152     
153     
154 }