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;                         // depricated.
32     public $method_id;                         // string(128)  not_null
33     
34     public $recur_id;                       //INT(11) not_null
35
36     
37     ###END_AUTOCODE
38     //NOTE recur_id and method are depricated.
39     
40     
41     /*
42       freq =  DAILY | YEARLY | MONTHLY
43         *
44         *        THESE ARE EXCLUSIVE..
45         *        freq_day =  1,2,3,4,5" - day number.. or dayofmonth USES TIME FROM DTSTART (unless hours are speced.)
46         *        >> must..
47         *        freq_hourly = 'what hours' << OR IF EMPTY USES TIME FROM DTSTART
48         *
49     /* the code above is auto generated do not remove the tag below */
50     
51     
52     function notifytimesRange($advance) {
53         
54         $start = date('Y-m-d H:i:s', max(strtotime("NOW"), strtotime($this->dtstart)));
55         $end  = date('Y-m-d H:i:s', min(strtotime("NOW + $advance DAYS"), strtotime($this->dtend)));
56         
57     }
58     
59     function method()
60     {
61         $e = DB_DataObject::Factory('core_enum');
62         $e->get($this->method_id);
63         return $e;
64     }
65     
66     function notifytimes($advance)
67     {
68         
69         // make a list of datetimes when notifies need to be generated for.
70         // it starts 24 hours ago.. or when dtstart
71         
72         list($start, $end) = $this->notifytimesRange($advance);
73         
74         if (strtotime($start) > strtotime($end)) {
75             return array(); // no data..
76         }
77         $ret = array();
78         $hours = empty($this->freq_hour) ? array() : array_unique(json_decode($this->freq_hour));
79         $days = empty($this->freq_day) ? array() : json_decode($this->freq_day);
80         
81         //days to use are = MON FRI SUN
82         
83         //ARE there events on these day in advance days in the future?
84         //TODAY = 25th of may (FRI)
85         //TODAY+1  = 26th  (SAT)
86         //TODAY +2 = 27th = SUN (2== advance)
87         //foreeach day in the future upto >>> advance <<< days?
88             
89          // - does an event occur on this day?
90         //    -YES - then we will generate an event for it.
91          //   -NO nothing happens..
92         $usedays = array();
93         for (  $i =0; $i < $advance +1; $i++) {
94             $ut = strtotime("NOW + $i DAYS");
95             $day = strtoupper(date("D", $ut));
96             if (in_array($day, $days)) {
97                 $usedays[] = date("Y-m-d", $ut);
98             }
99         }
100                 
101         //print_r($this);
102         
103         
104         foreach($usedays as $d){
105             foreach($hours as $h){
106                 $date = new DateTime($d. ' ' . $h, new DateTimeZone($this->tz));
107                 $date->setTimezone(new DateTimeZone(ini_get('date.timezone')));
108                 $ret[] = $date->format('Y-m-d H:i:s');
109             }
110         }
111         return $ret;
112     }
113     
114     function generateNotifications()
115     {
116         //DB_DataObject::debugLevel(1);
117         $w = DB_DataObject::factory($this->tableName());
118         $w->find();
119         
120         while($w->fetch()){
121             $w->generateNotificationsSingle();
122         
123         }
124     }
125     
126     
127     function generateNotificationsSingle()
128     {
129         
130
131         $notifytimes = $this->notifyTimes(2);
132         ////print_R($notifytimes);
133         
134         $newSearch = DB_DataObject::factory('core_notify');
135         $newSearch->whereAdd( 'act_start > NOW()');
136         $newSearch->recur_id = $this->id;
137         $old = $newSearch->fetchAll('act_start', 'id');
138         // returns array('2012-12-xx'=>12, 'date' => id....)
139
140         
141         
142         foreach($notifytimes as $time){
143            
144             if (isset($old[$time])) {
145                 // we already have it...
146                 
147                 $oo = clone($old[$time]);
148                 $oo->evtype = $this->method()->name;
149                 $oo->update($old[$time]);
150                 
151                 unset($old[$time]);
152                 continue;
153             }
154             if (strtotime($time) < time()) {
155                 // will not get deleted..
156                 //echo "SKIP BEFORE NOW";
157                 unset($old[$time]);
158                continue;
159             }
160             // do not have a notify event... creat it..
161             $add = DB_DataObject::factory('core_notify');
162             $add->setFrom(array(
163                 "recur_id" => $this->id,
164                 "act_start" => $time,
165                 "act_when" => $time,
166                 "person_id" => $this->person_id,
167                 "onid" => $this->onid,
168                 "ontable" => $this->ontable,
169                 'evtype' => $this->method()->name,
170             ));
171             $add->insert();
172         }
173         foreach($old as $date => $id ) {
174                 $del = DB_DataObject::factory('core_notify');
175                 $del->get($id);
176                 $del->delete();
177         }
178         //echo("UPDATED");
179
180     }
181     
182     function onUpdate($old, $request,$roo)
183     {
184         $this->generateNotificationsSingle();
185         
186     }
187     function onInsert($request,$roo)
188     {
189         $this->generateNotificationsSingle();
190         
191     }
192     function beforeDelete($dependants_array, $roo)
193     {
194         $n = DB_DataObject::Factory("core_notify");
195         $n->recur_id = $this->id;
196         $n->whereAdd('act_start > NOW() OR act_when > NOW()');
197         // should delete old events that have not occurred...
198         $n->delete(DB_DATAOBJECT_WHEREADD_ONLY);
199     }
200 }