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