aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Fullcalendar.php
blob: d3bc82314e4eae404e60da0e10c3afd786cd4db4 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Studip;

class Fullcalendar
{
    /**
     * The standard month view for Stud.IP calendars.
     */
    const VIEW_MONTH = 'dayGridMonth';

    /**
     * The standard week view for Stud.IP calendars.
     */
    const VIEW_WEEK = 'timeGridWeek';

    /**
     * The standard day view for Stud.IP calendars.
     */
    const VIEW_DAY = 'timeGridDay';

    /**
     * The standard week view for Stud.IP group calendars.
     */
    const GROUP_WEEK = 'resourceTimelineWeek';

    /**
     * The standard day view for Stud.IP group calendars.
     */
    const GROUP_DAY = 'resourceTimelineDay';


    protected $title;

    /**
     * Fullcalendar configuration options.
     * They are passed to the JavaScript fullcalendar class.
     */
    protected $config;

    /**
     * Additional HTML attributes that shall be attached to the
     * section element in which the fullcalendar instance is created.
     */
    protected $attributes;

    /**
     * The name of the fullcalendar for the data attribute. This is set
     * to "fullcalendar" by default, but custom fullcalendars may require
     * special data attributes to prevent the default Fullcalendar JS
     * initialiser to be executed.
     */
    protected $data_name;

    public static function create(
        $title = '',
        $config = [],
        $attributes = [],
        $data_name = 'fullcalendar'
    )
    {
        $instance = new \Studip\Fullcalendar(
            $title,
            $config,
            $attributes,
            $data_name
        );

        return $instance->render();
    }

    public function __construct(
        $title = '',
        $config = [],
        $attributes = [],
        $data_name = 'fullcalendar'
    )
    {
        $this->title = $title;
        $this->config = $config;
        $this->attributes = $attributes;
        $this->data_name = $data_name;
    }

    public function setDefaultView(?string $view): void
    {
        if ($view === null) {
            unset($this->config['initialView']);
        } else {
            $this->config['initialView'] = $view;
        }
    }

    public function setResponsiveDefaultView(?string $view): void
    {
        if ($view === null) {
            unset($this->config['responsiveDefaultView']);
        } else {
            $this->config['responsiveDefaultView'] = $view;
        }
    }

    public function render()
    {
        $factory = new \Flexi\Factory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
        $template = $factory->open('studip-fullcalendar.php');
        $real_data_name = sprintf('data-%s', $this->data_name);

        //Move the Stud.IP parts of the configuration out of the
        //fullcalendar configuration:
        $fullcalendar_config = $this->config;
        $template_params     = [
            'title'                          => $this->title,
            'dialogSize'                     => 'auto',
            'actionUrls'                     => [],
            'displayHolidays'                => true,
            'displayVacations'               => true,
            'externalDroppableContainerId'   => '',
            'externalDroppableEventSelector' => '',
            'eventColourPicker'              => false
        ];
        if (!empty($this->attributes['class'])) {
            $template_params['extraClasses'] = $this->attributes['class'];
            unset($this->attributes['class']);
        } else {
            $template_params['extraClasses'] = '';
        }
        $template_params['attributes'] = array_merge(
                $this->attributes,
                [$real_data_name => '1']
        );
        if (array_key_exists('studip_urls', $fullcalendar_config)
            && is_array($fullcalendar_config['studip_urls'])
        ) {
            $template_params['actionUrls'] = $fullcalendar_config['studip_urls'];
            unset($fullcalendar_config['studip_urls']);
        }

        $studip_config_map = [
            'dialog_size' => 'dialogSize',
            'display_holidays' => 'displayHolidays',
            'display_vacations' => 'displayVacations',
            'external_droppable_container_id' => 'externalDroppableContainerId',
            'external_droppable_event_selector' => 'externalDroppableEventSelector',
            'event_colour_picker' => 'eventColourPicker',
        ];

        foreach ($studip_config_map as $config_key => $param_key) {
            if (array_key_exists($config_key, $fullcalendar_config)) {
                $template_params[$param_key] = $fullcalendar_config[$config_key];
                unset($fullcalendar_config[$config_key]);
            }
        }

        $template_params['config'] = $fullcalendar_config;

        return $template->render($template_params);
    }

    /**
     * Creates an array with data for a Fullcalendar instance
     * from Stud.IP objects that implement the EventSource interface.
     */
    public static function createData($objects = [], $begin = null, $end = null)
    {
        if (!count($objects)) {
            //No data means there is nothing to do.
            return [];
        }

        $data = [];

        foreach ($objects as $object) {
            if ($object instanceof \Studip\Calendar\EventSource) {
                $events = $object->getFilteredEventData(
                    $GLOBALS['user']->id, null, null, $begin, $end
                );

                foreach ($events as $event) {
                    $data[] = $event->toFullcalendarEvent();
                }
            }
        }
        return $data;
    }
}