Projet

Général

Profil

Paste
Télécharger (21,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date_admin.inc @ 76df55b7

1 85ad3d82 Assos Assos
<?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
  $form['format_type'] = array(
18
    '#title' => t('Choose how users view dates and times:'),
19
    '#type' => 'select',
20
    '#options' => date_format_type_options(),
21
    '#default_value' => $settings['format_type'],
22
    '#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'))),
23
    '#weight' => 0,
24
  );
25
26
  $form['fromto'] = array(
27
    '#title' => t('Display:'),
28
    '#type' => 'select',
29
    '#options' => array(
30
      'both' => t('Both Start and End dates'),
31
      'value' => t('Start date only'),
32
      'value2' => t('End date only'),
33
    ),
34
    '#access' => $field['settings']['todate'],
35
    '#default_value' => $settings['fromto'],
36
    '#weight' => 1,
37
  );
38
39
  // Make the string translatable by keeping it as a whole rather than
40
  // translating prefix and suffix separately.
41
  list($prefix, $suffix) = explode('@count', t('Show @count value(s)'));
42
  $form['multiple_number'] = array(
43
    '#type' => 'textfield',
44
    '#title' => t('Multiple values:'),
45
    '#size' => 5,
46
    '#field_prefix' => $prefix,
47
    '#field_suffix' => $suffix,
48
    '#default_value' => $settings['multiple_number'],
49
    '#weight' => 2,
50
    '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
51
    '#description' => t('Identify a specific number of values to display, or leave blank to show all values.'),
52
  );
53
54
  list($prefix, $suffix) = explode('@isodate', t('starting from @isodate'));
55
  $form['multiple_from'] = array(
56
    '#type' => 'textfield',
57
    '#size' => 15,
58
    '#field_prefix' => $prefix,
59
    '#field_suffix' => $suffix,
60
    '#default_value' => $settings['multiple_from'],
61
    '#weight' => 3,
62
    '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
63
  );
64
65
  list($prefix, $suffix) = explode('@isodate', t('ending with @isodate'));
66
  $form['multiple_to'] = array(
67
    '#type' => 'textfield',
68
    '#size' => 15,
69
    '#field_prefix' => $prefix,
70
    '#field_suffix' => $suffix,
71
    '#default_value' => $settings['multiple_to'],
72
    '#weight' => 4,
73
    '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
74
    '#description' => t('Identify specific start and/or end dates in the format YYYY-MM-DDTHH:MM:SS, or leave blank for all available dates.'),
75
  );
76
77
  return $form;
78
}
79
80
/**
81
 * Settings for the interval formatter.
82
 */
83
function date_interval_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
84
  $display = $instance['display'][$view_mode];
85
  $settings = $display['settings'];
86
  $form = array();
87
  $form['interval'] = array(
88
    '#title' => t('Interval'),
89
    '#description' => t("How many time units should be shown in the 'time ago' string."),
90
    '#type' => 'select',
91
    '#options' => drupal_map_assoc(range(1, 6)),
92
    '#default_value' => $settings['interval'],
93
    '#weight' => 0,
94
  );
95
  // Uses the same options used by Views format_interval.
96
  $options = array(
97
    'raw time ago' => t('Time ago'),
98
    'time ago' => t('Time ago (with "ago" appended)'),
99
    'raw time hence' => t('Time hence'),
100
    'time hence' => t('Time hence (with "hence" appended)'),
101
    'raw time span' => t('Time span (future dates have "-" prepended)'),
102
    'inverse time span' => t('Time span (past dates have "-" prepended)'),
103
    'time span' => t('Time span (with "ago/hence" appended)'),
104
  );
105
  $form['interval_display'] = array(
106
    '#title' => t('Display'),
107
    '#description' => t("How to display the time ago or time hence for this field."),
108
    '#type' => 'select',
109
    '#options' => $options,
110
    '#default_value' => $settings['interval_display'],
111
    '#weight' => 0,
112
  );
113
  return $form;
114
}
115
116
/**
117
 * Settings summary for the default formatter.
118
 */
