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