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