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         DB_DAtaObject::debugLevel(1);
38         $w = DB_DataObject::factory('core_watch');
39         $w->ontable = $ontable;
40         $w->onid = $onid;
41         $w->person_id = $person_id;
42         if (empty($w->person_id)) {
43             return;
44         }
45         
46         $nw = clone($w);
47         $w->whereAdd($whereAdd);
48         
49         
50         if ($w->count()) {
51             return;
52         }
53         $nw->medium = 'email';
54         $nw->active = 1;
55         $nw->insert();
56         
57         
58     }
59     
60     function notify($ontable , $onid, $whereAdd)
61     {
62         $w = DB_DataObject::factory('core_watch');
63         $w->whereAdd($whereAdd);
64         $w->selectAdd();
65         $w->selectAdd('distinct(person_id) as person_id');
66         $people = $w->fetchAll('person_id');
67         $nn = DB_DataObject::Factory('core_notify');
68         $nn->ontable = $ontable;
69         $nn->onid = $onid;
70         foreach($people as $p) {
71             if (!$p) { // no people??? bugs in watch table
72                 continue;
73             }
74             $n = clone($nn);
75             $n->person_id = $p;
76             $nf = clone($n);
77             $nf->whereAdd('sent < act_when');
78             if ($nf->count()) {
79                 // we have a item in the queue for that waiting to be sent..
80                 continue;
81             }
82             $n->act_when = date("Y-m-d H:i:s");
83             $n->insert();
84             
85             
86         }
87         
88         
89         
90     }
91     /***
92      * The purpose of this is to gather all the events that have
93      * occured in the system (where watches exist)
94      * Do we want to send the user 1 email ?? or multiple...
95      * --> I guess multiple emails..
96      *
97      * so we need to return
98      *
99      *  array(
100           $USER_ID => array(
101                 $OBJECT:$ID, $OBJECT:$ID, $OBJECT:$ID, .....
102           )
103      * )
104      *
105      * The mailer can then go through and call each object ??
106      *
107      *
108      * -- Things we can watch..
109      *
110      * mtrack_change <- this is a neat log of all events.
111      *  which logs these things
112      *     Individual Ticket changes (already)
113      *     a Project -> which means ticket changes... which again can be discovered via mtrack_changes..
114      *     a Repo for Commits (-- which will be handled by mtrack_changes)
115      *     Wiki changes.. later...
116      *     
117      *
118      *
119      */
120     
121     function watched($medium, $watcher = null)
122     {
123         $w = DB_DataObject::factory('core_watch');
124         if ($watcher) {
125             $w->person_id = $watcher;
126         }
127         $w->active = 1;
128         $w->medium = $medium;
129         $ar = $w->fetchAll();
130         $ret = array();
131         foreach($ar as $o) {
132             if (!isset($ret[$o->person_id])) {
133                 $ret[$o->person_id] = array();
134             }
135             $ret[$o->person_id][] = $o->ontable .':'. $o->onid;
136         }
137         
138         return $ret;
139     }
140 }