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  * 
24  * 
25  */
26 require_once 'DB/DataObject.php';
27
28 class Pman_Core_DataObjects_Core_watch extends DB_DataObject 
29 {
30     ###START_AUTOCODE
31     /* the code below is auto generated do not remove the above tag */
32
33     public $__table = 'core_watch';                      // table name
34     public $id;                              // int(11)  not_null primary_key auto_increment
35     public $ontable;                         // string(128)  not_null
36     public $onid;                            // int(11)  not_null
37     public $person_id;                       // int(11)  not_null
38     public $event;                           // string(128)  not_null
39     public $medium;                          // string(128)  not_null
40     public $active;                          // int(11)  not_null
41
42     
43     /* the code above is auto generated do not remove the tag below */
44     ###END_AUTOCODE
45     /** make sure there is a watch for this user.. */
46     
47     /**
48      *
49      * Create a watch...
50      *
51      */
52     
53     function ensureNotify(  $ontable, $onid, $person_id, $whereAdd)
54     {
55         //DB_DAtaObject::debugLevel(1);
56         $w = DB_DataObject::factory('core_watch');
57         $w->person_id = $person_id;
58         if (empty($w->person_id)) {
59             return;
60         }
61         
62         $nw = clone($w);
63         $w->whereAdd($whereAdd);
64         
65         
66         if ($w->count()) {
67             return;
68         }
69         $nw->ontable = $ontable;
70         $nw->onid = $onid;
71         
72         $nw->medium = 'email';
73         $nw->active = 1;
74         $nw->insert();
75          
76     }
77     /**
78      * Generate a notify event based on watches (matching whereAdd)
79      *
80      * Usage: $core_watch->notify('mtrack_repos', 1, false, date()
81      */
82     function notify($ontable , $onid, $whereAdd = false, $when=false)
83     {
84         $w = DB_DataObject::factory('core_watch');
85         if ($whereAdd !== false) { 
86             $w->whereAdd($whereAdd  );
87         }
88         $w->active =1;
89         
90          $w->whereAdd('onid = 0 OR onid='. ((int) $onid));
91        
92         
93         $w->ontable = $ontable;
94         //$w->selectAdd();
95         //$w->selectAdd('distinct(person_id) as person_id');
96         
97         foreach($w->fetchAll() as $w) { 
98             if (!$w->person_id) { // no people??? bugs in watch table
99                 continue;
100             }
101          
102          
103          
104             $nn = DB_DataObject::Factory('core_notify');
105             $nn->ontable = $ontable;
106             $nn->onid = $onid;
107             $nn->evtype = $w->medium;
108             $nn->person_id = $w->person_id;
109             
110             $nf = clone($nn);
111             $nf->whereAdd("sent < '2000-01-01'");
112             if ($nf->count()) {
113                 // we have a item in the queue for that waiting to be sent..
114                 continue;
115             }
116             $nn->act_start( date("Y-m-d H:i:s", $when !== false ? strtotime($when) : time()) );
117             $nn->insert();
118         }
119           
120     }
121     // static really...
122     /**
123      *
124      * This get's called by roo->jok()
125      *
126      *
127      * it's basic usage is to fill in core_notify after an event has happend.
128      *
129      * We can also use it to notify other modules if something has happend.
130      *  = eg. mtrack_ticket * watch will notify mtrack_jira::
131      *
132      *  in that example:
133      *     public $ontable;                         // string(128)  not_null
134             public $onid;                            // int(11)  not_null
135             public $person_id;                       // int(11)  not_null
136             public $event;                           // string(128)  not_null
137             public $medium;                          // string(128)  not_null
138             public $active;                          // int(11)  not_null
139
140      */
141     
142     function notifyEvent($event)
143     {
144         //DB_DataObject::DebugLevel(1);
145         // see if there are any watches on events..
146         // notify everyone flagged except the person doing it...
147         // this is very basic logic... -
148         //    if more intelligence is needed...
149         //    then it 'rules' will have to be added by the watched object....
150         //    anyway leave that for another day..
151         if (empty($event->action)) {
152             return;
153         }
154         $w = DB_DataObject::factory('core_watch');
155         $w->ontable = $event->on_table;
156         $w->whereAdd('onid = 0 OR onid='. ((int) $event->on_id));
157        
158         $w->event  = $event->action;
159         $w->active = 1;
160         
161         
162         $w->whereAdd('person_id != '. (int) $event->person_id);
163  
164         $watches = $w->fetchAll();
165         
166         //print_R($watches);exit;
167         
168         $nn = DB_DataObject::Factory('core_notify');
169         $nn->ontable    = $event->on_table;
170         $nn->onid       = $event->on_id;
171         
172         foreach($watches as $watch) {
173             if (!$watch->person_id) { // no people??? bugs in watch table
174                 $dom = explode(':',$watch->medium);
175                 if (count($dom) != 2) {
176                     continue;
177                 }
178                 $do = DB_DataObject::factory($dom[0]);
179                 if (!method_exists($do,$dom[1])) {
180                     continue;
181                 }
182                 $do->{$dom[1]}($event);
183                 
184                 continue;
185             }
186             
187             $n = clone($nn);
188             $n->trigger_person_id = $event->person_id;
189             $n->trigger_event_id = $event->id;
190             $n->person_id = $watch->person_id;
191             $n->watch_id =  $watch->id;
192             
193             // does this watch already have a flag...
194             $nf = clone($n);
195             $nf->whereAdd("sent ><'2000-01-01'");
196             //$nf->whereAdd('sent < act_when');
197             if ($nf->count()) {
198                 // we have a item in the queue for that waiting to be sent..
199                 continue;
200             }
201             
202             $n->act_start( date("Y-m-d H:i:s") );
203             $n->insert();
204             
205             
206         }
207         
208         
209         
210     }
211     
212      
213 }