summaryrefslogtreecommitdiff
path: root/app/frontend/dto/GroupedCalendarGridDayDTO.php
blob: 7d2c041a446854b4437e7c8deb46d3ecd82ec54b (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
<?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
        );
    }

}

?>