From: priyankakaran26 Date: Sun, 7 Sep 2014 13:34:14 +0000 (+0100) Subject: generate recurrsions based on exclude dates X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ad82cae100e026079dca3ed91b4984ff7a9eea35;p=civicrm-core.git generate recurrsions based on exclude dates --- diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index 173674125d..d6edd05280 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -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++; } } diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php index 412cac8fdb..1fc5175a18 100644 --- a/CRM/Core/Form/RecurringEntity.php +++ b/CRM/Core/Form/RecurringEntity.php @@ -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'); diff --git a/CRM/Core/Page/AJAX/RecurringEntity.php b/CRM/Core/Page/AJAX/RecurringEntity.php index a5693fb601..0c3061008c 100644 --- a/CRM/Core/Page/AJAX/RecurringEntity.php +++ b/CRM/Core/Page/AJAX/RecurringEntity.php @@ -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']));