3 * Table Definition for core_notify_recur
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_notify_recur extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
16 public $dtstart; // datetime(19) not_null binary
17 public $dtend; // datetime(19) not_null binary
18 public $tz; // real(6) not_null
20 public $updated_dt; // datetime(19) not_null binary
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
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
34 public $recur_id; //INT(11) not_null
38 //NOTE recur_id and method are depricated.
42 freq = DAILY | YEARLY | MONTHLY
44 * THESE ARE EXCLUSIVE..
45 * freq_day = 1,2,3,4,5" - day number.. or dayofmonth USES TIME FROM DTSTART (unless hours are speced.)
47 * freq_hourly = 'what hours' << OR IF EMPTY USES TIME FROM DTSTART
49 /* the code above is auto generated do not remove the tag below */
51 function applyFilters($q, $au, $roo)
54 if (isset($q['query']['person_id_name']) ) {
55 $this->whereAdd( "join_person_id_id.name LIKE '{$this->escape($q['query']['person_id_name'])}%'");
64 function notifytimesRange($advance) {
66 $start = date('Y-m-d H:i:s', max(strtotime("NOW"), strtotime($this->dtstart)));
67 $end = min( new DateTime("NOW + $advance DAYS"), new DateTime($this->dtend ) )->format('Y-m-d H:i:s');
69 return array($start, $end);
74 $e = DB_DataObject::Factory('core_enum');
75 $e->get($this->method_id);
79 function notifytimes($advance)
82 // make a list of datetimes when notifies need to be generated for.
83 // it starts 24 hours ago.. or when dtstart
85 list($start, $end) = $this->notifytimesRange($advance);
86 //var_dump(array($start, $end));
89 if (strtotime($start) > strtotime($end)) {
90 return array(); // no data..
93 $hours = empty($this->freq_hour) ? array() : array_unique(json_decode($this->freq_hour));
94 $days = empty($this->freq_day) ? array() : json_decode($this->freq_day);
96 //days to use are = MON FRI SUN
98 //ARE there events on these day in advance days in the future?
99 //TODAY = 25th of may (FRI)
100 //TODAY+1 = 26th (SAT)
101 //TODAY +2 = 27th = SUN (2== advance)
102 //foreeach day in the future upto >>> advance <<< days?
104 // - does an event occur on this day?
105 // -YES - then we will generate an event for it.
106 // -NO nothing happens..
108 for ( $i =0; $i < $advance +1; $i++) {
109 $ut = strtotime("NOW + $i DAYS");
110 $day = strtoupper(date("D", $ut));
111 if (in_array($day, $days)) {
112 $usedays[] = date("Y-m-d", $ut);
119 foreach($usedays as $d){
120 foreach($hours as $h){
121 $date = new DateTime($d. ' ' . $h, new DateTimeZone($this->tz));
122 $tz= ini_get('date.timezone');
124 $date->setTimezone(new DateTimeZone($tz));
127 $ret[] = $date->format('Y-m-d H:i:s');
133 function generateNotifications()
135 //DB_DataObject::debugLevel(1);
136 $w = DB_DataObject::factory($this->tableName());
140 $w->generateNotificationsSingle();
146 function generateNotificationsSingle()
150 $notifytimes = $this->notifyTimes(2);
151 //echo "{$this->person()->email}\n";
152 //print_R($notifytimes);
154 $newSearch = DB_DataObject::factory('core_notify');
155 $newSearch->whereAdd( 'act_start > NOW()');
156 $newSearch->recur_id = $this->id;
157 $old = $newSearch->fetchAll('act_start', 'id');
158 // returns array('2012-12-xx'=>12, 'date' => id....)
161 foreach($notifytimes as $time){
163 if (isset($old[$time])) {
164 // we already have it...
165 $oo = DB_DataObject::Factory('core_notify');
166 $oo->get($old[$time]);
168 $oo->evtype = $this->method()->name;
175 if (strtotime($time) < time()) { // should not happen, just in case...
179 // do not have a notify event... creat it..
180 $add = DB_DataObject::factory('core_notify');
182 "recur_id" => $this->id,
183 "act_start" => $time,
185 "person_id" => $this->person_id,
186 "onid" => $this->onid,
187 "ontable" => $this->ontable,
188 'evtype' => $this->method()->name,
192 foreach($old as $date => $id ) {
193 $del = DB_DataObject::factory('core_notify');
203 $p = DB_DAtaObject::factory('Person');
204 $p->get($this->person_id);
208 function onUpdate($old, $request,$roo)
210 $this->generateNotificationsSingle();
213 function onInsert($request,$roo)
215 $this->generateNotificationsSingle();
218 function beforeDelete($dependants_array, $roo)
220 $n = DB_DataObject::Factory("core_notify");
221 $n->recur_id = $this->id;
222 $n->whereAdd('act_start > NOW() OR act_when > NOW()');
223 // should delete old events that have not occurred...
224 $n->delete(DB_DATAOBJECT_WHEREADD_ONLY);