Projet

Général

Profil

Paste
Télécharger (23,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_admin.inc @ ee46a8ed

1
<?php
2

    
3
/**
4
 * @file
5
 * Date administration code.
6
 */
7

    
8
/**
9
 * Settings for the default formatter.
10
 */
11
function date_default_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
12
  $display = $instance['display'][$view_mode];
13
  $settings = $display['settings'];
14
  $formatter = $display['type'];
15
  $form = array();
16

    
17
  $date_formats = date_format_type_options();
18
  $form['format_type'] = array(
19
    '#title' => t('Choose how users view dates and times:'),
20
    '#type' => 'select',
21
    '#options' => $date_formats + array('custom' => t('Custom')),
22
    '#default_value' => $settings['format_type'],
23
    '#description' => t('To add or edit options, visit <a href="@date-time-page">Date and time settings</a>.', array('@date-time-page' => url('admin/config/regional/date-time'))),
24
    '#weight' => 0,
25
  );
26

    
27
  $form['custom_date_format'] = array(
28
    '#type' => 'textfield',
29
    '#title' => t('Custom date format'),
30
    '#description' => t('If "Custom", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
31
    '#default_value' => isset($settings['custom_date_format']) ? $settings['custom_date_format'] : '',
32
    '#dependency' => array('edit-options-settings-format-type' => array('custom')),
33
  );
34

    
35
  $form['fromto'] = array(
36
    '#title' => t('Display:'),
37
    '#type' => 'select',
38
    '#options' => array(
39
      'both' => t('Both Start and End dates'),
40
      'value' => t('Start date only'),
41
      'value2' => t('End date only'),
42
    ),
43
    '#access' => $field['settings']['todate'],
44
    '#default_value' => $settings['fromto'],
45
    '#weight' => 1,
46
  );
47

    
48
  // Make the string translatable by keeping it as a whole rather than
49
  // translating prefix and suffix separately.
50
  list($prefix, $suffix) = explode('@count', t('Show @count value(s)'));
51
  $form['multiple_number'] = array(
52
    '#type' => 'textfield',
53
    '#title' => t('Multiple values:'),
54
    '#size' => 5,
55
    '#field_prefix' => $prefix,
56
    '#field_suffix' => $suffix,
57
    '#default_value' => $settings['multiple_number'],
58
    '#weight' => 2,
59
    '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
60
    '#description' => t('Identify a specific number of values to display, or leave blank to show all values.'),
61
  );
62

    
63
  list($prefix, $suffix) = explode('@isodate', t('starting from @isodate'));
64
  $form['multiple_from'] = array(
65
    '#type' => 'textfield',
66
    '#size' => 15,
67
    '#field_prefix' => $prefix,
68
    '#field_suffix' => $suffix,
69
    '#default_value' => $settings['multiple_from'],
70
    '#weight' => 3,
71
    '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
72
  );
73

    
74
  list($prefix, $suffix) = explode('@isodate', t('ending with @isodate'));
75
  $form['multiple_to'] = array(
76
    '#type' => 'textfield',
77
    '#size' => 15,
78
    '#field_prefix' => $prefix,
79
    '#field_suffix' => $suffix,
80
    '#default_value' => $settings['multiple_to'],
81
    '#weight' => 4,
82
    '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
83
    '#description' => t('Identify specific start and/or end dates in the format YYYY-MM-DDTHH:MM:SS, or leave blank for all available dates.'),
84
  );
85

    
86
  $form['show_remaining_days'] = array(
87
    '#title' => t('Show remaining days'),
88
    '#type' => 'checkbox',
89
    '#default_value' => $settings['show_remaining_days'],
90
    '#weight' => 0,
91
  );
92
  return $form;
93
}
94

    
95
/**
96
 * Settings for the interval formatter.
97
 */
98
function date_interval_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
99
  $display = $instance['display'][$view_mode];
100
  $settings = $display['settings'];
101
  $form = array();
102
  $form['interval'] = array(
103
    '#title' => t('Interval'),
104
    '#description' => t("How many time units should be shown in the 'time ago' string."),
105
    '#type' => 'select',
106
    '#options' => drupal_map_assoc(range(1, 6)),
107
    '#default_value' => $settings['interval'],
108
    '#weight' => 0,
109
  );
110
  // Uses the same options used by Views format_interval.
111
  $options = array(
112
    'raw time ago' => t('Time ago'),
113
    'time ago' => t('Time ago (with "ago" appended)'),
114
    'raw time hence' => t('Time hence'),
115
    'time hence' => t('Time hence (with "hence" appended)'),
116
    'raw time span' => t('Time span (future dates have "-" prepended)'),
117
    'inverse time span' => t('Time span (past dates have "-" prepended)'),
118
    'time span' => t('Time span (with "ago/hence" appended)'),
119
  );
120
  $form['interval_display'] = array(
121
    '#title' => t('Display'),
122
    '#description' => t("How to display the time ago or time hence for this field."),
123
    '#type' => 'select',
124
    '#options' => $options,
125
    '#default_value' => $settings['interval_display'],
126
    '#weight' => 0,
127
  );
128
  if (!empty($field['settings']['todate'])) {
129
    $form['use_end_date'] = array(
130
      '#title' => t('Use End date'),
131
      '#description' => 'Use the End date, instead of the start date',
132
      '#type' => 'checkbox',
133
      '#default_value' => $settings['use_end_date'],
134
    );
135
  }
136
  return $form;
137
}
138

    
139
/**
140
 * Settings summary for the default formatter.
141
 */
