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         
61         list($start, $end) = $this->notifytimesRange($advance);
62         
63         if (strtotime($start) > strtotime($end)) {
64             return array(); // no data..
65         }
66         
67         
68         
69         switch($this->freq) {
70             case 'HOURLY':
71                 
72                 
73                 
74                 // happens every day based on freq_hour.
75                 $hours = explode(',', $this->freq_hour);
76                 for ($day = date('Y-m-d', strtotime($start));
77                         strtotime($day) < strtotime($end);
78                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
79                 {
80                     foreach($hours as $h) {
81                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
82                         $ret[] = $day . ' ' . $hh;
83                     }
84                 }
85                 return $this->applyTimezoneToList($ret);
86                 
87             case 'DAILY':
88                 $hours = explode(',', $this->freq_hour);
89                 if (!$hours) {
90                     $hours = array(date('H:i', strtotime($this->dtstart)));
91                 }
92                 
93                 $days = explode(','. $this->freq_day);
94                 
95                 for ($day = date('Y-m-d', strtotime($start));
96                         strtotime($day) < strtotime($end);
97                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
98                 {
99                     // skip days not accounted for..
100                     if (!in_array(date('N', strtotime($day)), $days)) {
101                         continue;
102                     }
103                     
104                     foreach($hours as $h) {
105                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
106                         $ret[] = $day . ' ' . $hh;
107                     }
108                 }
109                 
110                 return $this->applyTimezoneToList($ret);
111                 
112                 
113             case 'MONTHLY': // ignored..
114             case 'YEARLY': // ignored..
115                 break;
116             
117         }
118          
119     }
120     function applyTimezoneToList($ar)
121     {
122         $ret = array();
123         
124         $tz = explode($this->tz, ":");
125         if ($tz < 0) {
126             
127         }
128         $append = ($tz[0] < 0) ? " - " : " + ";
129         
130         $append .= abs($tz[0]) . " HOURS";
131         if (!empty($tz[1])) {
132             $append .= $tz[1] . " MINUTES";
133         }
134         
135         
136         foreach($ar as $a) {
137             $ret[] = date('Y-m-d H:i', strtotime($a . $append));
138             
139         }
140         return $ret;
141         
142         
143     }
144     
145     function generateNotifications(){
146         //$this->notifytimes(2);
147         DB_DataObject::debugLevel(1);
148         $w = DB_DataObject::factory($this->__table);
149         //$this->notifytimes(2);
150         $w->find(true);
151         $test = $w->fetchAll();
152         
153         //$test = $this->notifytimes(2);
154         $test = array();
155         while($w->fetch()){
156             $test[] = clone($w);
157             error_log($w);
158         }
159 //        foreach($test as $item){
160 //            error_log($item);
161 //        }
162         
163     }
164     
165 }