-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullcalendar.module
440 lines (382 loc) · 12 KB
/
fullcalendar.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* @file
* Provides a views style plugin for FullCalendar
*/
/**
* The default path to the FullCalendar plugin.
*/
function fullcalendar_default_path() {
return module_exists('libraries') ? libraries_get_path('fullcalendar') : 'sites/all/libraries/fullcalendar';
}
/**
* The minimum supported version of the FullCalendar plugin.
*/
define('FULLCALENDAR_MIN_PLUGIN_VERSION', '1.4.10');
/**
* The recommended version of the FullCalendar plugin.
*/
define('FULLCALENDAR_RECOMMENDED_PLUGIN_VERSION', '1.5.1');
/**
* Implements hook_views_api().
*/
function fullcalendar_views_api() {
return array(
'api' => '3',
'path' => drupal_get_path('module', 'fullcalendar') . '/includes/views',
);
}
/**
* Implements hook_theme().
*/
function fullcalendar_theme($existing, $type, $theme, $path) {
return array(
'fullcalendar_event' => array(
'variables' => array(
'event' => NULL,
'entity' => NULL,
),
'file' => 'theme.inc',
'path' => $path . '/theme',
),
);
}
/**
* Implements hook_library().
*/
function fullcalendar_library() {
$path = variable_get('fullcalendar_path', fullcalendar_default_path());
$css_files = array($path . '/fullcalendar.css' => array('media' => 'screen'));
// Add fullcalendar.print.css if it is available.
if (version_compare(fullcalendar_get_version(), '1.5.0', '>=')) {
$css_files[$path . '/fullcalendar.print.css'] = array('media' => 'print');
}
$libraries['fullcalendar'] = array(
'title' => 'FullCalendar',
'website' => 'http://arshaw.com/fullcalendar',
'version' => FULLCALENDAR_MIN_PLUGIN_VERSION,
'js' => array(
fullcalendar_get_js_path() => array(),
$path . '/gcal.js' => array(),
),
'css' => $css_files,
'dependencies' => array(
array('system', 'ui.draggable'),
array('system', 'ui.droppable'),
array('system', 'ui.resizable'),
array('system', 'effects.highlight'),
),
);
return $libraries;
}
/**
* Implements hook_permission().
*
* @return array
* An array of valid permissions for the FullCalendar module.
*/
function fullcalendar_permission() {
return array(
'update any fullcalendar event' => array(
'title' => t('Update any FullCalendar event'),
'description' => t('Allow user to edit events, ignoring other permissions.'),
),
);
}
/**
* Implements hook_menu().
*
* @return array
* An array of menu items.
*/
function fullcalendar_menu() {
$items = array();
$items['admin/config/user-interface/fullcalendar'] = array(
'title' => 'FullCalendar',
'description' => 'Adjust FullCalendar settings.',
'file' => 'fullcalendar.admin.inc',
'file path' => drupal_get_path('module', 'fullcalendar') . '/includes',
'page callback' => 'drupal_get_form',
'page arguments' => array('fullcalendar_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'weight' => 0,
);
$items['admin/config/user-interface/fullcalendar/default'] = array(
'title' => 'FullCalendar',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['fullcalendar/ajax/update/%/%'] = array(
'title' => 'Update event',
'description' => 'Save the updated event datetime.',
'page callback' => 'fullcalendar_update',
'page arguments' => array(3, 4),
'access callback' => '_fullcalendar_update_access',
'access arguments' => array(4),
'type' => MENU_CALLBACK,
);
$items['fullcalendar/ajax/results'] = array(
'title' => 'Events',
'description' => 'Get events from views with arguments',
'page callback' => 'fullcalendar_results',
'access arguments' => array('access content'),
'delivery callback' => 'ajax_deliver',
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Saves the updated FullCalendar event's datetime.
*
* @param string $action
* Value can be 'drop' or 'resize'.
* @param int $eid
* The id of the entity that will be updated.
*/
function fullcalendar_update($action, $eid) {
if (empty($_POST['field']) || !isset($_POST['index'])) {
return;
}
// Retrieve the post vars form the ajax call.
$field = check_plain($_POST['field']);
$index = check_plain($_POST['index']);
$all_day = isset($_POST['all_day']) ? check_plain($_POST['all_day']) : '';
$delta = ' ' . check_plain($_POST['day_delta']) . ' days ' . check_plain($_POST['minute_delta']) . ' minutes';
$entity_type = check_plain($_POST['entity_type']);
$entity = entity_load($entity_type, array($eid));
$entity = reset($entity);
$langcode = field_language($entity_type, $entity, $field);
$item = &$entity->{$field}[$langcode][$index];
$old_start = $item['value'];
$old_end = $item['value2'];
// Adjust for different date formats.
$format = date_type_format($item['date_type']);
// Datestamp can't combine with words for strtotime, convert to ISO for now.
if ($format == DATE_FORMAT_UNIX) {
$old_start = date(DATE_FORMAT_ISO, $old_start);
$old_end = date(DATE_FORMAT_ISO, $old_end);
}
// No break after 'drop' so it will reuse the code of 'resize'.
switch ($action) {
case 'drop':
$item['value'] = date($format, strtotime($old_start . $delta));
case 'resize':
$item['value2'] = date($format, strtotime($old_end . $delta));
break;
}
// Save the new start/end values.
if (module_exists('entity')) {
entity_save($entity_type, $entity);
}
else {
field_attach_presave($entity_type, $entity);
field_attach_update($entity_type, $entity);
}
drupal_json_output(array('msg' => t('The new event time has been saved.') . ' [' . l(t('Close'), NULL, array('attributes' => array('class' => array('fullcalendar-status-close')))) . ']', 'dom_id' => $_POST['dom_id']));
}
/**
* Implements hook_fullcalendar_classes().
*/
function fullcalendar_fullcalendar_classes($entity) {
$classes = array(
'fc-event-default',
$entity->bundle,
);
// Add a class for the date field being used.
if (isset($entity->fullcalendar_date_field)) {
$classes[] = "fc-event-field-$entity->fullcalendar_date_field";
}
return $classes;
}
/**
* Implements hook_form_FORM_ID_alter() for views_ui_edit_display_form().
*
* Since we force the query to be distinct, reflect that in the UI.
*/
function fullcalendar_form_views_ui_edit_display_form_alter(&$form, &$form_state, $form_id) {
if ($form_state['view']->display_handler->get_option('style_plugin') != 'fullcalendar' || empty($form['options']['query']['options']['distinct'])) {
return;
}
$distinct = &$form['options']['query']['options']['distinct'];
if (!isset($distinct['#description'])) {
$distinct['#description'] = '';
}
else {
$distinct['#description'] .= '<br>';
}
$distinct['#disabled'] = TRUE;
$distinct['#description'] .= '<strong>' . t('FullCalendar requires that the query be distinct.') . '</strong>';
}
/**
* Returns events for FullCalendar.
*/
function fullcalendar_results($view_name = NULL, $view_display = NULL) {
// Bail out if no view_name or view_display is passed.
if (empty($view_name) || empty($view_display) || empty($_POST['dom_id'])) {
return;
}
// Find Views arguments.
$args = func_get_args();
unset($args[0], $args[1]);
$args = array_values($args);
$dom_id = check_plain($_POST['dom_id']);
$view_name = check_plain($view_name);
$view_display = check_plain($view_display);
// Add all $_POST data, because AJAX is always a post and many things,
// such as tablesorts, exposed filters and paging assume $_GET.
$_GET += $_POST;
// Get the view and check access.
$view = views_get_view($view_name);
if (!$view || !$view->access($view_display)) {
return;
}
if (!$view->set_display($view_display)) {
return;
}
$view->dom_id = $dom_id;
$view->fullcalendar_ajax = TRUE;
$view->pre_execute($args);
$view->init_style();
$view->execute($view_display);
$output = $view->style_plugin->render();
$view->post_execute();
return array(
'#type' => 'ajax',
'#commands' => array(
array('command' => 'fullcalendar_results_response', 'data' => $output),
),
);
}
/**
* Checks if the user has access to update the given FullCalendar event.
*
* @param object $entity
* The entity that will be updated.
*
* @return bool
* TRUE if the user is allowed to update the entity;
* FALSE if the user is not permitted to update the entity.
*/
function _fullcalendar_update_access($entity) {
global $user;
if (!empty($entity) && ((user_access('administer content')
|| user_access('update any fullcalendar event')
|| user_access('edit any ' . $entity->bundle . ' content')
|| (user_access('edit own ' . $entity->bundle . ' content') && $entity->uid == $user->uid)))) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_fullcalendar_editable().
*
* Use our access callback as the editable setting.
*/
function fullcalendar_fullcalendar_editable($entity, $view) {
return _fullcalendar_update_access($entity);
}
/**
* Determines if a given field is a date field.
*
* @param object $field
* A Views field handler object.
* @param bool $include_gcal
* Boolean indicating whether or not to count gcal fields as a date field.
*
* @return bool
* Boolean, TRUE if the field is a date field, FALSE otherwise.
*/
function fullcalendar_field_is_date($field, $include_gcal = FALSE) {
if ($include_gcal && $field->field == 'gcal') {
return TRUE;
}
return !empty($field->definition['is date']) && isset($field->field_info);
}
/**
* Checks FullCalendar dependencies.
*
* @return bool
* Array with TRUE/FALSE for each dependency.
*
* @see fullcalendar_requirements()
*/
function _fullcalendar_status() {
$status = array();
$status['fullcalendar_plugin'] = FALSE;
$path = fullcalendar_get_js_path();
if (!file_exists($path)) {
return FALSE;
}
if (version_compare(fullcalendar_get_version($path), FULLCALENDAR_MIN_PLUGIN_VERSION, '>=')) {
$status['fullcalendar_plugin'] = TRUE;
}
return $status;
}
/**
* Returns the version of FullCalendar plugin that is installed.
*
* This can be used by other modules' hook_requirements() to ensure that the
* proper version of FullCalendar plugin is installed.
*
* @see version_compare()
*/
function fullcalendar_get_version($fullcalendar_path = NULL) {
$version = &drupal_static(__FUNCTION__);
if (empty($version)) {
$version = 0;
$pattern = '#FullCalendar v([0-9\.a-z]+)#';
// No file is passed in so use the default location.
if (is_null($fullcalendar_path)) {
$fullcalendar_path = fullcalendar_get_js_path();
}
// Return the version of FullCalendar plugin.
$fullcalendar_plugin = file_get_contents($fullcalendar_path, NULL, NULL, 0, 40);
if (preg_match($pattern, $fullcalendar_plugin, $matches)) {
$version = $matches[1];
}
}
return $version;
}
/**
* Returns the path to the FullCalendar plugin.
*/
function fullcalendar_get_js_path() {
$fullcalendar_file = array('none' => 'fullcalendar.js', 'min' => 'fullcalendar.min.js');
return variable_get('fullcalendar_path', fullcalendar_default_path()) . '/' . $fullcalendar_file[variable_get('fullcalendar_compression_type', 'min')];
}
/**
* Includes all FullCalendar API plugins.
*/
function fullcalendar_include_api() {
ctools_include('plugins');
return ctools_plugin_api_include('fullcalendar', 'fullcalendar', fullcalendar_api_version(), fullcalendar_api_minimum_version());
}
/**
* Implements hook_ctools_plugin_api_hook_name().
*/
function fullcalendar_ctools_plugin_api_hook_name() {
return 'fullcalendar_api';
}
/**
* Declares the current FullCalendar API version.
*/
function fullcalendar_api_version() {
return '1';
}
/**
* Declares the minimum FullCalendar API version.
*/
function fullcalendar_api_minimum_version() {
return '1';
}
/**
* Implements hook_fullcalendar_api().
*/
function fullcalendar_fullcalendar_api() {
return array(
'api' => fullcalendar_api_version(),
'path' => drupal_get_path('module', 'fullcalendar') . '/includes',
);
}