summaryrefslogtreecommitdiff
path: root/app/php/facades/EventFacade.php
blob: 08133bcb101e035876632b8665e3a8049c385186 (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
75
76
77
<?php

Prado::using('Application.facades.Facade');
Prado::using('Application.dto.EventDTO');
Prado::using('Application.model.Calendar');
Prado::using('Application.facades.CalendarFacade');
Prado::using('Application.user.DbUser');

class EventFacade extends Facade {

    public function getEventList($dateFrom=NULL, $dateTo=NULL, $calendars=NULL, $order='ASC') {
        $calendarClause = '1=1';
        if ($calendars) {
            $calendarClause = sprintf(
                '_calendar IN (%s)',
                implode(
                    ',',
                    array_map(
                        function($calendar) {
                            return $this->quoteString($calendar->UID);
                        },
                        $calendars
                    )
                )
            );
        }
        return $this->fetchList(
            'getEvents',
            [
                'date_from' => $dateFrom ?: '0000-00-00 00:00:00',
                'date_to' => $dateTo ?: '9999-99-99',
                'calendar_clause' => $calendarClause,
                'order_clause' => $order
            ]
        );
    }

    public function getTimeframeListForUser(DbUser $user,
                                            DateTime $dateFrom,
                                            DateTime $dateTo) {
        $calendars = CalendarFacade::getInstance()->getCalendarPreference($user);
        if ($calendars) {
            $events = $this->getEventList(
                $dateFrom->format('Y-m-d H:i:s'),
                $dateTo->format('Y-m-d H:i:s'),
                $calendars
            );
            $calendars = $this->_getCalendarsForEvents($events);
            return array_map(
                function($event) use($calendars) {
                    $dto = new EventDTO();
                    $dto->loadRecord($event, $calendars);
                    return $dto;
                },
                $events
            );
        }
        return [];
    }

    private function _getCalendarsForEvents(array $events) {
        if ($events) {
            return Calendar::finder()->findAllByPks(
                array_map(
                    function($event) {
                        return $event->CalendarID;
                    },
                    $events
                )
            );
        }
        return [];
    }

}

?>