119
function date_default_formatter_settings_summary($field, $instance, $view_mode) {
120
  $display = $instance['display'][$view_mode];
121
  $settings = $display['settings'];
122
  $formatter = $display['type'];
123
  $format_types = date_format_type_options();
124
  $summary = array();
125
  $format = FALSE;
126
  switch ($formatter) {
127
    case 'date_plain':
128
      $format = t('Plain');
129
      break;
130
    case 'format_interval':
131
      $format = t('Interval');
132
      break;
133
    default:
134
      if (!empty($format_types[$settings['format_type']])) {
135
        $format = $format_types[$settings['format_type']];
136
      }
137
  }
138
  if ($format) {
139
    $summary[] = t('Display dates using the @format format', array('@format' => $format));
140
  }
141
  else {
142
    $summary[] = t('Display dates using the default format because the specified format (@format) is not defined', array('@format' => $settings['format_type']));
143
  }
144
145
  if (array_key_exists('fromto', $settings) && $field['settings']['todate']) {
146
    $options = array(
147
      'both' => t('Display both Start and End dates'),
148
      'value' => t('Display Start date only'),
149
      'value2' => t('Display End date only'),
150
    );
151
    $summary[] = $options[$settings['fromto']];
152
  }
153
154
  if (array_key_exists('multiple_number', $settings) && !empty($field['cardinality'])) {
155
    $summary[] = t('Show @count value(s) starting with @date1, ending with @date2', array(
156
      '@count' => !empty($settings['multiple_number']) ? $settings['multiple_number'] : t('all'),
157
      '@date1' => !empty($settings['multiple_from']) ? $settings['multiple_from'] : t('earliest'),
158
      '@date2' => !empty($settings['multiple_to']) ? $settings['multiple_to'] : t('latest'),
159
    ));
160
  }
161
162
  return $summary;
163
}
164
165
/**
166
 * Settings summary for the interval formatter.
167
 *
168
 * @TODO Add settings later.
169
 */
170
function date_interval_formatter_settings_summary($field, $instance, $view_mode) {
171
  $summary = array();
172
  $display = $instance['display'][$view_mode];
173
  $settings = $display['settings'];
174
  $formatter = $display['type'];
175
  $summary[] = t('Display time ago, showing @interval units.', array('@interval' => $settings['interval']));
176
177
  return $summary;
178
}
179
180
/**
181
 * Helper function for date_field_instance_settings_form().
182
 *
183
 * @see date_field_instance_settings_form_validate()
184
 */
185
function _date_field_instance_settings_form($field, $instance) {
186
  $widget = $instance['widget'];
187
  $settings = $instance['settings'];
188
  $widget_settings = $instance['widget']['settings'];
189
190
  $form['default_value'] = array(
191
    '#type' => 'select',
192
    '#title' => t('Default date'),
193
    '#default_value' => $settings['default_value'],
194
    '#options' => array('blank' => t('No default value'), 'now' => t('Now'), 'strtotime' => t('Relative')),
195
    '#weight' => 1,
196
    '#fieldset' => 'default_values',
197
  );
198
199
  $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')));
200
  $form['default_value_code'] = array(
201
    '#type' => 'textfield',
202
    '#title' => t('Relative default value'),
203
    '#description' => $description,
204
    '#default_value' => $settings['default_value_code'],
205
    '#states' => array(
206
      'visible' => array(
207
        ':input[name="instance[settings][default_value]"]' => array('value' => 'strtotime')),
208
      ),
209
    '#weight' => 1.1,
210
    '#fieldset' => 'default_values',
211
  );
212
  $form['default_value2'] = array(
213
    '#type' => !empty($field['settings']['todate']) ? 'select' : 'hidden',
214
    '#title' => t('Default end date'),
215
    '#default_value' => $settings['default_value2'],
216
    '#options' => array('same' => t('Same as Default date'), 'blank' => t('No default value'), 'now' => t('Now'), 'strtotime' => t('Relative')),
217
    '#weight' => 2,
218
    '#fieldset' => 'default_values',
219
  );
220
  $form['default_value_code2'] = array(
221
    '#type' => !empty($field['settings']['todate']) ? 'textfield' : 'hidden',
222
    '#title' => t('Relative default value for end date'),
223
    '#description' => $description,
224
    '#default_value' => $settings['default_value_code2'],
225
    '#states' => array(
226
      'visible' => array(
227
        ':input[name="instance[settings][default_value2]"]' => array('value' => 'strtotime')),
228
      ),
229
    '#weight' => 2.1,
230
    '#fieldset' => 'default_values',
231
  );
232
233
  $form['#element_validate'] = array('date_field_instance_settings_form_validate');
234
235
  $context = array(
236
    'field' => $field,
237
    'instance' => $instance,
238
  );
239
  drupal_alter('date_field_instance_settings_form', $form, $context);
240
241
  return $form;
242
}
243
244
/**
245
 * Form validation handler for _date_field_instance_settings_form().
246
 */
