summaryrefslogtreecommitdiff
path: root/app/php/components/UpcomingEvents.php
blob: 301e740d9bd57e41af767a48d55f121cbad5ff81 (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
<?php

Prado::using('Application.facades.EventFacade');
Prado::using('Application.dto.EventDTO');

class UpcomingEvents 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->Events->setDataSource(
            $this->_getEventsForUser($this->UserToDisplay)
        );
        $this->Events->dataBind();
    }

    private function _getEventsForUser(DbUser $user) {
        $utc = new DateTimeZone('UTC');
        $events = EventFacade::getInstance()->getEventList(
            (new DateTime('now', $utc))->format('Y-m-d H:i:s'),
            (new DateTime('+7 days', $utc))->format('Y-m-d H:i:s'),
            $user->getCalendarPreference()
        );
        $calendars = Calendar::finder()->findAllByPks(
            array_unique(
                array_map(
                    function($event) {
                        return $event->CalendarID;
                    },
                    $events
                )
            )
        );
        return array_map(
            function($event) use($calendars) {
                $dto = new EventDTO();
                $dto->loadRecord($event, $calendars);
                return $dto;
            },
            $events
        );
    }

}

?>