diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index daf86a7806e7..67648230068b 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -31,7 +31,7 @@ * @copyright CiviCRM LLC (c) 2004-2019 */ -require_once 'packages/When/When.php'; +use When\When; /** * Class CRM_Core_BAO_RecurringEntity. @@ -240,7 +240,7 @@ public function generate() { */ public function generateRecursion() { // return if already generated - if (is_a($this->recursion, 'When')) { + if (is_a($this->recursion, 'When\When')) { return $this->recursion; } @@ -334,7 +334,7 @@ public function generateRecursiveDates() { $this->generateRecursion(); $recursionDates = []; - if (is_a($this->recursion, 'When')) { + if (is_a($this->recursion, 'When\When')) { $initialCount = CRM_Utils_Array::value('start_action_offset', $this->schedule); $exRangeStart = $exRangeEnd = NULL; @@ -343,8 +343,12 @@ public function generateRecursiveDates() { $exRangeEnd = $this->excludeDateRangeColumns[1]; } + if (CRM_Core_Config::singleton()->userFramework == 'UnitTests') { + $this->recursion->RFC5545_COMPLIANT = When::IGNORE; + } $count = 1; - while ($result = $this->recursion->next()) { + $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 @@ -1004,14 +1008,14 @@ public function getRecursionFromSchedule($scheduleReminderDetails = []) { } $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->recur($start, $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']); @@ -1258,4 +1262,22 @@ public static function updateModeAndPriceSet($entityId, $entityTable, $mode, $li 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; + } + } diff --git a/composer.json b/composer.json index 42578e4e90e2..69eb155eadd9 100644 --- a/composer.json +++ b/composer.json @@ -64,7 +64,8 @@ "pear/log": "1.13.1", "katzien/php-mime-type": "2.1.0", "civicrm/composer-downloads-plugin": "^2.0", - "league/csv": "^9.2" + "league/csv": "^9.2", + "tplaner/when": "dev-master#c1ec099f421bff354cc5c929f83b94031423fc80" }, "require-dev": { "cache/integration-tests": "dev-master" diff --git a/composer.lock b/composer.lock index 9f5328a8ba4e..78a349252e21 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f0b735ec69179cc1b69b87e335db9c7b", + "content-hash": "0ac00e2168797fa522d7a6f036a5e296", "packages": [ { "name": "civicrm/civicrm-cxn-rpc", @@ -2228,6 +2228,53 @@ "homepage": "https://github.com/totten/ca_config", "time": "2017-05-10T20:08:17+00:00" }, + { + "name": "tplaner/when", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/tplaner/When.git", + "reference": "c1ec099f421bff354cc5c929f83b94031423fc80" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tplaner/When/zipball/c1ec099f421bff354cc5c929f83b94031423fc80", + "reference": "c1ec099f421bff354cc5c929f83b94031423fc80", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "When\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Tom Planer", + "email": "tplaner@gmail.com" + } + ], + "description": "Date/Calendar recursion library.", + "homepage": "https://github.com/tplaner/When", + "keywords": [ + "date", + "datetime", + "recurrence", + "repeat", + "time" + ], + "time": "2019-01-11T21:59:13+00:00" + }, { "name": "zendframework/zend-escaper", "version": "2.4.13", @@ -2708,6 +2755,7 @@ "stability-flags": { "zetacomponents/mail": 20, "pear/validate_finance_creditcard": 20, + "tplaner/when": 20, "cache/integration-tests": 20 }, "prefer-stable": false, diff --git a/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php b/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php index 4c0e6563a2be..008a05205520 100644 --- a/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php +++ b/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php @@ -75,10 +75,10 @@ public function testActivityGeneration() { $this->assertEquals(5, count($generatedEntities['civicrm_activity']), "Cehck if number of iterations are 5"); $expectedDates = [ '20141025103000', - '20141227103000', - '20150328103000', - '20150627103000', - '20150926103000', + '20150124103000', + '20150425103000', + '20150725103000', + '20151024103000', ]; foreach ($generatedEntities['civicrm_activity'] as $entityID) { $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id',