142
function date_default_formatter_settings_summary($field, $instance, $view_mode) {
143
  $display = $instance['display'][$view_mode];
144
  $settings = $display['settings'];
145
  $formatter = $display['type'];
146
  $format_types = date_format_type_options();
147
  $summary = array();
148
  $format = FALSE;
149
  switch ($formatter) {
150
    case 'date_plain':
151
      $format = t('Plain');
152
      break;
153

    
154
    case 'format_interval':
155
      $format = t('Interval');
156
      break;
157

    
158
    default:
159
      if (!empty($format_types[$settings['format_type']])) {
160
        $format = $format_types[$settings['format_type']];
161
      }
162
  }
163
  if ($format) {
164
    $summary[] = t('Display dates using the @format format', array('@format' => $format));
165
  }
166
  else {
167
    $summary[] = t('Display dates using the default format because the specified format (@format) is not defined', array('@format' => $settings['format_type']));
168
  }
169

    
170
  if (array_key_exists('fromto', $settings) && $field['settings']['todate']) {
171
    $options = array(
172
      'both' => t('Display both Start and End dates'),
173
      'value' => t('Display Start date only'),
174
      'value2' => t('Display End date only'),
175
    );
176
    if (isset($options[$settings['fromto']])) {
177
      $summary[] = $options[$settings['fromto']];
178
    }
179
  }
180

    
181
  if (array_key_exists('multiple_number', $settings) && !empty($field['cardinality'])) {
182
    $summary[] = t('Show @count value(s) starting with @date1, ending with @date2', array(
183
      '@count' => !empty($settings['multiple_number']) ? $settings['multiple_number'] : t('all'),
184
      '@date1' => !empty($settings['multiple_from']) ? $settings['multiple_from'] : t('earliest'),
185
      '@date2' => !empty($settings['multiple_to']) ? $settings['multiple_to'] : t('latest'),
186
    ));
187
  }
188

    
189
  if (array_key_exists('show_remaining_days', $settings)) {
190
    $summary[] = t('Show remaining days: @value', array('@value' => ($settings['show_remaining_days'] ? 'yes' : 'no')));
191
  }
192

    
193
  return $summary;
194
}
195

    
196
/**
197
 * Settings summary for the interval formatter.
198
 *
199
 * @TODO Add settings later.
200
 */
201
function date_interval_formatter_settings_summary($field, $instance, $view_mode) {
202
  $summary = array();
203
  $display = $instance['display'][$view_mode];
204
  $settings = $display['settings'];
205
  $formatter = $display['type'];
206
  $field = ($settings['use_end_date'] == 1) ? 'End' : 'Start';
207
  $summary[] = t('Display time ago, showing @interval units. Using @field Date',
208
      array('@interval' => $settings['interval'], '@field' => $field));
209

    
210
  return $summary;
211
}
212

    
213
/**
214
 * Helper function for date_field_instance_settings_form().
215
 *
216
 * @see date_field_instance_settings_form_validate()
217
 */
