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     public $act_start;
45     public $person_table;
46
47
48     /* the code above is auto generated do not remove the tag below */
49     ###END_AUTOCODE
50     
51             
52     
53     
54     function person($set = false)
55     {
56         if ($set !== false) {
57             $this->person_table = is_object($set) ? $set->tableName() : '';
58             
59             $person_table = empty($this->person_table) ? 'Person' : $this->person_table;
60             $col = $person_table  == "Person" ? 'person_id' : $person_table . '_id';
61             
62             $this->{$col} = is_object($set) ? $set->id : $set;
63             return;
64         }
65         static $cache  =array();
66         $person_table = empty($this->person_table) ? 'Person' : $this->person_table;
67         $col = $person_table == "Person" ? 'person_id' : $person_table . '_id';
68         
69         if (isset($cache[$person_table .':'. $this->{$col}])) {
70             return $cache[$person_table .':'. $this->{$col}];
71         }
72         
73         $c = DB_DataObject::Factory($person_table);
74         $c->get($this->{$col});
75         $cache[$person_table .':'. $this->{$col}] = $c;
76         return $c;
77         
78     }
79     function object($set = false)
80     {
81         if ($set !== false) {
82             $this->ontable = $set->tableName();
83             $this->onid = $set->id;
84             return $set;
85         }
86         $c = DB_DataObject::factory($this->ontable);
87         
88         if ($this->onid == 0) {
89             return $c; // empty dataobject.
90         }
91         
92         $c->autoJoin();
93         
94         if ($c->get($this->onid)) {
95             return $c;
96         }
97         return false;
98         
99     }
100     
101     function lookup($obj, $person, $evtype='')
102     {
103         $x = DB_DataObject::Factory($this->tableName());
104         $x->object($obj);
105         $x->person($person);
106         if (!empty($evtype)) {
107             $x->evtype = $evtype;
108         }
109         if ($x->count() != 1) {
110             return false;
111         }
112         $x->find(true);
113         return $x;
114         
115     }
116     
117     
118     function beforeDelete($dependants_array, $roo) {
119         if ($this->delivered()) {
120             $roo->jerr("you can not delete a record of a successfull delivery");
121         }
122     }
123     function  beforeInsert($request,$roo)
124     {
125         if (empty($request['act_when']) && !empty($request['act_start'])) {
126             $this->act_start($request['act_start']);
127         }
128         
129     }
130     function beforeUpdate($old, $request,$roo)
131     {
132         if (empty($request['act_when']) && !empty($request['act_start'])) {
133             $this->act_start($request['act_start']);
134         }
135     }
136     
137     
138     function act_start($set = false)
139     {
140         if ($set === false) {
141             return $this->act_start;
142         }
143         $this->act_when = $set;
144         $this->act_start = $set;
145         return $set;
146     }
147     
148     function event()
149     {
150
151         $c = DB_DataObject::factory('Events');
152         
153         if ($c->get($this->event_id)) {
154             return $c;
155         }
156         return false;
157         
158     }
159     
160     function triggerEvent()
161     {
162
163         $c = DB_DataObject::factory('Events');
164         
165         if ($c->get($this->trigger_event_id)) {
166             return $c;
167         }
168         return false;
169         
170     }
171     
172     function delivered()
173     {
174         return !empty($this->msgid);
175     }
176     
177     function whereAddDeliveryStatus($delivered = false)
178     {
179         $tn = $this->tableName();
180         if ($delivered) {
181             $this->whereAdd("$tn.msgid IS NOT NULL AND $tn.msgid != ''");
182         } else {
183             $this->whereAdd("$tn.msgid IS NULL OR $tn.msgid = ''");    
184         }
185     }
186     
187     function status() // used by commandline reporting at present..
188     {
189         switch($this->event_id) {
190             case -1:
191                 return 'DELIVERED';   //not valid..
192             case 0:
193                 return 'PENDING';
194             default:
195                 $p ='';
196                 if (strtotime($this->act_when) > time()) {
197                     $p = "RETRY: {$this->act_when} ";
198                 }
199                 return  $p. $this->event()->remarks;
200         }
201         
202     }
203     /**
204      * current state of process
205      *
206      * 0 = pending
207      * 1 = delivered
208      * -1 = failed
209      *
210      *
211      */
212     function state()
213     {
214            
215         if ($this->msgid != '') {
216             return 1;
217         }
218         
219         // msgid is empty now..
220         // if act_when is greater than now, then it's still pending.
221         if (strtotime($this->act_when) > time()) {
222             return 0; 
223         }
224         
225         // event id can be filled in with a failed attempt.
226         
227         if ($this->event_id > 0) {
228             return -1;
229         }
230         
231         // event id is empty, and act_when is in the past... not sent yet..
232         
233         return 0; // pending
234         
235         
236     }
237     
238     
239     function applyFilters($q, $au, $roo)
240     {
241         if (isset($q['ontable']) && !in_array($q['ontable'], array('Person', 'Events',  'core_watch'))) {
242             // this will only work on tables not joined to ours.
243             
244             //DB_DAtaObject::DebugLevel(1);
245             // then we can build a join..
246             $d = DB_DataObject::Factory($q['ontable']);
247             $ji = $d->autoJoin();
248             //echo '<PRE>';print_R($ji);
249             // get cols
250             foreach($ji['join_names'] as $cname=>$fname) {
251                  $this->selectAdd($fname . ' as ontable_id_' . $cname );
252             }
253             
254             //$this->selectAdd($d->_query['data_select']); -- this will cause the same dataIndex...
255             $this->_join .= "
256                 LEFT JOIN {$d->tableName()} ON {$this->tableName()}.onid = {$d->tableName()}.id
257                 {$d->_join}
258             "; 
259             $this->selectAs($d, 'core_notify_%s');
260         } 
261         if (!empty($q['query']['person_id_name']) ) {
262             $this->whereAdd( "join_person_id_id.name LIKE '{$this->escape($q['query']['person_id_name'])}%'");
263              
264         }
265          if (!empty($q['query']['status'])) {
266             switch ($q['query']['status']) {
267                 
268                 case 'SUCCESS';
269                     $this->whereAdd("msgid  != ''");
270                     break;
271                 case 'FAILED';
272                     
273                     $this->whereAdd("msgid  = '' AND event_id > 0 AND act_when < NOW()");
274                     
275                     break;
276                 case 'PENDING';
277                     $this->whereAdd('event_id = 0 OR (event_id  > 0 AND act_when > NOW() )');
278                     break;
279                 
280                 case 'OPENED';
281                     $this->whereAdd('is_open > 0');
282                     break;
283                 
284                 case 'ALL':
285                 default:
286                     break;
287             }
288         }
289         
290         if(!empty($q['_evtype_align'])){
291             $this->selectAdd("
292                 (SELECT
293                         display_name
294                 FROM
295                         core_enum
296                 WHERE
297                         etype = 'Core.NotifyType'
298                     AND
299                         name = core_notify.evtype
300                     AND
301                         active = 1
302                 ) AS evtype_align
303             ");
304         }
305         
306         if(!empty($q['from'])){
307             $this->whereAdd("
308                 act_when >= '{$q['from']}'
309             ");
310         }
311         
312         if(!empty($q['to'])){
313             $this->whereAdd("
314                 act_when <= '{$q['to']}'
315             ");
316         }
317         
318     }
319     
320 }