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