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