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