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     public $recur_id;                       //INT(11) not_null
16     
17     public $dtstart;                         // datetime(19)  not_null binary
18     public $dtend;                           // datetime(19)  not_null binary
19     public $tz;                              // real(6)  not_null
20     
21     public $updated_dt;                      // datetime(19)  not_null binary
22     
23     public $last_applied_dt;                 // datetime(19)  not_null binary
24 //    public $max_applied_dtl
25     public $freq; //  varchar(8) NOT NULL;
26     public $freq_day; // text NOT NULL;
27     public $freq_hour; // text
28     
29     public $onid;                            // int(11)  not_null
30     public $ontable;                         // string(128)  not_null
31     public $last_event_id;                   // int(11)  
32     public $method;                         // string(128)  not_null
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"), 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         $ret = array();
67         $hours = empty($this->freq_hour) ? array() : array_unique(json_decode($this->freq_hour));
68         $days = empty($this->freq_day) ? array() : json_decode($this->freq_day);
69         
70         //days to use are = MON FRI SUN
71         
72         //ARE there events on these day in advance days in the future?
73         //TODAY = 25th of may (FRI)
74         //TODAY+1  = 26th  (SAT)
75         //TODAY +2 = 27th = SUN (2== advance)
76         //foreeach day in the future upto >>> advance <<< days?
77             
78          // - does an event occur on this day?
79         //    -YES - then we will generate an event for it.
80          //   -NO nothing happens..
81         $usedays = array();
82         for (  $i =0; $i < $advance +1; $i++) {
83             $ut = strtotime("NOW + $i DAYS");
84             $day = strtoupper(date("D", $ut));
85             if (in_array($day, $days)) {
86                 $usedays[] = date("Y-m-d", $ut);
87             }
88         }
89                 
90         //print_r($this);
91         
92         
93         foreach($usedays as $d){
94             foreach($hours as $h){
95                 $date = new DateTime($d. ' ' . $h, new DateTimeZone($this->tz));
96                 $date->setTimezone(new DateTimeZone(ini_get('date.timezone')));
97                 $ret[] = $date->format('Y-m-d H:i:s');
98             }
99         }
100         return $ret;
101     }
102     
103     function generateNotifications()
104     {
105         //DB_DataObject::debugLevel(1);
106         $w = DB_DataObject::factory($this->tableName());
107         $w->find();
108         
109         while($w->fetch()){
110             $w->generateNotificationsSingle();
111         
112         }
113     }
114     
115     
116     function generateNotificationsSingle()
117     {
118         
119
120         $notifytimes = $this->notifyTimes(2);
121         print_R($notifytimes);exit;
122         
123         $newSearch = DB_DataObject::factory('core_notify');
124         $newSearch->whereAdd( 'act_start > NOW()');
125         $newSearch->recur_id = $this->id;
126         $old = $newSearch->fetchAll('act_start', 'id');
127         // returns array('2012-12-xx'=>12, 'date' => id....)
128
129         
130         foreach($notifytimes as $time){
131             if (strtotime($time) < time()) {
132                 // will not get deleted..
133                 echo "SKIP BEFORE NOW";
134                 unset($old[$time]);
135                 continue;
136             }
137             if (isset($old[$time])) {
138                 // we already have it...
139                 unset($old[$time]);
140                 continue;
141             }
142
143             // do not have a notify event... creat it..
144             $add = DB_DataObject::factory('core_notify');
145             $add->setFrom(array(
146                 "recur_id" => $this->id,
147                 "act_start" => $time,
148                 "act_when" => $time,
149                 "person_id" => $this->person_id,
150                 "onid" => $this->onid,
151                 "ontable" => $this->ontable,
152                 'evtype' => $this->method,
153             ));
154             $add->insert();
155         }
156         foreach($old as $date => $id ) {
157                 $del = DB_DataObject::factory('core_notify');
158                 $del->get($id);
159                 $del->delete();
160         }
161
162     }
163     
164     function onUpdate($old, $request,$roo)
165     {
166         $this->generateNotificationsSingle();
167         
168     }
169     function onInsert($request,$roo)
170     {
171         $this->generateNotificationsSingle();
172         
173     }
174     function beforeDelete($dependants_array, $roo)
175     {
176         $n = DB_DataObject::Factory("core_notify");
177         $n->recur_id = $this->id;
178         $n->whereAdd('act_start > NOW() OR act_when > NOW()');
179         // should delete old events that have not occurred...
180         $n->delete(DB_DATAOBJECT_WHEREADD_ONLY);
181     }
182 }