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;                         // string(128)  not_null
32     
33     
34     ###END_AUTOCODE
35     
36     
37     /*
38       freq =  DAILY | YEARLY | MONTHLY
39         *
40         *        THESE ARE EXCLUSIVE..
41         *        freq_day =  1,2,3,4,5" - day number.. or dayofmonth USES TIME FROM DTSTART (unless hours are speced.)
42         *        >> must..
43         *        freq_hourly = 'what hours' << OR IF EMPTY USES TIME FROM DTSTART
44         *
45     /* the code above is auto generated do not remove the tag below */
46     
47     
48     function notifytimesRange($advance) {
49         
50         $start = date('Y-m-d H:i:s', max(strtotime("NOW - 24 HOURS"), strtotime($this->dtstart)));
51         $end  = date('Y-m-d H:i:s', min(strtotime("NOW  + $advance DAYS"), strtotime($this->dtend)));
52         
53     }
54     
55     function notifytimes($advance)
56     {
57         
58         // make a list of datetimes when notifies need to be generated for.
59         // it starts 24 hours ago.. or when dtstart
60         //print_r($this);
61         list($start, $end) = $this->notifytimesRange($advance);
62         
63         if (strtotime($start) > strtotime($end)) {
64             return array(); // no data..
65         }
66         $ret = array();
67         print_r(json_decode($this->freq_day));
68         if($this->freq_day){
69             
70         }
71         if($this->freq_hour){
72             
73         }
74         switch($this->freq) {
75             case 'HOURLY':
76                 // happens every day based on freq_hour.
77                 $hours = explode(',', $this->freq_hour);
78                 for ($day = date('Y-m-d', strtotime($start));
79                         strtotime($day) < strtotime($end);
80                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
81                 {
82                     foreach($hours as $h) {
83                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
84                         $ret[] = $day . ' ' . $hh;
85                     }
86                 }
87                 return $this->applyTimezoneToList($ret);
88                 
89             case 'DAILY':
90                 $hours = explode(',', $this->freq_hour);
91                 if (!$hours) {
92                     $hours = array(date('H:i', strtotime($this->dtstart)));
93                 }
94                 
95                 $days = explode(','. $this->freq_day);
96                 
97                 for ($day = date('Y-m-d', strtotime($start));
98                         strtotime($day) < strtotime($end);
99                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
100                 {
101                     // skip days not accounted for..
102                     if (!in_array(date('N', strtotime($day)), $days)) {
103                         continue;
104                     }
105                     
106                     foreach($hours as $h) {
107                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
108                         $ret[] = $day . ' ' . $hh;
109                     }
110                 }
111                 
112                 return $this->applyTimezoneToList($ret);
113                 
114                 
115             case 'MONTHLY': // ignored..
116             case 'YEARLY': // ignored..
117                 break;
118             
119         }
120          
121     }
122     function applyTimezoneToList($ar)
123     {
124         $ret = array();
125         
126         $tz = explode($this->tz, ":");
127         if ($tz < 0) {
128             
129         }
130         $append = ($tz[0] < 0) ? " - " : " + ";
131         
132         $append .= abs($tz[0]) . " HOURS";
133         if (!empty($tz[1])) {
134             $append .= $tz[1] . " MINUTES";
135         }
136         
137         
138         foreach($ar as $a) {
139             $ret[] = date('Y-m-d H:i', strtotime($a . $append));
140             
141         }
142         return $ret;
143         
144         
145     }
146     
147     function generateNotifications(){
148         //$this->notifytimes(2);
149         //DB_DataObject::debugLevel(1);
150         
151         $w = DB_DataObject::factory($this->tableName());
152         //$this->notifytimes(2);
153         $w->find();
154         //$test = $w->fetchAll();
155         
156         //$test = $this->notifytimes(2);
157         
158         //$test = array();
159         while($w->fetch()){
160             
161             $notifytime = $w->notifyTimes(2);
162             var_dump($notifytime);
163 //            $this->id = $w->id;
164 //            $this->person_id = $w->person_id;
165 //            $this->dtstart = $w->dtstart;
166 //            $this->dtend = $w->dtend;
167 //            $this->tz = $w->tz;
168 //            $this->updated_dt = $w->updated_dt;
169 //            $this->last_applied_dt = $w->last_applied_dt;
170 //            $this->freq = $w->freq;
171 //            $this->freq_day = $w->freq_day;
172 //            $this->freq_hour = $w->freq_hour;
173 //            $this->onid = $w->onid;
174 //            $this->ontable = $w->ontable;
175 //            $this->last_event_id = $w->last_event_id;
176 //            $this->method = $w->method;
177             //$this->dtstart = $w->dtstart;
178             //$this->dtend = $w->dtend;
179             //$this = clone($w);
180             //$w->notifytimes(2);
181             //var_dump($w->notifytimes(2));
182         }
183 //        foreach($test as $item){
184 //            error_log($item);
185 //        }
186         
187     }
188     
189 }