Skip to content

Commit

Permalink
Merge pull request #155 from bluehousegroup/4
Browse files Browse the repository at this point in the history
updating recursionreader to use carbon api
  • Loading branch information
gheggie authored Nov 3, 2020
2 parents 7bc21f6 + b0e39e8 commit a3543a2
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/Helpers/RecursionReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class RecursionReader
{
use Injectable;

const DAY_SECONDS = 86400; // Seconds in a day
const WEEK_SECONDS = 604800; // Seconds in a week

Expand Down Expand Up @@ -38,7 +38,7 @@ class RecursionReader
* @var array
*/
protected $allowedDaysOfWeek = [];

/**
* @var array
*/
Expand All @@ -65,7 +65,7 @@ public function __construct(CalendarEvent $event)
$this->datetimeClass = $cal->getDateTimeClass();
$this->eventClass = $cal->getEventClass();
$relation = $cal->getDateToEventRelation();

if ($datetime = DataList::create($this->datetimeClass)
->filter($relation, $event->ID)->first()
) {
Expand All @@ -85,7 +85,7 @@ public function __construct(CalendarEvent $event)
}
}
}

if ($exceptions = $event->getComponents('Exceptions')) {
foreach ($exceptions as $exception) {
$this->exceptions[] = $exception->ExceptionDate;
Expand All @@ -99,12 +99,13 @@ public function __construct(CalendarEvent $event)
*/
public function recursionHappensOn($ts)
{
$testDate = Carbon::createFromTimestamp($ts);
$originalTestDate = Carbon::createFromTimestamp($ts);
$testDate = $originalTestDate->copy();
$startDate = Carbon::createFromTimestamp($this->ts);
$result = false;

// Current date is before the recurring event begins.
if ($testDate->getTimestamp() < $startDate->getTimestamp()
if ($testDate->getTimestamp() < $startDate->getTimestamp()
|| in_array($testDate->toDateString(), $this->exceptions)
) {
return $result;
Expand All @@ -124,15 +125,15 @@ public function recursionHappensOn($ts)
// Weekly
case CalendarEvent::RECUR_INTERVAL_WEEKLY:
$testFirstDay = clone $testDate;
$testFirstDay->modify(($testFirstDay->format('l') == 'Sunday')
? 'Monday last week'
$testFirstDay->modify(($testFirstDay->format('l') == 'Sunday')
? 'Monday last week'
: 'Monday this week'
);
if ((($testFirstDay->getTimestamp() - $startDate->startOfWeek()->getTimestamp()) / self::WEEK_SECONDS) % $this->event->WeeklyInterval == 0
&& in_array($testDate->format('w'), $this->allowedDaysOfWeek)
) {
$result = true;
};
};
break;

// Monthly
Expand All @@ -142,7 +143,7 @@ public function recursionHappensOn($ts)
if ($this->event->MonthlyRecursionType1 == 1) {

// A given set of dates in the month e.g. 2 and 15.
if (in_array($testDate->reset()->format('j'), $this->allowedDaysOfMonth)) {
if (in_array($originalTestDate->format('j'), $this->allowedDaysOfMonth)) {
$result = true;
}

Expand All @@ -151,15 +152,15 @@ public function recursionHappensOn($ts)
// e.g. "First Monday of the month"
if ($this->event->MonthlyIndex == 5) {
// Last day of the month?
$targetDate = $testDate->addMonth()->startOfMonth()->previous($this->event->MonthlyDayOfWeek)->dump();
$targetDate = $testDate->addMonth()->startOfMonth()->previous($this->event->MonthlyDayOfWeek)->format('Y-m-d H:i:s');
} else {
$testDate->modify("last day of previous month");
for ($i = 0; $i < $this->event->MonthlyIndex; $i++) {
$testDate->next($this->event->MonthlyDayOfWeek)->dump();
$testDate->next($this->event->MonthlyDayOfWeek)->format('Y-m-d H:i:s');
}
$targetDate = $testDate->dump();
$targetDate = $testDate->format('Y-m-d H:i:s');
}
return $testDate->reset()->dump() == $targetDate;
return $originalTestDate->format('Y-m-d H:i:s') == $targetDate;
}
}
break;
Expand Down

0 comments on commit a3543a2

Please sign in to comment.