summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-06-16 02:25:37 +0200
committeremkael <emkael@tlen.pl>2016-06-16 11:50:18 +0200
commit0fea2103dd4cc5a04a756d5a87e26d39ea347e49 (patch)
tree193c6af2ffe418c71348944528178e1b11e46fce /app
parentece88ff6d4c59418dc043c735252aca012eee207 (diff)
* data objects for grouped calendar view
Diffstat (limited to 'app')
-rw-r--r--app/frontend/controls/grid/IGridElementDTO.php9
-rw-r--r--app/frontend/dto/CalendarGridDTO.php2
-rw-r--r--app/frontend/dto/GridEventDTO.php7
-rw-r--r--app/frontend/dto/GridEventGroupDTO.php73
-rw-r--r--app/frontend/dto/GroupedCalendarGridDTO.php70
-rw-r--r--app/frontend/dto/GroupedCalendarGridDayDTO.php33
-rw-r--r--app/frontend/dto/GroupedGridEventDTO.php17
7 files changed, 209 insertions, 2 deletions
diff --git a/app/frontend/controls/grid/IGridElementDTO.php b/app/frontend/controls/grid/IGridElementDTO.php
new file mode 100644
index 0000000..b7c23f2
--- /dev/null
+++ b/app/frontend/controls/grid/IGridElementDTO.php
@@ -0,0 +1,9 @@
+<?php
+
+interface IGridElementDTO {
+
+ public function getRendererClass();
+
+}
+
+?>
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 @@
<?php
Prado::using('Application.dto.EventDTO');
+Prado::using('Application.controls.grid.IGridElementDTO');
-class GridEventDTO extends EventDTO {
+class GridEventDTO extends EventDTO implements IGridElementDTO {
public $DateFrom;
public $DateTo;
@@ -23,6 +24,10 @@ class GridEventDTO extends EventDTO {
return $ev1->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 @@
+<?php
+
+Prado::using('Application.dto.GridEventDTO');
+Prado::using('Application.controls.grid.IGridElementDTO');
+
+class GridEventGroupDTO implements IGridElementDTO {
+
+ public $Header;
+ public $Events;
+ public $Calendar;
+ public $GridPosition;
+
+ public function __construct(array $events) {
+ $this->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 @@
+<?php
+
+Prado::using('Application.dto.GroupedCalendarGridDayDTO');
+Prado::using('Application.dto.GridEventDTO');
+
+class GroupedCalendarGridDTO extends CalendarGridDTO {
+
+ private function _getContinuedEventGridPositions(
+ CalendarGridDayDTO $day,
+ CalendarGridDayDTO $previousDay = NULL) {
+ $eventPositions = [];
+ if ($previousDay) {
+ foreach ($day->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 @@
+<?php
+
+Prado::using('Application.dto.CalendarGridDayDTO');
+Prado::using('Application.dto.GridEventGroupDTO');
+
+class GroupedCalendarGridDayDTO extends CalendarGridDayDTO {
+
+ public function __construct(DateTimeImmutable $date, array $events) {
+ parent::__construct($date, $events);
+ $this->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 @@
+<?php
+
+Prado::using('Application.dto.GridEventDTO');
+
+class GroupedGridEventDTO extends GridEventDTO {
+
+ public $DateFrom;
+ public $DateTo;
+ public $AllDay;
+
+ public static function __compare(EventDTO $ev1, EventDTO $ev2) {
+ return EventDTO::__compare($ev1, $ev2);
+ }
+
+}
+
+?>