DataObjects/Core_notify.php
[Pman.Core] / DataObjects / Core_notify.php
1 <?php
2 /**
3  *
4  * Table is designed to be used with a mailer to notify or issue
5  * emails (or maybe others later??)
6  *
7  *
8 CREATE TABLE  core_notify  (
9   `id` int(11)  NOT NULL AUTO_INCREMENT,
10   `recur_id` INT(11) NOT NULL;
11   `act_when` DATETIME NOT NULL,
12   `onid` int(11)  NOT NULL DEFAULT 0,
13   `ontable` varchar(128)  NOT NULL DEFAULT '',
14   `person_id` int(11)  NOT NULL DEFAULT 0,
15   `msgid` varchar(128)  NOT NULL  DEFAULT '',
16   `sent` DATETIME  NOT NULL,
17   `event_id` int(11)  NOT NULL DEFAULT 0,
18   PRIMARY KEY (`id`),
19   INDEX `lookup`(`act_when`, `msgid`)
20 );
21 **/
22
23 require_once 'DB/DataObject.php';
24
25 class Pman_Core_DataObjects_Core_notify extends DB_DataObject 
26 {
27     ###START_AUTOCODE
28     /* the code below is auto generated do not remove the above tag */
29
30     public $__table = 'core_notify';                     // table name
31     public $id;                              // int(11)  not_null primary_key auto_increment
32     public $recur_id;                        // int(11) not_null
33     public $act_when;                        // datetime(19)  not_null multiple_key binary
34     public $onid;                            // int(11)  not_null
35     public $ontable;                         // string(128)  not_null
36     public $person_id;                       // int(11)  not_null
37     public $msgid;                           // string(128)  not_null
38     public $sent;                            // datetime(19)  not_null binary
39     public $event_id;                        // int(11)  
40     public $watch_id;                        // int(11)  
41     public $trigger_person_id;                 // int(11)
42     public $trigger_event_id;              // int(11)  
43     public $evtype;                         // event type (or method to call)fall
44     
45     /* the code above is auto generated do not remove the tag below */
46     ###END_AUTOCODE
47     
48     function person($set = false)
49     {
50         if ($set !== false) {
51             $this->person_id = is_object($set) ? $set->id : $set;
52             return;
53         }
54         $c = DB_DataObject::Factory('Person');
55         $c->get($this->person_id);
56         return $c;
57         
58     }
59     function object($set = false)
60     {
61         if ($set !== false) {
62             $this->ontable = $set->tableName();
63             $this->onid = $set->id;
64             return $set;
65         }
66         $c = DB_DataObject::factory($this->ontable);
67         
68         if ($this->onid == 0) {
69             return $c; // empty dataobject.
70         }
71         
72         $c->autoJoin();
73         
74         if ($c->get($this->onid)) {
75             return $c;
76         }
77         return false;
78         
79     }
80     function beforeDelete($dependants_array, $roo) {
81         if ($this->delivered()) {
82             $roo->jerr("you can not delete a record of a successfull delivery");
83         }
84     }
85     
86     
87     function act_start($set = false)
88     {
89         if ($set === false) {
90             return $this->act_start;
91         }
92         $this->act_when = $set;
93         $this->act_start = $set;
94         return $set;
95     }
96     
97     function event()
98     {
99
100         $c = DB_DataObject::factory('Events');
101         
102         if ($c->get($this->event_id)) {
103             return $c;
104         }
105         return false;
106         
107     }
108     
109     function triggerEvent()
110     {
111
112         $c = DB_DataObject::factory('Events');
113         
114         if ($c->get($this->trigger_event_id)) {
115             return $c;
116         }
117         return false;
118         
119     }
120     
121     function delivered()
122     {
123         return !empty($this->msgid);
124     }
125     
126     function whereAddDeliveryStatus($delivered = false)
127     {
128         $tn = $this->tableName();
129         if ($delivered) {
130             $this->whereAdd("$tn.msgid IS NOT NULL AND $tn.msgid != ''");
131         } else {
132             $this->whereAdd("$tn.msgid IS NULL OR $tn.msgid = ''");    
133         }
134     }
135     
136     function status() // used by commandline reporting at present..
137     {
138         switch($this->event_id) {
139             case -1:
140                 return 'DELIVERED';   //not valid..
141             case 0:
142                 return 'PENDING';
143             default:
144                 $p ='';
145                 if (strtotime($this->act_when) > time()) {
146                     $p = "RETRY: {$this->act_when} ";
147                 }
148                 return  $p. $this->event()->remarks;
149         }
150         
151     }
152     
153     function applyFilters($q, $au, $roo)
154     {
155         if (isset($q['ontable']) && !in_array($q['ontable'], array('Person', 'Events' . 'core_watch'))) {
156             // this will only work on tables not joined to ours.
157             
158             //DB_DAtaObject::DebugLevel(1);
159             // then we can build a join..
160             $d = DB_DataObject::Factory($q['ontable']);
161             $ji = $d->autoJoin();
162             //echo '<PRE>';print_R($ji);
163             // get cols
164             foreach($ji['join_names'] as $cname=>$fname) {
165                  $this->selectAdd($fname . ' as ontable_id_' . $cname );
166             }
167             
168             //$this->selectAdd($d->_query['data_select']); -- this will cause the same dataIndex...
169             $this->_join .= "
170                 LEFT JOIN {$d->tableName()} ON {$this->tableName()}.onid = {$d->tableName()}.id
171                 {$d->_join}
172             "; 
173             $this->selectAs($d, 'core_notify_%s');
174         } 
175         if (isset($q['query']['person_id_name']) ) {
176             $this->whereAdd( "join_person_id_id.name LIKE '{$this->escape($q['query']['person_id_name'])}%'");
177              
178         }
179          if (!empty($q['query']['status'])) {
180             switch ($q['query']['status']) {
181                 
182                 case 'SUCCESS';
183                     $this->whereAdd("msgid  != ''");
184                     break;
185                 case 'FAILED';
186                     
187                     $this->whereAdd("msgid  = '' AND event_id > 0 AND act_when < NOW()");
188                     
189                     break;
190                 case 'PENDING';
191                     $this->whereAdd('event_id = 0 OR (event_id  > 0 AND act_when > NOW() )');
192                     break;
193                 case 'OPENED';
194                     $this->whereAdd('is_open > 0');
195                     break;
196                 case 'ALL':
197                 default:
198                     break;
199             }
200         }
201         
202         
203         
204         
205         
206         
207     }
208     
209 }