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