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