DataObjects/Core_notify_recur.php
[Pman.Core] / DataObjects / Core_notify_recur.php
index a92a84b..8f1a787 100644 (file)
@@ -28,10 +28,14 @@ class Pman_Core_DataObjects_Core_notify_recur extends DB_DataObject
     public $onid;                            // int(11)  not_null
     public $ontable;                         // string(128)  not_null
     public $last_event_id;                   // int(11)  
-    public $method;                         // string(128)  not_null
+    public $method;                         // depricated.
+    public $method_id;                         // string(128)  not_null
     
+    public $recur_id;                       //INT(11) not_null
+
     
     ###END_AUTOCODE
+    //NOTE recur_id and method are depricated.
     
     
     /*
@@ -44,14 +48,33 @@ class Pman_Core_DataObjects_Core_notify_recur extends DB_DataObject
         *
     /* the code above is auto generated do not remove the tag below */
     
-    
+    function applyFilters($q, $au, $roo)
+    {
+        
+        if (isset($q['query']['person_id_name']) ) {
+            $this->whereAdd( "join_person_id_id.name LIKE '{$this->escape($q['query']['person_id_name'])}%'");
+             
+        }
+         
+        
+         
+        
+        
+    }
     function notifytimesRange($advance) {
         
-        $start = date('Y-m-d H:i:s', max(strtotime("NOW - 24 HOURS"), strtotime($this->dtstart)));
-        $end  = date('Y-m-d H:i:s', min(strtotime("NOW  + $advance DAYS"), strtotime($this->dtend)));
+        $start = date('Y-m-d H:i:s', max(strtotime("NOW"), strtotime($this->dtstart)));
+        $end  = date('Y-m-d H:i:s', min(strtotime("NOW + $advance DAYS"), strtotime($this->dtend)));
         
     }
     
