-
Notifications
You must be signed in to change notification settings - Fork 4
/
fullcalendar_views.module
261 lines (244 loc) · 7.79 KB
/
fullcalendar_views.module
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* @file
* Hooks and main code for the Fullcalendar Views NG module.
*/
/**
* Implements hook_views_api().
*/
function fullcalendar_views_views_api() {
return array(
'api' => 3,
'path' => backdrop_get_path('module', 'fullcalendar_views') . '/views',
);
}
/**
* Implements hook_autoload_info().
*/
function fullcalendar_views_autoload_info() {
return array(
'FullcalendarViewsDisplayFeed' => 'views/fullcalendar_views_display_feed.inc',
'FullcalendarViewsStyleCalendar' => 'views/fullcalendar_views_style_calendar.inc',
'FullcalendarViewsStyleJson' => 'views/fullcalendar_views_style_json.inc',
);
}
/**
* Implements template_preprocess_views_ui_view_preview_section().
*/
function fullcalendar_views_preprocess_views_ui_view_preview_section(&$variables) {
$view = $variables['view'];
$plugin = $view->display_handler->get_option('style_plugin');
if ($plugin == 'fullcalendar_view') {
$setup = $view->style_plugin->options['calendar_setup'];
if (!empty($setup['event_bg_color'])) {
// Inject an inline style tag to allow direct color preview in the views
// admin UI.
$inline_style = '<style>:root{';
$inline_style .= '--fc-event-bg-color:' . $setup['event_bg_color'] . ';';
$inline_style .= '--fc-event-border-color:' . $setup['event_bg_color'] . ';';
$inline_style .= '--fc-event-text-color:' . $setup['event_text_color'] . ';';
$inline_style .= '}</style>';
$variables['content'] .= $inline_style;
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function fullcalendar_views_form_views_ui_preview_form_alter(array $form, array &$form_state, $form_id) {
if (is_object($form_state['view'])) {
$has_calendar = FALSE;
// We can not rely on view::handler as it may not be set, yet.
foreach ($form_state['view']->display as $name => $view_display) {
// The display_options property still can be anything.
$display_options = $view_display->display_options;
if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'fullcalendar_view') {
$has_calendar = TRUE;
break;
}
}
// If any diplay of the current view has the fullcalendar plugin selected,
// attach assets to render a calendar.
// This does require to reload the page eventually, but otherwise we'd have
// to always attach the library and js, even if not needed at all.
if ($has_calendar) {
$module_path = backdrop_get_path('module', 'fullcalendar_views');
backdrop_add_library('fullcalendar_lib', 'fullcalendar');
backdrop_add_js($module_path . '/js/fullcalendar-views.js');
backdrop_add_css($module_path . '/css/fullcalendar-views.css');
}
}
}
/**
* Helper function to return supported field API types.
*
* @see FullcalendarViewsStyleCalendar::getDateFieldCandidates()
* @see FullcalendarViewsStyleJson::getDateFieldCandidates()
*
* @return array<string>
*/
function fullcalendar_views_supported_field_types() {
return array(
'date',
'datetime',
'datestamp',
'repeating_date',
'resource_timeslot',
);
}
/**
* Helper function to sanitize CSS classes.
*
* @param string $input
* Rendered view field value.
*
* @return string
* Sanitized string usable as CSS class(es).
*/
function fullcalendar_views_sanitize_css($input) {
$output = '';
$classes = explode(' ', $input);
foreach ($classes as $class) {
$output .= ' ' . backdrop_clean_css_identifier($class);
}
return trim($output);
}
/**
* Parse the various supported date field types into values FC can handle.
*
* @param array<mixed> $raw_values
* Raw views result values provided by get_field_value().
* @param string $type
* The field type as defined in hook_field_info().
* @param bool $date_only
* Whether this field should be handled without time.
* @param array<mixed> $rendered_fields
* Array of rendered field values, usually empty at this point.
*
* @return void
*
* @see FullcalendarViewsStyleCalendar::renderRow()
* @see FullcalendarViewsStyleJson::renderRow()
*/
function fullcalendar_views_parse_date_fields(array $raw_values, $type, $date_only, &$rendered_fields) {
$increment_end = FALSE;
// For date_only we need to increment, so the last day is inclusive.
if ($date_only) {
$increment_end = TRUE;
}
switch ($type) {
case 'date':
case 'datetime':
$tz_db = $raw_values[0]['timezone_db'];
$tz = $raw_values[0]['timezone'];
$end = FALSE;
$start = new BackdropDateTime($raw_values[0]['value'], $tz_db);
if ($tz != $tz_db) {
$start->setTimezone(new DateTimeZone($tz));
}
if (!empty($raw_values[0]['value2'])) {
$end = new BackdropDateTime($raw_values[0]['value2'], $tz_db);
if ($tz != $tz_db) {
$end->setTimezone(new DateTimeZone($tz));
}
// End dates are exclusive in FC, so if two values are the same we have
// to increment the end.
if ($start == $end) {
$increment_end = TRUE;
}
// Same as above, also with different dates.
if (date_is_all_day($start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'))) {
$increment_end = TRUE;
$date_only = TRUE;
}
}
if ($date_only) {
$rendered_fields['start'] = $start->format('Y-m-d');
}
else {
$rendered_fields['start'] = $start->format('U') * 1000;
}
if ($end) {
if ($date_only) {
if ($increment_end) {
$end->modify('+1 day');
}
$rendered_fields['end'] = $end->format('Y-m-d');
}
else {
if ($increment_end) {
$end->modify('+1 second');
}
$rendered_fields['end'] = $end->format('U') * 1000;
}
}
break;
case 'datestamp':
$start = $raw_values[0]['value'];
$end = FALSE;
if (!empty($raw_values[0]['value2'])) {
$end = $raw_values[0]['value2'];
if ($start == $end) {
$increment_end = TRUE;
}
$start_str = format_date($start, 'custom', 'Y-m-d H:i:s');
$end_str = format_date($end, 'custom', 'Y-m-d H:i:s');
if (date_is_all_day($start_str, $end_str)) {
$increment_end = TRUE;
$date_only = TRUE;
}
}
if ($date_only) {
$rendered_fields['start'] = format_date($start, 'custom', 'Y-m-d');
}
else {
$rendered_fields['start'] = $start * 1000;
}
if ($end) {
if ($date_only) {
if ($increment_end) {
$end = $end + 3600 * 24;
}
$rendered_fields['end'] = format_date($end, 'custom', 'Y-m-d');
}
else {
if ($increment_end) {
++$end;
}
$rendered_fields['end'] = $end * 1000;
}
}
break;
case 'repeating_date':
$start = $raw_values[0]['dtstart'];
$end = $raw_values[0]['dtend'];
if ($start == $end) {
$increment_end = TRUE;
}
$start_str = format_date($start, 'custom', 'Y-m-d H:i:s');
$end_str = format_date($end, 'custom', 'Y-m-d H:i:s');
if (date_is_all_day($start_str, $end_str)) {
$increment_end = TRUE;
$date_only = TRUE;
}
if ($date_only) {
$rendered_fields['start'] = format_date($start, 'custom', 'Y-m-d');
if ($increment_end) {
$end = $end + 3600 * 24;
}
$rendered_fields['end'] = format_date($end, 'custom', 'Y-m-d');
}
else {
$rendered_fields['start'] = $start * 1000;
if ($increment_end) {
++$end;
}
$rendered_fields['end'] = $end * 1000;
}
break;
case 'resource_timeslot':
$rendered_fields['start'] = $raw_values[0]['start'] * 1000;
$rendered_fields['end'] = $raw_values[0]['end'] * 1000;
break;
}
}