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