From 2ef70b7ddf7167da6f85a566794e0bb5c6bde54e Mon Sep 17 00:00:00 2001 From: Marcuz Date: Thu, 4 Oct 2018 10:50:30 +0200 Subject: [PATCH] Accomodate some new waitwhile API features --- CHANGELOG.md | 6 +++ composer.json | 2 +- src/models/Waitwhile.php | 99 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 635859c..bb3a695 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes for Everyday Waitwhile +## 1.1.5 - 2018-10-04 +- Added `craft.waitwhile.getAbsoluteBusinessHours` to accomodate Waitwhile's newest API addition in `businessHoursByDate` +- Added `craft.waitwhile.getAbsoluteWaitlistHours` to accomodate Waitwhile's newest API addition in `businessWaitlistByDate` +- Added `craft.waitwhile.getAbsoluteBookingHours` to accomodate Waitwhile's newest API addition in `businessBookingByDate` +- `craft.waitwhile.getBookingTimesForDay` now uses `getAbsoluteWaitlistHours` + ## 1.1.4 - 2018-09-19 - Added a birthdate field to Booking and Guest. This is a useful field a lot of people have a need for. Validation is if it's numeric and 6 in length (ddmmyy). - The above is sent as an addition to a note as I can not seem to make custom fields work at all diff --git a/composer.json b/composer.json index a0a253d..48a91cd 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "everyday-as/craft-waitwhile", "description": "A plugin that integrates waitwhile.com for Craft CMS", - "version": "1.1.4", + "version": "1.1.5", "type": "craft-plugin", "require": { "craftcms/cms": "^3.0.0", diff --git a/src/models/Waitwhile.php b/src/models/Waitwhile.php index 222c076..0dcb604 100644 --- a/src/models/Waitwhile.php +++ b/src/models/Waitwhile.php @@ -43,7 +43,6 @@ public function init() * Fetch the waitwhile session * * @return mixed - * @throws \craft\errors\MissingComponentException */ public function getSession() { @@ -179,6 +178,102 @@ public function getResources(): array }, 300); } + /** + * @return array + * @throws \Exception + */ + private function getWeekDays(): array + { + $days = [ + 'mon', + 'tue', + 'wed', + 'thu', + 'fri', + 'sat', + 'sun', + ]; + + $date = (new \DateTime('monday this week')); + foreach($days as $key => $day){ + $days[(int)$date->format('Ymd')] = $day; + unset($days[$key]); + + $date->add(new \DateInterval('P1D')); + } + + return $days; + } + + /** + * @return array + * @throws \Exception + */ + public function getAbsoluteBusinessHours(): array + { + $waitlist = $this->getWaitlist(); + + $days = $this->getWeekDays(); + + $hours = $waitlist['businessHours']; + $hoursByDate = $waitlist['businessHoursByDate']; + + foreach($days as $date => $day){ + $isOverrideSet = isset($hoursByDate[$date]); + if($isOverrideSet){ + $hours[$day] = $hoursByDate[$date]; + } + } + + return $hours; + } + + /** + * @return array + * @throws \Exception + */ + public function getAbsoluteWaitlistHours(): array + { + $waitlist = $this->getWaitlist(); + + $days = $this->getWeekDays(); + + $hours = $waitlist['waitlistHours']; + $hoursByDate = $waitlist['waitlistHoursByDate']; + + foreach($days as $date => $day){ + $isOverrideSet = isset($hoursByDate[$date]); + if($isOverrideSet){ + $hours[$day] = $hoursByDate[$date]; + } + } + + return $hours; + } + + /** + * @return array + * @throws \Exception + */ + public function getAbsoluteBookingHours(): array + { + $waitlist = $this->getWaitlist(); + + $days = $this->getWeekDays(); + + $hours = $waitlist['bookingHours']; + $hoursByDate = $waitlist['bookingHoursByDate']; + + foreach($days as $date => $day){ + $isOverrideSet = isset($hoursByDate[$date]); + if($isOverrideSet){ + $hours[$day] = $hoursByDate[$date]; + } + } + + return $hours; + } + /** * @param string $date * @return array @@ -194,7 +289,7 @@ public function getBookingTimesForDay(string $date): array // bookings from start of today $bookings = $this->getBookingsFrom($startOfDateUnixMs); - $waitlistHours = $this->getWaitlist()['waitlistHours']; + $waitlistHours = $this->getAbsoluteWaitlistHours(); $currentDayAbbreviation = strtolower($startOfDate->format('D')); $waitlistHoursToday = $waitlistHours[$currentDayAbbreviation]['periods']; $isOpen = $waitlistHours[$currentDayAbbreviation]['isOpen'];