forked from pdrakeweb/Drupal-FullCalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullcalendar.module
469 lines (429 loc) · 14.6 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
// $Id$
/**
* @file
* Provides a views style plugin for FullCalendar
*/
/**
* The default path to the FullCalender plugin.
*/
define('FULLCALENDAR_PATH', 'sites/all/libraries/fullcalendar');
define('FULLCALENDAR_MIN_PLUGIN_VERSION', '1.4.9');
define('FULLCALENDAR_MIN_JQUERY_VERSION', '1.3.2');
define('FULLCALENDAR_MAX_JQUERY_VERSION', '1.4+');
define('FULLCALENDAR_MIN_JQUERYUI_VERSION', '1.7.3');
define('FULLCALENDAR_MAX_JQUERYUI_VERSION', '1.8+');
/**
* Implementation of hook_views_api().
*/
function fullcalendar_views_api() {
return array(
'api' => '2',
'path' => drupal_get_path('module', 'fullcalendar'),
);
}
/**
* Implementation of hook_perm().
*
* @return array An array of valid permissions for the fullcalendar module
*/
function fullcalendar_perm() {
return array('update any fullcalendar event');
}
/**
* Implementation of hook_menu().
*
* @return An array of menu items.
*/
function fullcalendar_menu() {
$items = array();
$items['admin/settings/fullcalendar'] = array(
'title' => 'FullCalendar',
'description' => 'Adjust FullCalendar settings.',
'file' => 'fullcalendar.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('fullcalendar_admin_settings'),
'access arguments' => array('administer site configuration'),
);
$items['fullcalendar/ajax/update/%/%node'] = 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,
);
return $items;
}
/**
* Save the updated fullcalendar event's datetime.
*
* @param string $action
* Value can be 'drop' or 'resize'.
* @param object $node
*/
function fullcalendar_update($action, $node) {
// Retrieve the post vars form the ajax call.
$field = $_POST['field'];
$index = $_POST['index'];
$all_day = isset($_POST['all_day']) ? $_POST['all_day'] : '';
$day_delta = $_POST['day_delta'];
$minute_delta = $_POST['minute_delta'];
if (!empty($field) && isset($index)) {
$old_start = $node->{$field}[$index]['value'];
$old_end = $node->{$field}[$index]['value2'];
switch ($action) {
case 'drop':
$node->{$field}[$index]['value'] = date('Y-m-d H:i:s', strtotime($old_start . ' ' . $day_delta . ' days ' . $minute_delta . ' minutes'));
$node->{$field}[$index]['value2'] = date('Y-m-d H:i:s', strtotime($old_end . ' ' . $day_delta . ' days ' . $minute_delta . ' minutes'));
break;
case 'resize':
$node->{$field}[$index]['value2'] = date('Y-m-d H:i:s', strtotime($old_end . ' ' . $day_delta . ' days ' . $minute_delta . ' minutes'));
break;
}
// Save the new start/end values.
node_save($node);
drupal_json(array('msg' => 'The new event time has been saved. [<a href="javascript:void(0);" class="fullcalendar-status-close">close</a>]'));
}
}
/**
* Implementation of hook_theme().
*/
function fullcalendar_theme() {
return array(
'fullcalendar_classname' => array(
'arguments' => array('node' => NULL),
),
'fullcalendar_link' => array(
'arguments' => array(
'node' => NULL,
'attributes' => NULL,
'index' => NULL,
),
),
'fullcalendar_flyout' => array(
'arguments' => array(
'content' => NULL,
)
),
);
}
/**
* Create a flyout that can be insterted into a link to show more information
* when hovered.
*
* @param $content
* An array of lines to place in the flyout.
*
* @return
* A string of the themed flyout, to be inserted in a link.
*/
function theme_fullcalendar_flyout($content) {
$output = "<span class='fc-flyout'>";
$output .= "<span class='fc-flyout-content'>";
foreach ($content as $line) {
$output .= strip_tags($line, '<a><em><strong>');
$output .= '<br />';
}
$output .= "</span>";
$output .= "</span>";
return $output;
}
/**
* Construct CSS classes for a node.
*
* @param $node
* An object representing the node.
*
* @return
* A string suitable for use as a CSS class.
*/
function theme_fullcalendar_classname($node) {
$className = array(
'fc-event-default',
$node->type,
);
return implode(' ', $className);
}
function _fullcalendar_load_assets() {
drupal_add_js(fullcalendar_get_path());
drupal_add_css(variable_get('fullcalendar_path', FULLCALENDAR_PATH) . '/fullcalendar.css');
drupal_add_css(drupal_get_path('module', 'fullcalendar') . '/fullcalendar.custom.css');
ctools_include('modal');
ctools_modal_add_js();
// We need some jQuery UI files.
$files = array(
'ui.draggable',
'ui.droppable',
'ui.resizable',
'effects.highlight',
);
jquery_ui_add($files);
}
/**
* Pass settings to JavaScript.
*/
function template_preprocess_views_view_fullcalendar(&$vars) {
_fullcalendar_load_assets();
$settings = array(
'defaultView' => $vars['options']['display']['fc_view'],
'firstDay' => $vars['options']['display']['fc_firstday'],
'weekMode' => $vars['options']['display']['fc_weekmode'],
'theme' => $vars['options']['modules']['fc_theme'],
'sameWindow' => $vars['options']['modules']['fc_window'],
'colorbox' => $vars['options']['modules']['fc_url_colorbox'],
'left' => $vars['options']['header']['fc_left'],
'center' => $vars['options']['header']['fc_center'],
'right' => $vars['options']['header']['fc_right'],
'year' => $vars['options']['defaults']['fc_year'],
'month' => $vars['options']['defaults']['fc_month'],
'day' => $vars['options']['defaults']['fc_day'],
'agenda' => $vars['options']['times']['fc_timeformat'],
'clock' => $vars['options']['times']['fc_clock'],
'monthNames' => array_values(date_month_names(TRUE)),
'monthNamesShort' => array_values(date_month_names_abbr(TRUE)),
'dayNames' => date_week_days(TRUE),
'dayNamesShort' => date_week_days_abbr(TRUE),
'allDayText' => t('All day'),
'dayString' => t('Day'),
'weekString' => t('Week'),
'monthString' => t('Month'),
'todayString' => t('Today'),
);
if (!empty($vars['options']['modules']['ajax_callback_month'])) {
$settings['ajax_callback_month'] = url($vars['options']['modules']['ajax_callback_month']);
}
if (!empty($vars['options']['modules']['ajax_callback_day'])) {
$settings['ajax_callback_day'] = url($vars['options']['modules']['ajax_callback_day']);
}
if (!empty($vars['options']['modules']['ajax_callback_week'])) {
$settings['ajax_callback_week'] = url($vars['options']['modules']['ajax_callback_week']);
}
drupal_add_js(array('fullcalendar' => $settings), 'setting');
drupal_add_js(drupal_get_path('module', 'fullcalendar') . '/js/jquery.jcache.js', 'module');
drupal_add_js(drupal_get_path('module', 'fullcalendar') . '/fullcalendar.views.js', 'module');
}
/**
* Prepare variables for template file invoked for node row type
*/
function template_preprocess_views_view_node_fullcalendar(&$vars) {
_fullcalendar_load_assets();
if (isset($vars['view']->empty_text)) {
$vars['empty_text'] = $vars['view']->empty_text;
return;
}
$nid = $vars['row']->{$vars['field_alias']};
if (!is_numeric($nid)) {
return;
}
$node = node_load($nid);
if (empty($node)) {
return;
}
// Allow resize/drag/drop of an event if user has proper permissions.
$node->editable = _fullcalendar_update_access($node);
$node->class = theme('fullcalendar_classname', $node);
$vars['node'] = $node;
$vars['data'] = array(); // Contains the start, end & allDay values.
$node->url = 'node/' . $nid;
if ($url_field = $vars['options']['fullcalendar_url_field']) {
if (isset($node->{$url_field}[0]['value'])) {
$node->url = $node->{$url_field}[0]['value'];
}
}
$node->fc_flyout = !empty($vars['view']->style_options['display']['fc_flyout']);
$title_field = $vars['options']['fullcalendar_title_field'];
if (!empty($title_field) && !empty($node->{$title_field}[0]['value'])) {
$node->title = $node->{$title_field}[0]['value'];
}
$display_field = fullcalendar_date_fields($node);
$field_names = trim(strip_tags($vars['options']['fullcalendar_date_fields']));
if (!empty($field_names)) {
foreach (explode("\n", $field_names) as $field_name) {
$field_name = trim(strip_tags($field_name));
if (($field_name == 'created') || ($field_name == 'changed')) {
$attributes = _fullcalendar_set_display_times($node, $field_name);
$vars['data'][] = theme('fullcalendar_link', $node, $attributes);
$display_field = array();
break;
}
// If a date_type field exists
if ($display_field[$field_name]) {
$display_field = array($field_name => $display_field[$field_name]);
break;
}
}
}
// Iterate through available fields, using the first one found.
foreach ($display_field as $field_name => $field) {
foreach ($node->$field_name as $index => $item) {
$attributes = _fullcalendar_set_display_times($node, $field_name, $field, $item);
$vars['data'][] = theme('fullcalendar_link', $node, $attributes, $index);
}
break;
}
}
/**
* Pass times through date modules date_formatter_process function to
* translate them to the right display times.
*
* @param object $node
* @param string $field_name
* @param integer $index
* @param array $vars
*/
function _fullcalendar_set_display_times($node, $field_name, $field = NULL, $item = NULL) {
if (is_array($node->$field_name)) {
$dfp_info = array(
'#node' => $node,
'#field_name' => $field_name,
'#formatter' => NULL,
'#item' => $item,
);
$date = date_formatter_process($dfp_info);
$date1 = $date['value']['local']['object'];
$date2 = $date['value2']['local']['object'];
}
else {
$date1 = date_make_date($node->$field_name, date_default_timezone(), DATE_UNIX);
$date2 = $date1;
}
return array(
'field' => $field_name,
'allDay' => date_field_all_day($field, $date1, $date2),
'start' => $date1,
'end' => $date2,
'nid' => $node->nid,
'cn' => $node->class,
'title' => $node->title,
'class' => 'fullcalendar_event_details',
'editable' => $node->editable,
);
}
/**
* Set the text for the fallback display.
*/
function theme_fullcalendar_link($node, $attributes, $index = 0) {
$options = array();
$text = date_format_date($attributes['start']);
if (!$attributes['allDay']) {
$text .= ' to ' . date_format_date($attributes['end']);
}
if ($node->fc_flyout) {
$options['html'] = TRUE;
$content[] = '<strong>' . $node->title . '</strong>';
$content[] = '<em>' . $text . '</em>';
$content[] = $node->teaser;
$text .= theme('fullcalendar_flyout', $content);
}
$attributes['index'] = $index;
$attributes['start'] = date_format($attributes['start'], DATE_FORMAT_DATETIME);
$attributes['end'] = date_format($attributes['end'], DATE_FORMAT_DATETIME);
$options['attributes'] = $attributes;
return l($text, $node->url, $options);
}
/**
* Check if the user has access to update the given fullcalendar event.
*
* @param object $node
* @return bool
*/
function _fullcalendar_update_access($node) {
global $user;
if (!empty($node) && (user_access('administer nodes')
|| user_access('update any fullcalendar event')
|| user_access('edit any ' . $node->type . ' content')
|| (user_access('edit own ' . $node->type . ' content') && $node->uid == $user->uid))) {
return TRUE;
}
return FALSE;
}
/**
* Find all date fields for a given node.
*/
function fullcalendar_date_fields($node) {
$type = content_types($node->type);
$fields = array();
foreach ($type['fields'] as $field_name => $field) {
if (in_array($field['type'], array('date', 'datestamp', 'datetime'))) {
$fields[$field_name] = $field;
}
}
return $fields;
}
/**
* Check Colorbox dependencies, jQuery version and Colorbox plugin.
*
* @return
* array with TRUE/FALSE for each dependency.
*
* @see fullcalendar_requirements()
*/
function _fullcalendar_status() {
$status = array();
$status['jquery_version'] = FALSE;
$status['fullcalendar_plugin'] = FALSE;
if (function_exists('jquery_update_get_version')) {
if (version_compare(jquery_update_get_version(), FULLCALENDAR_MIN_JQUERY_VERSION, '>=')) {
$status['jquery_version'] = TRUE;
}
}
if (function_exists('jquery_ui_get_version')) {
if (version_compare(jquery_ui_get_version(), FULLCALENDAR_MIN_JQUERYUI_VERSION, '>=')) {
$status['jqueryui_version'] = TRUE;
}
}
if (version_compare(fullcalendar_get_version(), FULLCALENDAR_MIN_PLUGIN_VERSION, '>=')) {
$status['fullcalendar_plugin'] = TRUE;
}
return $status;
}
/**
* Return the version of Colorbox plugin that is installed.
*
* This can be used by other modules' hook_requirements() to ensure that the
* proper version of Colorbox plugin is installed.
*
* @see version_compare()
*/
function fullcalendar_get_version($fullcalendar_path = NULL) {
$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_path();
}
// Return the version of Colorbox plugin.
$fullcalendar_plugin = file_get_contents($fullcalendar_path, NULL, NULL, 0, 40);
if (preg_match($pattern, $fullcalendar_plugin, $matches)) {
$version = $matches[1];
}
return $version;
}
/**
* Return the path to the Colorbox plugin.
*/
function fullcalendar_get_path() {
$fullcalendar_file = array('none' => 'fullcalendar.js', 'min' => 'fullcalendar.min.js');
return variable_get('fullcalendar_path', FULLCALENDAR_PATH) . '/' . $fullcalendar_file[variable_get('fullcalendar_compression_type', 'min')];
}
/**
* Instance of hook_views_pre_execute for per-day limit
*/
function fullcalendar_views_pre_execute(&$view) {
if (!empty($view->display[$view->current_display]->display_options['style_options']['daily_limit'])) {
$query = $view->build_info['query'];
$fields = array();
foreach($view->query->fields as $key => &$field) {
if ($field['alias']) {
$fields[] = $field['alias'];
} else {
$fields[] = $field['field'];
}
}
$inner_fields = 'a.' . implode(', a.', $fields);
$outer_fields = 'node.' . implode(', node.', $fields). ', row_number';
$view->build_info['query'] = 'SELECT ' . $outer_fields . ' FROM ( SELECT ' . $inner_fields . ', @num := if(@type = daily_limiter, @num + 1, 1) AS row_number, @type := daily_limiter AS daily_limiter2 FROM (' . $query . ') AS a ) AS node WHERE row_number <= ' . ($view->display[$view->current_display]->display_options['style_options']['daily_limit'] + 1);
}
}