Passed
Push — master ( 38fb8e...f6b156 )
by Andreas
24:23
created

org_openpsa_calendar_handler_calendar::get_lang()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 11
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.calendar
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * @package org.openpsa.calendar
11
 */
12
class org_openpsa_calendar_handler_calendar extends midcom_baseclasses_components_handler
13
{
14
    private $prefix = '/org.openpsa.calendar/fullcalendar-4.4.0/';
15
16
    /**
17
     * Initialization of the handler class
18
     */
19 5
    public function _on_initialize()
20
    {
21 5
        midcom::get()->auth->require_valid_user();
22 5
        midcom::get()->uimessages->add_head_elements();
23 5
    }
24
25 1
    public function _handler_frontpage()
26
    {
27 1
        $selected_time = time();
28 1
        $view = $this->_config->get('start_view');
29 1
        if (in_array($view, ['day', 'week'])) {
30
            $view = 'timeGrid' . ucfirst($view);
0 ignored issues
show
Bug introduced by
It seems like $view can also be of type false; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            $view = 'timeGrid' . ucfirst(/** @scrutinizer ignore-type */ $view);
Loading history...
31 1
        } elseif ($view == 'month') {
32
            $view = 'dayGridMonth';
33
        }
34 1
        return new midcom_response_relocate($this->router->generate('calendar_view_mode_date', [
35 1
            'mode' => $view,
36 1
            'date' => date('Y-m-d', $selected_time)
37
        ]));
38
    }
39
40 3
    public function _handler_day(string $timestamp, array &$data)
41
    {
42 3
        $date = new DateTime($timestamp);
43 3
        $data['calendar_options'] = $this->get_calendar_options(['list']);
44 3
        $data['calendar_options']['defaultDate'] = $date->format('Y-m-d');
45 3
        $data['calendar_options']['plugins'] = ['list'];
46 3
        $data['date'] = $date;
47 3
        return $this->show('show-agenda');
48
    }
49
50
    /**
51
     * Calendar view
52
     */
53 1
    public function _handler_calendar(array &$data)
54
    {
55 1
        $root_event = org_openpsa_calendar_interface::find_root_event();
56 1
        $workflow = $this->get_workflow('datamanager');
57 1
        $buttons = [];
58 1
        if ($root_event->can_do('midgard:create')) {
59 1
            $buttons[] = $workflow->get_button('#', [
60 1
                MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('create event'),
61 1
                MIDCOM_TOOLBAR_GLYPHICON => 'plus',
62
                MIDCOM_TOOLBAR_OPTIONS => [
63
                    'id' => 'openpsa_calendar_add_event',
64
                ]
65
            ]);
66 1
            if (midcom::get()->auth->can_user_do('midgard:create', null, org_openpsa_calendar_resource_dba::class)) {
67 1
                $buttons[] = $workflow->get_button($this->router->generate('new_resource'), [
68 1
                    MIDCOM_TOOLBAR_LABEL => sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get('resource')),
69 1
                    MIDCOM_TOOLBAR_GLYPHICON => 'television',
70
                ]);
71
            }
72
        }
73 1
        $buttons[] = $workflow->get_button($this->router->generate('filters_edit'), [
74 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('choose calendars'),
75 1
            MIDCOM_TOOLBAR_GLYPHICON => 'sliders',
76
        ]);
77
78 1
        $buttons[] = [
79 1
            MIDCOM_TOOLBAR_URL => '#',
80 1
            MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('go to'),
81 1
            MIDCOM_TOOLBAR_GLYPHICON => 'calendar',
82
            MIDCOM_TOOLBAR_OPTIONS => [
83
                'rel' => 'directlink',
84
                'id' => 'date-navigation',
85
            ]
86
        ];
87 1
        $this->_view_toolbar->add_items($buttons);
88
89 1
        $data['calendar_options'] = $this->get_calendar_options(['daygrid', 'timegrid']);
90 1
        midcom::get()->head->enable_jquery_ui(['datepicker']);
91 1
        return $this->show('show-calendar');
92
    }
93
94 4
    private function get_calendar_options(array $views) : array
95
    {
96 4
        $this->add_head_elements($views);
97
        $options = [
98
            'businessHours' => [
99 4
                'start' => $this->_config->get('day_start_time') . ':00',
0 ignored issues
show
Bug introduced by
Are you sure $this->_config->get('day_start_time') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
                'start' => /** @scrutinizer ignore-type */ $this->_config->get('day_start_time') . ':00',
Loading history...
100 4
                'end' => $this->_config->get('day_end_time') . ':00',
0 ignored issues
show
Bug introduced by
Are you sure $this->_config->get('day_end_time') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                'end' => /** @scrutinizer ignore-type */ $this->_config->get('day_end_time') . ':00',
Loading history...
101
                'dow' => [1, 2, 3, 4, 5]
102
            ],
103 4
            'l10n' => ['cancel' => $this->_l10n_midcom->get('cancel')]
104
        ];
105
106 4
        if ($lang = $this->get_lang()) {
107
            $options['locale'] = $lang;
108
        }
109
110 4
        return $options;
111
    }
112
113 4
    private function add_head_elements(array $views)
114
    {
115 4
        $head = midcom::get()->head;
116 4
        $head->add_jsfile(MIDCOM_STATIC_URL . $this->prefix . 'core/main.min.js');
117 4
        $head->add_jsfile(MIDCOM_STATIC_URL . $this->prefix . 'interaction/main.min.js');
118 4
        foreach ($views as $view) {
119 4
            $head->add_jsfile(MIDCOM_STATIC_URL . $this->prefix . $view . '/main.min.js');
120
        }
121 4
        if ($lang = self::get_lang()) {
0 ignored issues
show
Bug Best Practice introduced by
The method org_openpsa_calendar_handler_calendar::get_lang() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        if ($lang = self::/** @scrutinizer ignore-call */ get_lang()) {
Loading history...
122
            $head->add_jsfile(MIDCOM_STATIC_URL . $this->prefix . "core/locales/{$lang}.js");
123
        }
124
125 4
        $head->add_stylesheet(MIDCOM_STATIC_URL . $this->prefix . 'core/main.min.css');
126 4
        foreach ($views as $view) {
127 4
            $head->add_stylesheet(MIDCOM_STATIC_URL . $this->prefix . $view . '/main.min.css');
128
        }
129 4
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/org.openpsa.calendar/calendar.css');
130
131 4
        $head->add_jsfile(MIDCOM_STATIC_URL . '/org.openpsa.calendar/calendar.js');
132 4
    }
133
134 4
    private function get_lang() : ?string
135
    {
136 4
        $lang = midcom::get()->i18n->get_current_language();
137 4
        if (file_exists(MIDCOM_STATIC_ROOT . $this->prefix . "core/locales/{$lang}.js")) {
138
            return $lang;
139
        }
140 4
        $lang = midcom::get()->i18n->get_fallback_language();
141 4
        if (file_exists(MIDCOM_STATIC_ROOT . $this->prefix . "core/locales/{$lang}.js")) {
142
            return $lang;
143
        }
144 4
        return null;
145
    }
146
}
147