CIVICRM-1223 follow the old method
authorPengyi Zhang <pengyi@agileware.com.au>
Thu, 12 Sep 2019 04:46:14 +0000 (14:46 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 3 Oct 2019 08:07:32 +0000 (18:07 +1000)
CRM/Core/BAO/RecurringEntity.php

index e0275b469f6b7886aaa94ad22b0cd612d699a27f..67648230068b796ef57e442392cd9947928f4479 100644 (file)
@@ -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;
+  }
+
 }