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