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