DataObjects/Core_notify_recur.php
[Pman.Core] / DataObjects / Core_notify_recur.php
1 <?php
2 /**
3  * Table Definition for core_notify_recur
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_notify_recur extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_notify_recur';    // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $person_id;                       // int(11)  not_null
15     
16     public $dtstart;                         // datetime(19)  not_null binary
17     public $dtend;                           // datetime(19)  not_null binary
18     public $tz;                              // real(6)  not_null
19     
20     public $updated_dt;                      // datetime(19)  not_null binary
21     
22     public $last_applied_dt;                 // datetime(19)  not_null binary
23 //    public $max_applied_dtl
24     public $freq; //  varchar(8) NOT NULL;
25     public $freq_day; // text NOT NULL;
26     public $freq_hour; // text
27     
28     public $onid;                            // int(11)  not_null
29     public $ontable;                         // string(128)  not_null
30     public $last_event_id;                   // int(11)  
31     public $method;                         // depricated.
32     public $method_id;                         // string(128)  not_null
33     
34     public $recur_id;                       //INT(11) not_null
35
36     
37     ###END_AUTOCODE
38     //NOTE recur_id and method are depricated.
39     
40     
41     /*
42       freq =  DAILY | YEARLY | MONTHLY
43         *
44         *        THESE ARE EXCLUSIVE..
45         *        freq_day =  1,2,3,4,5" - day number.. or dayofmonth USES TIME FROM DTSTART (unless hours are speced.)
46         *        >> must..
47         *        freq_hourly = 'what hours' << OR IF EMPTY USES TIME FROM DTSTART
48         *
49     /* the code above is auto generated do not remove the tag below */
50     
51     function applyFilters($q, $au, $roo)
52     {
53         
54         if (isset($q['query']['person_id_name']) ) {
55             $this->whereAdd( "join_person_id_id.name LIKE '{$this->escape($q['query']['person_id_name'])}%'");
56              
57         }
58          
59         
60          
61         
62         
63     }
64     function notifytimesRange($advance) {
65         
66         $start = date('Y-m-d H:i:s', max(strtotime("NOW"), strtotime($this->dtstart)));
67         $end  = date('Y-m-d H:i:s', min(strtotime("NOW + $advance DAYS"), strtotime($this->dtend)));
68         
69         return array($start, $end);
70     }
71     
72     function method()
73     {
74         $e = DB_DataObject::Factory('core_enum');
75         $e->get($this->method_id);
76         return $e;
77     }
78     
79     function notifytimes($advance)
80     {
81         
82         // make a list of datetimes when notifies need to be generated for.
83         // it starts 24 hours ago.. or when dtstart
84         
85         list($start, $end) = $this->notifytimesRange($advance);
86         
87         if (strtotime($start) > strtotime($end)) {
88             return array(); // no data..
89         }
90         $ret = array();
91         $hours = empty($this->freq_hour) ? array() : array_unique(json_decode($this->freq_hour));
92         $days = empty($this->freq_day) ? array() : json_decode($this->freq_day);
93         
94         //days to use are = MON FRI SUN
95         
96         //ARE there events on these day in advance days in the future?
97         //TODAY = 25th of may (FRI)
98         //TODAY+1  = 26th  (SAT)
99         //TODAY +2 = 27th = SUN (2== advance)
100         //foreeach day in the future upto >>> advance <<< days?
101             
102          // - does an event occur on this day?
103         //    -YES - then we will generate an event for it.
104          //   -NO nothing happens..
105         $usedays = array();
106         for (  $i =0; $i < $advance +1; $i++) {
107             $ut = strtotime("NOW + $i DAYS");
108             $day = strtoupper(date("D", $ut));
109             if (in_array($day, $days)) {
110                 $usedays[] = date("Y-m-d", $ut);
111             }
112         }
113                
114         //print_r($this);
115         
116         
117         foreach($usedays as $d){
118             foreach($hours as $h){
119                 $date = new DateTime($d. ' ' . $h, new DateTimeZone($this->tz));
120                 print_R(ini_get('date.timezone'));exit;
121                 $date->setTimezone(new DateTimeZone(ini_get('date.timezone')));
122                 $ret[] = $date->format('Y-m-d H:i:s');
123             }
124         }
125         return $ret;
126     }
127     
128     function generateNotifications()
129     {
130         //DB_DataObject::debugLevel(1);
131         $w = DB_DataObject::factory($this->tableName());
132         $w->find();
133         
134         while($w->fetch()){
135             $w->generateNotificationsSingle();
136         
137         }
138     }
139     
140     
141     function generateNotificationsSingle()
142     {
143         
144
145         $notifytimes = $this->notifyTimes(2);
146         //print_R($notifytimes);
147         
148         $newSearch = DB_DataObject::factory('core_notify');
149         $newSearch->whereAdd( 'act_start > NOW()');
150         $newSearch->recur_id = $this->id;
151         $old = $newSearch->fetchAll('act_start', 'id');
152         // returns array('2012-12-xx'=>12, 'date' => id....)
153
154         
155         
156         foreach($notifytimes as $time){
157            
158             if (isset($old[$time])) {
159                 // we already have it...
160                 
161                 $oo = DB_DataObject::Factory('core_notify');
162                 $oo->get($old[$time]);
163                 $oc = clone($oo);
164                 $oo->evtype = $this->method()->name;
165                 $oo->update($oc);
166                 
167                 unset($old[$time]);
168                 continue;
169             }
170             if (strtotime($time) < time()) {
171                 // will not get deleted..
172                 //echo "SKIP BEFORE NOW";
173                 unset($old[$time]);
174                continue;
175             }
176             // do not have a notify event... creat it..
177             $add = DB_DataObject::factory('core_notify');
178             $add->setFrom(array(
179                 "recur_id" => $this->id,
180                 "act_start" => $time,
181                 "act_when" => $time,
182                 "person_id" => $this->person_id,
183                 "onid" => $this->onid,
184                 "ontable" => $this->ontable,
185                 'evtype' => $this->method()->name,
186             ));
187             $add->insert();
188         }
189         foreach($old as $date => $id ) {
190                 $del = DB_DataObject::factory('core_notify');
191                 $del->get($id);
192                 $del->delete();
193         }
194         //echo("UPDATED");
195
196     }
197     
198     function onUpdate($old, $request,$roo)
199     {
200         $this->generateNotificationsSingle();
201         
202     }
203     function onInsert($request,$roo)
204     {
205         $this->generateNotificationsSingle();
206         
207     }
208     function beforeDelete($dependants_array, $roo)
209     {
210         $n = DB_DataObject::Factory("core_notify");
211         $n->recur_id = $this->id;
212         $n->whereAdd('act_start > NOW() OR act_when > NOW()');
213         // should delete old events that have not occurred...
214         $n->delete(DB_DATAOBJECT_WHEREADD_ONLY);
215     }
216     
217 }