DataObjects/Core_watch.php
[Pman.Core] / DataObjects / Core_watch.php
1 <?php
2 /**
3  * Table Definition for core_watch
4  *
5  * works with 'core_notify'
6  *
7  * any object can call
8  *   $watch->notify($ontable, $onid)
9  *
10  *   in which case it should create a core_notify event.
11  *
12  *
13  * Should 'event' trigger this..
14  *   -> eg. somebody makes a 'EDIT' on 'person'
15  *   -> a watch exists for
16  *        ontable=person,
17  *        onid = -1 <<-- every entry..
18  *        person_id -> who is goes to.
19  *        event = CRUD (eg. shortcut for edit/create/delete)
20  *        medium = "REVIEW" << eg. review needed..
21  *        
22  *
23  *  if onid == 0 then it will monitor all changes to that table..
24  *
25  *
26  * 
27  * 
28  */
29 require_once 'DB/DataObject.php';
30
31 class Pman_Core_DataObjects_Core_watch extends DB_DataObject 
32 {
33     ###START_AUTOCODE
34     /* the code below is auto generated do not remove the above tag */
35
36     public $__table = 'core_watch';                      // table name
37     public $id;                              // int(11)  not_null primary_key auto_increment
38     public $ontable;                         // string(128)  not_null
39     public $onid;                            // int(11)  not_null
40     public $person_id;                       // int(11)  not_null
41     public $event;                           // string(128)  not_null
42     public $medium;                          // string(128)  not_null
43     public $active;                          // int(11)  not_null
44
45     
46     /* the code above is auto generated do not remove the tag below */
47     ###END_AUTOCODE
48     /** make sure there is a watch for this user.. */
49     
50     
51     function applyFilters($q,$au, $roo)
52     {
53         if (!empty($q['_list_actions'])) {
54             $this->listActions($roo,$q);
55         }
56         //die("apply filters");
57         if (!empty($q['_split_event_name'])) {
58             $this->selectAdd("
59                 
60                 substr( event, 1, LOCATE(event, '.')) as event_left,
61                 substr( event,   LOCATE(event, '.')) as event_right        
62                              
63             ");
64             
65             
66             
67         }
68         
69     }
70     
71     function toRooSingleArray($au,$q)
72     {
73         $ret = $this->toArray();
74         if (empty($q['_split_event_name'])) {
75             return $ret;
76         }
77         $bits = explode('.', $this->event);
78         $ret['event_left'] = $bits[0];
79         $ret['event_right'] = $bits[1];
80         // check core enu.
81         if (!empty($ret['event_right'])) {
82             $ce = DB_DataObject::factory('core_enum')->lookupObject($q['_split_event_name'], $ret['event_right']);
83             $ret['event_right_display_name'] = $ce->display_name;
84         }
85         
86         return $ret;
87         
88         
89     }
90     
91     function listActions($roo, $q) {
92         
93         //print_r($q);
94         $d = DB_DataObject::Factory($q['on_table']);
95         $ret = array();
96         
97         foreach(get_class_methods($d) as $m) {
98             //var_dump($m);
99             if (!preg_match('/^notify/', $m)) {
100                 continue;
101             }
102             $ret[] = array(
103                 'display_name' => preg_replace('/^notify/', '' , $m),
104                 'name' => $q['on_table'] .':'. $m
105             );
106         }
107         $roo->jdata($ret);
108     }
109     
110     /**
111      *
112      * Create a watch...
113      *
114      */
115     
116     
117     
118     
119     function ensureNotify(  $ontable, $onid, $person_id, $whereAdd)
120     {
121         //DB_DAtaObject::debugLevel(1);
122         $w = DB_DataObject::factory('core_watch');
123         $w->person_id = $person_id;
124         if (empty($w->person_id)) {
125             return;
126         }
127         
128         $nw = clone($w);
129         $w->whereAdd($whereAdd);
130         
131         
132         if ($w->count()) {
133             return;
134         }
135         $nw->ontable = $ontable;
136         $nw->onid = $onid;
137         
138         $nw->medium = 'email';
139         $nw->active = 1;
140         $nw->insert();
141          
142     }
143     /**
144      * Generate a notify event based on watches (matching whereAdd)
145      *
146      * Usage: $core_watch->notify('mtrack_repos', 1, false, date()
147      *
148      * This can match any 'event' type - eg. it can be blank etc...
149      *   Generally used by non-event driven notifications, like our
150      *   Daily commit message.
151      *
152      *  @param string $ontable - the database table that has been updated/changed etc.
153      *  @param int    $onid    - the id of the row changed
154      *  @param string  $whereAdd (optiona) - a DB whereAdd() condition to filter the search for watches
155      *  @param datetime    $when   (default now)  - date/time to create the notification for (Eg. end of day..)
156      * 
157      */
158     function notify($ontable , $onid, $whereAdd = false, $when=false)
159     {
160         $w = DB_DataObject::factory('core_watch');   
161         if ($whereAdd !== false) { 
162             $w->whereAdd($whereAdd  );
163         }
164         $w->active =1;
165         
166         $w->whereAdd('onid = 0 OR onid='. ((int) $onid));
167        
168         
169         $w->ontable = $ontable;
170         //$w->selectAdd();
171         //$w->selectAdd('distinct(person_id) as person_id');
172         
173         foreach($w->fetchAll() as $w) { 
174             if (!$w->person_id) { // no people??? bugs in watch table
175                 continue;
176             }
177          
178          
179          
180             $nn = DB_DataObject::Factory('core_notify');
181             $nn->ontable = $ontable;
182             $nn->onid = $onid;
183             $nn->evtype = $w->medium;
184             $nn->person_id = $w->person_id;
185             
186             $nf = clone($nn);
187             $nf->whereAdd("sent < '2000-01-01'");
188             if ($nf->count()) {
189                 // we have a item in the queue for that waiting to be sent..
190                 continue;
191             }
192             $nn->act_start( date("Y-m-d H:i:s", $when !== false ? strtotime($when) : time()) );
193             $nn->insert();
194         }
195           
196     }
197     // static really...
198     /**
199      *
200      * This get's called by roo->addEvent()
201      *
202      * And searches for matching '$watch->event' == $event->action
203      *  along with id/table etc..
204      *
205      * it's basic usage is to fill in core_notify after an event has happend.
206      *
207      * We can also use it to notify other modules if something has happend.
208      *  = eg. mtrack_ticket * watch will notify mtrack_jira::
209      *
210      * @param Pman_Core_DataObject_Events $event - the Pman event dataobject that was created
211      * 
212      */
213     
214     function notifyEvent($event)
215     {
216         //print_r($event);
217        //DB_DataObject::DebugLevel(1);
218         // see if there are any watches on events..
219         // notify everyone flagged except the person doing it...
220         // this is very basic logic... -
221         //    if more intelligence is needed...
222         //    then it 'rules' will have to be added by the watched object....
223         //    anyway leave that for another day..
224         if (empty($event->action)) {
225             return;
226         }
227         $w = DB_DataObject::factory('core_watch');
228         $w->ontable = $event->on_table;
229         $w->whereAdd('onid = 0 OR onid='. ((int) $event->on_id));
230        
231         $w->event  = $event->action;
232         $w->active = 1;
233         
234         
235         $w->whereAdd('person_id != '. (int) $event->person_id);
236  
237         $watches = $w->fetchAll();
238         
239         //print_R($watches); //exit;
240         
241         $nn = DB_DataObject::Factory('core_notify');
242         $nn->ontable    = $event->on_table;
243         $nn->onid       = $event->on_id;
244         
245         foreach($watches as $watch) {
246             $n = clone($nn);
247             if (!$watch->person_id) { // no people??? bugs in watch table
248                 $dom = explode(':',$watch->medium);
249                 if (count($dom) != 2) {
250                     continue;
251                 }
252                 // in some scenarios (like watching for new articles)
253                 // we need to create a core, notify on the medium..
254                 // in which case we set the  set $nn->evtype = medium..
255                 // in that case - just let the called method generate the notify..
256                 
257                 //print_R($dom);
258                 $do = DB_DataObject::factory($dom[0]);
259                 if (!method_exists($do,$dom[1])) {
260                     continue;
261                 }
262                 //echo "calling {$watch->medium}\n";
263                 // the triggered method, can either do something
264                 // or modify the notify event..
265                 if ($do->{$dom[1]}($event, $n) !== false) {
266                     //echo "method did not return false?";
267                     continue;
268                 }
269                 
270                 
271             }
272             
273             
274             $n->trigger_person_id = $event->person_id;
275             $n->trigger_event_id = $event->id;
276             $n->person_id = $watch->person_id;
277             $n->watch_id =  $watch->id;
278             
279             // does this watch already have a flag...
280             $nf = clone($n);
281             $nf->whereAdd("sent < '2000-01-01'");
282             //$nf->whereAdd('sent < act_when');
283             if ($nf->count()) {
284                 // we have a item in the queue for that waiting to be sent..
285                 continue;
286             }
287             //echo "inserting notify?";
288             $n->act_start( empty($n->act_start) ? date("Y-m-d H:i:s") : $n->act_start );
289             $n->insert();
290         }
291         
292         
293         
294     }
295     function initDatabase($roo, $data) {
296         foreach($data as $d) {
297             $dd = $d;
298             if (isset($dd['active'])) {
299                 unset($dd['active']);
300             }
301             $t = DB_DataObject::Factory($this->tableName());
302             $t->setFrom($dd);
303             if ($t->find(true)) {
304                 continue;
305             }
306             $t = DB_DataObject::Factory($this->tableName());
307             $t->setFrom($d);
308             $t->insert();
309             
310             
311         }
312     }
313     
314      
315 }