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  * 
14  */
15 require_once 'DB/DataObject.php';
16
17 class Pman_Core_DataObjects_Core_watch extends DB_DataObject 
18 {
19     ###START_AUTOCODE
20     /* the code below is auto generated do not remove the above tag */
21
22     public $__table = 'core_watch';                      // table name
23     public $ontable;                         // string(128)  not_null primary_key
24     public $onid;                            // int(11)  not_null primary_key
25     public $person_id;                         // int(11)  not_null primary_key
26     public $event;                           // string(128)  not_null primary_key
27     public $medium;                          // string(128)  not_null primary_key
28     public $active;                          // int(11)  not_null
29
30     
31     /* the code above is auto generated do not remove the tag below */
32     ###END_AUTOCODE
33     /** make sure there is a watch for this user.. */
34     
35     function ensureNotify(  $ontable, $onid, $person_id, $whereAdd)
36     {
37         $w = DB_DataObject::factory('core_watch');
38         $w->ontable = $ontable;
39         $w->onid = $onid;
40         $w->person_id = $person_id;
41         $nw = clone($w);
42         $w->whereAdd($whereAdd);
43         
44         
45         if ($w->count()) {
46             return;
47         }
48         $nw->medium = 'email';
49         $nw->active = 1;
50         $nw->insert();
51         
52         
53     }
54     
55     function notify($ontable , $onid, $whereAdd)
56     {
57         $w = DB_DataObject::factory('core_watch');
58         $w->whereAdd($whereAdd);
59         $w->selectAdd();
60         $w->selectAdd('distinct(person_id) as person_id');
61         $people = $w->fetchAll('person_id');
62         $nn = DB_DataObject::Factory('core_notify');
63         $nn->ontable = $ontable;
64         $nn->onid = $onid;
65         foreach($people as $p) {
66             if (!$p) { // no people??? bugs in watch table
67                 continue;
68             }
69             $n = clone($nn);
70             $n->person_id = $p;
71             $nf = clone($n);
72             $nf->whereAdd('sent < act_when');
73             if ($nf->count()) {
74                 // we have a item in the queue for that waiting to be sent..
75                 continue;
76             }
77             $n->act_when = date("Y-m-d H:i:s");
78             $n->insert();
79             
80             
81         }
82         
83         
84         
85     }
86     /***
87      * The purpose of this is to gather all the events that have
88      * occured in the system (where watches exist)
89      * Do we want to send the user 1 email ?? or multiple...
90      * --> I guess multiple emails..
91      *
92      * so we need to return
93      *
94      *  array(
95           $USER_ID => array(
96                 $OBJECT:$ID, $OBJECT:$ID, $OBJECT:$ID, .....
97           )
98      * )
99      *
100      * The mailer can then go through and call each object ??
101      *
102      *
103      * -- Things we can watch..
104      *
105      * mtrack_change <- this is a neat log of all events.
106      *  which logs these things
107      *     Individual Ticket changes (already)
108      *     a Project -> which means ticket changes... which again can be discovered via mtrack_changes..
109      *     a Repo for Commits (-- which will be handled by mtrack_changes)
110      *     Wiki changes.. later...
111      *     
112      *
113      *
114      */
115     
116     function watched($medium, $watcher = null)
117     {
118         $w = DB_DataObject::factory('core_watch');
119         if ($watcher) {
120             $w->person_id = $watcher;
121         }
122         $w->active = 1;
123         $w->medium = $medium;
124         $ar = $w->fetchAll();
125         $ret = array();
126         foreach($ar as $o) {
127             if (!isset($ret[$o->person_id])) {
128                 $ret[$o->person_id] = array();
129             }
130             $ret[$o->person_id][] = $o->ontable .':'. $o->onid;
131         }
132         
133         return $ret;
134     }
135 }