4 * Table Definition for core_watch
6 * works with 'core_notify'
9 * $watch->notify($ontable, $onid)
11 * in which case it should create a core_notify event.
14 * Should 'event' trigger this..
15 * -> eg. somebody makes a 'EDIT' on 'person'
16 * -> a watch exists for
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..
24 * if onid == 0 then it will monitor all changes to that table..
30 require_once 'DB/DataObject.php';
32 class Pman_Core_DataObjects_Core_watch extends DB_DataObject
35 /* the code below is auto generated do not remove the above tag */
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
47 /* the code above is auto generated do not remove the tag below */
49 /** make sure there is a watch for this user.. */
52 function applyFilters($q,$au, $roo)
54 if (!empty($q['_list_actions'])) {
55 $this->listActions($roo,$q);
60 function listActions($roo, $q) {
62 $d = DB_DataObject::Factory($q['on_table']);
64 foreach(get_class_methods($d) as $m) {
65 if (!preg_match('/^notify/', $m)) {
69 'display_name' => preg_replace('/^notify/', '' , $m),
70 'name' => $q['on_table'] .':'. $m
85 function ensureNotify( $ontable, $onid, $person_id, $whereAdd)
87 //DB_DAtaObject::debugLevel(1);
88 $w = DB_DataObject::factory('core_watch');
89 $w->person_id = $person_id;
90 if (empty($w->person_id)) {
95 $w->whereAdd($whereAdd);
101 $nw->ontable = $ontable;
104 $nw->medium = 'email';
110 * Generate a notify event based on watches (matching whereAdd)
112 * Usage: $core_watch->notify('mtrack_repos', 1, false, date()
114 * This can match any 'event' type - eg. it can be blank etc...
115 * Generally used by non-event driven notifications, like our
116 * Daily commit message.
118 * @param string $ontable - the database table that has been updated/changed etc.
119 * @param int $onid - the id of the row changed
120 * @param string $whereAdd (optiona) - a DB whereAdd() condition to filter the search for watches
121 * @param datetime $when (default now) - date/time to create the notification for (Eg. end of day..)
124 function notify($ontable , $onid, $whereAdd = false, $when=false)
126 $w = DB_DataObject::factory('core_watch');
127 if ($whereAdd !== false) {
128 $w->whereAdd($whereAdd );
132 $w->whereAdd('onid = 0 OR onid='. ((int) $onid));
135 $w->ontable = $ontable;
137 //$w->selectAdd('distinct(person_id) as person_id');
139 foreach($w->fetchAll() as $w) {
140 if (!$w->person_id) { // no people??? bugs in watch table
146 $nn = DB_DataObject::Factory('core_notify');
147 $nn->ontable = $ontable;
149 $nn->evtype = $w->medium;
150 $nn->person_id = $w->person_id;
153 $nf->whereAdd("sent < '2000-01-01'");
155 // we have a item in the queue for that waiting to be sent..
158 $nn->act_start( date("Y-m-d H:i:s", $when !== false ? strtotime($when) : time()) );
166 * This get's called by roo->addEvent()
168 * And searches for matching '$watch->event' == $event->action
169 * along with id/table etc..
171 * it's basic usage is to fill in core_notify after an event has happend.
173 * We can also use it to notify other modules if something has happend.
174 * = eg. mtrack_ticket * watch will notify mtrack_jira::
176 * @param Pman_Core_DataObject_Events $event - the Pman event dataobject that was created
180 function notifyEvent($event)
183 //DB_DataObject::DebugLevel(1);
184 // see if there are any watches on events..
185 // notify everyone flagged except the person doing it...
186 // this is very basic logic... -
187 // if more intelligence is needed...
188 // then it 'rules' will have to be added by the watched object....
189 // anyway leave that for another day..
190 if (empty($event->action)) {
193 $w = DB_DataObject::factory('core_watch');
194 $w->ontable = $event->on_table;
195 $w->whereAdd('onid = 0 OR onid='. ((int) $event->on_id));
197 $w->event = $event->action;
201 $w->whereAdd('person_id != '. (int) $event->person_id);
203 $watches = $w->fetchAll();
205 //print_R($watches); //exit;
207 $nn = DB_DataObject::Factory('core_notify');
208 $nn->ontable = $event->on_table;
209 $nn->onid = $event->on_id;
211 foreach($watches as $watch) {
213 if (!$watch->person_id) { // no people??? bugs in watch table
214 $dom = explode(':',$watch->medium);
215 if (count($dom) != 2) {
218 // in some scenarios (like watching for new articles)
219 // we need to create a core, notify on the medium..
220 // in which case we set the set $nn->evtype = medium..
221 // in that case - just let the called method generate the notify..
224 $do = DB_DataObject::factory($dom[0]);
225 if (!method_exists($do,$dom[1])) {
228 //echo "calling {$watch->medium}\n";
229 // the triggered method, can either do something
230 // or modify the notify event..
231 if ($do->{$dom[1]}($event, $n) !== false) {
232 //echo "method did not return false?";
240 $n->trigger_person_id = $event->person_id;
241 $n->trigger_event_id = $event->id;
242 $n->person_id = $watch->person_id;
243 $n->watch_id = $watch->id;
245 // does this watch already have a flag...
247 $nf->whereAdd("sent < '2000-01-01'");
248 //$nf->whereAdd('sent < act_when');
250 // we have a item in the queue for that waiting to be sent..
253 //echo "inserting notify?";
254 $n->act_start( empty($n->act_start) ? date("Y-m-d H:i:s") : $n->act_start );
261 function initDatabase($roo, $data) {
262 foreach($data as $d) {
264 if (isset($dd['active'])) {
265 unset($dd['active']);
267 $t = DB_DataObject::Factory($this->tableName());
269 if ($t->find(true)) {
272 $t = DB_DataObject::Factory($this->tableName());