1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Creates the option of Repeating date fields and manages Date fields that use the Date Repeat API.
|
6 |
|
|
*
|
7 |
|
|
* The Repeating functionality is pretty tightly intermingled with other code,
|
8 |
|
|
* so the process of pulling it out into this module will happen gradually.
|
9 |
|
|
*
|
10 |
|
|
* The current implementation adds a repeat form to the date field so the user
|
11 |
|
|
* can select the repeat rules. That selection is built into an RRULE
|
12 |
|
|
* which is stored in the zero position of the field values. During widget
|
13 |
|
|
* validation, the rule is parsed to see what dates it will create,
|
14 |
|
|
* and multiple values are added to the field, one for each repeat date.
|
15 |
|
|
* That update only happens when the rule, the start date, or the end date
|
16 |
|
|
* change, no need to waste processing cycles for other changes to the node
|
17 |
|
|
* values.
|
18 |
|
|
*
|
19 |
|
|
* Lots of possible TODOs, the biggest one is figuring out the best
|
20 |
|
|
* way to handle dates with no UNTIL date since we can't add an infinite
|
21 |
|
|
* number of values to the field. For now, we require the UNTIL date.
|
22 |
|
|
*/
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* Implements hook_theme().
|
26 |
|
|
*/
|
27 |
|
|
function date_repeat_field_theme() {
|
28 |
|
|
$themes = array(
|
29 |
|
|
'date_repeat_display' => array(
|
30 |
|
|
'variables' => array(
|
31 |
|
|
'field' => NULL,
|
32 |
|
|
'item' => NULL,
|
33 |
|
|
'entity_type' => NULL,
|
34 |
|
|
'entity' => NULL,
|
35 |
|
|
'dates' => NULL
|
36 |
|
|
),
|
37 |
|
|
'function' => 'theme_date_repeat_display',
|
38 |
|
|
),
|
39 |
|
|
);
|
40 |
|
|
|
41 |
|
|
return $themes;
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Theme the human-readable description for a Date Repeat rule.
|
46 |
|
|
*
|
47 |
|
|
* TODO -
|
48 |
|
|
* add in ways to store the description in the date so it isn't regenerated
|
49 |
|
|
* over and over and find a way to allow description to be shown or hidden.
|
50 |
|
|
*/
|
51 |
|
|
function theme_date_repeat_display($vars) {
|
52 |
|
|
$field = $vars['field'];
|
53 |
|
|
$item = $vars['item'];
|
54 |
|
|
$entity = !empty($vars['node']) ? $vars['node'] : NULL;
|
55 |
|
|
$output = '';
|
56 |
|
|
if (!empty($item['rrule'])) {
|
57 |
|
|
$output = date_repeat_rrule_description($item['rrule']);
|
58 |
|
|
$output = '<div class="date-repeat-rule">' . $output . '</div>';
|
59 |
|
|
}
|
60 |
|
|
return $output;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* Implements hook_menu().
|
65 |
|
|
*
|
66 |
|
|
* Add menu tabs to display pages with details about repeating date values.
|
67 |
|
|
*/
|
68 |
|
|
function date_repeat_field_menu() {
|
69 |
|
|
$items = array();
|
70 |
|
|
|
71 |
|
|
$values = date_repeat_field_bundles();
|
72 |
|
|
foreach ($values as $entity_type => $bundles) {
|
73 |
|
|
if (module_exists('field_collection') && $entity_type == 'field_collection_item') {
|
74 |
|
|
foreach ($bundles as $bundle => $fields) {
|
75 |
|
|
$field = field_info_field($bundle);
|
76 |
|
|
if ($field['type'] == 'field_collection') {
|
77 |
|
|
$path = field_collection_field_get_path($field);
|
78 |
|
|
$count = count(explode('/', $path));
|
79 |
|
|
$items[$path . '/%field_collection_item/repeats'] = array(
|
80 |
b720ea3e
|
Assos Assos
|
'title' => 'Repeats',
|
81 |
|
|
'page callback' => 'date_repeat_field_page',
|
82 |
|
|
'page arguments' => array($entity_type, $count),
|
83 |
|
|
'access callback' => 'date_repeat_field_show',
|
84 |
|
|
'access arguments' => array($entity_type, $count),
|
85 |
|
|
'type' => MENU_LOCAL_TASK,
|
86 |
|
|
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
|
87 |
85ad3d82
|
Assos Assos
|
);
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
else {
|
92 |
|
|
$path = $entity_type . '/%' . $entity_type;
|
93 |
|
|
$items[$path . '/repeats'] = array(
|
94 |
b720ea3e
|
Assos Assos
|
'title' => 'Repeats',
|
95 |
|
|
'page callback' => 'date_repeat_field_page',
|
96 |
|
|
'page arguments' => array($entity_type, 1),
|
97 |
|
|
'access callback' => 'date_repeat_field_show',
|
98 |
|
|
'access arguments' => array($entity_type, 1),
|
99 |
|
|
'type' => MENU_LOCAL_TASK,
|
100 |
|
|
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
|
101 |
85ad3d82
|
Assos Assos
|
);
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
return $items;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Implements hook_permission().
|
109 |
|
|
*/
|
110 |
|
|
function date_repeat_field_permission() {
|
111 |
b720ea3e
|
Assos Assos
|
return array(
|
112 |
|
|
'view date repeats' => array(
|
113 |
|
|
'title' => t('View Repeating Dates'),
|
114 |
|
|
'description' => t('Allow user to see a page with all the times a date repeats.'),
|
115 |
|
|
),
|
116 |
|
|
);
|
117 |
85ad3d82
|
Assos Assos
|
}
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
db9ffd17
|
Assos Assos
|
* See if the user can access repeat date info for this entity.
|
121 |
|
|
*
|
122 |
|
|
* @param string $entity_type
|
123 |
|
|
* The entity type.
|
124 |
|
|
* @param string $entity
|
125 |
|
|
* The specific entity to check (optional).
|
126 |
|
|
*
|
127 |
|
|
* @return bool
|
128 |
|
|
* Return TRUE if there is at least one date field attached to this entity,
|
129 |
|
|
* and the current user has the permission 'view date repeats'; FALSE otherwise.
|
130 |
85ad3d82
|
Assos Assos
|
*/
|
131 |
|
|
function date_repeat_field_show($entity_type = 'node', $entity = NULL) {
|
132 |
db9ffd17
|
Assos Assos
|
if (!user_access('view date repeats')) {
|
133 |
|
|
return FALSE;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
85ad3d82
|
Assos Assos
|
$bundle = date_get_entity_bundle($entity_type, $entity);
|
137 |
db9ffd17
|
Assos Assos
|
|
138 |
|
|
// In Drupal 7.22 the field_info_field_map() function was added, which is more
|
139 |
|
|
// memory-efficient in certain cases than field_info_fields().
|
140 |
|
|
// @see https://drupal.org/node/1915646
|
141 |
|
|
$field_map_available = version_compare(VERSION, '7.22', '>=');
|
142 |
|
|
$field_list = $field_map_available ? field_info_field_map() : field_info_fields();
|
143 |
|
|
|
144 |
|
|
foreach ($field_list as $field_name => $data) {
|
145 |
|
|
if (in_array($data['type'], array('date', 'datestamp', 'datetime'))
|
146 |
|
|
&& array_key_exists($entity_type, $data['bundles'])
|
147 |
|
|
&& in_array($bundle, $data['bundles'][$entity_type])) {
|
148 |
|
|
$field_info = $field_map_available ? field_info_field($field_name) : $data;
|
149 |
|
|
if (date_is_repeat_field($field_info)) {
|
150 |
|
|
return TRUE;
|
151 |
|
|
}
|
152 |
85ad3d82
|
Assos Assos
|
}
|
153 |
|
|
}
|
154 |
|
|
return FALSE;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
/**
|
158 |
|
|
* A page to list all values for a repeating date.
|
159 |
|
|
*/
|
160 |
|
|
function date_repeat_field_page($entity_type = 'node', $entity = NULL) {
|
161 |
|
|
$bundle = date_get_entity_bundle($entity_type, $entity);
|
162 |
|
|
$info = entity_get_info($entity_type);
|
163 |
|
|
$key = $info['entity keys']['id'];
|
164 |
|
|
drupal_set_title(t('Repeats'));
|
165 |
|
|
$entity->date_repeat_show_all = TRUE;
|
166 |
|
|
$entity->content = array();
|
167 |
|
|
$output = '';
|
168 |
|
|
foreach (field_info_fields() as $field_name => $field) {
|
169 |
|
|
if (in_array($field['type'], array('date', 'datestamp', 'datetime')) && date_is_repeat_field($field)) {
|
170 |
|
|
foreach ($field['bundles'] as $field_entity_type => $bundles) {
|
171 |
|
|
foreach ($bundles as $field_bundle) {
|
172 |
|
|
if ($entity_type == $field_entity_type && $bundle == $field_bundle) {
|
173 |
|
|
$data = field_view_field($entity_type, $entity, $field_name);
|
174 |
|
|
$output .= drupal_render($data);
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
return $output;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/**
|
184 |
|
|
* Return an array of all entity types and bundles that have repeating date fields.
|
185 |
|
|
*/
|
186 |
|
|
function date_repeat_field_bundles() {
|
187 |
|
|
$values = array();
|
188 |
|
|
foreach (field_info_fields() as $field_name => $field) {
|
189 |
|
|
if (in_array($field['type'], array('date', 'datestamp', 'datetime')) && $field['settings']['repeat']) {
|
190 |
|
|
foreach ($field['bundles'] as $entity_type => $bundles) {
|
191 |
|
|
foreach ($bundles as $bundle) {
|
192 |
|
|
$values[$entity_type][$bundle][] = $field_name;
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
return $values;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
b720ea3e
|
Assos Assos
|
/**
|
201 |
|
|
* Check field is repeat.
|
202 |
|
|
*/
|
203 |
85ad3d82
|
Assos Assos
|
function date_is_repeat_field($field, $instance = NULL) {
|
204 |
|
|
if (is_string($field)) {
|
205 |
|
|
$field = field_info_field($field);
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
if (!isset($field['settings']['repeat'])) {
|
209 |
|
|
return FALSE;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
$value = $field['settings']['repeat'];
|
213 |
|
|
|
214 |
|
|
// This might be either a field form or a real field.
|
215 |
|
|
if (is_array($value)) {
|
216 |
|
|
return $value['#value'];
|
217 |
|
|
}
|
218 |
|
|
else {
|
219 |
|
|
return $value;
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
|
223 |
b720ea3e
|
Assos Assos
|
/**
|
224 |
85ad3d82
|
Assos Assos
|
* Implements hook_date_field_insert_alter().
|
225 |
|
|
*/
|
226 |
|
|
function date_repeat_field_date_field_insert_alter(&$items, $context) {
|
227 |
|
|
|
228 |
|
|
$entity = $context['entity'];
|
229 |
|
|
$field = $context['field'];
|
230 |
|
|
$instance = $context['instance'];
|
231 |
|
|
$langcode = $context['langcode'];
|
232 |
|
|
|
233 |
|
|
// If an RRULE with a frequency of NONE made it this far, unset it.
|
234 |
|
|
if (!empty($items[0]['rrule']) && strpos($items[0]['rrule'], 'FREQ=NONE')) {
|
235 |
|
|
$items[0]['rrule'] = NULL;
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
// We can't use hook_devel_generate() because we need custom handling for
|
239 |
|
|
// repeating date fields. So we wait until the entity is inserted, then
|
240 |
|
|
// intervene here to fix it.
|
241 |
|
|
if (!empty($entity->devel_generate) && !empty($field['settings']['repeat'])) {
|
242 |
|
|
module_load_include('inc', 'date_repeat_field', 'date_repeat_field.devel_generate');
|
243 |
|
|
date_repeat_field_date_field_insert($items, $context);
|
244 |
|
|
}
|
245 |
|
|
}
|
246 |
|
|
|
247 |
b720ea3e
|
Assos Assos
|
/**
|
248 |
85ad3d82
|
Assos Assos
|
* Implements hook_date_field_update_alter().
|
249 |
|
|
*/
|
250 |
|
|
function date_repeat_field_date_field_update_alter(&$items, $context) {
|
251 |
|
|
|
252 |
|
|
// If an RRULE with a frequency of NONE made it this far, unset it.
|
253 |
|
|
if (!empty($items[0]['rrule']) && strpos($items[0]['rrule'], 'FREQ=NONE')) {
|
254 |
|
|
$items[0]['rrule'] = NULL;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
// If you have a repeating date field on a user and don't check the box to repeat it,
|
258 |
|
|
// we end up with $items[0]['rrule'] = array('additions' => '', 'exceptions' => ''));
|
259 |
|
|
// This will clean it up by getting rid of those bogus values.
|
260 |
|
|
// @TODO Figure out where that's coming from. It doesn't happen on nodes.
|
261 |
|
|
if (!empty($items[0]['rrule']) && is_array($items[0]['rrule'])) {
|
262 |
|
|
$items[0]['rrule'] = NULL;
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* Implements hook_field_widget_form_alter().
|
268 |
|
|
*/
|
269 |
|
|
function date_repeat_field_field_widget_form_alter(&$element, &$form_state, $context) {
|
270 |
|
|
|
271 |
|
|
$field = $context['field'];
|
272 |
|
|
$instance = $context['instance'];
|
273 |
|
|
$items = $context['items'];
|
274 |
|
|
$delta = $context['delta'];
|
275 |
|
|
|
276 |
|
|
if (in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
|
277 |
|
|
if (!empty($field['settings']['repeat'])) {
|
278 |
|
|
$element['#element_validate'][] = 'date_repeat_field_widget_validate';
|
279 |
|
|
$element['show_repeat_settings'] = array(
|
280 |
|
|
'#type' => 'checkbox',
|
281 |
|
|
'#title' => t('Repeat'),
|
282 |
|
|
'#weight' => $instance['widget']['weight'] + .3,
|
283 |
|
|
'#prefix' => '<div class="date-clear">',
|
284 |
|
|
'#suffix' => '</div>',
|
285 |
|
|
'#default_value' => isset($items[$delta]['rrule']) && !empty($items[$delta]['rrule']) ? 1 : 0,
|
286 |
|
|
);
|
287 |
b720ea3e
|
Assos Assos
|
|
288 |
|
|
// Make changes if instance is set to be rendered as a regular field.
|
289 |
|
|
if (!empty($instance['widget']['settings']['no_fieldset'])) {
|
290 |
|
|
$element['#title'] = check_plain($instance['label']);
|
291 |
|
|
$element['#description'] = field_filter_xss($instance['description']);
|
292 |
|
|
$element['#theme_wrappers'] = array('date_form_element');
|
293 |
|
|
}
|
294 |
85ad3d82
|
Assos Assos
|
}
|
295 |
|
|
}
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
/**
|
299 |
|
|
* Validation for date repeat form element.
|
300 |
|
|
*
|
301 |
|
|
* Create multiple values from the RRULE results.
|
302 |
|
|
* Lots more work needed here.
|
303 |
|
|
*/
|
304 |
|
|
function date_repeat_field_widget_validate($element, &$form_state) {
|
305 |
|
|
$field = field_widget_field($element, $form_state);
|
306 |
|
|
if (empty($field['settings']['repeat'])) {
|
307 |
|
|
return;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
$field_name = $element['#field_name'];
|
311 |
|
|
$delta = $element['#delta'];
|
312 |
|
|
$langcode = $element['#language'];
|
313 |
|
|
|
314 |
|
|
// If the widget has been hidden by #access, the RRULE will still be in its
|
315 |
|
|
// original string form here. Nothing to process.
|
316 |
|
|
if (date_hidden_element($element)) {
|
317 |
|
|
|
318 |
|
|
// If this was a hidden repeating date, we lost all the repeating values in the widget processing.
|
319 |
|
|
// Add them back here if that happened since we are skipping the re-creation of those values.
|
320 |
|
|
if (!empty($form_state['storage']['date_items'][$field_name])) {
|
321 |
|
|
array_pop($element['#parents']);
|
322 |
|
|
form_set_value($element, $form_state['storage']['date_items'][$field_name][$langcode], $form_state);
|
323 |
|
|
}
|
324 |
|
|
return;
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
module_load_include('inc', 'date_repeat', 'date_repeat_form');
|
328 |
|
|
$instance = field_widget_instance($element, $form_state);
|
329 |
|
|
|
330 |
|
|
// Here 'values' returns an array of input values, which includes the original RRULE, as a string.
|
331 |
|
|
// and 'input' returns an array of the form elements created by the repeating date widget, with
|
332 |
|
|
// RRULE values as an array of the selected elements and their chosen values.
|
333 |
|
|
$item = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
|
334 |
|
|
$input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);
|
335 |
|
|
|
336 |
|
|
$rrule_values = date_repeat_merge($input['rrule'], $element['rrule']);
|
337 |
|
|
|
338 |
|
|
// If no repeat information was set, treat this as a normal, non-repeating value.
|
339 |
|
|
if ($rrule_values['FREQ'] == 'NONE' || empty($input['show_repeat_settings'])) {
|
340 |
|
|
$item['rrule'] = NULL;
|
341 |
|
|
form_set_value($element, $item, $form_state);
|
342 |
|
|
return;
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
// If no start date was set, clean up the form and return.
|
346 |
|
|
if (empty($item['value'])) {
|
347 |
|
|
form_set_value($element, NULL, $form_state);
|
348 |
|
|
return;
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
// Require the UNTIL date for now.
|
352 |
|
|
// The RRULE has already been created by this point, so go back
|
353 |
|
|
// to the posted values to see if this was filled out.
|
354 |
|
|
$error_field_base = implode('][', $element['#parents']);
|
355 |
b720ea3e
|
Assos Assos
|
$error_field_until = $error_field_base . '][rrule][until_child][datetime][';
|
356 |
85ad3d82
|
Assos Assos
|
if (!empty($item['rrule']) && $rrule_values['range_of_repeat'] === 'UNTIL' && empty($rrule_values['UNTIL']['datetime'])) {
|
357 |
|
|
switch ($instance['widget']['type']) {
|
358 |
|
|
case 'date_text':
|
359 |
|
|
case 'date_popup':
|
360 |
|
|
form_set_error($error_field_until . 'date', t("Missing value in 'Range of repeat'. (UNTIL).", array(), array('context' => 'Date repeat')));
|
361 |
|
|
break;
|
362 |
b720ea3e
|
Assos Assos
|
|
363 |
85ad3d82
|
Assos Assos
|
case 'date_select':
|
364 |
|
|
form_set_error($error_field_until . 'year', t("Missing value in 'Range of repeat': Year (UNTIL)", array(), array('context' => 'Date repeat')));
|
365 |
|
|
form_set_error($error_field_until . 'month', t("Missing value in 'Range of repeat': Month (UNTIL)", array(), array('context' => 'Date repeat')));
|
366 |
|
|
form_set_error($error_field_until . 'day', t("Missing value in 'Range of repeat': Day (UNTIL)", array(), array('context' => 'Date repeat')));
|
367 |
|
|
break;
|
368 |
|
|
}
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
$error_field_count = $error_field_base . '][rrule][count_child';
|
372 |
|
|
if (!empty($item['rrule']) && $rrule_values['range_of_repeat'] === 'COUNT' && empty($rrule_values['COUNT'])) {
|
373 |
|
|
form_set_error($error_field_count, t("Missing value in 'Range of repeat'. (COUNT).", array(), array('context' => 'Date repeat')));
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
if (form_get_errors()) {
|
377 |
|
|
return;
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
// If the rule, the start date, or the end date have changed, re-calculate
|
381 |
|
|
// the repeating dates, wipe out the previous values, and populate the
|
382 |
|
|
// field with the new values.
|
383 |
|
|
|
384 |
|
|
$rrule = $item['rrule'];
|
385 |
|
|
if (!empty($rrule)) {
|
386 |
|
|
|
387 |
|
|
// Avoid undefined index problems on dates that don't have all parts.
|
388 |
|
|
$possible_items = array('value', 'value2', 'timezone', 'offset', 'offset2');
|
389 |
|
|
foreach ($possible_items as $key) {
|
390 |
|
|
if (empty($item[$key])) {
|
391 |
|
|
$item[$key] = '';
|
392 |
|
|
}
|
393 |
|
|
}
|
394 |
|
|
|
395 |
|
|
// We only collect a date for UNTIL, but we need it to be inclusive,
|
396 |
|
|
// so force it to a full datetime element at the last possible second of the day.
|
397 |
|
|
if (!empty($rrule_values['UNTIL'])) {
|
398 |
b720ea3e
|
Assos Assos
|
$gran = array('year', 'month', 'day', 'hour', 'minute', 'second');
|
399 |
85ad3d82
|
Assos Assos
|
$rrule_values['UNTIL']['datetime'] .= ' 23:59:59';
|
400 |
b720ea3e
|
Assos Assos
|
$rrule_values['UNTIL']['granularity'] = serialize(drupal_map_assoc($gran));
|
401 |
85ad3d82
|
Assos Assos
|
$rrule_values['UNTIL']['all_day'] = 0;
|
402 |
|
|
}
|
403 |
|
|
$value = date_repeat_build_dates($rrule, $rrule_values, $field, $item);
|
404 |
|
|
// Unset the delta value of the parents.
|
405 |
|
|
array_pop($element['#parents']);
|
406 |
|
|
// Set the new delta values for this item to the array of values returned by the repeat rule.
|
407 |
|
|
form_set_value($element, $value, $form_state);
|
408 |
|
|
}
|
409 |
|
|
}
|
410 |
|
|
|
411 |
|
|
/**
|
412 |
|
|
* Implements the form after_build().
|
413 |
|
|
*
|
414 |
|
|
* Remove the 'Add more' elements from a repeating date form.
|
415 |
|
|
*/
|
416 |
|
|
function date_repeat_after_build(&$element, &$form_state) {
|
417 |
|
|
foreach ($form_state['storage']['repeat_fields'] as $field_name => $parents) {
|
418 |
|
|
// Remove unnecessary items in the form added by the Add more handling.
|
419 |
|
|
$value = drupal_array_get_nested_value($element, $parents);
|
420 |
|
|
$langcode = $value['#language'];
|
421 |
|
|
unset($value[$langcode]['add_more'], $value[$langcode]['#suffix'], $value[$langcode]['#prefix'], $value[$langcode][0]['_weight']);
|
422 |
|
|
$value[$langcode]['#cardinality'] = 1;
|
423 |
|
|
$value[$langcode]['#max_delta'] = 1;
|
424 |
|
|
drupal_array_set_nested_value($element, $parents, $value);
|
425 |
|
|
}
|
426 |
|
|
return $element;
|
427 |
|
|
}
|
428 |
|
|
|
429 |
|
|
/**
|
430 |
|
|
* Helper function to build repeating dates from a $node_field.
|
431 |
|
|
*
|
432 |
|
|
* Pass in either the RRULE or the $form_values array for the RRULE,
|
433 |
|
|
* whichever is missing will be created when needed.
|
434 |
|
|
*/
|
435 |
b720ea3e
|
Assos Assos
|
// @codingStandardsIgnoreStart
|
436 |
85ad3d82
|
Assos Assos
|
function date_repeat_build_dates($rrule = NULL, $rrule_values = NULL, $field, $item) {
|
437 |
b720ea3e
|
Assos Assos
|
// @codingStandardsIgnoreEnd
|
438 |
|
|
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
|
439 |
85ad3d82
|
Assos Assos
|
$field_name = $field['field_name'];
|
440 |
|
|
|
441 |
|
|
if (empty($rrule)) {
|
442 |
|
|
$rrule = date_api_ical_build_rrule($rrule_values);
|
443 |
|
|
}
|
444 |
|
|
elseif (empty($rrule_values)) {
|
445 |
|
|
$rrule_values = date_ical_parse_rrule(NULL, $rrule);
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
// By the time we get here, the start and end dates have been
|
449 |
|
|
// adjusted back to UTC, but we want localtime dates to do
|
450 |
|
|
// things like '+1 Tuesday', so adjust back to localtime.
|
451 |
|
|
$timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
|
452 |
|
|
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
|
453 |
|
|
$start = new DateObject($item['value'], $timezone_db, date_type_format($field['type']));
|
454 |
|
|
$start->limitGranularity($field['settings']['granularity']);
|
455 |
|
|
if ($timezone != $timezone_db) {
|
456 |
|
|
date_timezone_set($start, timezone_open($timezone));
|
457 |
|
|
}
|
458 |
|
|
if (!empty($item['value2']) && $item['value2'] != $item['value']) {
|
459 |
|
|
$end = new DateObject($item['value2'], date_get_timezone_db($field['settings']['tz_handling']), date_type_format($field['type']));
|
460 |
|
|
$end->limitGranularity($field['settings']['granularity']);
|
461 |
|
|
date_timezone_set($end, timezone_open($timezone));
|
462 |
|
|
}
|
463 |
|
|
else {
|
464 |
|
|
$end = $start;
|
465 |
|
|
}
|
466 |
|
|
$duration = $start->difference($end);
|
467 |
|
|
$start_datetime = date_format($start, DATE_FORMAT_DATETIME);
|
468 |
|
|
|
469 |
|
|
if (!empty($rrule_values['UNTIL']['datetime'])) {
|
470 |
|
|
$end = date_ical_date($rrule_values['UNTIL'], $timezone);
|
471 |
|
|
$end_datetime = date_format($end, DATE_FORMAT_DATETIME);
|
472 |
|
|
}
|
473 |
|
|
elseif (!empty($rrule_values['COUNT'])) {
|
474 |
|
|
$end_datetime = NULL;
|
475 |
|
|
}
|
476 |
|
|
else {
|
477 |
|
|
// No UNTIL and no COUNT?
|
478 |
|
|
return array();
|
479 |
|
|
}
|
480 |
|
|
|
481 |
|
|
// Split the RRULE into RRULE, EXDATE, and RDATE parts.
|
482 |
|
|
$parts = date_repeat_split_rrule($rrule);
|
483 |
|
|
$parsed_exceptions = (array) $parts[1];
|
484 |
|
|
$exceptions = array();
|
485 |
|
|
foreach ($parsed_exceptions as $exception) {
|
486 |
|
|
$date = date_ical_date($exception, $timezone);
|
487 |
|
|
$exceptions[] = date_format($date, 'Y-m-d');
|
488 |
|
|
}
|
489 |
|
|
|
490 |
|
|
$parsed_rdates = (array) $parts[2];
|
491 |
|
|
$additions = array();
|
492 |
|
|
foreach ($parsed_rdates as $rdate) {
|
493 |
|
|
$date = date_ical_date($rdate, $timezone);
|
494 |
|
|
$additions[] = date_format($date, 'Y-m-d');
|
495 |
|
|
}
|
496 |
|
|
|
497 |
|
|
$dates = date_repeat_calc($rrule, $start_datetime, $end_datetime, $exceptions, $timezone, $additions);
|
498 |
|
|
$value = array();
|
499 |
|
|
foreach ($dates as $delta => $date) {
|
500 |
|
|
// date_repeat_calc always returns DATE_DATETIME dates, which is
|
501 |
|
|
// not necessarily $field['type'] dates.
|
502 |
|
|
// Convert returned dates back to db timezone before storing.
|
503 |
|
|
$date_start = new DateObject($date, $timezone, DATE_FORMAT_DATETIME);
|
504 |
|
|
$date_start->limitGranularity($field['settings']['granularity']);
|
505 |
|
|
date_timezone_set($date_start, timezone_open($timezone_db));
|
506 |
|
|
$date_end = clone($date_start);
|
507 |
|
|
date_modify($date_end, '+' . $duration . ' seconds');
|
508 |
|
|
$value[$delta] = array(
|
509 |
|
|
'value' => date_format($date_start, date_type_format($field['type'])),
|
510 |
|
|
'value2' => date_format($date_end, date_type_format($field['type'])),
|
511 |
|
|
'offset' => date_offset_get($date_start),
|
512 |
|
|
'offset2' => date_offset_get($date_end),
|
513 |
|
|
'timezone' => $timezone,
|
514 |
|
|
'rrule' => $rrule,
|
515 |
b720ea3e
|
Assos Assos
|
);
|
516 |
85ad3d82
|
Assos Assos
|
}
|
517 |
b720ea3e
|
Assos Assos
|
|
518 |
85ad3d82
|
Assos Assos
|
return $value;
|
519 |
|
|
}
|
520 |
|
|
|
521 |
|
|
/**
|
522 |
|
|
* Implements hook_date_combo_process_alter().
|
523 |
|
|
*
|
524 |
|
|
* This hook lets us make changes to the date_combo element.
|
525 |
|
|
*/
|
526 |
|
|
function date_repeat_field_date_combo_process_alter(&$element, &$form_state, $context) {
|
527 |
|
|
|
528 |
|
|
$field = $context['field'];
|
529 |
|
|
$instance = $context['instance'];
|
530 |
|
|
$field_name = $element['#field_name'];
|
531 |
|
|
$delta = $element['#delta'];
|
532 |
|
|
|
533 |
|
|
// Add a date repeat form element, if needed.
|
534 |
|
|
// We delayed until this point so we don't bother adding it to hidden fields.
|
535 |
|
|
if (date_is_repeat_field($field, $instance)) {
|
536 |
|
|
|
537 |
|
|
$item = $element['#value'];
|
538 |
|
|
$element['rrule'] = array(
|
539 |
|
|
'#type' => 'date_repeat_rrule',
|
540 |
|
|
'#theme_wrappers' => array('date_repeat_rrule'),
|
541 |
|
|
'#default_value' => isset($item['rrule']) ? $item['rrule'] : '',
|
542 |
|
|
'#date_timezone' => $element['#date_timezone'],
|
543 |
|
|
'#date_format' => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
|
544 |
|
|
'#date_text_parts' => (array) $instance['widget']['settings']['text_parts'],
|
545 |
|
|
'#date_increment' => $instance['widget']['settings']['increment'],
|
546 |
|
|
'#date_year_range' => $instance['widget']['settings']['year_range'],
|
547 |
|
|
'#date_label_position' => $instance['widget']['settings']['label_position'],
|
548 |
|
|
'#date_repeat_widget' => str_replace('_repeat', '', $instance['widget']['type']),
|
549 |
|
|
'#date_repeat_collapsed' => $instance['widget']['settings']['repeat_collapsed'],
|
550 |
|
|
'#date_flexible' => 0,
|
551 |
|
|
'#weight' => $instance['widget']['weight'] + .4,
|
552 |
|
|
);
|
553 |
|
|
}
|
554 |
|
|
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
/**
|
558 |
|
|
* Implements hook_date_combo_pre_validate_alter().
|
559 |
|
|
*
|
560 |
|
|
* This hook lets us alter the element or the form_state before the rest
|
561 |
|
|
* of the date_combo validation gets fired.
|
562 |
|
|
*/
|
563 |
|
|
function date_repeat_field_date_combo_pre_validate_alter(&$element, &$form_state, $context) {
|
564 |
|
|
// Just a placeholder for now.
|
565 |
|
|
}
|
566 |
|
|
|
567 |
|
|
/**
|
568 |
|
|
* Implements hook_field_info_alter().
|
569 |
|
|
*
|
570 |
|
|
* This Field API hook lets us add a new setting to the fields.
|
571 |
|
|
*/
|
572 |
|
|
function date_repeat_field_field_info_alter(&$info) {
|
573 |
|
|
|
574 |
|
|
$info['date']['settings'] += array(
|
575 |
|
|
'repeat' => 0,
|
576 |
|
|
);
|
577 |
|
|
$info['datetime']['settings'] += array(
|
578 |
|
|
'repeat' => 0,
|
579 |
|
|
);
|
580 |
|
|
$info['datestamp']['settings'] += array(
|
581 |
|
|
'repeat' => 0,
|
582 |
|
|
);
|
583 |
|
|
}
|
584 |
|
|
|
585 |
|
|
/**
|
586 |
|
|
* Implements hook_field_formatter_info_alter().
|
587 |
|
|
*
|
588 |
|
|
* This hook lets us add settings to the formatters.
|
589 |
|
|
*/
|
590 |
|
|
function date_repeat_field_field_formatter_info_alter(&$info) {
|
591 |
|
|
|
592 |
|
|
if (isset($info['date_default'])) {
|
593 |
|
|
$info['date_default']['settings'] += array(
|
594 |
|
|
'show_repeat_rule' => 'show',
|
595 |
|
|
);
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
|
599 |
|
|
/**
|
600 |
|
|
* Implements hook_field_widget_info_alter().
|
601 |
|
|
*
|
602 |
|
|
* This Field API hook lets us add a new setting to the widgets.
|
603 |
|
|
*/
|
604 |
|
|
function date_repeat_field_field_widget_info_alter(&$info) {
|
605 |
|
|
|
606 |
|
|
$info['date_text']['settings'] += array(
|
607 |
|
|
'repeat_collapsed' => 0,
|
608 |
|
|
);
|
609 |
|
|
$info['date_select']['settings'] += array(
|
610 |
|
|
'repeat_collapsed' => 0,
|
611 |
|
|
);
|
612 |
|
|
if (module_exists('date_popup')) {
|
613 |
|
|
$info['date_popup']['settings'] += array(
|
614 |
|
|
'repeat_collapsed' => 0,
|
615 |
|
|
);
|
616 |
|
|
}
|
617 |
|
|
}
|
618 |
|
|
|
619 |
|
|
/**
|
620 |
|
|
* Implements hook_date_field_settings_form_alter().
|
621 |
|
|
*
|
622 |
|
|
* This hook lets us alter the field settings form.
|
623 |
|
|
*/
|
624 |
|
|
function date_repeat_field_date_field_settings_form_alter(&$form, $context) {
|
625 |
|
|
|
626 |
|
|
$field = $context['field'];
|
627 |
|
|
$instance = $context['instance'];
|
628 |
|
|
$has_data = $context['has_data'];
|
629 |
|
|
|
630 |
|
|
$form['repeat'] = array(
|
631 |
|
|
'#type' => 'select',
|
632 |
|
|
'#title' => t('Repeating date'),
|
633 |
|
|
'#default_value' => $field['settings']['repeat'],
|
634 |
|
|
'#options' => array(0 => t('No'), 1 => t('Yes')),
|
635 |
|
|
'#attributes' => array('class' => array('container-inline')),
|
636 |
|
|
'#description' => t("Repeating dates use an 'Unlimited' number of values. Instead of the 'Add more' button, they include a form to select when and how often the date should repeat."),
|
637 |
|
|
'#disabled' => $has_data,
|
638 |
|
|
);
|
639 |
|
|
}
|
640 |
|
|
|
641 |
|
|
/**
|
642 |
|
|
* Implements hook_form_FORM_ID_alter() for field_ui_field_edit_form().
|
643 |
|
|
*/
|
644 |
|
|
function date_repeat_field_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
|
645 |
|
|
$field = $form['#field'];
|
646 |
|
|
$instance = $form['#instance'];
|
647 |
|
|
|
648 |
|
|
if (!in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
|
649 |
|
|
return;
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
// If using repeating dates, override the Field module's handling of the multiple values option.
|
653 |
|
|
if (date_is_repeat_field($field, $instance)) {
|
654 |
|
|
$form['field']['cardinality']['#disabled'] = TRUE;
|
655 |
|
|
$form['field']['cardinality']['#value'] = FIELD_CARDINALITY_UNLIMITED;
|
656 |
|
|
}
|
657 |
|
|
// Repeating dates need unlimited values, confirm that in element_validate.
|
658 |
|
|
$form['field']['#element_validate'] = array('date_repeat_field_set_cardinality');
|
659 |
|
|
}
|
660 |
|
|
|
661 |
|
|
/**
|
662 |
|
|
* Ensure the cardinality gets updated if the option to make a date repeating is checked.
|
663 |
|
|
*/
|
664 |
|
|
function date_repeat_field_set_cardinality($element, &$form_state) {
|
665 |
|
|
if (!empty($form_state['values']['field']['settings']['repeat'])) {
|
666 |
|
|
form_set_value($element['cardinality'], FIELD_CARDINALITY_UNLIMITED, $form_state);
|
667 |
|
|
}
|
668 |
|
|
}
|
669 |
|
|
|
670 |
|
|
/**
|
671 |
|
|
* Implements hook_date_field_instance_settings_form_alter().
|
672 |
|
|
*
|
673 |
|
|
* This hook lets us alter the field instance settings form.
|
674 |
|
|
*/
|
675 |
|
|
function date_repeat_field_date_field_instance_settings_form_alter(&$form, $context) {
|
676 |
|
|
// Just a placeholder for now.
|
677 |
|
|
}
|
678 |
|
|
|
679 |
|
|
/**
|
680 |
|
|
* Implements hook_date_field_widget_settings_form_alter().
|
681 |
|
|
*
|
682 |
|
|
* This hook lets us alter the field widget settings form.
|
683 |
|
|
*/
|
684 |
|
|
function date_repeat_field_date_field_widget_settings_form_alter(&$form, $context) {
|
685 |
|
|
|
686 |
|
|
$field = $context['field'];
|
687 |
|
|
$instance = $context['instance'];
|
688 |
|
|
|
689 |
|
|
if (date_is_repeat_field($field, $instance)) {
|
690 |
|
|
$form['repeat_collapsed'] = array(
|
691 |
|
|
'#type' => 'value',
|
692 |
|
|
'#default_value' => 1,
|
693 |
|
|
'#options' => array(
|
694 |
|
|
0 => t('Expanded', array(), array('context' => 'Date repeat')),
|
695 |
|
|
1 => t('Collapsed', array(), array('context' => 'Date repeat'))
|
696 |
|
|
),
|
697 |
|
|
'#title' => t('Repeat display', array(), array('context' => 'Date repeat')),
|
698 |
|
|
'#description' => t("Should the repeat options form start out expanded or collapsed? Set to 'Collapsed' to make those options less obtrusive.", array(), array('context' => 'Date repeat')),
|
699 |
|
|
'#fieldset' => 'date_format',
|
700 |
b720ea3e
|
Assos Assos
|
);
|
701 |
85ad3d82
|
Assos Assos
|
}
|
702 |
|
|
}
|
703 |
|
|
|
704 |
|
|
/**
|
705 |
|
|
* Implements hook_date_field_foramatter_settings_form_alter().
|
706 |
|
|
*
|
707 |
|
|
* This hook lets us alter the field formatter settings form.
|
708 |
|
|
*/
|
709 |
|
|
function date_repeat_field_date_field_formatter_settings_form_alter(&$form, &$form_state, $context) {
|
710 |
|
|
|
711 |
|
|
$field = $context['field'];
|
712 |
|
|
$instance = $context['instance'];
|
713 |
|
|
$view_mode = $context['view_mode'];
|
714 |
|
|
$display = $instance['display'][$view_mode];
|
715 |
|
|
$formatter = $display['type'];
|
716 |
|
|
$settings = $display['settings'];
|
717 |
|
|
if ($formatter == 'date_default') {
|
718 |
|
|
$form['show_repeat_rule'] = array(
|
719 |
|
|
'#title' => t('Repeat rule:'),
|
720 |
|
|
'#type' => 'select',
|
721 |
|
|
'#options' => array(
|
722 |
|
|
'show' => t('Show repeat rule'),
|
723 |
|
|
'hide' => t('Hide repeat rule')),
|
724 |
|
|
'#default_value' => $settings['show_repeat_rule'],
|
725 |
|
|
'#access' => $field['settings']['repeat'],
|
726 |
|
|
'#weight' => 5,
|
727 |
|
|
);
|
728 |
|
|
}
|
729 |
|
|
}
|
730 |
|
|
|
731 |
|
|
/**
|
732 |
|
|
* Implements hook_date_field_foramatter_settings_summary_alter().
|
733 |
|
|
*
|
734 |
|
|
* This hook lets us alter the field formatter settings summary.
|
735 |
|
|
*/
|
736 |
|
|
function date_repeat_field_date_field_formatter_settings_summary_alter(&$summary, $context) {
|
737 |
|
|
|
738 |
|
|
$field = $context['field'];
|
739 |
|
|
$instance = $context['instance'];
|
740 |
|
|
$view_mode = $context['view_mode'];
|
741 |
|
|
$display = $instance['display'][$view_mode];
|
742 |
|
|
$formatter = $display['type'];
|
743 |
|
|
$settings = $display['settings'];
|
744 |
|
|
if (isset($settings['show_repeat_rule']) && !empty($field['settings']['repeat'])) {
|
745 |
|
|
if ($settings['show_repeat_rule'] == 'show') {
|
746 |
|
|
$summary[] = t('Show repeat rule');
|
747 |
|
|
}
|
748 |
|
|
else {
|
749 |
|
|
$summary[] = t('Hide repeat rule');
|
750 |
|
|
}
|
751 |
|
|
}
|
752 |
|
|
} |