sync
[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 = array_unique(json_decode($this->freq_hour));
68         $days = json_decode($this->freq_day);
69         
70         foreach($days as $d){
71             foreach($hours as $h){
72                 $date = new DateTime(date('Y-m-d', strtotime($d)) . ' ' . $h, new DateTimeZone($this->tz));
73                 $date->setTimezone(new DateTimeZone('Asia/Hong_Kong'));
74                 $ret[] = $date->format('Y-m-d H:i:s');
75             }
76         }
77         return $ret;
78     }
79     
80     function generateNotifications(){
81         
82         //DB_DataObject::debugLevel(1);
83         $w = DB_DataObject::factory($this->tableName());
84         $w->find();
85         
86         while($w->fetch()){
87             $notifytimes = $w->notifyTimes(2);
88             $newSearch = DB_DataObject::factory('core_notify');
89             $newSearch->whereAdd( 'act_start > NOW() and act_start < NOW() + INTERVAL 2 DAY');
90             $newSearch->recur_id = $w->id;
91             $old = $newSearch->fetchAll('act_start', 'id');
92             // returns array('2012-12-xx'=>12, 'date' => id....)
93             
94  
95             foreach($notifytimes as $time){
96                 if (strtotime($time) < time()) {
97                     continue;
98                 }
99                 if (isset($old[$time])) {
100                     // we already have it...
101                     unset($old[$time]);
102                     continue;
103                 }
104                 
105                 // do not have a notify event... creat it..
106                 $add = DB_DataObject::factory('core_notify');
107                 $add->setFrom(array(
108                     "recur_id" => $w->id,
109                     "act_start" => $time,
110                     "act_when" => $time,
111                     "person_id" => $w->person_id,
112                     "onid" => $w->onid,
113                     "ontable" => $w->ontable
114                 ));
115                 $add->insert();
116             }
117             foreach($old as $date => $id ) {
118                  $del = DB_DataObject::factory('core_notify');
119                  $del->get($id);
120                  $del->delete();
121             }
122  
123         }
124     }
125     
126 }