-
Notifications
You must be signed in to change notification settings - Fork 9
/
fullcalendar.views_execution.inc
224 lines (179 loc) · 6.93 KB
/
fullcalendar.views_execution.inc
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
<?php
/**
* @file
* Contains Views module runtime hooks.
*/
use Drupal\Component\Utility\Html;
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_plugins_alter().
*/
function fullcalendar_views_plugins_alter(&$plugins) {
$plugins['display']['fullcalendar'] = [
'title' => t('FullCalendar - Deprecated, use page display instead!'),
'help' => t('Stop using this display, use page instead'),
'no ui' => TRUE,
] + $plugins['display']['page'];
}
/**
* Implements hook_views_pre_view().
*
* Add an argument that provides the current date for each date field present.
*/
function fullcalendar_views_pre_view(ViewExecutable $view, $display_id, array $args) {
$style = $view->display_handler->getOption('style');
if ($style['type'] != 'fullcalendar') {
return;
}
// Get the current view settings.
$view->initStyle();
/** @var \Drupal\fullcalendar\Plugin\views\style\FullCalendar $view_style */
$view_style = $view->style_plugin;
$settings = $view->style_plugin->options;
foreach ($view_style->getPlugins() as $plugin) {
$plugin->preView($settings);
}
if (empty($view->display_handler->getOption('use_ajax'))) {
$view->style_plugin->options = $settings;
return;
}
$settings['fullcalendar_fields_count'] = 0;
$exposed_input = $view->getExposedInput();
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$field_manager = \Drupal::getContainer()->get('entity_field.manager');
$entity_type = $view->getBaseEntityType();
$entity_type_id = $entity_type->id();
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storages */
$field_storages = $field_manager->getFieldStorageDefinitions($entity_type_id);
// Loop through each date field and provide an argument for it.
foreach ($view->display_handler->getHandlers('field') as $field_id => $field) {
/** @var \Drupal\views\Plugin\views\field\EntityField $field */
if (!fullcalendar_field_is_date($field)) {
continue;
}
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
$field_storage = $field_storages[$field->definition['field_name']];
// Default table name for the field.
$field_table = $field_storage->getTargetEntityTypeId() . '__' . $field_storage->getName();
// If the field is a DateRecur field, need to apply table prefix.
if ($field_storage->getType() == 'date_recur') {
$field_table = 'date_recur__' . $field_table;
}
$field_value = $field_storage->getName() . '_value';
// Min and Max dates for exposed filter.
$dateMin = new DateTime();
$dateMax = new DateTime();
// First, we try to set initial Min and Max date values based on the
// exposed form values.
if (isset($exposed_input[$field_value])) {
$dateMin->setTimestamp(strtotime($exposed_input[$field_value]['min']));
$dateMax->setTimestamp(strtotime($exposed_input[$field_value]['max']));
}
// If no exposed values set, use user-defined date values.
elseif (!empty($settings['date']['month']) && !empty($settings['date']['year'])) {
$ts = mktime(0, 0, 0, $settings['date']['month'] + 1, 1, $settings['date']['year']);
$dateMin->setTimestamp($ts);
$dateMax->setTimestamp($ts);
$dateMin->modify('first day of this month');
$dateMax->modify('first day of next month');
}
// Use default 1 month date-range.
else {
$dateMin->modify('first day of this month');
$dateMax->modify('first day of next month');
}
$options = [
'exposed' => TRUE,
'form_type' => 'date_select',
'operator' => 'between',
'value' => [
'type' => 'date',
'min' => $dateMin->format('Y-m-d'),
'max' => $dateMax->format('Y-m-d'),
],
'group' => 'fullcalendar',
];
if (!empty($field->options['relationship'])) {
$options['relationship'] = $field->options['relationship'];
}
$option_id = $view->addHandler($display_id, 'filter', $field_table, $field_value, $options);
$settings['fullcalendar_fields'][$option_id] = Html::getClass($option_id);
$settings['fullcalendar_fields_count']++;
$view->setHandlerOption($display_id, 'filter', $option_id, 'expose', [
'identifier' => $option_id,
'operator' => $option_id . '_op',
]);
}
// Needs for JavaScript.
$settings['ajax'] = $view->display_handler->getOption('use_ajax');
$view->style_plugin->options = $settings;
}
/**
* Implements hook_views_query_alter().
*/
function fullcalendar_views_query_alter($view, $query) {
$style = $view->display_handler->getOption('style');
if ($style['type'] != 'fullcalendar') {
return;
}
// Force the query to be distinct.
$query->distinct = TRUE;
// Try to add additional condition to the query.
foreach ($query->where as $group => &$condition_group) {
if ($group !== 'fullcalendar') {
continue;
}
// Prepare array for extracted query data.
$data = [
'field_min' => NULL,
'field_max' => NULL,
'date_min' => NULL,
'date_max' => NULL,
];
foreach ($condition_group['conditions'] as $condition) {
// This is an example for the original condition:
// DATE_FORMAT(cm_event__field_cm_event_date.field_cm_event_date_value, '%Y-%m-%d\T%H:%i:%s')
// BETWEEN
// DATE_FORMAT('2017-08-31T22:00:00', '%Y-%m-%d\T%H:%i:%s') AND DATE_FORMAT('2017-09-29T22:00:00', '%Y-%m-%d\T%H:%i:%s')
// Try to extract field names from the current condition.
$parts = explode(' BETWEEN ', $condition['field']);
if (count($parts) !== 2) {
continue;
}
$data['field_min'] = $parts[0];
$data['field_max'] = str_replace('_value,', '_end_value,', $data['field_min']);
// Try to extract dates from the current condition.
$parts = explode(' AND ', $parts[1]);
if (count($parts) !== 2) {
continue;
}
$data['date_min'] = $parts[0];
$data['date_max'] = $parts[1];
}
// If 'date_max' exists, then all required values exist, so we can add our
// custom conditions.
if (!empty($data['date_max'])) {
// Change condition group type to 'OR'.
$condition_group['type'] = 'OR';
$or_conditions = [];
// If event starts before and ends after the first day of the calendar.
// ('event_start' <= 'calendar_start') AND 'event_end' >= 'calendar_start'
$or_conditions[] = $data['field_min'] . ' <= ' . $data['date_min'] . ' AND ' . $data['field_max'] . ' >= ' . $data['date_min'];
// Add additional condition to the group.
$condition_group['conditions'][] = [
'field' => '(' . implode(') OR (', $or_conditions) . ')',
'value' => [],
'operator' => 'formula',
];
}
}
}
/**
* Implements hook_views_ajax_data_alter().
*/
function fullcalendar_views_ajax_data_alter(&$commands, &$view) {
$style = $view->display_handler->getOption('style');
if ($style['type'] != 'fullcalendar') {
$commands = [];
}
}