77ff4e1645245736d7e871b3ec757c3af6b7f193
[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     public $dtstart;                         // datetime(19)  not_null binary
16     public $dtend;                           // datetime(19)  not_null binary
17     public $tz;                              // real(6)  not_null
18     public $updated_dt;                      // datetime(19)  not_null binary
19     public $last_applied_dt;                 // datetime(19)  not_null binary
20
21     public $freq; //  varchar(8) NOT NULL;
22     public $freq_day; // text NOT NULL;
23     public $freq_hour; // text 
24     ###END_AUTOCODE
25     
26     
27     /*
28       freq =  DAILY | YEARLY | MONTHLY
29         *
30         *        THESE ARE EXCLUSIVE..
31         *        freq_day =  1,2,3,4,5" - day number.. or dayofmonth USES TIME FROM DTSTART (unless hours are speced.)
32         *        >> must..
33         *        freq_hourly = 'what hours' << OR IF EMPTY USES TIME FROM DTSTART
34         *
35     /* the code above is auto generated do not remove the tag below */
36     
37     
38     function notifytimesRange($advance) {
39        
40         $start = date('Y-m-d H:i:s', max(strtotime("NOW - 24 HOURS"), sttotime($this->dtstart)));
41         $end  = date('Y-m-d H:i:s', min(strtotime("NOW  + $advance DAYS"), sttotime($this->dtend)));
42     
43     }
44     
45     function notifytimes($advance)
46     {
47         
48         // make a list of datetimes when notifies need to be generated for.
49         // it starts 24 hours ago.. or when dtstart
50         list($start, $end) = $this->notifytimesRange($advance);
51         if (strtotime($start) > strtotime($end)) {
52             return array(); // no data..
53         }
54         
55         
56         
57         switch($this->freq) {
58             case 'HOURLY':
59                 
60                 
61                 
62                 // happens every day based on freq_hour.
63                 $hours = explode(',', $this->freq_hour);
64                 for ($day = date('Y-m-d', strtotime($start));
65                         strtotime($day) < strtotime($end);
66                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
67                 {
68                     foreach($hours as $h) {
69                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
70                         $ret[] = $day . ' ' . $hh;
71                     }
72                 }
73                 return $this->applyTimezoneToList($ret);
74                 
75             case 'DAILY':
76                 $hours = explode(',', $this->freq_hour);
77                 if (!$hours) {
78                     $hours = array(date('H:i', strtotime($this->dtstart)));
79                 }
80                 
81                 $days = explode(','. $this->freq_day);
82                 
83                 for ($day = date('Y-m-d', strtotime($start));
84                         strtotime($day) < strtotime($end);
85                         $day = date('Y-m-d', strtotime("$day + 1 DAY")))
86                 {
87                     // skip days not accounted for..
88                     if (!in_array(date('N', strtotime($day)), $days)) {
89                         continue;
90                     }
91                     
92                     foreach($hours as $h) {
93                         $hh = strpos($h,":") > 0 ? $h : "$H:00";
94                         $ret[] = $day . ' ' . $hh;
95                     }
96                 }
97                 
98                 return $this->applyTimezoneToList($ret);
99                 
100                 
101             case 'MONTHLY': // ignored..
102             case 'YEARLY': // ignored..
103                 break;
104             
105         }
106          
107     }
108     function applyTimezoneToList($ar)
109     {
110         $ret = array();
111         
112         $tz = explode($this->tz, ":");
113         if ($tz < 0) {
114             
115         }
116         $append = ($tz[0] < 0) ? " - " : " + ";
117         
118         $append .= abs($tz[0]) . " HOURS";
119         if (!empty($tz[1])) {
120             $append .= $tz[1] . " MINUTES";
121         }
122         
123         
124         foreach($ar as $a) {
125             $ret[] = date('Y-m-d H:i', strtotime($a . $append));
126             
127         }
128         return $ret;
129         
130         
131     }
132     
133     
134 }