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     function notify($ontable , $onid, $whereAdd)
79     {
80         $w = DB_DataObject::factory('core_watch');
81         $w->whereAdd($whereAdd);
82         $w->selectAdd();
83         $w->selectAdd('distinct(person_id) as person_id');
84         $people = $w->fetchAll('person_id');
85         
86         $nn = DB_DataObject::Factory('core_notify');
87         $nn->ontable = $ontable;
88         $nn->onid = $onid;
89         foreach($people as $p) {
90             if (!$p) { // no people??? bugs in watch table
91                 continue;
92             }
93             $n = clone($nn);
94             $n->person_id = $p;
95             $nf = clone($n);
96             $nf->whereAdd('sent < act_when');
97             if ($nf->count()) {
98                 // we have a item in the queue for that waiting to be sent..
99                 continue;
100             }
101             $n->act_start( date("Y-m-d H:i:s") );
102             $n->insert();
103             
104             
105         }
106          
107     }
108     // static really...
109     function notifyEvent($event)
110     {
111         // see if there are any watches on events..
112         
113         $w = DB_DataObject::factory('core_watch');
114         $w->ontable = $event->on_table;
115         $w->whereAdd('onid = 0 OR onid='. ((int) $event->on_id));
116         $w->event  = $event->action;
117         $w->whereAdd('person_id != '. (int) $event->person_id)));
118
119         
120         $watches = $w->fetchAll();
121         
122         $nn = DB_DataObject::Factory('core_notify');
123         $nn->ontable    = $event->on_table;
124         $nn->onid       = $event->on_id;
125         
126         foreach($watches as $watch) {
127             if (!$watch->person_id) { // no people??? bugs in watch table
128                 continue;
129             }
130             
131             $n = clone($nn);
132             $n->person_id = $p;
133             $n->watch_id =  $watch->id;
134             
135             // does this watch already have a flag...
136             $nf = clone($n);
137             $nf->whereAdd('sent < act_when');
138             if ($nf->count()) {
139                 // we have a item in the queue for that waiting to be sent..
140                 continue;
141             }
142             
143             $n->act_start( date("Y-m-d H:i:s") );
144             $n->insert();
145             
146             
147         }
148         
149         
150         
151     }
152     
153      
154 }