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