From 9326204f83752680eea3aaa41c9a4f0441b5d6c9 Mon Sep 17 00:00:00 2001 From: Grant Heggie Date: Mon, 5 Oct 2020 16:24:05 +1100 Subject: [PATCH] FIX: week subtraction with Carbon API ADD: change URL param format for next/previous month links MINOR: code formatting; removed unused 'use' classes --- src/Pages/CalendarController.php | 49 ++++++++++++++++++-------------- src/Pages/CalendarEvent.php | 4 +-- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/Pages/CalendarController.php b/src/Pages/CalendarController.php index 0bcc1f0..4e1813d 100644 --- a/src/Pages/CalendarController.php +++ b/src/Pages/CalendarController.php @@ -13,24 +13,16 @@ use Carbon\Carbon; use UncleCheese\EventCalendar\Helpers\CalendarUtil; use UncleCheese\EventCalendar\Pages\Calendar; -use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Control\Email\Email; use SilverStripe\Control\HTTP; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\RSS\RSSFeed; use SilverStripe\Core\Convert; -use SilverStripe\Forms\CheckboxSetField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; -use SilverStripe\Forms\NumericField; -use SilverStripe\Forms\TextField; -use SilverStripe\Forms\GridField\GridField; -use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; -use SilverStripe\ORM\ArrayList; -use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\View\Requirements; use \PageController; @@ -83,8 +75,8 @@ public function init() } } - public function index(HTTPRequest $r) { - + public function index(HTTPRequest $r) + { $this->extend('index', $r); switch ($this->DefaultView) { @@ -134,22 +126,26 @@ public function week(HTTPRequest $r) return $this->respond(); } - public function weekend(HTTPRequest $r) { + public function weekend(HTTPRequest $r) + { $this->setWeekendView(); return $this->respond(); } - public function month(HTTPRequest $r) { + public function month(HTTPRequest $r) + { $this->setMonthView(); return $this->respond(); } - public function show(HTTPRequest $r) { + public function show(HTTPRequest $r) + { $this->parseURL($r); return $this->respond(); } - public function rss() { + public function rss() + { $this->setDefaultView(); $events = $this->getEvents(); foreach($events as $event) { @@ -185,7 +181,8 @@ public function rss() { /** * @return string */ - public function monthjson(HTTPRequest $r) { + public function monthjson(HTTPRequest $r) + { $json = []; if (!$r->param('ID')) { return json_encode($json); @@ -551,8 +548,8 @@ public function NextDayLink() */ public function PreviousWeekLink() { - $start = Carbon::parse($this->startDate)->subtractWeek(); - $end = Carbon::parse($this->endDate)->subtractWeek(); + $start = Carbon::parse($this->startDate)->subWeek(); + $end = Carbon::parse($this->endDate)->subWeek(); return $this->getRangeLink($start, $end); } @@ -572,8 +569,7 @@ public function NextWeekLink() public function NextMonthLink() { $start = Carbon::parse($this->startDate)->addMonth(); - $end = Carbon::parse($start)->endOfMonth(); - return $this->getRangeLink($start, $end); + return $this->getMonthLink($start); } /** @@ -582,8 +578,19 @@ public function NextMonthLink() public function PreviousMonthLink() { $start = Carbon::parse($this->startDate)->subMonth(); - $end = Carbon::parse($start)->endOfMonth(); - return $this->getRangeLink($start, $end); + return $this->getMonthLink($start); + } + + /** + * @return string + */ + private function getMonthLink($start) + { + return parent::join_links( + $this->Link(), + "show", + $start->format('Ym') + ); } /** diff --git a/src/Pages/CalendarEvent.php b/src/Pages/CalendarEvent.php index 6f684b5..de04434 100644 --- a/src/Pages/CalendarEvent.php +++ b/src/Pages/CalendarEvent.php @@ -72,8 +72,8 @@ class CalendarEvent extends Page private static $can_be_root = false; - public function getCMSFields() { - + public function getCMSFields() + { $self = $this; $this->beforeUpdateCMSFields(function($f) use ($self) {