-
Notifications
You must be signed in to change notification settings - Fork 0
/
fullcalendar_api.module
executable file
·161 lines (148 loc) · 4.32 KB
/
fullcalendar_api.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
<?php
/**
* @file
* Functions and hooks for FullCalendar API module.
*/
/**
* Implements hook_menu().
*/
function fullcalendar_api_menu() {
$items['fullcalendar-api/ajax/update/%/%'] = array(
'title' => 'Update entity',
'description' => 'Save the updated entity date.',
'page callback' => 'fullcalendar_api_update',
'page arguments' => array(3, 4),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Saves the updated FullCalendar event's datetime.
*/
function fullcalendar_api_update($action, $id) {
if (empty($_REQUEST['id'])) {
return;
}
// Retrieve and sanitize POST data.
$id = check_plain($_REQUEST['id']);
$title = check_plain($_REQUEST['title']);
$entity_type = check_plain($_REQUEST['entityType']);
$bundle = check_plain($_REQUEST['bundle']);
$all_day = $_REQUEST['allDay'] ? TRUE : FALSE;
$date_field = check_plain($_REQUEST['dateField']);
$start_time = intval($_REQUEST['startTime']);
$end_time = $_REQUEST['endTime'] ? intval($_REQUEST['endTime']) : NULL;
$entity = entity_load_single($entity_type, $id);
// Directly assign the date value instead of using entity metadata wrappers
// to support custom field type.
// @todo this could be a lot more flexible
$date_value = $entity->{$date_field}[LANGUAGE_NONE][0];
$format_string = DATE_FORMAT_DATETIME;
switch ($date_value['date_type']) {
case 'date':
$format_string = DATE_FORMAT_ISO;
break;
case 'datestamp':
$format_string = DATE_FORMAT_UNIX;
break;
case 'datetime':
case 'ep_datetime':
$format_string = DATE_FORMAT_DATETIME;
break;
}
if ($date_value['date_type'] == 'ep_datetime') {
$date_value['type'] = 'exact';
}
// Get date format and use php date() function.
// @todo timezone?
$date_value['value'] = date($format_string, $start_time);
if (!empty($date_value['value2'])) {
if (empty($end_time)) {
$date_value['value2'] = $date_value['value'];
}
else {
$date_value['value2'] = date($format_string, $end_time);
}
}
$entity->{$date_field}[LANGUAGE_NONE][0] = $date_value;
// entity_save() will throw an error if it did not successfully save,
// which will prevent 'success' from being returned.
entity_save($entity_type, $entity);
drupal_json_output('success');
}
/**
* Implements hook_libraries_info().
*/
function fullcalendar_api_libraries_info() {
$libraries['fullcalendar'] = array(
'name' => 'Full Calendar',
'vendor url' => 'http://fullcalendar.io/',
'download url' => 'http://fullcalendar.io/download/',
'version arguments' => array(
'file' => 'fullcalendar.min.js',
'pattern' => '/FullCalendar v(\d+\.+\d+)/',
'lines' => 2,
),
'files' => array(
'js' => array(
'fullcalendar.min.js' => array(
'scope' => 'footer',
),
),
'css' => array(
'fullcalendar.min.css',
),
),
'dependencies' => array(
'moment',
),
);
return $libraries;
}
/**
* Implements hook_theme().
*/
function fullcalendar_api_theme($existing, $type, $theme, $path) {
return array(
'fullcalendar_calendar' => array(
'file' => 'theme.inc',
'path' => "$path/theme",
'variables' => array(
'calendar_settings' => array(NULL),
),
),
'fullcalendar_calendar_datasource' => array(
'file' => 'theme.inc',
'path' => "$path/theme",
'variables' => array(
'datasource_uri' => NULL,
'calendar_id' => NULL,
'calendar_settings' => array(NULL),
),
),
'fullcalendar_calendar_entity' => array(
'file' => 'theme.inc',
'path' => "$path/theme",
'variables' => array(
'entities' => array(NULL),
'date_field_map' => array(NULL),
'calendar_id' => NULL,
'calendar_settings' => array(NULL),
),
),
);
}
/**
* Helper function to load all scripts necessary to embed FullCalendar.
*/
function fullcalendar_api_load_calendar($calendar_id, $calendar_settings) {
libraries_load('fullcalendar');
drupal_add_js(drupal_get_path('module', 'fullcalendar_api') . '/js/fullcalendar.load.js', array('scope' => 'footer'));
$js_settings = array(
'instances' => array(
$calendar_id => $calendar_settings,
),
);
drupal_add_js(array('fullCalendarApi' => $js_settings), 'setting');
}