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