218
function _date_field_instance_settings_form($field, $instance) {
219
  $widget = $instance['widget'];
220
  $settings = $instance['settings'];
221
  $widget_settings = $instance['widget']['settings'];
222

    
223
  $form['default_value'] = array(
224
    '#type' => 'select',
225
    '#title' => t('Default date'),
226
    '#default_value' => $settings['default_value'],
227
    '#options' => array(
228
      'blank'     => t('No default value'),
229
      'now'       => t('Now'),
230
      'strtotime' => t('Relative'),
231
    ),
232
    '#weight' => 1,
233
    '#fieldset' => 'default_values',
234
  );
235

    
236
  $description = t("Describe a time by reference to the current day, like '+90 days' (90 days from the day the field is created) or '+1 Saturday' (the next Saturday). See !strtotime for more details.", array('!strtotime' => l(t('strtotime'), 'http://www.php.net/manual/en/function.strtotime.php')));
237
  $form['default_value_code'] = array(
238
    '#type' => 'textfield',
239
    '#title' => t('Relative default value'),
240
    '#description' => $description,
241
    '#default_value' => $settings['default_value_code'],
242
    '#states' => array(
243
      'visible' => array(
244
        ':input[name="instance[settings][default_value]"]' => array(
245
          'value' => 'strtotime',
246
        ),
247
      ),
248
    ),
249
    '#weight' => 1.1,
250
    '#fieldset' => 'default_values',
251
  );
252
  $form['default_value2'] = array(
253
    '#type' => !empty($field['settings']['todate']) ? 'select' : 'hidden',
254
    '#title' => t('Default end date'),
255
    '#default_value' => $settings['default_value2'],
256
    '#options' => array(
257
      'same'      => t('Same as Default date'),
258
      'blank'     => t('No default value'),
259
      'now'       => t('Now'),
260
      'strtotime' => t('Relative'),
261
    ),
262
    '#weight' => 2,
263
    '#fieldset' => 'default_values',
264
  );
265
  $form['default_value_code2'] = array(
266
    '#type' => !empty($field['settings']['todate']) ? 'textfield' : 'hidden',
267
    '#title' => t('Relative default value for end date'),
268
    '#description' => $description,
269
    '#default_value' => $settings['default_value_code2'],
270
    '#states' => array(
271
      'visible' => array(
272
        ':input[name="instance[settings][default_value2]"]' => array(
273
          'value' => 'strtotime',
274
        ),
275
      ),
276
    ),
277
    '#weight' => 2.1,
278
    '#fieldset' => 'default_values',
279
  );
280

    
281
  $form['#element_validate'] = array('date_field_instance_settings_form_validate');
282

    
283
  $context = array(
284
    'field' => $field,
285
    'instance' => $instance,
286
  );
287
  drupal_alter('date_field_instance_settings_form', $form, $context);
288

    
289
  return $form;
290
}
291

    
292
/**
293
 * Form validation handler for _date_field_instance_settings_form().
294
 */
295
function _date_field_instance_settings_form_validate(&$form, &$form_state) {
296
  $settings = $form_state['values']['instance']['settings'];
297

    
298
  if ($settings['default_value'] == 'strtotime') {
299
    $is_strtotime = @strtotime($settings['default_value_code']);
300
    if (!$is_strtotime) {
301
      form_set_error('instance][settings][default_value_code', t('The Strtotime default value is invalid.'));
302
    }
303
  }
304
  if (isset($settings['default_value2']) && $settings['default_value2'] == 'strtotime') {
305
    $is_strtotime = @strtotime($settings['default_value_code2']);
306
    if (!$is_strtotime) {
307
      form_set_error('instance][settings][default_value_code2', t('The Strtotime default value for the End Date is invalid.'));
308
    }
309
  }
310
}
311

    
312
/**
313
 * Helper function for date_field_widget_settings_form().
314
 *
315
 * @see date_field_widget_settings_form_validate()
316
 */
