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