From 0fea2103dd4cc5a04a756d5a87e26d39ea347e49 Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 16 Jun 2016 02:25:37 +0200 Subject: * data objects for grouped calendar view --- app/frontend/dto/CalendarGridDTO.php | 2 +- app/frontend/dto/GridEventDTO.php | 7 ++- app/frontend/dto/GridEventGroupDTO.php | 73 ++++++++++++++++++++++++++ app/frontend/dto/GroupedCalendarGridDTO.php | 70 ++++++++++++++++++++++++ app/frontend/dto/GroupedCalendarGridDayDTO.php | 33 ++++++++++++ app/frontend/dto/GroupedGridEventDTO.php | 17 ++++++ 6 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 app/frontend/dto/GridEventGroupDTO.php create mode 100644 app/frontend/dto/GroupedCalendarGridDTO.php create mode 100644 app/frontend/dto/GroupedCalendarGridDayDTO.php create mode 100644 app/frontend/dto/GroupedGridEventDTO.php (limited to 'app/frontend/dto') diff --git a/app/frontend/dto/CalendarGridDTO.php b/app/frontend/dto/CalendarGridDTO.php index f5d91a5..c6f1d00 100644 --- a/app/frontend/dto/CalendarGridDTO.php +++ b/app/frontend/dto/CalendarGridDTO.php @@ -69,7 +69,7 @@ class CalendarGridDTO { return $events; } - private function _getGridDay(DateTimeImmutable $date, + protected function _getGridDay(DateTimeImmutable $date, array $events, CalendarGridDayDTO $previousDay = NULL) { $day = new CalendarGridDayDTO($date, $events); diff --git a/app/frontend/dto/GridEventDTO.php b/app/frontend/dto/GridEventDTO.php index 0d2bb37..ca985f0 100644 --- a/app/frontend/dto/GridEventDTO.php +++ b/app/frontend/dto/GridEventDTO.php @@ -1,8 +1,9 @@ GridPosition - $ev2->GridPosition; } + public function getRendererClass() { + return 'Event'; + } + } ?> diff --git a/app/frontend/dto/GridEventGroupDTO.php b/app/frontend/dto/GridEventGroupDTO.php new file mode 100644 index 0000000..5845076 --- /dev/null +++ b/app/frontend/dto/GridEventGroupDTO.php @@ -0,0 +1,73 @@ +Events = $events; + if ($this->Events) { + $this->Calendar = $this->Events[0]->Calendar; + } + usort($this->Events, ['GridEventDTO', '__compare']); + $this->Header = $this->_determineCommonHeader($this->Events); + } + + public static function __compare(GridEventGroupDTO $ev1, GridEventGroupDTO $ev2) { + if ($ev1->GridPosition === NULL || $ev2->GridPosition === NULL) { + if (count($ev1->Events) && count($ev2->Events)) { + return EventDTO::__compare($ev1->Events[0], $ev2->Events[0]); + } + } + return $ev1->GridPosition - $ev2->GridPosition; + } + + private function _determineCommonHeader(array $events) { + if (!$events) { + return NULL; + } + $names = array_map( + function(EventDTO $event) { + return $event->Name; + }, + $events + ); + $prefix = $names[0]; + $passed = FALSE; + while (!$passed && $prefix) { + $passed = TRUE; + $prefixLength = strlen($prefix); + foreach ($names as $name) { + if (substr($name, 0, $prefixLength) != $prefix) { + $passed = FALSE; + break; + } + } + if ($passed) { + foreach ($names as $name) { + if (is_numeric(trim(substr($name, $prefixLength)))) { + $passed = FALSE; + break; + } + } + } + if (!$passed) { + $prefix = substr($prefix, 0, strrpos($prefix, ' ')); + } + } + return $prefix ?: $events[0]->Calendar->Name; + } + + public function getRendererClass() { + return 'Group'; + } + +} + +?> diff --git a/app/frontend/dto/GroupedCalendarGridDTO.php b/app/frontend/dto/GroupedCalendarGridDTO.php new file mode 100644 index 0000000..401d8c2 --- /dev/null +++ b/app/frontend/dto/GroupedCalendarGridDTO.php @@ -0,0 +1,70 @@ +Events as $group) { + foreach ($previousDay->Events as $pGroup) { + if ($group && $pGroup) { + if ($group->Calendar == $pGroup->Calendar) { + $group->GridPosition = $pGroup->GridPosition; + $eventPositions[] = $group->GridPosition; + } + } + } + } + } + return $eventPositions; + } + + private function _alignEvents(array $eventPositions, array $events) { + $previousCount = count($eventPositions); + foreach ($events as $event) { + if ($event->GridPosition === NULL) { + $event->GridPosition = min( + array_diff( + range(0, count($events) + $previousCount), + $eventPositions + ) + ); + $eventPositions[] = $event->GridPosition; + } + } + usort($events, ['GridEventGroupDTO', '__compare']); + return $events; + } + + private function _fillEventGrid(array $events) { + $previousEvent = -1; + foreach ($events as $event) { + $eventStep = $event->GridPosition - $previousEvent; + if ($eventStep > 1) { + array_splice( + $events, $previousEvent + 1, 0, array_fill(0, $eventStep - 1, NULL) + ); + } + $previousEvent = $event->GridPosition; + } + return $events; + } + + protected function _getGridDay(DateTimeImmutable $date, + array $events, + CalendarGridDayDTO $previousDay = NULL) { + $day = new GroupedCalendarGridDayDTO($date, $events); + $eventPositions = $this->_getContinuedEventGridPositions($day, $previousDay); + $day->Events = $this->_alignEvents($eventPositions, $day->Events); + $day->Events = $this->_fillEventGrid($day->Events); + return $day; + } + +} + +?> diff --git a/app/frontend/dto/GroupedCalendarGridDayDTO.php b/app/frontend/dto/GroupedCalendarGridDayDTO.php new file mode 100644 index 0000000..7d2c041 --- /dev/null +++ b/app/frontend/dto/GroupedCalendarGridDayDTO.php @@ -0,0 +1,33 @@ +Events = $this->_getEventGroups($this->Events); + // initial sort events are going to be re-sorted after assigning grid priorities + usort($this->Events, ['GridEventGroupDTO', '__compare']); + } + + private function _getEventGroups(array $events) { + $clusters = []; + foreach ($events as $event) { + if (!isset($clusters[$event->Calendar->ID])) { + $clusters[$event->Calendar->ID] = []; + } + $clusters[$event->Calendar->ID][] = $event; + } + return array_map( + function($cluster) { + return new GridEventGroupDTO($cluster); + }, + $clusters + ); + } + +} + +?> diff --git a/app/frontend/dto/GroupedGridEventDTO.php b/app/frontend/dto/GroupedGridEventDTO.php new file mode 100644 index 0000000..e9e9d0b --- /dev/null +++ b/app/frontend/dto/GroupedGridEventDTO.php @@ -0,0 +1,17 @@ + -- cgit v1.2.3