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  *     
21  *
22  * 
23  * 
24  */
25 require_once 'DB/DataObject.php';
26
27 class Pman_Core_DataObjects_Core_watch extends DB_DataObject 
28 {
29     ###START_AUTOCODE
30     /* the code below is auto generated do not remove the above tag */
31
32     public $__table = 'core_watch';                      // table name
33     public $id;                              // int(11)  not_null primary_key auto_increment
34     public $ontable;                         // string(128)  not_null
35     public $onid;                            // int(11)  not_null
36     public $person_id;                       // int(11)  not_null
37     public $event;                           // string(128)  not_null
38     public $medium;                          // string(128)  not_null
39     public $active;                          // int(11)  not_null
40
41     
42     /* the code above is auto generated do not remove the tag below */
43     ###END_AUTOCODE
44     /** make sure there is a watch for this user.. */
45     
46     /**
47      *
48      * Create a watch...
49      *
50      */
51     
52     function ensureNotify(  $ontable, $onid, $person_id, $whereAdd)
53     {
54         //DB_DAtaObject::debugLevel(1);
55         $w = DB_DataObject::factory('core_watch');
56         $w->person_id = $person_id;
57         if (empty($w->person_id)) {
58             return;
59         }
60         
61         $nw = clone($w);
62         $w->whereAdd($whereAdd);
63         
64         
65         if ($w->count()) {
66             return;
67         }
68         $nw->ontable = $ontable;
69         $nw->onid = $onid;
70         
71         $nw->medium = 'email';
72         $nw->active = 1;
73         $nw->insert();
74          
75     }
76     
77     function notify($ontable , $onid, $whereAdd)
78     {
79         $w = DB_DataObject::factory('core_watch');
80         $w->whereAdd($whereAdd);
81         $w->selectAdd();
82         $w->selectAdd('distinct(person_id) as person_id');
83         $people = $w->fetchAll('person_id');
84         $nn = DB_DataObject::Factory('core_notify');
85         $nn->ontable = $ontable;
86         $nn->onid = $onid;
87         foreach($people as $p) {
88             if (!$p) { // no people??? bugs in watch table
89                 continue;
90             }
91             $n = clone($nn);
92             $n->person_id = $p;
93             $nf = clone($n);
94             $nf->whereAdd('sent < act_when');
95             if ($nf->count()) {
96                 // we have a item in the queue for that waiting to be sent..
97                 continue;
98             }
99             $n->act_start( date("Y-m-d H:i:s") );
100             $n->insert();
101             
102             
103         }
104         
105         
106         
107     }
108      
109 }