summaryrefslogtreecommitdiff
path: root/app/frontend/dto/CalendarGroupDTO.php
blob: 3f83d4b68c8b25e32642452ed7af657807c25cce (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
<?php

Prado::using('Application.dto.BaseDTO');
Prado::using('Application.model.Category');
Prado::using('Application.dto.CalendarDTO');

class CalendarGroupDTO extends BaseDTO {

    public $Name;
    public $ID;
    public $Priority;
    public $Calendars = [];

    public function loadRecord(Category $categoryRecord, array $calendars) {
        $this->Name = self::getTranslation(
            $categoryRecord->Name,
            'CalendarGroup.Name'
        );
        $this->ID = $categoryRecord->ID;
        $this->Priority = $categoryRecord->Priority;
        $this->Calendars = array_map(
            function(Calendar $calendarRecord) {
                $dto = new CalendarDTO();
                $dto->loadRecord($calendarRecord);
                return $dto;
            },
            array_filter(
                $calendars,
                function(Calendar $calendarRecord) use($categoryRecord) {
                    return $categoryRecord->ID == $calendarRecord->CategoryID;
                }
            )
        );
        usort($this->Calendars, ['CalendarDTO', '__compare']);
    }

    public static function __compare(CalendarGroupDTO $cat1,
                                     CalendarGroupDTO $cat2) {
        $cmp = ($cat1->Priority ?: PHP_INT_MAX) - ($cat2->Priority ?: PHP_INT_MAX);
        if ($cmp !== 0) {
            return $cmp;
        }
        return strcmp($cat1->Name, $cat2->Name);
    }

}

?>