oops
[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->person_id = $person_id;
40         if (empty($w->person_id)) {
41             return;
42         }
43         
44         $nw = clone($w);
45         $w->whereAdd($whereAdd);
46         
47         
48         if ($w->count()) {
49             return;
50         }
51         $nw->ontable = $ontable;
52         $nw->onid = $onid;
53         
54         $nw->medium = 'email';
55         $nw->active = 1;
56         $nw->insert();
57         
58         
59     }
60     
61     function notify($ontable , $onid, $whereAdd)
62     {
63         $w = DB_DataObject::factory('core_watch');
64         $w->whereAdd($whereAdd);
65         $w->selectAdd();
66         $w->selectAdd('distinct(person_id) as person_id');
67         $people = $w->fetchAll('person_id');
68         $nn = DB_DataObject::Factory('core_notify');
69         $nn->ontable = $ontable;
70         $nn->onid = $onid;
71         foreach($people as $p) {
72             if (!$p) { // no people??? bugs in watch table
73                 continue;
74             }
75             $n = clone($nn);
76             $n->person_id = $p;
77             $nf = clone($n);
78             $nf->whereAdd('sent < act_when');
79             if ($nf->count()) {
80                 // we have a item in the queue for that waiting to be sent..
81                 continue;
82             }
83             $n->act_when = date("Y-m-d H:i:s");
84             $n->insert();
85             
86             
87         }
88         
89         
90         
91     }
92      
93 }