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