317
function _date_field_widget_settings_form($field, $instance) {
318
  $widget = $instance['widget'];
319
  $settings = $widget['settings'];
320

    
321
  $form = array(
322
    '#element_validate' => array('date_field_widget_settings_form_validate'),
323
  );
324

    
325
  $options = array();
326
  if ($widget['type'] == 'date_popup' && module_exists('date_popup')) {
327
    $formats = date_popup_formats();
328
  }
329
  else {
330
    // Example input formats must show all possible date parts, so add seconds.
331
    $formats = str_replace('i', 'i:s', array_keys(system_get_date_formats('short')));
332
    $formats = drupal_map_assoc($formats);
333
  }
334
  $now = date_example_date();
335
  $options['site-wide'] = t('Short date format: @date', array('@date' => date_format_date($now, 'short')));
336
  foreach ($formats as $f) {
337
    $options[$f] = date_format_date($now, 'custom', $f);
338
  }
339
  $form['input_format'] = array(
340
    '#type' => 'select',
341
    '#title' => t('Date entry options'),
342
    '#default_value' => $settings['input_format'],
343
    '#options' => $options,
344
    '#description' => t('Control the order and format of the options users see.'),
345
    '#weight' => 3,
346
    '#fieldset' => 'date_format',
347
  );
348
  // Only a limited set of formats is available for the Date Popup module.
349
  if ($widget['type'] != 'date_popup') {
350
    $form['input_format']['#options']['custom'] = t('Custom format');
351
    $form['input_format_custom'] = array(
352
      '#type' => 'textfield',
353
      '#title' => t('Custom input format'),
354
      '#default_value' => $settings['input_format_custom'],
355
      '#description' => t("Override the input format selected above. Define a php date format string like 'm-d-Y H:i' (see <a href=\"@link\">http://php.net/date</a> for more details).", array('@link' => 'http://php.net/date')),
356
      '#weight' => 5,
357
      '#fieldset' => 'date_format',
358
      '#attributes' => array('class' => array('indent')),
359
      '#states' => array(
360
        'visible' => array(
361
          ':input[name="instance[widget][settings][input_format]"]' => array('value' => 'custom'),
362
        ),
363
      ),
364
    );
365
  }
366
  else {
367
    $form['input_format_custom'] = array(
368
      '#type' => 'hidden',
369
      '#value' => '',
370
    );
371
  }
372

    
373
  if (in_array($widget['type'], array('date_select', 'date_popup'))) {
374
    $form['year_range'] = array(
375
      '#type' => 'date_year_range',
376
      '#default_value' => $settings['year_range'],
377
      '#fieldset' => 'date_format',
378
      '#weight' => 6,
379
    );
380
    $form['increment'] = array(
381
      '#type' => 'select', '#title' => t('Time increments'),
382
      '#default_value' => $settings['increment'],
383
      '#options' => array(
384
        1 => t('1 minute'),
385
        5 => t('5 minute'),
386
        10 => t('10 minute'),
387
        15 => t('15 minute'),
388
        30 => t('30 minute')),
389
      '#weight' => 7,
390
      '#fieldset' => 'date_format',
391
    );
392
  }
393
  else {
394
    $form['year_range'] = array(
395
      '#type' => 'hidden',
396
      '#value' => $settings['year_range'],
397
    );
398
    $form['increment'] = array(
399
      '#type' => 'hidden',
400
      '#value' => $settings['increment'],
401
    );
402
  }
403

    
404
  $form['label_position'] = array(
405
    '#type' => 'value',
406
    '#value' => $settings['label_position'],
407
  );
408
  $form['text_parts'] = array(
409
    '#type' => 'value',
410
    '#value' => $settings['text_parts'],
411
  );
412
  $form['advanced'] = array(
413
    '#type' => 'fieldset',
414
    '#title' => t('Advanced settings'),
415
    '#collapsible' => TRUE,
416
    '#collapsed' => TRUE,
417
    '#fieldset' => 'date_format',
418
    '#weight' => 9,
419
  );
420
  if (in_array($widget['type'], array('date_select'))) {
421
    $options = array(
422
      'above' => t('Above'),
423
      'within' => t('Within'),
424
      'none' => t('None'),
425
    );
426
    $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'Within' inserts the label as the first option in the select list and in blank textfields. 'None' doesn't visually label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
427
  }
428
  else {
429
    $options = array(
430
      'above' => t('Above'),
431
      'none' => t('None'),
432
    );
433
    $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'None' doesn't visually label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
434
  }
435
  $form['advanced']['label_position'] = array(
436
    '#type' => 'radios',
437
    '#options' => $options,
438
    '#default_value' => $settings['label_position'],
439
    '#title' => t('Position of date part labels'),
440
    '#description' => $description,
441
  );
442
  $form['advanced']['text_parts'] = array(
443
    '#theme' => $widget['type'] == 'date_select' ? 'date_text_parts' : '',
444
  );
445
  $text_parts = (array) $settings['text_parts'];
446
  foreach (date_granularity_names() as $key => $value) {
447
    if ($widget['type'] == 'date_select') {
448
      $form['advanced']['text_parts'][$key] = array(
449
        '#type' => 'radios',
450
        '#default_value' => in_array($key, $text_parts) ? 1 : 0,
451
        '#options' => array(0 => '', 1 => ''),
452
      );
453
    }
454
    else {
455
      $form['advanced']['text_parts'][$key] = array(
456
        '#type' => 'value',
457
        '#value' => (int) in_array($key, (array) $settings['text_parts']),
458
      );
459
    }
460
  }
461

    
462
  $form['advanced']['no_fieldset'] = array(
463
    '#type' => 'checkbox',
464
    '#title' => t('Render as a regular field'),
465
    '#default_value' => !empty($settings['no_fieldset']),
466
    '#description' => t('Whether to render this field as a regular field instead of a fieldset. The date field elements are wrapped in a fieldset by default, and may not display well without it.'),
467
  );
468

    
469
  $context = array(
470
    'field' => $field,
471
    'instance' => $instance,
472
  );
473
  drupal_alter('date_field_widget_settings_form', $form, $context);
474

    
475
  return $form;
476
}
477

    
478
/**
479
 * Form validation handler for _date_field_widget_settings_form().
480
 */
