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