summaryrefslogtreecommitdiff
path: root/app/frontend/dto/CalendarGridDTO.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-06-07 15:17:49 +0200
committeremkael <emkael@tlen.pl>2016-06-10 11:46:41 +0200
commit823d71ced9b4947b1a5a5ade7245d521ed490061 (patch)
treea9a6c7cb0de74ff705e8320c284de423a698f5b5 /app/frontend/dto/CalendarGridDTO.php
parentdf401552aac363655ab8f056a6c910a7611954d6 (diff)
* renaming php directory
Diffstat (limited to 'app/frontend/dto/CalendarGridDTO.php')
-rw-r--r--app/frontend/dto/CalendarGridDTO.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/app/frontend/dto/CalendarGridDTO.php b/app/frontend/dto/CalendarGridDTO.php
new file mode 100644
index 0000000..f5d91a5
--- /dev/null
+++ b/app/frontend/dto/CalendarGridDTO.php
@@ -0,0 +1,84 @@
+<?php
+
+Prado::using('Application.dto.CalendarGridDayDTO');
+Prado::using('Application.dto.GridEventDTO');
+
+class CalendarGridDTO {
+
+ public $DateFrom;
+ public $DateTo;
+ public $Weeks = [];
+
+ public function __construct($events, DateTime $dateFrom, DateTime $dateTo) {
+ $this->DateFrom = DateTimeImmutable::createFromMutable($dateFrom);
+ $this->DateTo = DateTimeImmutable::createFromMutable($dateTo);
+ $date = $this->DateFrom;
+ $days = [];
+ $previousDay = NULL;
+ while ($date <= $this->DateTo) {
+ $day = $this->_getGridDay($date, $events, $previousDay);
+ $days[] = $day;
+ $previousDay = $day;
+ $date = $date->modify('+1 day');
+ }
+ $this->Weeks = array_chunk($days, 7);
+ }
+
+ private function _getContinuedEventGridPositions(
+ CalendarGridDayDTO $day,
+ CalendarGridDayDTO $previousDay = NULL) {
+ $eventPositions = [];
+ if ($previousDay) {
+ foreach ($day->Events as $event) {
+ if (in_array($event, $previousDay->Events)) {
+ $eventPositions[] = $event->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, ['GridEventDTO', '__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;
+ }
+
+ private function _getGridDay(DateTimeImmutable $date,
+ array $events,
+ CalendarGridDayDTO $previousDay = NULL) {
+ $day = new CalendarGridDayDTO($date, $events);
+ $eventPositions = $this->_getContinuedEventGridPositions($day, $previousDay);
+ $day->Events = $this->_alignEvents($eventPositions, $day->Events);
+ $day->Events = $this->_fillEventGrid($day->Events);
+ return $day;
+ }
+
+}
+
+?>