481
function _date_field_widget_settings_form_validate(&$form, &$form_state) {
482
  // The widget settings are in the wrong place in the form because of #tree on
483
  // the top level.
484
  $settings = $form_state['values']['instance']['widget']['settings'];
485
  $settings = array_merge($settings, $settings['advanced']);
486
  unset($settings['advanced']);
487
  form_set_value(array('#parents' => array('instance', 'widget', 'settings')), $settings, $form_state);
488

    
489
  $widget = &$form_state['values']['instance']['widget'];
490
  // Munge the table display for text parts back into an array of text parts.
491
  if (is_array($widget['settings']['text_parts'])) {
492
    form_set_value($form['text_parts'], array_keys(array_filter($widget['settings']['text_parts'])), $form_state);
493
  }
494

    
495
  if ($widget['settings']['input_format'] === 'custom' && empty($widget['settings']['input_format_custom'])) {
496
    form_set_error('instance][widget][settings][input_format_custom', t('Please enter a custom date format, or choose one of the preset formats.'));
497
  }
498
}
499

    
500
/**
501
 * Helper function for date_field_settings_form().
502
 *
503
 * @see date_field_settings_validate()
504
 */
505
function _date_field_settings_form($field, $instance, $has_data) {
506
  $settings = $field['settings'];
507

    
508
  $form = array(
509
    '#element_validate' => array('date_field_settings_validate'),
510
  );
511

    
512
  // Make sure granularity is in the right format and has no empty values.
513
  if (!empty($settings['granularity']) && is_array($settings['granularity'])) {
514
    $granularity = array_filter($settings['granularity']);
515
  }
516
  $tz_handling = $settings['tz_handling'];
517

    
518
  $description = t('Select the date attributes to collect and store.');
519
  if ($has_data) {
520
    $description .= ' ' . t('Changes to date attributes only effects new or updated content.');
521
  }
522
  $options = date_granularity_names();
523
  $checkbox_year = array(
524
    '#type' => 'checkbox',
525
    '#title' => check_plain($options['year']),
526
    '#value' => 'year',
527
    '#return_value' => 'year',
528
    '#disabled' => TRUE,
529
  );
530
  unset($options['year']);
531
  $form['granularity'] = array(
532
    '#type' => 'checkboxes',
533
    '#title' => t('Date attributes to collect'),
534
    '#default_value' => $granularity,
535
    '#options' => $options,
536
    '#attributes' => array(
537
      'class' => array('container-inline'),
538
    ),
539
    '#description' => $description,
540
    'year' => $checkbox_year,
541
  );
542

    
543
  $description = t('End dates are used to collect duration. E.g., allow an event to start on September 15, and end on September 16.');
544
  $form['enddate_get'] = array(
545
    '#type' => 'checkbox',
546
    '#title' => t('Collect an end date'),
547
    '#description' => $description,
548
    '#default_value' => (empty($settings['todate']) ? FALSE : TRUE),
549
    '#disabled' => $has_data,
550
  );
551
  $form['enddate_required'] = array(
552
    '#type' => 'checkbox',
553
    '#title' => t('Required'),
554
    '#default_value' => ((isset($settings['todate']) && $settings['todate'] === 'required') ? TRUE : FALSE),
555
    '#disabled' => $has_data,
556
    '#states' => array(
557
      'invisible' => array(
558
        'input[name="field[settings][enddate_get]"]' => array('checked' => FALSE),
559
      ),
560
    ),
561
  );
562
  $description = t('Select the timezone handling method for this date field.');
563
  $form['tz_handling'] = array(
564
    '#type' => 'select',
565
    '#title' => t('Time zone handling'),
566
    '#default_value' => $tz_handling,
567
    '#options' => date_timezone_handling_options(),
568
    '#description' => $description,
569
    '#attached' => array(
570
      'js' => array(drupal_get_path('module', 'date') . '/date_admin.js'),
571
    ),
572
  );
573
  // Force this value to hidden because we don't want to allow it to be changed
574
  // right now, but allow it to be a variable if needed.
575
  $form['timezone_db'] = array(
576
    '#type' => 'hidden',
577
    '#value' => date_get_timezone_db($tz_handling),
578
  );
579

    
580
  $form['cache_enabled'] = array(
581
    '#type' => 'checkbox',
582
    '#title' => t('Cache dates'),
583
    '#description' => t('Date objects can be created and cached as date fields are loaded, rather than when they are displayed, to improve performance.'),
584
    '#default_value' => !empty($settings['cache_enabled']),
585
    '#weight' => 10,
586
  );
587
  $form['cache_count'] = array(
588
    '#type' => 'textfield',
589
    '#title' => t('Maximum dates per field'),
590
    '#default_value' => (isset($settings['cache_count'])) ? $settings['cache_count'] : 4,
591
    '#description' => t("If set to '0', all date values on every entity will be cached. Note that caching every date on fields that may have a large number of multiple or repeating values may create a significant performance penalty when the cache is cleared. The suggested setting for multiple value and repeating fields is no more than 4 values per field."),
592
    '#size' => 3,
593
    '#weight' => 11,
594
    '#states' => array(
595
      'visible' => array(
596
        'input[name="field[settings][cache_enabled]"]' => array(
597
          'checked' => TRUE,
598
        ),
599
      ),
600
    ),
601
  );
602

    
603
  $context = array(
604
    'field' => $field,
605
    'instance' => $instance,
606
    'has_data' => $has_data,
607
  );
608
  drupal_alter('date_field_settings_form', $form, $context);
609

    
610
  return $form;
611
}
612

    
613
/**
614
 * Form validation handler for _date_field_settings_form().
615
 */
