diff --git a/CHANGELOG.md b/CHANGELOG.md index d9fc9ba1..eea1b23a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Solspace Calendar Changelog +## 2.0.19 - 2019-04-15 +### Fixed +- Fixed a bug where the `occurrences` object date ranges were not working correctly with events using the Select Dates repeat rule type. +- Fixed a bug where Calendar would hard error for events attempting to be created without times. +- Fixed a bug where events couldn't be ordered by `RAND()`. + ## 2.0.18 - 2019-04-05 ### Fixed - Fixed a bug where events that started early in day but repeated daily and ended on a specific date would error due to timezone issues. diff --git a/composer.json b/composer.json index 1198cd87..27e984ec 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.18", + "version": "2.0.19", "type": "craft-plugin", "authors": [ { diff --git a/src/Controllers/EventsController.php b/src/Controllers/EventsController.php index f4afd326..1cdb628c 100644 --- a/src/Controllers/EventsController.php +++ b/src/Controllers/EventsController.php @@ -256,10 +256,15 @@ public function actionSaveEvent() ); } - $event->startDateLocalized = new Carbon($event->getStartDate()->toDateTimeString()); - $event->endDateLocalized = new Carbon($event->getEndDate()->toDateTimeString()); - $event->initialStartDate = $event->getStartDate()->copy(); - $event->initialEndDate = $event->getEndDate()->copy(); + if ($event->getStartDate()) { + $event->startDateLocalized = new Carbon($event->getStartDate()->toDateTimeString()); + $event->initialStartDate = $event->getStartDate()->copy(); + } + + if ($event->getEndDate()) { + $event->endDateLocalized = new Carbon($event->getEndDate()->toDateTimeString()); + $event->initialEndDate = $event->getEndDate()->copy(); + } $this->handleRepeatRules($event, $values); diff --git a/src/Elements/Db/EventQuery.php b/src/Elements/Db/EventQuery.php index b95d8ee9..cecc6529 100644 --- a/src/Elements/Db/EventQuery.php +++ b/src/Elements/Db/EventQuery.php @@ -514,6 +514,10 @@ public function all($db = null): array $this->orderDates($this->eventCache); } + if ($this->shouldRandomize()) { + $this->randomizeDates($this->eventCache); + } + if ($this->shuffle) { shuffle($this->eventCache); } @@ -855,11 +859,13 @@ protected function beforePrepare(): bool } } - if (\is_array($this->orderBy) && isset($this->orderBy['dateCreated'])) { - $sortDirection = $this->orderBy['dateCreated']; - $this->orderBy['[[calendar_events.dateCreated]]'] = $sortDirection; + if (\is_array($this->orderBy)) { + if (isset($this->orderBy['dateCreated'])) { + $sortDirection = $this->orderBy['dateCreated']; + $this->orderBy['[[calendar_events.dateCreated]]'] = $sortDirection; - unset($this->orderBy['dateCreated']); + unset($this->orderBy['dateCreated']); + } } return parent::beforePrepare(); @@ -1197,6 +1203,14 @@ function (array $arrayA, array $arrayB) use ($modifier) { ); } + /** + * @param array $dates + */ + private function randomizeDates(array &$dates) + { + shuffle($dates); + } + /** * Orders events by their start dates * @@ -1408,6 +1422,20 @@ private function shouldOrderByStartDate(): bool return false; } + /** + * Checks whether the events should be randomized + * + * @return bool + */ + private function shouldRandomize(): bool + { + if (\is_array($this->orderBy)) { + return array_key_exists('RAND()', $this->orderBy); + } + + return null !== $this->orderBy && $this->orderBy === 'RAND()'; + } + /** * Returns 1 for ASC and -1 for DESC * Based on ::$sort diff --git a/src/Elements/Event.php b/src/Elements/Event.php index 73f61a12..504e865f 100644 --- a/src/Elements/Event.php +++ b/src/Elements/Event.php @@ -1173,7 +1173,7 @@ public function getOccurrences(array $config = null): array if ($this->getRRuleObject()) { $occurrenceDates = $this->getOccurrenceDatesBetween($rangeStart, $rangeEnd); } else if ($this->getSelectDates()) { - $occurrenceDates = $this->getSelectDatesAsDates(); + $occurrenceDates = $this->getSelectDatesAsDates($rangeStart, $rangeEnd); } $occurrences = [];