summaryrefslogtreecommitdiff
path: root/app/php/components/UserSelection.php
blob: 681e1543ff120688a862e5ce249baaff5c2ed993 (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
71
72
73
74
<?php

Prado::using('Application.dto.CalendarGroupDTO');
Prado::using('Application.model.UserPreference');

class UserSelection extends TTemplateControl {

    public function getUserToDisplay() {
        return $this->getControlState('user');
    }

    public function setUserToDisplay($user) {
        $this->setControlState('user', $user);
    }

    public function onPreRender($param) {
        parent::onPreRender($param);
        $this->Categories->setDataSource(
            $this->_getUserSelection($this->UserToDisplay)
        );
        $this->Categories->dataBind();
    }

    public function categoryDataBind($sender, $param) {
        $param->Item->Calendars->setDataSource($param->Item->Data->Calendars);
        $param->Item->Calendars->dataBind();
    }

    public function removeFromSelection($sender, $param) {
        if (!$this->UserToDisplay->IsGuest) {
            $preferenceRecord = UserPreference::finder()->find(
                '_user = ? AND _calendar = ?',
                $this->UserToDisplay->DbRecord->ID,
                $param->CommandParameter
            );
            $preferenceRecord->delete();
        }
    }

    private function _getUserSelection(DbUser $user) {
        $calendars = $user->getCalendarPreference();
        if ($calendars) {
            $categories = Category::finder()->findAllByPks(
                array_unique(
                    array_map(
                        function($calendar) {
                            return $calendar->CategoryID;
                        },
                        $calendars
                    )
                )
            );
            $categories = array_map(
                function($category) use($calendars) {
                    $dto = new CalendarGroupDTO();
                    $dto->loadRecord($category, $calendars);
                    return $dto;
                },
                $categories
            );
            usort(
                $categories,
                function ($cat1, $cat2) {
                    return strcmp($cat1->Name, $cat2->Name);
                }
            );
            return $categories;
        }
        return [];
    }

}

?>