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