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