summaryrefslogtreecommitdiff
path: root/app/frontend/dto/GroupedCalendarGridDTO.php
blob: 401d8c2b3b63c3afa57430c438a0f31be7cc87d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
    }

}

?>