247
function date_field_instance_settings_form_validate(&$form, &$form_state) {
248
  $settings = $form_state['values']['instance']['settings'];
249
250
  if ($settings['default_value'] == 'strtotime') {
251
    $is_strtotime = @strtotime($settings['default_value_code']);
252
    if (!$is_strtotime) {
253
      form_set_error('instance][settings][default_value_code', t('The Strtotime default value is invalid.'));
254
    }
255
  }
256
  if (isset($settings['default_value2']) && $settings['default_value2'] == 'strtotime') {
257
    $is_strtotime = @strtotime($settings['default_value_code2']);
258
    if (!$is_strtotime) {
259
      form_set_error('instance][settings][default_value_code2', t('The Strtotime default value for the End Date is invalid.'));
260
    }
261
  }
262
}
263
264
/**
265
 * Helper function for date_field_widget_settings_form().
266
 *
267
 * @see date_field_widget_settings_form_validate()
268
 */
269
function _date_field_widget_settings_form($field, $instance) {
270
  $widget = $instance['widget'];
271
  $settings = $widget['settings'];
272
273
  $form = array(
274
    '#element_validate' => array('date_field_widget_settings_form_validate'),
275
  );
276
277
  $options = array();
278
  if ($widget['type'] == 'date_popup' && module_exists('date_popup')) {
279
    $formats = date_popup_formats();
280
  }
281
  else {
282
    // Example input formats must show all possible date parts, so add seconds.
283
    $formats = str_replace('i', 'i:s', array_keys(system_get_date_formats('short')));
284
    $formats = drupal_map_assoc($formats);
285
  }
286
  $now = date_example_date();
287
  foreach ($formats as $f) {
288
    $options[$f] = date_format_date($now, 'custom', $f);
289
  }
290
  $form['input_format'] = array(
291
    '#type' => 'select',
292
    '#title' => t('Date entry options'),
293
    '#default_value' => $settings['input_format'],
294
    '#options' => $options,
295
    '#description' => t('Control the order and format of the options users see.'),
296
    '#weight' => 3,
297
    '#fieldset' => 'date_format',
298
  );
299
  // Only a limited set of formats is available for the Date Popup module.
300
  if ($widget['type'] != 'date_popup') {
301
    $form['input_format']['#options']['custom'] = t('Custom format');
302
    $form['input_format_custom'] = array(
303
      '#type' => 'textfield',
304
      '#title' => t('Custom input format'),
305
      '#default_value' => $settings['input_format_custom'],
306
      '#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')),
307
      '#weight' => 5,
308
      '#fieldset' => 'date_format',
309
      '#attributes' => array('class' => array('indent')),
310
      '#states' => array(
311
        'visible' => array(
312
          ':input[name="instance[widget][settings][input_format]"]' => array('value' => 'custom'),
313
        ),
314
      ),
315
    );
316
  }
317
  else {
318
    $form['input_format_custom'] = array(
319
      '#type' => 'hidden',
320
      '#value' => '',
321
    );
322
  }
323
324
  if (in_array($widget['type'], array('date_select', 'date_popup'))) {
325
    $form['year_range'] = array(
326
      '#type' => 'date_year_range',
327
      '#default_value' => $settings['year_range'],
328
      '#fieldset' => 'date_format',
329
      '#weight' => 6,
330
    );
331
    $form['increment'] = array(
332
      '#type' => 'select', '#title' => t('Time increments'),
333
      '#default_value' => $settings['increment'],
334
      '#options' => array(
335
        1 => t('1 minute'),
336
        5 => t('5 minute'),
337
        10 => t('10 minute'),
338
        15 => t('15 minute'),
339
        30 => t('30 minute')),
340
      '#weight' => 7,
341
      '#fieldset' => 'date_format',
342
    );
343
  }
344
  else {
345
    $form['year_range'] = array(
346
      '#type' => 'hidden',
347
      '#value' => $settings['year_range'],
348
    );
349
    $form['increment'] = array(
350
      '#type' => 'hidden',
351
      '#value' => $settings['increment'],
352
    );
353
  }
354
355
  $form['label_position'] = array(
356
    '#type' => 'value',
357
    '#value' => $settings['label_position'],
358
  );
359
  $form['text_parts'] = array(
360
    '#type' => 'value',
361
    '#value' => $settings['text_parts'],
362
  );
363
  $form['advanced'] = array(
364
    '#type' => 'fieldset',
365
    '#title' => t('Advanced settings'),
366
    '#collapsible' => TRUE,
367
    '#collapsed' => TRUE,
368
    '#fieldset' => 'date_format',
369
    '#weight' => 9,
370
  );
371
  if (in_array($widget['type'], array('date_select'))) {
372
    $options = array('above' => t('Above'), 'within' => t('Within'), 'none' => t('None'));
373
    $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.");
374
  }
