From 30aa3e540390372a458e548fb10398831a6f1eca Mon Sep 17 00:00:00 2001 From: Pengyi Zhang Date: Thu, 12 Sep 2019 14:46:14 +1000 Subject: [PATCH] CIVICRM-1223 follow the old method --- CRM/Core/BAO/RecurringEntity.php | 64 ++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index e0275b469f..6764823006 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -240,7 +240,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { */ public function generateRecursion() { // return if already generated - if (is_a($this->recursion, 'When')) { + if (is_a($this->recursion, 'When\When')) { return $this->recursion; } @@ -343,18 +343,17 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { $exRangeEnd = $this->excludeDateRangeColumns[1]; } + if (CRM_Core_Config::singleton()->userFramework == 'UnitTests') { + $this->recursion->RFC5545_COMPLIANT = When::IGNORE; + } $count = 1; - try { - if (CRM_Core_Config::singleton()->userFramework == 'UnitTests') { - $this->recursion->RFC5545_COMPLIANT = When::IGNORE; + $result = $this->recursion_start_date; + while ($result = $this->getNextOccurrence($result)) { + $skip = FALSE; + if ($result == $this->recursion_start_date) { + // skip the recursion-start-date from the list we going to generate + $skip = TRUE; } - $this->recursion->generateOccurrences(); - } - catch (Exception $e) { - CRM_Core_Session::setStatus(ts($e->getMessage())); - return $recursionDates; - } - foreach ($this->recursion->occurrences as $result) { $baseDate = $result->format('YmdHis'); foreach ($this->dateColumns as $col) { @@ -388,6 +387,15 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { } } + if ($skip) { + unset($recursionDates[$count]); + if ($initialCount && ($initialCount > 0)) { + // lets increase the counter, so we get correct number of occurrences + $initialCount++; + $this->recursion->count($initialCount); + } + continue; + } $count++; } } @@ -1000,16 +1008,14 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { } $start = new DateTime($currDate); $this->recursion_start_date = $start; - if ($scheduleReminderDetails['repetition_frequency_unit']) { - $repetition_frequency_unit = $scheduleReminderDetails['repetition_frequency_unit']; - if ($repetition_frequency_unit == "day") { - $repetition_frequency_unit = "dai"; - } - $repetition_frequency_unit = $repetition_frequency_unit . 'ly'; - $r->startDate($start) - ->exclusions([$start]) - ->freq($repetition_frequency_unit); + $repetition_frequency_unit = $scheduleReminderDetails['repetition_frequency_unit']; + if ($repetition_frequency_unit == "day") { + $repetition_frequency_unit = "dai"; } + $repetition_frequency_unit = $repetition_frequency_unit . 'ly'; + $r->startDate($start) + ->exclusions([$start]) + ->freq($repetition_frequency_unit); if ($scheduleReminderDetails['repetition_frequency_interval']) { $r->interval($scheduleReminderDetails['repetition_frequency_interval']); @@ -1256,4 +1262,22 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { return $finalResult; } + /** + * Get next occurrence for the given date + * + * @param \DateTime $occurDate + * @param bool $strictly_after + * + * @return bool + */ + private function getNextOccurrence($occurDate, $strictly_after = TRUE) { + try { + return $this->recursion->getNextOccurrence($occurDate, $strictly_after); + } + catch (Exception $exception) { + CRM_Core_Session::setStatus(ts($exception->getMessage())); + } + return FALSE; + } + } -- 2.25.1