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