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