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