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