375
  else {
376
    $options = array('above' => t('Above'), 'none' => t('None'));
377
    $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.");
378
  }
379
  $form['advanced']['label_position'] = array(
380
    '#type' => 'radios',
381
    '#options' => $options,
382
    '#default_value' => $settings['label_position'],
383
    '#title' => t('Position of date part labels'),
384
    '#description' => $description,
385
  );
386
  $form['advanced']['text_parts'] = array(
387
    '#theme' => $widget['type'] == 'date_select' ? 'date_text_parts' : '',
388
  );
389
  $text_parts = (array) $settings['text_parts'];
390
  foreach (date_granularity_names() as $key => $value) {
391
    if ($widget['type'] == 'date_select') {
392
      $form['advanced']['text_parts'][$key] = array(
393
        '#type' => 'radios',
394
        '#default_value' => in_array($key, $text_parts) ? 1 : 0,
395
        '#options' => array(0 => '', 1 => ''),
396
      );
397
    }
398
    else {
399
      $form['advanced']['text_parts'][$key] = array(
400
        '#type' => 'value',
401
        '#value' => (int) in_array($key, (array) $settings['text_parts']),
402
      );
403
    }
404
  }
405
406
  $context = array(
407
    'field' => $field,
408
    'instance' => $instance,
409
  );
410
  drupal_alter('date_field_widget_settings_form', $form, $context);
411
412
  return $form;
413
}
414
415
/**
416
 * Form validation handler for _date_field_widget_settings_form().
417
 */
418
function date_field_widget_settings_form_validate(&$form, &$form_state) {
419
  // The widget settings are in the wrong place in the form because of #tree on
420
  // the top level.
421
  $settings = $form_state['values']['instance']['widget']['settings'];
422
  $settings = array_merge($settings, $settings['advanced']);
423
  unset($settings['advanced']);
424
  form_set_value(array('#parents' => array('instance', 'widget', 'settings')), $settings, $form_state);
425
426
  $widget = &$form_state['values']['instance']['widget'];
427
  // Munge the table display for text parts back into an array of text parts.
428
  if (is_array($widget['settings']['text_parts'])) {
429
    form_set_value($form['text_parts'], array_keys(array_filter($widget['settings']['text_parts'])), $form_state);
430
  }
431
432
  if ($widget['settings']['input_format'] === 'custom' && empty($widget['settings']['input_format_custom'])) {
433
    form_set_error('instance][widget][settings][input_format_custom', t('Please enter a custom date format, or choose one of the preset formats.'));
434
  }
435
}
436
437
/**
438
 * Helper function for date_field_settings_form().
439
 *
440
 * @see date_field_settings_validate()
441
 */
