aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Fullcalendar.class.php
blob: 467b5839cf0f9f69fa078970b2f141676e03241f (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
<?php


namespace Studip;


class Fullcalendar
{
    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 render()
    {
        $factory = new \Flexi_TemplateFactory($GLOBALS['STUDIP_BASE_PATH'] . '/templates');
        $template = $factory->open('studip-fullcalendar.php');
        $real_data_name = sprintf('data-%s', $this->data_name);
        return $template->render(
            [
                'title' => $this->title,
                'config' => $this->config,
                'attributes' => array_merge(
                    $this->attributes,
                    [$real_data_name => '1']
                )
            ]
        );
    }


    /**
     * 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;
    }
}