generate recurrsions based on exclude dates
authorpriyankakaran26 <priyanka.karan26@gmail.com>
Sun, 7 Sep 2014 13:34:14 +0000 (14:34 +0100)
committerdeepak-srivastava <deepak.srivastava.0303@gmail.com>
Fri, 3 Oct 2014 08:55:16 +0000 (09:55 +0100)
CRM/Core/BAO/RecurringEntity.php
CRM/Core/Form/RecurringEntity.php
CRM/Core/Page/AJAX/RecurringEntity.php

index 173674125d6e8137b2b740e51833f36f07dc19fa..d6edd0528032d89ba2747ddc2c9cac33631801ec 100644 (file)
@@ -403,14 +403,16 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
     return $dao;
   }  
   
-  static public function generateRecursions($recursionObj, $params=array())
+  static public function generateRecursions($recursionObj, $params = array(), $excludeDates = array()) 
     $newParams = $recursionResult = array();
-    if($recursionObj && !empty($params)){ 
+    if ($recursionObj && !empty($params)) { 
+      $initialCount = CRM_Utils_Array::value('start_action_offset', $params);
       if(CRM_Utils_Array::value('parent_event_start_date', $params) && CRM_Utils_Array::value('parent_event_id', $params)){
         $count = 1;
-        while($result = $recursionObj->next()){
+        while ($result = $recursionObj->next()) {
           $newParams['start_date'] = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s'));
           $parentStartDate = new DateTime($params['parent_event_start_date']);
+
           //If events with end date
           if(CRM_Utils_Array::value('parent_event_end_date', $params)){
             $parentEndDate = new DateTime($params['parent_event_end_date']);
@@ -421,6 +423,27 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
             $recursionResult[$count]['end_date'] = $newParams['end_date'];
           }
           $recursionResult[$count]['start_date'] = $newParams['start_date'];
+
+          $skip = FALSE;
+          foreach ($excludeDates as $date) {
+            $date = CRM_Utils_Date::processDate($date, NULL, FALSE, 'Ymd');
+            if (($date == $result->format('Ymd')) || 
+              ($end_date && ($date > $result->format('Ymd')) && ($date <= $end_date->format('Ymd')))
+            ) {
+                $skip = TRUE;
+                break;
+            }
+          }
+
+          if ($skip) {
+            unset($recursionResult[$count]);
+            if ($initialCount && ($initialCount > 0)) {
+              // lets increase the counter, so we get correct number of occurrences
+              $initialCount++;
+              $recursionObj->count($initialCount);
+            }
+            continue;
+          }
           $count++;
         }
       }
index 412cac8fdb378ba9c8a66374ec742c772d910e36..1fc5175a183ec3bd2de7240c58deacbb52f43921 100644 (file)
@@ -218,6 +218,7 @@ class CRM_Core_Form_RecurringEntity {
     
     
     //TO DO - Exclude date functionality
+    $excludeDateList = array();
     if(CRM_Utils_Array::value('copyExcludeDates', $params) && CRM_Utils_Array::value('parent_event_id', $params)){   
       //Since we get comma separated values lets get them in array
       $exclude_date_list = array();
@@ -254,6 +255,7 @@ class CRM_Core_Form_RecurringEntity {
                 'weight'          =>  CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues),
                 'is_active'       =>  1
               );
+          $excludeDateList[] = $optionGroupValue['value'];
           CRM_Core_BAO_OptionValue::add($optionGroupValue);
         }
       }
@@ -264,8 +266,8 @@ class CRM_Core_Form_RecurringEntity {
       CRM_Core_BAO_RecurringEntity::delEntityRelations($params['parent_event_id'], 'civicrm_event');
     }
     //Give call to create recursions
-    $recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params);
-    if(!empty($recurResult) && $params['parent_event_id']){
+    $recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params, $excludeDateList);
+    if(!empty($recurResult) && $params['parent_event_id']) {
       CRM_Core_BAO_RecurringEntity::addEntityThroughRecursion($recurResult, $params['parent_event_id']);
     }
     $status = ts('Repeat Configuration has been saved');
index a5693fb60112c9eb0833306a51ae3e7a729bd20c..0c3061008cdbc2d135e3dbb22a023511bc07b779 100644 (file)
@@ -54,8 +54,9 @@ class CRM_Core_Page_AJAX_RecurringEntity {
             $params['parent_event_id'] = $parent_event_id;
             $params['parent_event_start_date'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['parent_event_id'], 'start_date');
             $params['parent_event_end_date'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['parent_event_id'], 'end_date');
+            $params['start_action_offset'] = $formValues['start_action_offset'];
           }
-          $recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params); 
+          $recurResult = CRM_Core_BAO_RecurringEntity::generateRecursions($recursionObject, $params, $formValues['exclude_date_list']); 
           $count = 1;
           foreach ($recurResult as $key => $value) {
             $recurDates[$count]['start_date'] = date('M d, Y h:i:s A \o\n l', strtotime($value['start_date']));