aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/calendar/EventData.php
blob: db1e47264941f7edee40dadae7f32fe9f2c63565 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php


namespace Studip\Calendar;


class EventData
{
    public $begin;
    public $end;
    public $title;
    public $event_classes;
    public $text_colour;
    public $background_colour;
    public $editable;
    public $object_class;
    public $object_id;
    public $parent_object_class;
    public $parent_object_id;
    public $range_type;
    public $range_id;
    public $view_urls;
    public $api_urls;
    public $icon;
    public $border_colour;
    public $all_day;

    /**
     * @var string The ID in this field is used to group events when displayed
     * in Fullcalendar so that they can be moved together.
     */
    public string $group_id;

    public function __construct(
        \DateTime $begin,
        \DateTime $end,
        string $title,
        Array $event_classes,
        string $text_colour,
        string $background_colour,
        bool $editable,
        string $object_class,
        string $object_id,
        string $parent_object_class,
        string $parent_object_id,
        string $range_type,
        string $range_id,
        Array $view_urls = [],
        Array $api_urls = [],
        string $icon = '',
        string $border_colour = '',
        bool $all_day = false,
        string $group_id = ''
    )
    {
        $this->begin = $begin;
        $this->end = $end;
        $this->title = $title;
        $this->event_classes = $event_classes;
        $this->text_colour = $text_colour;
        $this->background_colour = $background_colour;
        $this->editable = $editable;
        $this->object_class = $object_class;
        $this->object_id = $object_id;
        $this->parent_object_class = $parent_object_class;
        $this->parent_object_id = $parent_object_id;
        $this->range_type = $range_type;
        $this->range_id = $range_id;
        $this->view_urls = $view_urls;
        $this->api_urls = $api_urls;
        $this->icon = $icon;
        $this->border_colour = $border_colour ?: $background_colour;
        $this->all_day = $all_day;
        $this->group_id = $group_id;
    }


    public function toFullcalendarEvent()
    {
        // Note: The timezone must not be transmitted or
        // the events may be shifted when there is a timezone
        // or daylight saving time difference between the server
        // and the client!
        // To display all-day events correctly in fullcalendar
        // reduce the start and end to date without time and add one day
        // to the end date...
        if ($this->all_day) {
            $fc_date = [
                'allDay' => true,
                'start'  => $this->begin->format('Y-m-d'),
                'end'    => $this->end->modify('+1 day')->format('Y-m-d')
            ];
        } else {
            $fc_date = [
                'allDay' => false,
                'start'  => $this->begin->format('Y-m-d\TH:i:s'),
                'end'    => $this->end->format('Y-m-d\TH:i:s')
            ];
        }

        return $fc_date + [
            'resourceId' => $this->range_id,
            'title' => $this->title,
            'classNames' => $this->event_classes,
            'textColor' => $this->text_colour,
            'color' => $this->background_colour,
            'borderColor' => $this->border_colour,
            'editable' => $this->editable,
            'studip_weekday_begin' => $this->begin->format('N'),
            'studip_weekday_end' => $this->end->format('N'),
            'studip_object_class' => $this->object_class,
            'studip_object_id' => $this->object_id,
            'studip_parent_object_class' => $this->parent_object_class,
            'studip_parent_object_id' => $this->parent_object_id,
            'studip_range_type' => $this->range_type,
            'studip_range_id' => $this->range_id,
            'studip_view_urls' => $this->view_urls,
            'studip_api_urls' => $this->api_urls,
            'icon' => $this->icon
        ] + ($this->group_id ? ['groupId' => $this->group_id] : []);
    }
}