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     public $start;
34     public $end;
35     
36     ###END_AUTOCODE
37     
38     
39     /*
40       freq =  DAILY | YEARLY | MONTHLY
41         *
42         *        THESE ARE EXCLUSIVE..
43         *        freq_day =  1,2,3,4,5" - day number.. or dayofmonth USES TIME FROM DTSTART (unless hours are speced.)
44         *        >> must..
45         *        freq_hourly = 'what hours' << OR IF EMPTY USES TIME FROM DTSTART
46         *
47     /* the code above is auto generated do not remove the tag below */
48     
49     
50     function notifytimesRange($advance) {
51         error_log($this->dtstart);
52         error_log($this->dtend);
53         $this->start = date('Y-m-d H:i:s', max(strtotime("NOW - 24 HOURS"), strtotime($this->dtstart)));
54         $this->end  = date('Y-m-d H:i:s', min(strtotime("NOW"), strtotime($this->dtend)));
55         error_log($this->start);
56         error_log($this->end);
57     
58     }
59     
60     function notifytimes($advance)
61     {
62         
63         // make a list of datetimes when notifies need to be generated for.
64         // it starts 24 hours ago.. or when dtstart
65         
66         $this->notifytimesRange($advance);
67         error_log($this->start);
68         error_log($this->end);
69         if (strtotime($this->start) > strtotime($this->end)) {
70             return array(); // no data..
71         }
72         
73         
74         
75         switch($this->freq) {
76             case 'HOURLY':
77                 
78                 
79                 
80                 // happens every day based on freq_hour.
81                 $hours = explode(',', $this->freq_hour);
82                 for ($day = date('Y-m-d', strtotime($start));
83                         strtotime($day) < strtotime($end);
84                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
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             case 'DAILY':
94                 $hours = explode(',', $this->freq_hour);
95                 if (!$hours) {
96                     $hours = array(date('H:i', strtotime($this->dtstart)));
97                 }
98                 
99                 $days = explode(','. $this->freq_day);
100                 
101                 for ($day = date('Y-m-d', strtotime($start));
102                         strtotime($day) < strtotime($end);
103                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
104                 {
105                     // skip days not accounted for..
106                     if (!in_array(date('N', strtotime($day)), $days)) {
107                         continue;
108                     }
109                     
110                     foreach($hours as $h) {
111                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
112                         $ret[] = $day . ' ' . $hh;
113                     }
114                 }
115                 
116                 return $this->applyTimezoneToList($ret);
117                 
118                 
119             case 'MONTHLY': // ignored..
120             case 'YEARLY': // ignored..
121                 break;
122             
123         }
124          
125     }
126     function applyTimezoneToList($ar)
127     {
128         $ret = array();
129         
130         $tz = explode($this->tz, ":");
131         if ($tz < 0) {
132             
133         }
134         $append = ($tz[0] < 0) ? " - " : " + ";
135         
136         $append .= abs($tz[0]) . " HOURS";
137         if (!empty($tz[1])) {
138             $append .= $tz[1] . " MINUTES";
139         }
140         
141         
142         foreach($ar as $a) {
143             $ret[] = date('Y-m-d H:i', strtotime($a . $append));
144             
145         }
146         return $ret;
147         
148         
149     }
150     
151     function generateNotifications(){
152         //$this->notifytimes(2);
153         $test = $this->notifytimes(2);
154         foreach($test as $item){
155             error_log($item);
156         }
157         
158     }
159     
160 }