1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* Contains the Calendar row style plugin.
|
5 |
|
|
*
|
6 |
|
|
* This plugin takes the view results, finds the date value for each,
|
7 |
|
|
* then compares that date to the date range for the current view.
|
8 |
|
|
* Items that started before or ended after the current date range
|
9 |
|
|
* are shortened to the current range. Items that extend over more
|
10 |
|
|
* than one day are cloned to create a calendar item for each day.
|
11 |
|
|
* The resulting array of results (which may have a different number
|
12 |
|
|
* of items than the original view result) are then passed back
|
13 |
|
|
* to the style plugin so they can be displayed in a calendar.
|
14 |
|
|
*
|
15 |
|
|
*/
|
16 |
|
|
|
17 |
|
|
/**
|
18 |
|
|
* Plugin which creates a view on the resulting object
|
19 |
|
|
* and formats it as a Calendar node.
|
20 |
|
|
*/
|
21 |
|
|
class calendar_plugin_row extends views_plugin_row_fields {
|
22 |
|
|
|
23 |
|
|
// Stores the entities loaded with pre_render.
|
24 |
|
|
var $entities = array();
|
25 |
|
|
|
26 |
|
|
function init(&$view, &$display, $options = NULL) {
|
27 |
|
|
parent::init($view, $display, $options);
|
28 |
|
|
$this->base_table = $view->base_table;
|
29 |
|
|
$this->base_field = $view->base_field;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* Helper function to find the date argument handler for this view.
|
34 |
|
|
*/
|
35 |
|
|
function date_argument_handler() {
|
36 |
|
|
foreach ($this->view->argument as $name => $handler) {
|
37 |
|
|
if (date_views_handler_is_date($handler, 'argument')) {
|
38 |
|
|
return $handler;
|
39 |
|
|
}
|
40 |
|
|
}
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
function option_definition() {
|
44 |
|
|
$options = parent::option_definition();
|
45 |
|
|
$options['date_fields'] = array('default' => array());
|
46 |
|
|
$options['calendar_date_link'] = array('default' => '');
|
47 |
|
|
$options['colors'] = array(
|
48 |
|
|
'contains' => array(
|
49 |
|
|
'legend' => array('default' => ''),
|
50 |
|
|
'calendar_colors_type' => array('default' => array()),
|
51 |
|
|
'taxonomy_field' => array('default' => ''),
|
52 |
|
|
'calendar_colors_vocabulary' => array('default' => array()),
|
53 |
|
|
'calendar_colors_taxonomy' => array('default' => array()),
|
54 |
|
|
'calendar_colors_group' => array('default' => array()),
|
55 |
|
|
));
|
56 |
|
|
return $options;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* Provide a form for setting options.
|
61 |
|
|
*/
|
62 |
|
|
function options_form(&$form, &$form_state) {
|
63 |
|
|
parent::options_form($form, $form_state);
|
64 |
|
|
|
65 |
|
|
$form['markup']['#markup'] = t("The calendar row plugin will format view results as calendar items. Make sure this display has a 'Calendar' format and uses a 'Date' contextual filter, or this plugin will not work correctly.");
|
66 |
|
|
$form['calendar_date_link'] = array(
|
67 |
|
|
'#title' => t('Add new date link'),
|
68 |
|
|
'#type' => 'select',
|
69 |
|
|
'#default_value' => $this->options['calendar_date_link'],
|
70 |
|
|
'#options' => array('' => t('No link')) + node_type_get_names(),
|
71 |
|
|
'#description' => t('Display a link to add a new date of the specified content type. Displayed only to users with appropriate permissions.'),
|
72 |
|
|
);
|
73 |
|
|
$form['colors'] = array(
|
74 |
|
|
'#type' => 'fieldset',
|
75 |
|
|
'#title' => t('Legend Colors'),
|
76 |
|
|
'#description' => t('Set a hex color value (like #ffffff) to use in the calendar legend for each content type. Items with empty values will have no stripe in the calendar and will not be added to the legend.'),
|
77 |
|
|
);
|
78 |
|
|
$options = array(
|
79 |
|
|
'' => t('None')
|
80 |
|
|
);
|
81 |
|
|
if ($this->view->base_table == 'node') {
|
82 |
|
|
$options['type'] = t('Based on Content Type');
|
83 |
|
|
}
|
84 |
|
|
if (module_exists('taxonomy')) {
|
85 |
|
|
$options['taxonomy'] = t('Based on Taxonomy');
|
86 |
|
|
}
|
87 |
|
|
if (module_exists('og')) {
|
88 |
|
|
$options['group'] = t('Based on Organic Group');
|
89 |
|
|
}
|
90 |
|
|
// If none of the options but the None option is available, stop here.
|
91 |
|
|
if (count($options) == 1) {
|
92 |
|
|
return;
|
93 |
|
|
}
|
94 |
|
|
$form['colors']['legend'] = array(
|
95 |
|
|
'#title' => t('Stripes'),
|
96 |
|
|
'#description' => t('Add stripes to calendar items.'),
|
97 |
|
|
'#type' => 'select',
|
98 |
|
|
'#options' => $options,
|
99 |
|
|
'#default_value' => $this->options['colors']['legend'],
|
100 |
|
|
);
|
101 |
|
|
if ($this->view->base_table == 'node') {
|
102 |
|
|
$colors = $this->options['colors']['calendar_colors_type'];
|
103 |
|
|
$type_names = node_type_get_names();
|
104 |
|
|
foreach ($type_names as $key => $name) {
|
105 |
|
|
$form['colors']['calendar_colors_type'][$key] = array(
|
106 |
|
|
'#title' => check_plain($name),
|
107 |
|
|
'#default_value' => isset($colors[$key]) ? $colors[$key] : CALENDAR_EMPTY_STRIPE,
|
108 |
|
|
'#dependency' => array('edit-row-options-colors-legend' => array('type')),
|
109 |
|
|
'#type' => 'textfield',
|
110 |
|
|
'#size' => 7,
|
111 |
|
|
'#maxlength' => 7,
|
112 |
|
|
'#element_validate' => array('calendar_validate_hex_color'),
|
113 |
|
|
'#prefix' => '<div class="calendar-colorpicker-wrapper">',
|
114 |
|
|
'#suffix' => '<div class="calendar-colorpicker"></div></div>',
|
115 |
|
|
'#attributes' => array('class' => array('edit-calendar-colorpicker')),
|
116 |
|
|
'#attached' => array(
|
117 |
|
|
// Add Farbtastic color picker.
|
118 |
|
|
'library' => array(
|
119 |
|
|
array('system', 'farbtastic'),
|
120 |
|
|
),
|
121 |
|
|
// Add javascript to trigger the colorpicker.
|
122 |
|
|
'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'),
|
123 |
|
|
),
|
124 |
|
|
);
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
if (module_exists('taxonomy')) {
|
128 |
|
|
// Get the display's field names of taxonomy fields.
|
129 |
|
|
$vocab_field_options = array();
|
130 |
|
|
$fields = $this->display->handler->get_option('fields');
|
131 |
|
|
foreach ($fields as $name => $field_info) {
|
132 |
|
|
$field = field_info_field($name);
|
133 |
|
|
// Select the proper field type.
|
134 |
|
|
if (!empty($field['type']) && $field['type'] == 'taxonomy_term_reference') {
|
135 |
|
|
$vocab_field_options[$name] = $field_info['label'];
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
$form['colors']['taxonomy_field'] = array(
|
139 |
|
|
'#title' => t('Term field'),
|
140 |
|
|
'#type' => !empty($vocab_field_options) ? 'select' : 'hidden',
|
141 |
|
|
'#default_value' => $this->options['colors']['taxonomy_field'],
|
142 |
|
|
'#description' => t("Select the taxonomy term field to use when setting stripe colors. This works best for vocabularies with only a limited number of possible terms."),
|
143 |
|
|
'#options' => $vocab_field_options,
|
144 |
|
|
'#dependency' => array('edit-row-options-colors-legend' => array('taxonomy')),
|
145 |
|
|
);
|
146 |
|
|
if (empty($vocab_field_options)) {
|
147 |
|
|
$form['colors']['taxonomy_field']['#options'] = array('' => '');
|
148 |
|
|
$form['colors']['taxonomy_field']['#suffix'] = t('You must add a term field to this view to use taxonomy stripe values. This works best for vocabularies with only a limited number of possible terms.');
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
// Get the Vocabulary names.
|
152 |
|
|
$vocab_names = array();
|
153 |
|
|
foreach ($vocab_field_options as $field_name => $label) {
|
154 |
|
|
$taxonomy_field = field_info_field($field_name);
|
155 |
|
|
foreach ((array) $taxonomy_field['settings']['allowed_values'] as $delta => $options) {
|
156 |
|
|
$vocab_names[$field_name] = $options['vocabulary'];
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
// Get the Vocabulary id's.
|
161 |
|
|
$vocab_vids = array();
|
162 |
|
|
foreach ($vocab_names as $field_name => $vocab_name) {
|
163 |
|
|
$vocab = taxonomy_vocabulary_machine_name_load($vocab_name);
|
164 |
|
|
$vocab_vids[$field_name] = $vocab->vid;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
$this->options['colors']['calendar_colors_vocabulary'] = $vocab_vids;
|
168 |
|
|
|
169 |
|
|
$form['colors']['calendar_colors_vocabulary'] = array(
|
170 |
|
|
'#title' => t('Vocabulary Legend Types'),
|
171 |
|
|
'#type' => 'value',
|
172 |
|
|
'#value' => $vocab_vids,
|
173 |
|
|
);
|
174 |
|
|
|
175 |
|
|
// Get the Vocabulary term id's and map to colors.
|
176 |
|
|
$term_colors = $this->options['colors']['calendar_colors_taxonomy'];
|
177 |
|
|
foreach ($vocab_vids as $field_name => $vid) {
|
178 |
|
|
$vocab = taxonomy_get_tree($vid);
|
179 |
|
|
foreach ($vocab as $key => $term) {
|
180 |
|
|
$form['colors']['calendar_colors_taxonomy'][$term->tid] = array(
|
181 |
|
|
'#title' => check_plain(t($term->name)),
|
182 |
|
|
'#default_value' => isset($term_colors[$term->tid]) ? $term_colors[$term->tid] : CALENDAR_EMPTY_STRIPE,
|
183 |
|
|
'#access' => !empty($vocab_field_options),
|
184 |
|
|
'#dependency_count' => 2, // All 2 of the following #dependencies must be met.
|
185 |
|
|
'#dependency' => array(
|
186 |
|
|
'edit-row-options-colors-legend' => array('taxonomy'),
|
187 |
|
|
'edit-row-options-colors-taxonomy-field' => array($field_name),
|
188 |
|
|
),
|
189 |
|
|
'#type' => 'textfield',
|
190 |
|
|
'#size' => 7,
|
191 |
|
|
'#maxlength' => 7,
|
192 |
|
|
'#element_validate' => array('calendar_validate_hex_color'),
|
193 |
|
|
'#prefix' => '<div class="calendar-colorpicker-wrapper">',
|
194 |
|
|
'#suffix' => '<div class="calendar-colorpicker"></div></div>',
|
195 |
|
|
'#attributes' => array('class' => array('edit-calendar-colorpicker')),
|
196 |
|
|
'#attached' => array(
|
197 |
|
|
// Add Farbtastic color picker.
|
198 |
|
|
'library' => array(
|
199 |
|
|
array('system', 'farbtastic'),
|
200 |
|
|
),
|
201 |
|
|
// Add javascript to trigger the colorpicker.
|
202 |
|
|
'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'),
|
203 |
|
|
),
|
204 |
|
|
);
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
}
|
208 |
|
|
if (module_exists('og')) {
|
209 |
|
|
$colors_group = $this->options['colors']['calendar_colors_group'];
|
210 |
|
|
$options = array();
|
211 |
|
|
|
212 |
|
|
// The 7.1 version of OG.
|
213 |
|
|
if (function_exists('og_label')) {
|
214 |
|
|
$gids = og_get_all_group();
|
215 |
|
|
foreach ($gids as $gid) {
|
216 |
|
|
$options[$gid] = check_plain(t(og_label($gid)));
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
// The 7.2 version of OG.
|
221 |
|
|
else {
|
222 |
|
|
$types = og_get_all_group_entity();
|
223 |
|
|
foreach ($types as $entity_type => $name) {
|
224 |
|
|
$gids = og_get_all_group($entity_type);
|
225 |
|
|
$entities = entity_load($entity_type, $gids);
|
226 |
|
|
foreach ($entities as $entity) {
|
227 |
|
|
list($gid) = entity_extract_ids($entity_type, $entity);
|
228 |
|
|
$options[$gid] = check_plain(t(entity_label($entity_type, $entity)));
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
foreach ($options as $gid => $title) {
|
234 |
|
|
$form['colors']['calendar_colors_group'][$gid] = array(
|
235 |
|
|
'#title' => $title,
|
236 |
|
|
'#default_value' => isset($colors_group[$gid]) ? $colors_group[$gid] : CALENDAR_EMPTY_STRIPE,
|
237 |
|
|
'#dependency' => array('edit-row-options-colors-legend' => array('group')),
|
238 |
|
|
'#type' => 'textfield',
|
239 |
|
|
'#size' => 7,
|
240 |
|
|
'#maxlength' => 7,
|
241 |
|
|
'#element_validate' => array('calendar_validate_hex_color'),
|
242 |
|
|
'#prefix' => '<div class="calendar-colorpicker-wrapper">',
|
243 |
|
|
'#suffix' => '<div class="calendar-colorpicker"></div></div>',
|
244 |
|
|
'#attributes' => array('class' => array('edit-calendar-colorpicker')),
|
245 |
|
|
'#attached' => array(
|
246 |
|
|
// Add Farbtastic color picker.
|
247 |
|
|
'library' => array(
|
248 |
|
|
array('system', 'farbtastic'),
|
249 |
|
|
),
|
250 |
|
|
// Add javascript to trigger the colorpicker.
|
251 |
|
|
'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'),
|
252 |
|
|
),
|
253 |
|
|
);
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
function options_submit(&$form, &$form_state) {
|
259 |
|
|
parent::options_submit($form, $form_state);
|
260 |
|
|
if ($this->view->base_table == 'node') {
|
261 |
|
|
if (isset($this->view->display['default']->display_options['link_display'])) {
|
262 |
|
|
$default_display = $this->view->display['default']->display_options['link_display'];
|
263 |
|
|
$path = $this->view->display[$default_display]->handler->get_option('path');
|
264 |
|
|
// If this display has been set up as a default tab, the current path
|
265 |
|
|
// is actually the base path, i.e. if the path is 'calendar/month'
|
266 |
|
|
// and this is a default tab, the path for this display will actually
|
267 |
|
|
// be 'calendar'.
|
268 |
|
|
if ($this->view->display[$default_display]->handler->options['menu']['type'] == 'default tab') {
|
269 |
|
|
$parts = explode('/', $path);
|
270 |
|
|
array_pop($parts);
|
271 |
|
|
$path = implode('/', $parts);
|
272 |
|
|
}
|
273 |
|
|
calendar_clear_link_path($path);
|
274 |
|
|
if (!empty($form_state['values']['row_options']['calendar_date_link'])) {
|
275 |
|
|
$node_type = $form_state['values']['row_options']['calendar_date_link'];
|
276 |
|
|
calendar_set_link('node', $node_type, $path);
|
277 |
|
|
}
|
278 |
|
|
}
|
279 |
|
|
}
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
function pre_render($values) {
|
283 |
|
|
|
284 |
|
|
// Preload each entity used in this view from the cache.
|
285 |
|
|
// Provides all the entity values relatively cheaply, and we don't
|
286 |
|
|
// need to do it repeatedly for the same entity if there are
|
287 |
|
|
// multiple results for one entity.
|
288 |
|
|
$ids = array();
|
289 |
|
|
foreach ($values as $row) {
|
290 |
|
|
// Use the $id as the key so we don't create more than one value per entity.
|
291 |
|
|
// This alias will automatically adjust to be the id of related entity, if applicable.
|
292 |
|
|
$id = $row->{$this->field_alias};
|
293 |
|
|
|
294 |
|
|
// Node revisions need special loading.
|
295 |
|
|
if ($this->view->base_table == 'node_revision') {
|
296 |
|
|
$this->entities[$id] = node_load(NULL, $id);
|
297 |
|
|
}
|
298 |
|
|
// For other entities we just create an array of ids to pass
|
299 |
|
|
// to entity_load().
|
300 |
|
|
else {
|
301 |
|
|
$ids[$id] = $id;
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
$base_tables = date_views_base_tables();
|
306 |
|
|
$this->entity_type = $base_tables[$this->view->base_table];
|
307 |
|
|
if (!empty($ids)) {
|
308 |
|
|
$this->entities = entity_load($this->entity_type, $ids);
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
// Let the style know if a link to create a new date is required.
|
312 |
|
|
$this->view->date_info->calendar_date_link = $this->options['calendar_date_link'];
|
313 |
|
|
|
314 |
|
|
// Identify the date argument and fields that apply to this view.
|
315 |
|
|
// Preload the Date Views field info for each field, keyed by the
|
316 |
|
|
// field name, so we know how to retrieve field values from the cached node.
|
317 |
|
|
$data = date_views_fields($this->view->base_table);
|
318 |
|
|
$data = $data['name'];
|
319 |
|
|
$date_fields = array();
|
320 |
|
|
foreach ($this->view->argument as $handler) {
|
321 |
|
|
if (date_views_handler_is_date($handler, 'argument')) {
|
322 |
|
|
// If this is the complex Date argument, the date fields are stored in the handler options,
|
323 |
|
|
// otherwise we are using the simple date field argument handler.
|
324 |
|
|
if ($handler->definition['handler'] != 'date_views_argument_handler') {
|
325 |
|
|
$alias = $handler->table_alias . '.' . $handler->field;
|
326 |
|
|
$info = $data[$alias];
|
327 |
|
|
$field_name = str_replace(array('_value2', '_value'), '', $info['real_field_name']);
|
328 |
|
|
$date_fields[$field_name] = $info;
|
329 |
|
|
}
|
330 |
|
|
else {
|
331 |
|
|
foreach ($handler->options['date_fields'] as $alias) {
|
332 |
|
|
// If this date field is unknown (as when it has been deleted), ignore it.
|
333 |
|
|
if (!array_key_exists($alias, $data)) {
|
334 |
|
|
continue;
|
335 |
|
|
}
|
336 |
|
|
$info = $data[$alias];
|
337 |
|
|
$field_name = str_replace(array('_value2', '_value'), '', $info['real_field_name']);
|
338 |
|
|
|
339 |
|
|
// This is ugly and hacky but I can't figure out any generic way to
|
340 |
|
|
// recognize that the node module is going to give some the revision timestamp
|
341 |
|
|
// a different field name on the entity than the actual column name in the database.
|
342 |
|
|
if ($this->view->base_table == 'node_revision' && $field_name == 'timestamp') {
|
343 |
|
|
$field_name = 'revision_timestamp';
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
$date_fields[$field_name] = $info;
|
347 |
|
|
}
|
348 |
|
|
}
|
349 |
|
|
$this->date_argument = $handler;
|
350 |
|
|
$this->date_fields = $date_fields;
|
351 |
|
|
}
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
// Get the language for this view.
|
355 |
|
|
$this->language = $this->display->handler->get_option('field_language');
|
356 |
|
|
$substitutions = views_views_query_substitutions($this->view);
|
357 |
|
|
if (array_key_exists($this->language, $substitutions)) {
|
358 |
|
|
$this->language = $substitutions[$this->language];
|
359 |
|
|
}
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
function render($row) {
|
363 |
|
|
global $base_url;
|
364 |
|
|
$rows = array();
|
365 |
|
|
$date_info = $this->date_argument->view->date_info;
|
366 |
|
|
$id = $row->{$this->field_alias};
|
367 |
|
|
|
368 |
|
|
if (!is_numeric($id)) {
|
369 |
|
|
return $rows;
|
370 |
|
|
}
|
371 |
|
|
|
372 |
|
|
// There could be more than one date field in a view
|
373 |
|
|
// so iterate through all of them to find the right values
|
374 |
|
|
// for this view result.
|
375 |
|
|
foreach ($this->date_fields as $field_name => $info) {
|
376 |
|
|
|
377 |
|
|
// Load the specified node:
|
378 |
|
|
// We have to clone this or nodes on other views on this page,
|
379 |
|
|
// like an Upcoming block on the same page as a calendar view,
|
380 |
|
|
// will end up acquiring the values we set here.
|
381 |
|
|
$entity = clone($this->entities[$id]);
|
382 |
|
|
|
383 |
|
|
if (empty($entity)) {
|
384 |
|
|
return $rows;
|
385 |
|
|
}
|
386 |
|
|
|
387 |
|
|
$table_name = $info['table_name'];
|
388 |
|
|
$delta_field = $info['delta_field'];
|
389 |
|
|
$tz_handling = $info['tz_handling'];
|
390 |
|
|
$tz_field = $info['timezone_field'];
|
391 |
|
|
$rrule_field = $info['rrule_field'];
|
392 |
|
|
$is_field = $info['is_field'];
|
393 |
|
|
|
394 |
|
|
$info = entity_get_info($this->entity_type);
|
395 |
|
|
$this->id_field = $info['entity keys']['id'];
|
396 |
|
|
$this->id = $entity->{$this->id_field};
|
397 |
|
|
$this->type = !empty($info['entity keys']['bundle']) ? $info['entity keys']['bundle'] : $this->entity_type;
|
398 |
|
|
$this->title = entity_label($this->entity_type, $entity);
|
399 |
|
|
$uri = entity_uri($this->entity_type, $entity);
|
400 |
|
|
$uri['options']['absolute'] = TRUE;
|
401 |
|
|
$this->url = url($uri['path'], $uri['options']);
|
402 |
|
|
|
403 |
|
|
// Retrieve the field value(s) that matched our query from the cached node.
|
404 |
|
|
// Find the date and set it to the right timezone.
|
405 |
|
|
|
406 |
|
|
$entity->date_id = array();
|
407 |
|
|
$item_start_date = NULL;
|
408 |
|
|
$item_end_date = NULL;
|
409 |
|
|
$granularity = 'second';
|
410 |
|
|
$increment = 1;
|
411 |
|
|
if ($is_field) {
|
412 |
|
|
|
413 |
|
|
// Set the date_id for the node, used to identify which field value to display for
|
414 |
|
|
// fields that have multiple values. The theme expects it to be an array.
|
415 |
|
|
$date_id = 'date_id_' . $field_name;
|
416 |
|
|
$date_delta = 'date_delta_' . $field_name;
|
417 |
|
|
if (isset($row->$date_id)) {
|
418 |
|
|
$delta = $row->$date_delta;
|
419 |
|
|
$entity->date_id = array('calendar.' . $row->$date_id . '.' . $field_name. '.' . $delta);
|
420 |
|
|
$delta_field = $date_delta;
|
421 |
|
|
}
|
422 |
|
|
else {
|
423 |
|
|
$delta = isset($row->$delta_field) ? $row->$delta_field : 0;
|
424 |
|
|
$entity->date_id = array('calendar.' . $id . '.' . $field_name . '.' . $delta);
|
425 |
|
|
}
|
426 |
|
|
|
427 |
|
|
$items = field_get_items($this->entity_type, $entity, $field_name, $this->language);
|
428 |
|
|
$item = $items[$delta];
|
429 |
|
|
$db_tz = date_get_timezone_db($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
|
430 |
|
|
$to_zone = date_get_timezone($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
|
431 |
|
|
if (isset($item['value'])) {
|
432 |
|
|
$item_start_date = new dateObject($item['value'], $db_tz);
|
433 |
|
|
$item_end_date = array_key_exists('value2', $item) ? new dateObject($item['value2'], $db_tz) : $item_start_date;
|
434 |
|
|
}
|
435 |
|
|
|
436 |
|
|
$cck_field = field_info_field($field_name);
|
437 |
|
|
$instance = field_info_instance($this->entity_type, $field_name, $this->type);
|
438 |
|
|
$granularity = date_granularity_precision($cck_field['settings']['granularity']);
|
439 |
|
|
$increment = $instance['widget']['settings']['increment'];
|
440 |
|
|
|
441 |
|
|
}
|
442 |
|
|
elseif (!empty($entity->$field_name)) {
|
443 |
|
|
$item = $entity->$field_name;
|
444 |
|
|
$db_tz = date_get_timezone_db($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
|
445 |
|
|
$to_zone = date_get_timezone($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
|
446 |
|
|
$item_start_date = new dateObject($item, $db_tz);
|
447 |
|
|
$item_end_date = $item_start_date;
|
448 |
|
|
$entity->date_id = array('calendar.' . $id . '.' . $field_name . '.0');
|
449 |
|
|
}
|
450 |
|
|
|
451 |
|
|
// If we don't have a date value, go no further.
|
452 |
|
|
if (empty($item_start_date)) {
|
453 |
|
|
continue;
|
454 |
|
|
}
|
455 |
|
|
|
456 |
|
|
// Set the item date to the proper display timezone;
|
457 |
|
|
$item_start_date->setTimezone(new dateTimezone($to_zone));
|
458 |
|
|
$item_end_date->setTimezone(new dateTimezone($to_zone));
|
459 |
|
|
|
460 |
|
|
$event = new stdClass();
|
461 |
|
|
$event->id = $this->id;
|
462 |
|
|
$event->title = $this->title;
|
463 |
|
|
$event->type = $this->type;
|
464 |
|
|
$event->date_start = $item_start_date;
|
465 |
|
|
$event->date_end = $item_end_date;
|
466 |
|
|
$event->db_tz = $db_tz;
|
467 |
|
|
$event->to_zone = $to_zone;
|
468 |
|
|
$event->granularity = $granularity;
|
469 |
|
|
$event->increment = $increment;
|
470 |
|
|
$event->field = $is_field ? $item : NULL;
|
471 |
|
|
$event->url = $this->url;
|
472 |
|
|
$event->row = $row;
|
473 |
|
|
$event->entity = $entity;
|
474 |
|
|
$event->stripe = array();
|
475 |
|
|
$event->stripe_label = array();
|
476 |
|
|
|
477 |
|
|
// All calendar row plugins should provide a date_id that the theme can use.
|
478 |
|
|
$event->date_id = $entity->date_id[0];
|
479 |
|
|
|
480 |
|
|
// We are working with an array of partially rendered items
|
481 |
|
|
// as we process the calendar, so we can group and organize them.
|
482 |
|
|
// At the end of our processing we'll need to swap in the fully formatted
|
483 |
|
|
// display of the row. We save it here and switch it in
|
484 |
|
|
// template_preprocess_calendar_item().
|
485 |
|
|
$event->rendered = theme($this->theme_functions(),
|
486 |
|
|
array(
|
487 |
|
|
'view' => $this->view,
|
488 |
|
|
'options' => $this->options,
|
489 |
|
|
'row' => $row,
|
490 |
|
|
'field_alias' => isset($this->field_alias) ? $this->field_alias : '',
|
491 |
|
|
));
|
492 |
|
|
|
493 |
|
|
$entities = $this->explode_values($event);
|
494 |
|
|
foreach ($entities as $entity) {
|
495 |
|
|
switch ($this->options['colors']['legend']) {
|
496 |
|
|
case 'type':
|
497 |
|
|
$this->calendar_node_type_stripe($entity);
|
498 |
|
|
break;
|
499 |
|
|
case 'taxonomy':
|
500 |
|
|
$this->calendar_taxonomy_stripe($entity);
|
501 |
|
|
break;
|
502 |
|
|
case 'group':
|
503 |
|
|
$this->calendar_group_stripe($entity);
|
504 |
|
|
break;
|
505 |
|
|
}
|
506 |
|
|
$rows[] = $entity;
|
507 |
|
|
}
|
508 |
|
|
|
509 |
|
|
}
|
510 |
|
|
|
511 |
|
|
return $rows;
|
512 |
|
|
}
|
513 |
|
|
|
514 |
|
|
function explode_values($event) {
|
515 |
|
|
$rows = array();
|
516 |
|
|
|
517 |
|
|
$date_info = $this->date_argument->view->date_info;
|
518 |
|
|
$item_start_date = $event->date_start;
|
519 |
|
|
$item_end_date = $event->date_end;
|
520 |
|
|
$to_zone = $event->to_zone;
|
521 |
|
|
$db_tz = $event->db_tz;
|
522 |
|
|
$granularity = $event->granularity;
|
523 |
|
|
$increment = $event->increment;
|
524 |
|
|
|
525 |
|
|
// Now that we have an 'entity' for each view result, we need
|
526 |
|
|
// to remove anything outside the view date range,
|
527 |
|
|
// and possibly create additional nodes so that we have a 'node'
|
528 |
|
|
// for each day that this item occupies in this view.
|
529 |
|
|
$now = max($date_info->min_zone_string, $item_start_date->format(DATE_FORMAT_DATE));
|
530 |
|
|
$to = min($date_info->max_zone_string, $item_end_date->format(DATE_FORMAT_DATE));
|
531 |
|
|
$next = new DateObject($now . ' 00:00:00', $date_info->display_timezone);
|
532 |
|
|
if ($date_info->display_timezone_name != $to_zone) {
|
533 |
|
|
// Make $start and $end (derived from $node) use the timezone $to_zone, just as the original dates do.
|
534 |
|
|
date_timezone_set($next, timezone_open($to_zone));
|
535 |
|
|
}
|
536 |
|
|
if (empty($to) || $now > $to) {
|
537 |
|
|
$to = $now;
|
538 |
|
|
}
|
539 |
|
|
// $now and $next are midnight (in display timezone) on the first day where node will occur.
|
540 |
|
|
// $to is midnight on the last day where node will occur.
|
541 |
|
|
// All three were limited by the min-max date range of the view.
|
542 |
|
|
$pos = 0;
|
543 |
|
|
while (!empty($now) && $now <= $to) {
|
544 |
|
|
$entity = clone($event);
|
545 |
|
|
|
546 |
|
|
// Get start and end of current day.
|
547 |
|
|
$start = $next->format(DATE_FORMAT_DATETIME);
|
548 |
|
|
date_modify($next, '+1 day');
|
549 |
|
|
date_modify($next, '-1 second');
|
550 |
|
|
$end = $next->format(DATE_FORMAT_DATETIME);
|
551 |
|
|
|
552 |
|
|
// Get start and end of item, formatted the same way.
|
553 |
|
|
$item_start = $item_start_date->format(DATE_FORMAT_DATETIME);
|
554 |
|
|
$item_end = $item_end_date->format(DATE_FORMAT_DATETIME);
|
555 |
|
|
|
556 |
|
|
// Get intersection of current day and the node value's duration (as strings in $to_zone timezone).
|
557 |
|
|
$entity->calendar_start = $item_start < $start ? $start : $item_start;
|
558 |
|
|
$entity->calendar_end = !empty($item_end) ? ($item_end > $end ? $end : $item_end) : $node->calendar_start;
|
559 |
|
|
|
560 |
|
|
// Make date objects
|
561 |
|
|
$entity->calendar_start_date = date_create($entity->calendar_start, timezone_open($to_zone));
|
562 |
|
|
$entity->calendar_end_date = date_create($entity->calendar_end, timezone_open($to_zone));
|
563 |
|
|
|
564 |
|
|
// Change string timezones into
|
565 |
|
|
// calendar_start and calendar_end are UTC dates as formatted strings
|
566 |
|
|
$entity->calendar_start = date_format($entity->calendar_start_date, DATE_FORMAT_DATETIME);
|
567 |
|
|
$entity->calendar_end = date_format($entity->calendar_end_date, DATE_FORMAT_DATETIME);
|
568 |
|
|
$entity->calendar_all_day = date_is_all_day($entity->calendar_start, $entity->calendar_end, $granularity, $increment);
|
569 |
|
|
|
570 |
|
|
unset($entity->calendar_fields);
|
571 |
|
|
if (isset($entity) && (empty($entity->calendar_start))) {
|
572 |
|
|
// if no date for the node and no date in the item
|
573 |
|
|
// there is no way to display it on the calendar
|
574 |
|
|
unset($entity);
|
575 |
|
|
}
|
576 |
|
|
else {
|
577 |
|
|
$entity->date_id .= '.' . $pos;
|
578 |
|
|
|
579 |
|
|
$rows[] = $entity;
|
580 |
|
|
unset($entity);
|
581 |
|
|
}
|
582 |
|
|
date_modify($next, '+1 second');
|
583 |
|
|
$now = date_format($next, DATE_FORMAT_DATE);
|
584 |
|
|
$pos++;
|
585 |
|
|
|
586 |
|
|
}
|
587 |
|
|
return $rows;
|
588 |
|
|
}
|
589 |
|
|
|
590 |
|
|
/**
|
591 |
|
|
* Create a stripe base on node type.
|
592 |
|
|
*/
|
593 |
|
|
function calendar_node_type_stripe(&$result) {
|
594 |
|
|
$colors = isset($this->options['colors']['calendar_colors_type']) ? $this->options['colors']['calendar_colors_type'] : array();
|
595 |
|
|
if (empty($colors)) {
|
596 |
|
|
return;
|
597 |
|
|
}
|
598 |
|
|
$entity = $result->entity;
|
599 |
|
|
if (empty($entity->type)) {
|
600 |
|
|
return;
|
601 |
|
|
}
|
602 |
|
|
|
603 |
|
|
$type_names = node_type_get_names();
|
604 |
|
|
$type = $entity->type;
|
605 |
|
|
$label = '';
|
606 |
|
|
$stripe = '';
|
607 |
|
|
if (array_key_exists($type, $type_names) || $colors[$type] == CALENDAR_EMPTY_STRIPE) {
|
608 |
|
|
$label = $type_names[$type];
|
609 |
|
|
}
|
610 |
|
|
if (array_key_exists($type, $colors)) {
|
611 |
|
|
$stripe = $colors[$type];
|
612 |
|
|
}
|
613 |
|
|
|
614 |
|
|
$result->stripe[] = $stripe;
|
615 |
|
|
$result->stripe_label[] = $label;
|
616 |
|
|
return;
|
617 |
|
|
}
|
618 |
|
|
|
619 |
|
|
/**
|
620 |
|
|
* Create a stripe based on a taxonomy term.
|
621 |
|
|
*/
|
622 |
|
|
|
623 |
|
|
function calendar_taxonomy_stripe(&$result) {
|
624 |
|
|
$colors = isset($this->options['colors']['calendar_colors_taxonomy']) ? $this->options['colors']['calendar_colors_taxonomy'] : array();
|
625 |
|
|
if (empty($colors)) {
|
626 |
|
|
return;
|
627 |
|
|
}
|
628 |
|
|
|
629 |
|
|
$entity = $result->entity;
|
630 |
|
|
$term_field_name = $this->options['colors']['taxonomy_field'];
|
631 |
|
|
if ($terms_for_entity = field_get_items($this->view->base_table, $entity, $term_field_name)) {
|
632 |
|
|
foreach ($terms_for_entity as $delta => $item) {
|
633 |
|
|
$term_for_entity = taxonomy_term_load($item['tid']);
|
634 |
|
|
if (!array_key_exists($term_for_entity->tid, $colors) || $colors[$term_for_entity->tid] == CALENDAR_EMPTY_STRIPE) {
|
635 |
|
|
continue;
|
636 |
|
|
}
|
637 |
|
|
$result->stripe[] = $colors[$term_for_entity->tid];
|
638 |
|
|
$result->stripe_label[] = $term_for_entity->name;
|
639 |
|
|
}
|
640 |
|
|
}
|
641 |
|
|
|
642 |
|
|
return;
|
643 |
|
|
}
|
644 |
|
|
|
645 |
|
|
/**
|
646 |
|
|
* Create a stripe based on group.
|
647 |
|
|
*/
|
648 |
|
|
function calendar_group_stripe(&$result) {
|
649 |
|
|
$colors = isset($this->options['colors']['calendar_colors_group']) ? $this->options['colors']['calendar_colors_group'] : array();
|
650 |
|
|
|
651 |
|
|
if (empty($colors)) {
|
652 |
|
|
return;
|
653 |
|
|
}
|
654 |
|
|
if (!function_exists('og_get_entity_groups')) {
|
655 |
|
|
return;
|
656 |
|
|
}
|
657 |
|
|
|
658 |
|
|
$entity = $result->entity;
|
659 |
|
|
$groups_for_entity = og_get_entity_groups($this->view->base_table, $entity);
|
660 |
|
|
|
661 |
|
|
// The 7.1 version of OG.
|
662 |
|
|
if (function_exists('og_label')) {
|
663 |
|
|
if (count($groups_for_entity)) {
|
664 |
|
|
foreach ($groups_for_entity as $gid => $group_name) {
|
665 |
|
|
if (!array_key_exists($gid, $colors) || $colors[$gid] == CALENDAR_EMPTY_STRIPE) {
|
666 |
|
|
continue;
|
667 |
|
|
}
|
668 |
|
|
$result->stripe[] = $colors[$gid];
|
669 |
|
|
$result->stripe_label[] = $group_name;
|
670 |
|
|
}
|
671 |
|
|
}
|
672 |
|
|
}
|
673 |
|
|
// The 7.2 version of OG.
|
674 |
|
|
else {
|
675 |
|
|
if (count($groups_for_entity)) {
|
676 |
|
|
foreach ($groups_for_entity as $entity_type => $gids) {
|
677 |
|
|
foreach ($gids as $gid => $group_name) {
|
678 |
|
|
if (!array_key_exists($gid, $colors) || $colors[$gid] == CALENDAR_EMPTY_STRIPE) {
|
679 |
|
|
continue;
|
680 |
|
|
}
|
681 |
|
|
$result->stripe[] = $colors[$gid];
|
682 |
|
|
$result->stripe_label[] = $group_name;
|
683 |
|
|
}
|
684 |
|
|
}
|
685 |
|
|
}
|
686 |
|
|
}
|
687 |
|
|
return;
|
688 |
|
|
}
|
689 |
|
|
} |