Révision 599a39cd
Ajouté par Assos Assos il y a environ 3 ans
drupal7/sites/all/modules/date/date_all_day/date_all_day.module | ||
---|---|---|
4 | 4 |
* @file |
5 | 5 |
* Adds All Day functionality to the Date field. |
6 | 6 |
* |
7 |
* This module implements a number of hooks in the Date field and |
|
8 |
* Date api element processing and to add an All Day checkbox to |
|
9 |
* date widgets, and also adds an All Day theme and |
|
10 |
* All Day information to the formatting. |
|
7 |
* This module implements a number of hooks in the Date field and Date API |
|
8 |
* element processing and to add an All Day checkbox to date widgets, and also |
|
9 |
* adds an All Day theme and All Day information to the formatting. |
|
11 | 10 |
* |
12 |
* Keep in mind that the process hooks are fired from the top down, |
|
13 |
* first date_combo, then the date_select or date_popup.
|
|
11 |
* Keep in mind that the process hooks are fired from the top down, first
|
|
12 |
* date_combo, then the date_select or date_popup. |
|
14 | 13 |
* |
15 |
* Validation fires from the bottom up, first date_select and |
|
16 |
* date_popup, then date_combo.
|
|
14 |
* Validation fires from the bottom up, first date_select and date_popup, then
|
|
15 |
* date_combo. |
|
17 | 16 |
*/ |
18 | 17 |
|
19 | 18 |
/** |
... | ... | |
44 | 43 |
|
45 | 44 |
/** |
46 | 45 |
* Implements hook_date_formatter_dates_alter(). |
47 |
* |
|
48 |
* This allows us to alter the $dates array created |
|
49 |
* by date_formatter_process. |
|
50 | 46 |
*/ |
51 | 47 |
function date_all_day_date_formatter_dates_alter(&$dates, $context) { |
52 |
|
|
48 |
// This allows us to alter the $dates array created by date_formatter_process. |
|
53 | 49 |
$field = $context['field']; |
54 | 50 |
$instance = $context['instance']; |
55 | 51 |
$format = $context['format']; |
... | ... | |
57 | 53 |
$entity = $context['entity']; |
58 | 54 |
$date1 = $dates['value']['local']['object']; |
59 | 55 |
$date2 = $dates['value2']['local']['object']; |
60 |
|
|
61 | 56 |
$is_all_day = date_all_day_field($field, $instance, $date1, $date2); |
62 | 57 |
|
63 | 58 |
$all_day1 = ''; |
... | ... | |
71 | 66 |
'date2' => $date2, |
72 | 67 |
'format' => $format, |
73 | 68 |
'entity_type' => $entity_type, |
74 |
'entity' => $entity)); |
|
69 |
'entity' => $entity, |
|
70 |
)); |
|
75 | 71 |
$all_day2 = theme('date_all_day', array( |
76 | 72 |
'field' => $field, |
77 | 73 |
'instance' => $instance, |
... | ... | |
80 | 76 |
'date2' => $date2, |
81 | 77 |
'format' => $format, |
82 | 78 |
'entity_type' => $entity_type, |
83 |
'entity' => $entity)); |
|
79 |
'entity' => $entity, |
|
80 |
)); |
|
84 | 81 |
$dates['value']['formatted_time'] = theme('date_all_day_label'); |
85 | 82 |
$dates['value2']['formatted_time'] = theme('date_all_day_label'); |
86 | 83 |
$dates['value']['formatted'] = $all_day1; |
... | ... | |
91 | 88 |
/** |
92 | 89 |
* Adjust start/end date format to account for 'all day' . |
93 | 90 |
* |
94 |
* @params array $field
|
|
91 |
* @param array $field |
|
95 | 92 |
* The field definition for this date field. |
96 |
* |
|
97 |
* @params string $which |
|
93 |
* @param string $which |
|
98 | 94 |
* Which value to return, 'date1' or 'date2'. |
99 |
* |
|
100 |
* @params object $date1 |
|
95 |
* @param object $date1 |
|
101 | 96 |
* A date/time object for the 'start' date. |
102 |
* |
|
103 |
* @params object $date2 |
|
97 |
* @param object $date2 |
|
104 | 98 |
* A date/time object for the 'end' date. |
105 |
* |
|
106 |
* @params string $format |
|
107 |
* A date/time format |
|
108 |
* |
|
109 |
* @params object $entity |
|
99 |
* @param string $format |
|
100 |
* A date/time format. |
|
101 |
* @param object $entity |
|
110 | 102 |
* The node this date comes from (may be incomplete, always contains nid). |
111 |
* |
|
112 |
* @params object $view |
|
103 |
* @param object $view |
|
113 | 104 |
* The view this node comes from, if applicable. |
114 | 105 |
* |
115 |
* @return string |
|
106 |
* @return string|null
|
|
116 | 107 |
* Formatted date. |
117 | 108 |
*/ |
118 | 109 |
function theme_date_all_day($vars) { |
119 |
$field = $vars['field'];
|
|
110 |
$field = $vars['field']; |
|
120 | 111 |
$instance = $vars['instance']; |
121 |
$which = $vars['which'];
|
|
122 |
$date1 = $vars['date1'];
|
|
123 |
$date2 = $vars['date2'];
|
|
124 |
$format = $vars['format'];
|
|
125 |
$entity = $vars['entity'];
|
|
126 |
$view = !empty($vars['view']) ? $vars['view'] : NULL;
|
|
112 |
$which = $vars['which']; |
|
113 |
$date1 = $vars['date1']; |
|
114 |
$date2 = $vars['date2']; |
|
115 |
$format = $vars['format']; |
|
116 |
$entity = $vars['entity']; |
|
117 |
$view = !empty($vars['view']) ? $vars['view'] : NULL; |
|
127 | 118 |
|
128 | 119 |
if (empty($date1) || !is_object($date1) || $format == 'format_interval') { |
129 | 120 |
return; |
... | ... | |
156 | 147 |
* Theme the way an 'all day' label will look. |
157 | 148 |
*/ |
158 | 149 |
function theme_date_all_day_label() { |
159 |
return '(' . t('All day', array(), array('context' => 'datetime')) . ')';
|
|
150 |
return t('(All day)', array(), array('context' => 'datetime'));
|
|
160 | 151 |
} |
161 | 152 |
|
162 | 153 |
/** |
... | ... | |
164 | 155 |
* |
165 | 156 |
* @param array $field |
166 | 157 |
* The field definition for this date field. |
167 |
* |
|
168 | 158 |
* @param array $instance |
169 | 159 |
* The field instance for this date field. |
170 |
* |
|
171 | 160 |
* @param object $date1 |
172 | 161 |
* A date/time object for the 'Start' date. |
173 |
* |
|
174 | 162 |
* @param object $date2 |
175 | 163 |
* A date/time object for the 'End' date. |
176 | 164 |
* |
177 | 165 |
* @return bool |
178 | 166 |
* TRUE or FALSE. |
179 | 167 |
*/ |
180 |
function date_all_day_field($field, $instance, $date1, $date2 = NULL) {
|
|
168 |
function date_all_day_field(array $field, array $instance, $date1, $date2 = NULL) {
|
|
181 | 169 |
if (empty($date1) || !is_object($date1)) { |
182 | 170 |
return FALSE; |
183 | 171 |
} |
184 | 172 |
elseif (!date_has_time($field['settings']['granularity'])) { |
185 |
return TRUE; |
|
173 |
return FALSE; |
|
174 |
} |
|
175 |
// The master setting for the 'all day' functionality. |
|
176 |
elseif (empty($instance['widget']['settings']['display_all_day'])) { |
|
177 |
return FALSE; |
|
186 | 178 |
} |
179 |
|
|
187 | 180 |
if (empty($date2)) { |
188 | 181 |
$date2 = $date1; |
189 | 182 |
} |
190 | 183 |
|
184 |
// Several scenarios result in the two date values being the same, e.g. |
|
185 |
// date_formatter_process(). |
|
186 |
if ($date1 == $date2) { |
|
187 |
// Clone the date object so that changes made to the object won't affect |
|
188 |
// both variables. |
|
189 |
$date1 = clone $date2; |
|
190 |
|
|
191 |
// Set the time to midnight so that the two variables can be properly |
|
192 |
// compared later in date_is_all_day(). |
|
193 |
$date1->setTime(0, 0, 0); |
|
194 |
} |
|
195 |
|
|
196 |
// Use central logic for determining the granularity. |
|
191 | 197 |
$granularity = date_granularity_precision($field['settings']['granularity']); |
192 |
$increment = isset($instance['widget']['settings']['increment']) ? $instance['widget']['settings']['increment'] : 1; |
|
198 |
|
|
199 |
// The increment is 1 by default, but it can be overridden. |
|
200 |
$increment = 1; |
|
201 |
if (isset($instance['widget']['settings']['increment'])) { |
|
202 |
$increment = $instance['widget']['settings']['increment']; |
|
203 |
} |
|
204 |
|
|
193 | 205 |
return date_is_all_day(date_format($date1, DATE_FORMAT_DATETIME), date_format($date2, DATE_FORMAT_DATETIME), $granularity, $increment); |
194 | 206 |
} |
195 | 207 |
|
196 | 208 |
/** |
197 | 209 |
* Implements hook_date_combo_process_alter(). |
198 |
* |
|
199 |
* This hook lets us make changes to the date_combo element. |
|
200 | 210 |
*/ |
201 | 211 |
function date_all_day_date_combo_process_alter(&$element, &$form_state, $context) { |
202 |
|
|
212 |
// This hook lets us make changes to the date_combo element. |
|
203 | 213 |
$field = $context['field']; |
204 | 214 |
$instance = $context['instance']; |
205 | 215 |
|
... | ... | |
211 | 221 |
$all_day_id = $first_parent . '[' . implode('][', $parents) . '][all_day]';; |
212 | 222 |
foreach (array('value', 'value2') as $key) { |
213 | 223 |
if (array_key_exists($key, $element)) { |
214 |
$element[$key]['#date_all_day_id'] = $all_day_id;
|
|
224 |
$element[$key]['#date_all_day_id'] = $all_day_id; |
|
215 | 225 |
} |
216 | 226 |
} |
217 | 227 |
|
218 | 228 |
$from = $element['#default_value']['value']; |
219 | 229 |
$to = !empty($element['#default_value']['value2']) ? $element['#default_value']['value2'] : $element['#default_value']['value']; |
220 |
$date_is_all_day = date_is_all_day($from, $to); |
|
230 |
$granularity = date_granularity_precision($context['field']['settings']['granularity']); |
|
231 |
$increment = isset($context['instance']['widget']['settings']['increment']) ? $context['instance']['widget']['settings']['increment'] : 1; |
|
232 |
$date_is_all_day = date_is_all_day($from, $to, $granularity, $increment); |
|
221 | 233 |
$all_day = !empty($form_state['values']['all_day']) || $date_is_all_day; |
222 | 234 |
$element['all_day'] = array( |
223 | 235 |
'#title' => t('All Day'), |
... | ... | |
236 | 248 |
} |
237 | 249 |
} |
238 | 250 |
|
239 |
|
|
240 | 251 |
/** |
241 | 252 |
* Implements hook_date_text_process_alter(). |
242 |
* |
|
243 |
* This hook lets us make changes to the date_select widget. |
|
244 | 253 |
*/ |
245 | 254 |
function date_all_day_date_text_process_alter(&$element, &$form_state, $context) { |
255 |
// This hook lets us make changes to the date_select widget. |
|
246 | 256 |
$all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : ''; |
247 | 257 |
if ($all_day_id != '') { |
248 |
// All Day handling on text dates works only |
|
249 |
// if the user leaves the time out of the input value. |
|
250 |
// There is no element to hide or show. |
|
258 |
// All Day handling on text dates works only if the user leaves the time |
|
259 |
// out of the input value. There is no element to hide or show. |
|
251 | 260 |
} |
252 | 261 |
} |
253 | 262 |
|
254 | 263 |
/** |
255 | 264 |
* Implements hook_date_select_process_alter(). |
256 |
* |
|
257 |
* This hook lets us make changes to the date_select widget. |
|
258 | 265 |
*/ |
259 | 266 |
function date_all_day_date_select_process_alter(&$element, &$form_state, $context) { |
260 |
|
|
261 |
// Hide or show this element in reaction |
|
262 |
// to the all_day status for this element. |
|
267 |
// This hook lets us make changes to the date_select widget. Hide or show |
|
268 |
// this element in reaction to the all_day status for this element. |
|
263 | 269 |
$all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : ''; |
264 | 270 |
if ($all_day_id != '') { |
265 | 271 |
foreach (array('hour', 'minute', 'second', 'ampm') as $field) { |
... | ... | |
267 | 273 |
$element[$field]['#states'] = array( |
268 | 274 |
'visible' => array( |
269 | 275 |
'input[name="' . $all_day_id . '"]' => array('checked' => FALSE), |
270 |
)); |
|
276 |
), |
|
277 |
); |
|
271 | 278 |
} |
272 | 279 |
} |
273 | 280 |
} |
... | ... | |
275 | 282 |
|
276 | 283 |
/** |
277 | 284 |
* Implements hook_date_popup_process_alter(). |
278 |
* |
|
279 |
* This hook lets us make changes to the date_popup element. |
|
280 | 285 |
*/ |
281 | 286 |
function date_all_day_date_popup_process_alter(&$element, &$form_state, $context) { |
282 |
|
|
283 |
// Hide or show this element in reaction to |
|
284 |
// the all_day status for this element. |
|
287 |
// This hook lets us make changes to the date_popup element. Hide or show |
|
288 |
// this element in reaction to the all_day status for this element. |
|
285 | 289 |
$all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : ''; |
286 | 290 |
if ($all_day_id != '' && array_key_exists('time', $element)) { |
287 | 291 |
$element['time']['#states'] = array( |
288 | 292 |
'visible' => array( |
289 | 293 |
'input[name="' . $all_day_id . '"]' => array('checked' => FALSE), |
290 |
)); |
|
294 |
), |
|
295 |
); |
|
291 | 296 |
} |
292 | 297 |
} |
293 | 298 |
|
294 | 299 |
/** |
295 | 300 |
* Implements hook_date_select_pre_validate_alter(). |
296 |
* |
|
297 |
* This hook lets us alter the element or the form_state before the rest |
|
298 |
* of the date_select validation gets fired. |
|
299 | 301 |
*/ |
300 | 302 |
function date_all_day_date_text_pre_validate_alter(&$element, &$form_state, &$input) { |
301 |
// Let Date module massage the format for all day |
|
302 |
// values so they will pass validation. |
|
303 |
// The All day flag, if used, actually exists on the parent element. |
|
303 |
// This hook lets us alter the element or the form_state before the rest of |
|
304 |
// the date_select validation gets fired. Let Date module massage the format |
|
305 |
// for all day values so they will pass validation. The All day flag, if |
|
306 |
// used, actually exists on the parent element. |
|
304 | 307 |
date_all_day_value($element, $form_state); |
305 | 308 |
} |
306 | 309 |
|
307 | 310 |
/** |
308 | 311 |
* Implements hook_date_select_pre_validate_alter(). |
309 |
* |
|
310 |
* This hook lets us alter the element or the form_state before the rest |
|
311 |
* of the date_select validation gets fired. |
|
312 | 312 |
*/ |
313 | 313 |
function date_all_day_date_select_pre_validate_alter(&$element, &$form_state, &$input) { |
314 |
// Let Date module massage the format for all |
|
315 |
// day values so they will pass validation. |
|
316 |
// The All day flag, if used, actually exists on the parent element. |
|
314 |
// This hook lets us alter the element or the form_state before the rest of |
|
315 |
// the date_select validation gets fired. Let Date module massage the format |
|
316 |
// for all day values so they will pass validation. The All day flag, if |
|
317 |
// used, actually exists on the parent element. |
|
317 | 318 |
date_all_day_value($element, $form_state); |
318 | 319 |
} |
319 | 320 |
|
320 | 321 |
/** |
321 | 322 |
* Implements hook_date_select_pre_validate_alter(). |
322 |
* |
|
323 |
* This hook lets us alter the element or the form_state before the rest |
|
324 |
* of the date_popup validation gets fired. |
|
325 | 323 |
*/ |
326 | 324 |
function date_all_day_date_popup_pre_validate_alter(&$element, &$form_state, &$input) { |
327 |
// Let Date module massage the format for all |
|
328 |
// day values so they will pass validation. |
|
329 |
// The All day flag, if used, actually exists on the parent element. |
|
325 |
// This hook lets us alter the element or the form_state before the rest of |
|
326 |
// the date_popup validation gets fired. Let Date module massage the format |
|
327 |
// for all day values so they will pass validation. The All day flag, if |
|
328 |
// used, actually exists on the parent element. |
|
330 | 329 |
date_all_day_value($element, $form_state); |
331 | 330 |
} |
332 | 331 |
|
333 | 332 |
/** |
334 | 333 |
* A helper function date_all_day_value(). |
335 |
* |
|
336 |
* To check if the all day flag is set on the parent of an |
|
337 |
* element, and adjust the date_format accordingly so the missing time will |
|
338 |
* not cause validation errors. |
|
339 | 334 |
*/ |
340 | 335 |
function date_all_day_value(&$element, $form_state) { |
336 |
// To check if the all day flag is set on the parent of an element, and |
|
337 |
// adjust the date_format accordingly so the missing time will not cause |
|
338 |
// validation errors. |
|
341 | 339 |
if (!empty($element['#date_all_day_id'])) { |
342 | 340 |
$parents = $element['#parents']; |
343 | 341 |
array_pop($parents); |
... | ... | |
352 | 350 |
|
353 | 351 |
/** |
354 | 352 |
* Implements hook_date_combo_pre_validate_alter(). |
355 |
* |
|
356 |
* This hook lets us alter the element or the form_state before the rest |
|
357 |
* of the date_combo validation gets fired. |
|
358 | 353 |
*/ |
359 | 354 |
function date_all_day_date_combo_pre_validate_alter(&$element, &$form_state, $context) { |
360 |
|
|
355 |
// This hook lets us alter the element or the form_state before the rest of |
|
356 |
// the date_combo validation gets fired. |
|
361 | 357 |
if (!empty($context['item']['all_day'])) { |
362 |
|
|
363 | 358 |
$field = $context['field']; |
364 | 359 |
|
365 | 360 |
// If we have an all day flag on this date and the time is empty, |
... | ... | |
375 | 370 |
|
376 | 371 |
/** |
377 | 372 |
* Implements hook_date_combo_validate_date_start_alter(). |
378 |
* |
|
379 |
* This hook lets us alter the local date objects |
|
380 |
* created by the date_combo validation before they are |
|
381 |
* converted back to the database timezone and stored. |
|
382 | 373 |
*/ |
383 | 374 |
function date_all_day_date_combo_validate_date_start_alter(&$date, &$form_state, $context) { |
384 |
// If this is an 'All day' value, set the time to midnight. |
|
375 |
// This hook lets us alter the local date objects created by the date_combo |
|
376 |
// validation before they are converted back to the database timezone and |
|
377 |
// stored. If this is an 'All day' value, set the time to midnight. |
|
385 | 378 |
if (!empty($context['element']['#date_is_all_day'])) { |
386 | 379 |
$date->setTime(0, 0, 0); |
387 | 380 |
} |
... | ... | |
389 | 382 |
|
390 | 383 |
/** |
391 | 384 |
* Implements hook_date_combo_validate_date_end_alter(). |
392 |
* |
|
393 |
* This hook lets us alter the local date objects |
|
394 |
* created by the date_combo validation before |
|
395 |
* they are converted back to the database timezone and stored. |
|
396 | 385 |
*/ |
397 | 386 |
function date_all_day_date_combo_validate_date_end_alter(&$date, &$form_state, $context) { |
398 |
// If this is an 'All day' value, set the time to midnight. |
|
387 |
// This hook lets us alter the local date objects created by the date_combo |
|
388 |
// validation before they are converted back to the database timezone and |
|
389 |
// stored. If this is an 'All day' value, set the time as close to midnight as |
|
390 |
// possible. |
|
399 | 391 |
if (!empty($context['element']['#date_is_all_day'])) { |
400 |
$date->setTime(0, 0, 0); |
|
392 |
$increment = isset($context['instance']['widget']['settings']['increment']) ? $context['instance']['widget']['settings']['increment'] : 1; |
|
393 |
// @todo Should this reduce by an additional 1 minute if the increment is |
|
394 |
// more than 1? For example, if the increment is 15 should the upper value |
|
395 |
// be 23:45:59 or 23:44:59? |
|
396 |
$date->setTime(23, 60 - $increment, 59); |
|
401 | 397 |
} |
402 | 398 |
} |
403 | 399 |
|
404 | 400 |
/** |
405 | 401 |
* Implements hook_field_widget_info_alter(). |
406 |
* |
|
407 |
* This Field API hook lets us add a new setting to the widgets. |
|
408 | 402 |
*/ |
409 | 403 |
function date_all_day_field_widget_info_alter(&$info) { |
404 |
// This Field API hook lets us add a new setting to the widgets. |
|
410 | 405 |
// Add a setting to a widget type. |
411 | 406 |
$info['date_select']['settings'] += array( |
412 | 407 |
'display_all_day' => 0, |
... | ... | |
423 | 418 |
|
424 | 419 |
/** |
425 | 420 |
* Implements hook_date_field_widget_settings_form_alter(). |
426 |
* |
|
427 |
* This hook lets us alter the field settings form by adding a place |
|
428 |
* to set the value added above. |
|
429 | 421 |
*/ |
430 | 422 |
function date_all_day_date_field_widget_settings_form_alter(&$form, $context) { |
431 |
|
|
423 |
// This hook lets us alter the field settings form by adding a place to set |
|
424 |
// the value added above. |
|
432 | 425 |
$field = $context['field']; |
433 | 426 |
$instance = $context['instance']; |
434 | 427 |
|
Formats disponibles : Unified diff
Weekly update of contrib modules