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         
69         
70         
71         if($this->freq_day){
72             $hours = json_decode($this->freq_hour);
73             //$dayAry = json_decode($this->freq_day);
74             if (!$hours) {
75                 $hours = array(date('H:i', strtotime($this->dtstart)));
76             }
77
78             $days = json_decode($this->freq_day);
79
80             for ($day = date('Y-m-d', strtotime($start));
81                     strtotime($day) < strtotime($end);
82                     $day = date('Y-m-d', strtotime("$day + 1 DAY")))
83             {
84                 // skip days not accounted for..
85                 if (!in_array(date('N', strtotime($day)), $days)) {
86                     continue;
87                 }
88
89                 foreach($hours as $h) {
90                     $hh = strpos($h,":") > 0 ? $h : "$H:00";
91                     $ret[] = $day . ' ' . $hh;
92                 }
93             }
94
95             return $this->applyTimezoneToList($ret);
96         }
97         if($this->freq_hour){
98             // happens every day based on freq_hour.
99             $hours = json_decode($this->freq_hour);
100             for ($day = date('Y-m-d', strtotime($start));
101                     strtotime($day) < strtotime($end);
102                     $day = date('Y-m-d', strtotime("$day + 1 DAY")))
103             {
104                 foreach($hours as $h) {
105                     $hh = strpos($h,":") > 0 ? $h : "$H:00";
106                     $ret[] = $day . ' ' . $hh;
107                 }
108             }
109             return $this->applyTimezoneToList($ret);
110         }
111 //        switch($this->freq) {
112 //            case 'HOURLY':
113 //                // happens every day based on freq_hour.
114 //                $hours = explode(',', $this->freq_hour);
115 //                for ($day = date('Y-m-d', strtotime($start));
116 //                        strtotime($day) < strtotime($end);
117 //                        $day = date('Y-m-d', strtotime("$day + 1 DAY")))
118 //                {
119 //                    foreach($hours as $h) {
120 //                        $hh = strpos($h,":") > 0 ? $h : "$H:00";
121 //                        $ret[] = $day . ' ' . $hh;
122 //                    }
123 //                }
124 //                return $this->applyTimezoneToList($ret);
125 //                
126 //            case 'DAILY':
127 //                $hours = explode(',', $this->freq_hour);
128 //                if (!$hours) {
129 //                    $hours = array(date('H:i', strtotime($this->dtstart)));
130 //                }
131 //                
132 //                $days = explode(','. $this->freq_day);
133 //                
134 //                for ($day = date('Y-m-d', strtotime($start));
135 //                        strtotime($day) < strtotime($end);
136 //                        $day = date('Y-m-d', strtotime("$day + 1 DAY")))
137 //                {
138 //                    // skip days not accounted for..
139 //                    if (!in_array(date('N', strtotime($day)), $days)) {
140 //                        continue;
141 //                    }
142 //                    
143 //                    foreach($hours as $h) {
144 //                        $hh = strpos($h,":") > 0 ? $h : "$H:00";
145 //                        $ret[] = $day . ' ' . $hh;
146 //                    }
147 //                }
148 //                
149 //                return $this->applyTimezoneToList($ret);
150 //                
151 //                
152 //            case 'MONTHLY': // ignored..
153 //            case 'YEARLY': // ignored..
154 //                break;
155 //            
156 //        }
157          
158     }
159     function applyTimezoneToList($ar)
160     {
161         $ret = array();
162         
163         $tz = explode($this->tz, ":");
164         if ($tz < 0) {
165             
166         }
167         $append = ($tz[0] < 0) ? " - " : " + ";
168         
169         $append .= abs($tz[0]) . " HOURS";
170         if (!empty($tz[1])) {
171             $append .= $tz[1] . " MINUTES";
172         }
173         
174         
175         foreach($ar as $a) {
176             $ret[] = date('Y-m-d H:i', strtotime($a . $append));
177             
178         }
179         return $ret;
180         
181         
182     }
183     
184     function generateNotifications(){
185         //$this->notifytimes(2);
186         //DB_DataObject::debugLevel(1);
187         
188         $w = DB_DataObject::factory($this->tableName());
189         //$this->notifytimes(2);
190         $w->find();
191         //$test = $w->fetchAll();
192         
193         //$test = $this->notifytimes(2);
194         
195         //$test = array();
196         while($w->fetch()){
197             
198             $notifytime = $w->notifyTimes(2);
199             var_dump($notifytime);
200 //            $this->id = $w->id;
201 //            $this->person_id = $w->person_id;
202 //            $this->dtstart = $w->dtstart;
203 //            $this->dtend = $w->dtend;
204 //            $this->tz = $w->tz;
205 //            $this->updated_dt = $w->updated_dt;
206 //            $this->last_applied_dt = $w->last_applied_dt;
207 //            $this->freq = $w->freq;
208 //            $this->freq_day = $w->freq_day;
209 //            $this->freq_hour = $w->freq_hour;
210 //            $this->onid = $w->onid;
211 //            $this->ontable = $w->ontable;
212 //            $this->last_event_id = $w->last_event_id;
213 //            $this->method = $w->method;
214             //$this->dtstart = $w->dtstart;
215             //$this->dtend = $w->dtend;
216             //$this = clone($w);
217             //$w->notifytimes(2);
218             //var_dump($w->notifytimes(2));
219         }
220 //        foreach($test as $item){
221 //            error_log($item);
222 //        }
223         
224     }
225     
226 }