442
function _date_field_settings_form($field, $instance, $has_data) {
443
  $settings = $field['settings'];
444
445
  $form = array(
446
    '#element_validate' => array('date_field_settings_validate'),
447
  );
448
449
  // Make sure granularity is in the right format and has no empty values.
450
  if (!empty($settings['granularity']) && is_array($settings['granularity'])) {
451
    $granularity = array_filter($settings['granularity']);
452
  }
453
  $tz_handling = $settings['tz_handling'];
454
455
  $description = t('Select the date attributes to collect and store.');
456
  if ($has_data) {
457
    $description .= ' ' . t('Changes to date attributes only effects new or updated content.');
458
  }
459
  $options = date_granularity_names();
460
  $checkbox_year = array(
461
    '#type' => 'checkbox',
462
    '#title' => check_plain($options['year']),
463
    '#value' => 'year',
464
    '#return_value' => 'year',
465
    '#disabled' => TRUE,
466
  );
467
  unset($options['year']);
468
  $form['granularity'] = array(
469
    '#type' => 'checkboxes',
470
    '#title' => t('Date attributes to collect'),
471
    '#default_value' => $granularity,
472
    '#options' => $options,
473
    '#attributes' => array('class' => array('container-inline')),
474
    '#description' => $description,
475
    'year' => $checkbox_year,
476
  );
477
478
  $description = t('End dates are used to collect duration. E.g., allow an event to start on September 15, and end on September 16.');
479
  $form['enddate_get'] = array(
480
    '#type' => 'checkbox',
481
    '#title' => t('Collect an end date'),
482
    '#description' => $description,
483
    '#default_value' => (empty($settings['todate']) ? FALSE : TRUE),
484
    '#disabled' => $has_data,
485
  );
486
  $form['enddate_required'] = array(
487
    '#type' => 'checkbox',
488
    '#title' => t('Required'),
489
    '#default_value' => ((isset($settings['todate']) && $settings['todate'] === 'required') ? TRUE : FALSE),
490
    '#disabled' => $has_data,
491
    '#states' => array(
492
      'invisible' => array(
493
        'input[name="field[settings][enddate_get]"]' => array('checked' => FALSE),
494
      ),
495
    ),
496
  );
497
  $description = t('Select the timezone handling method for this date field.');
498
  $form['tz_handling'] = array(
499
    '#type' => 'select',
500
    '#title' => t('Time zone handling'),
501
    '#default_value' => $tz_handling,
502
    '#options' => date_timezone_handling_options(),
503
    '#description' => $description,
504
    '#attached' => array(
505
      'js' => array(drupal_get_path('module', 'date') . '/date_admin.js'),
506
    ),
507
  );
508
  // Force this value to hidden because we don't want to allow it to be changed
509
  // right now, but allow it to be a variable if needed.
510
  $form['timezone_db'] = array(
511
    '#type' => 'hidden',
512
    '#value' => date_get_timezone_db($tz_handling),
513
  );
514
515
  $form['cache_enabled'] = array(
516
    '#type' => 'checkbox',
517
    '#title' => t('Cache dates'),
518
    '#description' => t('Date objects can be created and cached as date fields are loaded rather than when they are displayed to improve performance.'),
519
    '#default_value' => !empty($settings['cache_enabled']),
520
    '#weight' => 10,
521
  );
522
  $form['cache_count'] = array(
523
    '#type' => 'textfield',
524
    '#title' => t('Maximum dates per field'),
525
    '#default_value' => (isset($settings['cache_count'])) ? $settings['cache_count'] : 4,
526
    '#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."),
527
    '#size' => 3,
528
    '#weight' => 11,
529
    '#states' => array(
530
      'visible' => array(
531
        'input[name="field[settings][cache_enabled]"]' => array('checked' => TRUE),
532
      ),
533
    ),
534
  );
535
536
  $context = array(
537
    'field' => $field,
538
    'instance' => $instance,
539
    'has_data' => $has_data,
540
  );
541
  drupal_alter('date_field_settings_form', $form, $context);
542
543
  return $form;
544
}
545
546
/**
547
 * Form validation handler for _date_field_settings_form().
548
 */
549
function date_field_settings_validate(&$form, &$form_state) {
550
  $field = &$form_state['values']['field'];
551
552
  if ($field['settings']['tz_handling'] == 'none') {
553
    form_set_value($form['timezone_db'], '', $form_state);
554
  }
555
  else {
556
    form_set_value($form['timezone_db'], date_get_timezone_db($field['settings']['tz_handling']), $form_state);
557
  }
558
559
  if ($field['settings']['tz_handling'] != 'none' && !in_array('hour', array_filter($field['settings']['granularity']))) {
560
    form_set_error('field[settings][tz_handling]', t('Dates without hours granularity must not use any timezone handling.'));
561
  }
562
563
  // Extract the correct 'todate' value out of the two end date checkboxes.
564
  if ($field['settings']['enddate_get']) {
565
    if ($field['settings']['enddate_required']) {
566
      $field['settings']['todate'] = 'required';
567
    }
568
    else {
569
      $field['settings']['todate'] = 'optional';
570
    }
571
  }
572
  else {
573
    $field['settings']['todate'] = '';
574
  }
575
576
  // Don't save the pseudo values created in the UI.
577
  unset($field['settings']['enddate_get'], $field['settings']['enddate_required']);
578
579
  if (!empty($field['settings']['cache_enabled'])) {
580
    if (!is_numeric($field['settings']['cache_count'])) {
581
      form_set_error('field[settings][cache_count]', t('The number of cache values must be a number.'));
582
    }
583
    elseif ($field['settings']['cache_count'] < 0) {
584
      form_set_error('field[settings][cache_count]', t('The number of cache values must be a number 0 or greater.'));
585
    }
586
  }
587
}
588
589
/**
590
 * Timezone handling options.
591
 *
592
 * The 'none' option will do no timezone conversions and will store and display
593
 * dates exactly as entered useful in locales or situations where timezone
594
 * conversions are not working reliably, for dates with no times, for historical
595
 * dates where timezones are irrelevant, or anytime conversion is unnecessary or
596
 * undesirable.
597
 */
598
function date_timezone_handling_options() {
599
  return array(
600
    'site' => t("Site's time zone"),
601
    'date' => t("Date's time zone"),
602
    'user' => t("User's time zone"),
603
    'utc' => 'UTC',
604
    'none' => t('No time zone conversion'),
605
  );
606
}