Fix #5661 - MTrack - daily email large and no branch
[Pman.Core] / DataObjects / Core_watch.php
1 <?php
2 /**
3  * Table Definition for core_watch
4  *
5  * works with 'core_notify'
6  *
7  * any object can call
8  *   $watch->notify($ontable, $onid)
9  *
10  *   in which case it should create a core_notify event.
11  *
12  *
13  * Should 'event' trigger this..
14  *   -> eg. somebody makes a 'EDIT' on 'person'
15  *   -> a watch exists for
16  *        ontable=person,
17  *        onid = -1 <<-- every entry..
18  *        person_id -> who is goes to.
19  *        event = CRUD (eg. shortcut for edit/create/delete)
20  *        medium = "REVIEW" << eg. review needed..
21  *        
22  *
23  *  if onid == 0 then it will monitor all changes to that table..
24  *
25  *
26  * 
27  * 
28  */
29 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
30
31 class Pman_Core_DataObjects_Core_watch extends DB_DataObject 
32 {
33     ###START_AUTOCODE
34     /* the code below is auto generated do not remove the above tag */
35
36     public $__table = 'core_watch';                      // table name
37     public $id;                              // int(11)  not_null primary_key auto_increment
38     public $ontable;                         // string(128)  not_null
39     public $onid;                            // int(11)  not_null
40     public $person_id;                       // int(11)  not_null
41     public $event;                           // string(128)  not_null
42     public $medium;                          // string(128)  not_null
43     public $active;                          // int(11)  not_null
44
45     
46     /* the code above is auto generated do not remove the tag below */
47     ###END_AUTOCODE
48     /** make sure there is a watch for this user.. */
49     
50     
51     function applyFilters($q,$au, $roo)
52     {
53         if (!empty($q['_list_actions'])) {
54             $this->listActions($roo,$q);
55         }
56         //die("apply filters");
57         if (!empty($q['_split_event_name'])) {
58             $this->selectAdd("
59                 
60                 substr( event, 1, LOCATE( '.',event) -1) as event_left,
61                 substr( event,   LOCATE( '.',event) +1) as event_right,
62                 (SELECT
63                     display_name FROM core_enum where etype = '{$this->escape($q['_split_event_name'])}'
64                     AND name = substr( event,   LOCATE( '.',event) +1)
65                 ) as event_right_display_name
66                              
67             ");
68             
69             
70             
71         }
72         
73     }
74     
75     function toRooSingleArray($au,$q)
76     {
77         $ret = $this->toArray();
78         if (empty($q['_split_event_name'])) {
79             return $ret;
80         }
81         $bits = explode('.', $this->event);
82         $ret['event_left'] = $bits[0];
83         $ret['event_right'] = $bits[1];
84         // check core enu.
85         if (!empty($ret['event_right'])) {
86             $ce = DB_DataObject::factory('core_enum')->lookupObject($q['_split_event_name'], $ret['event_right']);
87             $ret['event_right_display_name'] = $ce->display_name;
88         }
89         
90         return $ret;
91         
92         
93     }
94     
95     function listActions($roo, $q) {
96         
97         //print_r($q);
98         $d = DB_DataObject::Factory($q['on_table']);
99         $ret = array();
100         
101         foreach(get_class_methods($d) as $m) {
102             //var_dump($m);
103             if (!preg_match('/^notify/', $m)) {
104                 continue;
105             }
106             $ret[] = array(
107                 'display_name' => preg_replace('/^notify/', '' , $m),
108                 'name' => $q['on_table'] .':'. $m
109             );
110         }
111         $roo->jdata($ret);
112     }
113     
114     /**
115      *
116      * Create a watch...
117      *
118      */
119     
120     
121     
122     
123     function ensureNotify(  $ontable, $onid, $person_id, $whereAdd)
124     {
125         //DB_DAtaObject::debugLevel(1);
126         $w = DB_DataObject::factory('core_watch');
127         $w->person_id = $person_id;
128         if (empty($w->person_id)) {
129             return;
130         }
131         
132         $nw = clone($w);
133         $w->whereAdd($whereAdd);
134         
135         
136         if ($w->count()) {
137             return;
138         }
139         $nw->ontable = $ontable;
140         $nw->onid = $onid;
141         
142         $nw->medium = 'email';
143         $nw->active = 1;
144         $nw->insert();
145          
146     }
147     /**
148      * Generate a notify event based on watches (matching whereAdd)
149      *
150      * Usage: $core_watch->notify('mtrack_repos', 1, false, date()
151      *
152      * This can match any 'event' type - eg. it can be blank etc...
153      *   Generally used by non-event driven notifications, like our
154      *   Daily commit message.
155      *
156      *  @param string $ontable - the database table that has been updated/changed etc.
157      *  @param int    $onid    - the id of the row changed
158      *  @param string  $whereAdd (optiona) - a DB whereAdd() condition to filter the search for watches
159      *  @param datetime    $when   (default now)  - date/time to create the notification for (Eg. end of day..)
160      *  @param string $to_ontable  - notify event create on this table, rather than watch table.
161      *  @param string $to_id  - notify event create on this id, rather than watch id.
162      *  
163      * 
164      */
165     function notify($ontable , $onid, $whereAdd = false, $when=false, $to_ontable=false, $to_onid=false)
166     {
167         $w = DB_DataObject::factory('core_watch');   
168         if ($whereAdd !== false) { 
169             $w->whereAdd($whereAdd  );
170         }
171         $w->active =1;
172         
173         $w->whereAdd('onid = 0 OR onid='. ((int) $onid));
174        
175         
176         $w->ontable = $ontable;
177         //$w->selectAdd();
178         //$w->selectAdd('distinct(person_id) as person_id');
179         
180         foreach($w->fetchAll() as $w) { 
181             if (!$w->person_id) { // no people??? bugs in watch table
182                 continue;
183             }
184          
185          
186          
187             $nn = DB_DataObject::Factory('core_notify');
188             $nn->ontable = $to_ontable === false ? $ontable : $to_ontable;
189             $nn->onid = $to_onid === false ?  $onid : $to_onid;
190             $nn->evtype = $w->medium;
191             $nn->person_id = $w->person_id;
192             
193             $nf = clone($nn);
194             $nf->whereAdd("sent < '2000-01-01'");
195             if ($nf->count()) {
196                 // we have a item in the queue for that waiting to be sent..
197                 continue;
198             }
199             $nn->act_start( date("Y-m-d H:i:s", $when !== false ? strtotime($when) : time()) );
200             $nn->insert();
201         }
202           
203     }
204     // static really...
205     /**
206      *
207      * This get's called by roo->addEvent()
208      *
209      * And searches for matching '$watch->event' == $event->action
210      *  along with id/table etc..
211      *
212      * it's basic usage is to fill in core_notify after an event has happend.
213      *
214      * We can also use it to notify other modules if something has happend.
215      *  = eg. mtrack_ticket * watch will notify mtrack_jira::
216      *
217      * @param Pman_Core_DataObject_Events $event - the Pman event dataobject that was created
218      * 
219      */
220     
221     function notifyEvent($event)
222     {
223         //print_r($event);
224         //DB_DataObject::DebugLevel(1);
225         // see if there are any watches on events..
226         // notify everyone flagged except the person doing it...
227         // this is very basic logic... -
228         //    if more intelligence is needed...
229         //    then it 'rules' will have to be added by the watched object....
230         //    anyway leave that for another day..
231         if (empty($event->action)) {
232             return;
233         }
234         $w = DB_DataObject::factory('core_watch');
235         $w->ontable = $event->on_table;
236         $w->whereAdd('onid = 0 OR onid='. ((int) $event->on_id));
237        
238         $w->event  = $event->action;
239         $w->active = 1;
240         
241         // not sure why this is here... - it breaks on the reader article -> 
242         if ($event->person_id) {
243             $w->whereAdd('person_id != '. (int) $event->person_id);
244         }
245  
246         $watches = $w->fetchAll();
247         
248         //print_R($watches); 
249         
250         $nn = DB_DataObject::Factory('core_notify');
251         $nn->ontable    = $event->on_table;
252         $nn->onid       = $event->on_id;
253         
254         foreach($watches as $watch) {
255             $n = clone($nn);
256             if (!$watch->person_id) { // no people??? bugs in watch table
257                 $dom = explode(':',$watch->medium);
258                 if (count($dom) != 2) {
259                     continue;
260                 }
261                 // in some scenarios (like watching for new articles)
262                 // we need to create a core, notify on the medium..
263                 // in which case we set the  set $nn->evtype = medium..
264                 // in that case - just let the called method generate the notify..
265                 
266                 //print_R($dom);
267                 $do = DB_DataObject::factory($dom[0]);
268                 if (!method_exists($do,$dom[1])) {
269                     continue;
270                 }
271                 //echo "calling {$watch->medium}\n";
272                 // the triggered method, can either do something
273                 // or modify the notify event..
274                 if ($do->{$dom[1]}($event, $n) !== false) {
275                     //echo "method did not return false?";
276                     continue;
277                 }
278                 
279                 
280             }
281             
282             
283             $n->trigger_person_id = $event->person_id;
284             $n->trigger_event_id = $event->id;
285             $n->person_id = $watch->person_id;
286             $n->watch_id =  $watch->id;
287             
288             // does this watch already have a flag...
289             $nf = clone($n);
290             $nf->whereAdd("sent < '2000-01-01'");
291             //$nf->whereAdd('sent < act_when');
292             if ($nf->count()) {
293                 // we have a item in the queue for that waiting to be sent..
294                 continue;
295             }
296             //echo "inserting notify?";
297             $n->act_start( empty($n->act_start) ? date("Y-m-d H:i:s") : $n->act_start );
298             $n->insert();
299         }
300         
301         
302         
303     }
304     function initDatabase($roo, $data) {
305         foreach($data as $d) {
306             $dd = $d;
307             if (isset($dd['active'])) {
308                 unset($dd['active']);
309             }
310             $t = DB_DataObject::Factory($this->tableName());
311             $t->setFrom($dd);
312             if ($t->find(true)) {
313                 continue;
314             }
315             $t = DB_DataObject::Factory($this->tableName());
316             $t->setFrom($d);
317             $t->insert();
318             
319             
320         }
321     }
322     
323      
324 }