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