+    function method()
+    {
+        $e = DB_DataObject::Factory('core_enum');
+        $e->get($this->method_id);
+        return $e;
+    }
+    
     function notifytimes($advance)
     {
         
@@ -63,67 +86,172 @@ class Pman_Core_DataObjects_Core_notify_recur extends DB_DataObject
         if (strtotime($start) > strtotime($end)) {
             return array(); // no data..
         }
+        
         $ret = array();
-        $hours = array_unique(json_decode($this->freq_hour));
-        $days = json_decode($this->freq_day);
-        foreach($days as $d){
-            foreach($hours as $h){
-                $ret[] = date('Y-m-d', strtotime($d)) . ' ' . $h;
+        $hours = empty($this->freq_hour) ? array() : array_unique(json_decode($this->freq_hour));
+        $days = empty($this->freq_day) ? array() : json_decode($this->freq_day);
+        
+        //days to use are = MON FRI SUN
+        
+        //ARE there events on these day in advance days in the future?
+        //TODAY = 25th of may (FRI)
+        //TODAY+1  = 26th  (SAT)
+        //TODAY +2 = 27th = SUN (2== advance)
+        //foreeach day in the future upto >>> advance <<< days?
+            
+         // - does an event occur on this day?
+        //    -YES - then we will generate an event for it.
+         //   -NO nothing happens..
+        $usedays = array();
+        for (  $i =0; $i < $advance +1; $i++) {
+            $ut = strtotime("NOW + $i DAYS");
+            $day = strtoupper(date("D", $ut));
+            if (in_array($day, $days)) {
+                $usedays[] = date("Y-m-d", $ut);
             }
         }
-        return $this->applyTimezoneToList($ret);
-    }
-    function applyTimezoneToList($ar)
-    {
-        $ret = array();
-        foreach($ar as $a) {
-            $date = new DateTime($a);
-            $date->setTimezone(new DateTimeZone($this->tz));
-            $ret[] = $date->format('Y-m-d H:i');
+                
+        //print_r($this);
+        
+        
+        foreach($usedays as $d){
+            foreach($hours as $h){
+                $date = new DateTime($d. ' ' . $h, new DateTimeZone($this->tz));
+                $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
+                $ret[] = $date->format('Y-m-d H:i:s');
+            }
         }
         return $ret;
     }
     
-    function generateNotifications(){
-        //$this->notifytimes(2);
+    function generateNotifications()
+    {
         //DB_DataObject::debugLevel(1);
-        
         $w = DB_DataObject::factory($this->tableName());
-        //$this->notifytimes(2);
+        $w->id = 117;
         $w->find();
-        //$test = $w->fetchAll();
-        
-        //$test = $this->notifytimes(2);
         
-        //$test = array();
         while($w->fetch()){
+            $w->generateNotificationsSingle();
+        
+        }
+    }
+    
+    
+    function generateNotificationsSingle()
+    {
+        
+
+        $notifytimes = $this->notifyTimes(2);
+        print_R($notifytimes);exit;
+        
+        $newSearch = DB_DataObject::factory('core_notify');
+        $newSearch->whereAdd( 'act_start > NOW()');
+        $newSearch->recur_id = $this->id;
+        $old = $newSearch->fetchAll('act_start', 'id');
+        // returns array('2012-12-xx'=>12, 'date' => id....)
+
+        
+        
+        foreach($notifytimes as $time){
+           
+            if (isset($old[$time])) {
+                // we already have it...
+                
+                $oo = DB_DataObject::Factory('core_notify');
+                $oo->get($old[$time]);
+                $oc = clone($oo);
+                $oo->evtype = $this->method()->name;
+                $oo->update($oc);
+                
+                unset($old[$time]);
+                continue;
+            }
+            if (strtotime($time) < time()) {
+                // will not get deleted..
+                //echo "SKIP BEFORE NOW";
+                unset($old[$time]);
+               continue;
+            }
+            // do not have a notify event... creat it..
+            $add = DB_DataObject::factory('core_notify');
+            $add->setFrom(array(
+                "recur_id" => $this->id,
+                "act_start" => $time,
+                "act_when" => $time,
+                "person_id" => $this->person_id,
+                "onid" => $this->onid,
+                "ontable" => $this->ontable,
+                'evtype' => $this->method()->name,
+            ));
+            $add->insert();
+        }
+        foreach($old as $date => $id ) {
+                $del = DB_DataObject::factory('core_notify');
+                $del->get($id);
+                $del->delete();
+        }
+        //echo("UPDATED");
+
+    }
+    
+    function onUpdate($old, $request,$roo)
+    {
+        $this->generateNotificationsSingle();
+        
+    }
+    function onInsert($request,$roo)
+    {
+        $this->generateNotificationsSingle();
+        
+    }
+    function beforeDelete($dependants_array, $roo)
+    {
+        $n = DB_DataObject::Factory("core_notify");
+        $n->recur_id = $this->id;
+        $n->whereAdd('act_start > NOW() OR act_when > NOW()');
+        // should delete old events that have not occurred...
+        $n->delete(DB_DATAOBJECT_WHEREADD_ONLY);
+    }
+    
+    function toRooSingleArray($authUser, $request)
+    {
+        $ret = $this->toArray();
+        
+        if(!empty($ret['keyword_filters'])){
+            $keywords = array_unique(array_filter(explode(',', $ret['keyword_filters'])));
+            
+            $clipping_keywords = DB_DataObject::factory('clipping_keywords');
+            $clipping_keywords->whereAddIn('id', $keywords, 'int');
             
-            $notifytime = $w->notifyTimes(2);
-            var_dump($notifytime);
-//            $this->id = $w->id;
-//            $this->person_id = $w->person_id;
-//            $this->dtstart = $w->dtstart;
-//            $this->dtend = $w->dtend;
-//            $this->tz = $w->tz;
-//            $this->updated_dt = $w->updated_dt;
-//            $this->last_applied_dt = $w->last_applied_dt;
-//            $this->freq = $w->freq;
-//            $this->freq_day = $w->freq_day;
-//            $this->freq_hour = $w->freq_hour;
-//            $this->onid = $w->onid;
-//            $this->ontable = $w->ontable;
-//            $this->last_event_id = $w->last_event_id;
-//            $this->method = $w->method;
-            //$this->dtstart = $w->dtstart;
-            //$this->dtend = $w->dtend;
-            //$this = clone($w);
-            //$w->notifytimes(2);
-            //var_dump($w->notifytimes(2));
+            $li = array();
+            
+            foreach ($clipping_keywords->fetchAll('id', 'keyword') as $k => $v){
+                $li[] = array(
+                    'id' => $k,
+                    'keyword' => $v
+                );
+            }
+            
+            $ret['keywords'] = json_encode($li);
         }
-//        foreach($test as $item){
-//            error_log($item);
-//        }
         
+        return $ret;
     }
     
+    function toRooArray($request)
+    {
+        $ret = $this->toArray();
+        
+        if(!empty($ret['keyword_filters'])){
+            $keywords = array_unique(array_filter(explode(',', $ret['keyword_filters'])));
+            
+            $clipping_keywords = DB_DataObject::factory('clipping_keywords');
+            $clipping_keywords->whereAddIn('id', $keywords, 'int');
+            
+            $ret['keywords'] = implode(',', $clipping_keywords->fetchAll('keyword'));
+        }
+        
+        return $ret;
+    }
 }