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