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