616
function _date_field_settings_validate(&$form, &$form_state) {
617
  $field = &$form_state['values']['field'];
618

    
619
  if ($field['settings']['tz_handling'] == 'none') {
620
    form_set_value($form['timezone_db'], '', $form_state);
621
  }
622
  else {
623
    form_set_value($form['timezone_db'], date_get_timezone_db($field['settings']['tz_handling']), $form_state);
624
  }
625

    
626
  if ($field['settings']['tz_handling'] != 'none' && !in_array('hour', array_filter($field['settings']['granularity']))) {
627
    form_set_error('field[settings][tz_handling]', t('Dates without hours granularity must not use any timezone handling.'));
628
  }
629

    
630
  // Extract the correct 'todate' value out of the two end date checkboxes.
631
  if ($field['settings']['enddate_get']) {
632
    if ($field['settings']['enddate_required']) {
633
      $field['settings']['todate'] = 'required';
634
    }
635
    else {
636
      $field['settings']['todate'] = 'optional';
637
    }
638
  }
639
  else {
640
    $field['settings']['todate'] = '';
641
  }
642

    
643
  // Don't save the pseudo values created in the UI.
644
  unset($field['settings']['enddate_get'], $field['settings']['enddate_required']);
645

    
646
  if (!empty($field['settings']['cache_enabled'])) {
647
    if (!is_numeric($field['settings']['cache_count'])) {
648
      form_set_error('field[settings][cache_count]', t('The number of cache values must be a number.'));
649
    }
650
    elseif ($field['settings']['cache_count'] < 0) {
651
      form_set_error('field[settings][cache_count]', t('The number of cache values must be a number 0 or greater.'));
652
    }
653
  }
654
}
655

    
656
/**
657
 * Timezone handling options.
658
 *
659
 * The 'none' option will do no timezone conversions and will store and display
660
 * dates exactly as entered useful in locales or situations where timezone
661
 * conversions are not working reliably, for dates with no times, for historical
662
 * dates where timezones are irrelevant, or anytime conversion is unnecessary or
663
 * undesirable.
664
 */
665
function date_timezone_handling_options() {
666
  return array(
667
    'site' => t("Site's time zone"),
668
    'date' => t("Date's time zone"),
669
    'user' => t("User's time zone"),
670
    'utc'  => 'UTC',
671
    'none' => t('No time zone conversion'),
672
  );
673
}