diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ca55664..a9acf121 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Solspace Calendar Changelog +## 2.0.9 - 2018-11-01 +### Changed +- Updated demo templates approach for viewing individual recurrences to validate that the date segments in URL match against an available recurrence. + +### Fixed +- Fixed a bug where `startDateLocalized|date()` was not displaying the correct date when viewing recurrences. +- Fixed a bug where the `occurrenceDate` parameter was not working (was originally named `targetDate` - aliased now). + ## 2.0.8 - 2018-10-17 ### Fixed - Fixed a bug where the CP Events list page would not load because of an accidental reference to Freeform. diff --git a/composer.json b/composer.json index d5faf7e5..46dee833 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "solspace/craft3-calendar", "description": "The most powerful event management plugin for Craft.", - "version": "2.0.8", + "version": "2.0.9", "type": "craft-plugin", "minimum-stability": "dev", "authors": [ diff --git a/src/Elements/Event.php b/src/Elements/Event.php index 5f195188..c0fc5b3f 100644 --- a/src/Elements/Event.php +++ b/src/Elements/Event.php @@ -16,12 +16,12 @@ use Solspace\Calendar\Library\Configurations\Occurrences; use Solspace\Calendar\Library\DateHelper; use Solspace\Calendar\Library\Duration\EventDuration; +use Solspace\Calendar\Library\Exceptions\CalendarException; use Solspace\Calendar\Library\RecurrenceHelper; use Solspace\Calendar\Models\CalendarModel; use Solspace\Calendar\Models\ExceptionModel; use Solspace\Calendar\Models\SelectDateModel; use Symfony\Component\PropertyAccess\PropertyAccessor; -use yii\base\Model; class Event extends Element implements \JsonSerializable { @@ -413,6 +413,10 @@ public function cloneForDate(\DateTime $date): Event } if (null !== $date) { + if (!$this->happensOn($date)) { + throw new CalendarException('Invalid event date'); + } + $startDate = $this->getStartDate()->copy(); $endDate = $this->getEndDate()->copy(); @@ -429,8 +433,10 @@ public function cloneForDate(\DateTime $date): Event $endDate = $startDate->copy(); $endDate->addSeconds($diffInSeconds); - $clone->startDate = $startDate; - $clone->endDate = $endDate; + $clone->startDate = $startDate; + $clone->endDate = $endDate; + $clone->startDateLocalized = new Carbon($startDate); + $clone->endDateLocalized = new Carbon($endDate); } return $clone; @@ -755,6 +761,37 @@ public function getOccurrenceDatesBetween(\DateTime $rangeStart = null, \DateTim return $occurrences; } + /** + * @param \DateTime $date + * + * @return bool + * @throws \Exception + */ + public function happensOn(\DateTime $date): bool + { + if (!$date instanceof Carbon) { + $date = new Carbon($date->format('Y-m-d'), DateHelper::UTC); + } + $date->setTime(0, 0, 0); + + if ($date->toDateString() === $this->getStartDate()->toDateString()) { + return true; + } + + if ($this->repeatsOnSelectDates()) { + $dates = $this->getSelectDatesAsString(); + + return \in_array($date->toDateString(), $dates, true); + } + + $rrule = $this->getRRuleObject(); + if (null === $rrule) { + return false; + } + + return $rrule->occursAt($date); + } + /** * @return Carbon|null */ @@ -1391,7 +1428,7 @@ private function getRRuleObject() [ 'FREQ' => $this->getFrequency(), 'INTERVAL' => $this->interval, - 'DTSTART' => $this->initialStartDate, + 'DTSTART' => $this->initialStartDate->copy()->setTime(0, 0, 0), 'UNTIL' => $this->getUntil(), 'COUNT' => $this->count, 'BYDAY' => $this->byDay, diff --git a/src/Variables/CalendarVariable.php b/src/Variables/CalendarVariable.php index 9414c7c7..92f0897d 100644 --- a/src/Variables/CalendarVariable.php +++ b/src/Variables/CalendarVariable.php @@ -65,6 +65,8 @@ public function event($id, array $options = []) $targetDate = null; if (isset($options['targetDate'])) { $targetDate = new Carbon($options['targetDate'], DateHelper::UTC); + } else if (isset($options['occurrenceDate'])) { + $targetDate = new Carbon($options['occurrenceDate'], DateHelper::UTC); } $siteId = null; diff --git a/src/codepack/templates/event.html b/src/codepack/templates/event.html index 2f30247d..95c19aab 100755 --- a/src/codepack/templates/event.html +++ b/src/codepack/templates/event.html @@ -139,12 +139,12 @@

{{ event.title }}