Projet

Général

Profil

Paste
Télécharger (28,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.module @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Defines date/time field types.
6
 */
7

    
8
module_load_include('theme', 'date', 'date');
9
module_load_include('inc', 'date', 'date.field');
10
module_load_include('inc', 'date', 'date_elements');
11

    
12
/**
13
 * Helper function to figure out the bundle name for an entity.
14
 */
15
function date_get_entity_bundle($entity_type, $entity) {
16
  switch ($entity_type) {
17
    case 'field_collection_item':
18
      $bundle = $entity->field_name;
19
      break;
20

    
21
    default:
22
      $bundle = field_extract_bundle($entity_type, $entity);
23
  }
24

    
25
  // If there is no bundle name, field_info() uses the entity name as the bundle
26
  // name in its arrays.
27
  if (empty($bundle)) {
28
    $bundle = $entity_type;
29
  }
30
  return $bundle;
31
}
32

    
33
/**
34
 * Gets the default date format for the given field widget.
35
 */
36
function date_default_format($type) {
37
  // Example input formats must show all possible date parts, so add seconds.
38
  $default_format = str_replace('i', 'i:s', variable_get('date_format_short', 'm/d/Y - H:i'));
39
  return $default_format;
40
}
41

    
42
/**
43
 * Wrapper function around each of the widget types for creating a date object.
44
 */
45
function date_input_date($field, $instance, $element, $input) {
46
  // Trim extra spacing off user input of text fields.
47
  if (isset($input['date'])) {
48
    $input['date'] = trim($input['date']);
49
  }
50

    
51
  switch ($instance['widget']['type']) {
52
    case 'date_text':
53
      $function = 'date_text_input_date';
54
      break;
55

    
56
    case 'date_popup':
57
      $function = 'date_popup_input_date';
58
      break;
59

    
60
    default:
61
      $function = 'date_select_input_date';
62
  }
63
  return $function($element, $input);
64
}
65

    
66
/**
67
 * Implements hook_theme().
68
 */
69
function date_theme() {
70
  $path = drupal_get_path('module', 'date');
71
  module_load_include('theme', 'date', 'date');
72

    
73
  $base = array(
74
    'file' => 'date.theme',
75
    'path' => "$path",
76
  );
77
  $themes = array(
78
    'date_combo' => $base + array('render element' => 'element'),
79
    'date_form_element' => $base + array('render element' => 'element'),
80
    'date_text_parts' => $base + array('render element' => 'element'),
81
    'date' => $base + array('render element' => 'element'),
82
    'date_display_single' => $base + array(
83
      'variables' => array(
84
        'date' => NULL,
85
        'timezone' => NULL,
86
        'dates' => NULL,
87
        'attributes' => array(),
88
        'rdf_mapping' => NULL,
89
        'add_rdf' => NULL,
90
        'microdata' => NULL,
91
        'add_microdata' => NULL,
92
      ),
93
    ),
94
    'date_display_range' => $base + array(
95
      'variables' => array(
96
        'date1' => NULL,
97
        'date2' => NULL,
98
        'timezone' => NULL,
99
        'dates' => NULL,
100
        // HTML attributes that will be applied to both the start and end dates
101
        // (unless overridden).
102
        'attributes' => array(),
103
        // HTML attributes that will be applied to the start date only.
104
        'attributes_start' => array(),
105
        // HTML attributes that will be applied to the end date only.
106
        'attributes_end' => array(),
107
        'rdf_mapping' => NULL,
108
        'add_rdf' => NULL,
109
        'microdata' => NULL,
110
        'add_microdata' => NULL,
111
      ),
112
    ),
113
    'date_display_remaining' => $base + array(
114
      'variables' => array(
115
        'remaining_days' => NULL,
116
      ),
117
    ),
118
    'date_display_combination' => $base + array(
119
      'variables' => array(
120
        'entity_type' => NULL,
121
        'entity' => NULL,
122
        'field' => NULL,
123
        'instance' => NULL,
124
        'langcode' => NULL,
125
        'item' => NULL,
126
        'delta' => NULL,
127
        'display' => NULL,
128
        'dates' => NULL,
129
        'attributes' => array(),
130
        'rdf_mapping' => NULL,
131
        'add_rdf' => NULL,
132
        'microdata' => NULL,
133
        'add_microdata' => NULL,
134
      ),
135
    ),
136
    'date_display_interval' => $base + array(
137
      'variables' => array(
138
        'entity_type' => NULL,
139
        'entity' => NULL,
140
        'field' => NULL,
141
        'instance' => NULL,
142
        'langcode' => NULL,
143
        'item' => NULL,
144
        'delta' => NULL,
145
        'display' => NULL,
146
        'dates' => NULL,
147
        'attributes' => array(),
148
        'rdf_mapping' => NULL,
149
        'add_rdf' => NULL,
150
      ),
151
    ),
152
  );
153

    
154
  return $themes;
155
}
156

    
157
/**
158
 * Implements hook_element_info().
159
 *
160
 * Date_combo will create a 'start' and optional 'end' date, along with
161
 * an optional 'timezone' column for date-specific timezones. Each
162
 * 'start' and 'end' date will be constructed from date_select or date_text.
163
 */
164
function date_element_info() {
165
  $type = array();
166
  $type['date_combo'] = array(
167
    '#input' => TRUE,
168
    '#delta' => 0,
169
    '#columns' => array('value', 'value2', 'timezone', 'offset', 'offset2'),
170
    '#process' => array('date_combo_element_process'),
171
    '#element_validate' => array('date_combo_validate'),
172
    '#theme_wrappers' => array('date_combo'),
173
    '#attached' => array('css' => array(
174
      drupal_get_path('module', 'date_api') . '/date.css',
175
    )),
176
  );
177
  if (module_exists('ctools')) {
178
    $type['date_combo']['#pre_render'] = array('ctools_dependent_pre_render');
179
  }
180
  return $type;
181
}
182

    
183
/**
184
 * Helper function for creating formatted date arrays from a formatter.
185
 *
186
 * Use the Date API to get an object representation of a date field.
187
 *
188
 * @param string $formatter
189
 *   The date formatter.
190
 * @param string $entity_type
191
 *   The entity_type for the instance.
192
 * @param object $entity
193
 *   The entity object.
194
 * @param array $field
195
 *   The field info array.
196
 * @param array $instance
197
 *   The field instance array.
198
 * @param string $langcode
199
 *   The language code used by this field.
200
 * @param array $item
201
 *   An entity field item, like $entity->myfield[0].
202
 * @param array $display
203
 *   The instance display settings.
204
 *
205
 * @return array
206
 *   An array that holds the Start and End date objects.
207
 *   Each date object looks like:
208
 *     date [value] => array (
209
 *       [db] => array (  // the value stored in the database
210
 *         [object] => the datetime object
211
 *         [datetime] => 2007-02-15 20:00:00
212
 *       )
213
 *       [local] => array (  // the local representation of that value
214
 *         [object] => the datetime object
215
 *         [datetime] => 2007-02-15 14:00:00
216
 *         [timezone] => US/Central
217
 *         [offset] => -21600
218
 *       )
219
 *     )
220
 */
221
function date_formatter_process($formatter, $entity_type, $entity, array $field, array $instance, $langcode, array $item, array $display) {
222
  $dates = array();
223
  $timezone = date_default_timezone();
224
  if (empty($timezone)) {
225
    return $dates;
226
  }
227

    
228
  $granularity = date_granularity($field);
229
  $settings = $display['settings'];
230
  $field_name = $field['field_name'];
231
  $format = date_formatter_format($formatter, $settings, $granularity, $langcode);
232
  if (!isset($field['settings']['tz_handling']) || $field['settings']['tz_handling'] !== 'utc') {
233
    $timezone = isset($item['timezone']) ? $item['timezone'] : '';
234
    $timezone = date_get_timezone($field['settings']['tz_handling'], $timezone);
235
  }
236
  $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
237
  $db_format = date_type_format($field['type']);
238
  $process = date_process_values($field);
239
  foreach ($process as $processed) {
240
    if (empty($item[$processed])) {
241
      $dates[$processed] = NULL;
242
    }
243
    else {
244
      // Create a date object with a GMT timezone from the database value.
245
      $dates[$processed] = array();
246

    
247
      // Check to see if this date was already created by date_field_load().
248
      if (isset($item['db'][$processed])) {
249
        $date = $item['db'][$processed];
250
      }
251
      else {
252
        $date = new DateObject($item[$processed], $timezone_db, $db_format);
253
        $date->limitGranularity($field['settings']['granularity']);
254
      }
255

    
256
      $dates[$processed]['db']['object'] = $date;
257
      $dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
258

    
259
      date_timezone_set($date, timezone_open($timezone));
260
      $dates[$processed]['local']['object'] = $date;
261
      $dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
262
      $dates[$processed]['local']['timezone'] = $timezone;
263
      $dates[$processed]['local']['offset'] = date_offset_get($date);
264

    
265
      // Format the date, special casing the 'interval' format which doesn't
266
      // need to be processed.
267
      $dates[$processed]['formatted'] = '';
268
      $dates[$processed]['formatted_iso'] = date_format_date($date, 'custom', 'c');
269
      if (is_object($date)) {
270
        if ($format == 'format_interval') {
271
          $dates[$processed]['interval'] = date_format_interval($date);
272
        }
273
        elseif ($format == 'format_calendar_day') {
274
          $dates[$processed]['calendar_day'] = date_format_calendar_day($date);
275
        }
276
        elseif ($format == 'U' || $format == 'r' || $format == 'c') {
277
          $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
278
          $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
279
          $dates[$processed]['formatted_time'] = '';
280
          $dates[$processed]['formatted_timezone'] = '';
281
        }
282
        elseif (!empty($format)) {
283
          $formats = _get_custom_date_format($date, $format);
284
          $dates[$processed]['formatted'] = $formats['formatted'];
285
          $dates[$processed]['formatted_date'] = $formats['date'];
286
          $dates[$processed]['formatted_time'] = $formats['time'];
287
          $dates[$processed]['formatted_timezone'] = $formats['zone'];
288
        }
289
      }
290
    }
291
  }
292

    
293
  if (empty($dates['value2'])) {
294
    $dates['value2'] = $dates['value'];
295
  }
296

    
297
  // Allow other modules to alter the date values.
298
  $context = array(
299
    'field' => $field,
300
    'instance' => $instance,
301
    'format' => $format,
302
    'entity_type' => $entity_type,
303
    'entity' => $entity,
304
    'langcode' => $langcode,
305
    'item' => $item,
306
    'display' => $display,
307
  );
308
  drupal_alter('date_formatter_dates', $dates, $context);
309

    
310
  $dates['format'] = $format;
311
  return $dates;
312
}
313

    
314
/**
315
 * Get a custom date format.
316
 */
317
function _get_custom_date_format($date, $format) {
318
  $custom = array();
319
  $custom['granularities'] = array(
320
    'date' => array('year', 'month', 'day'),
321
    'time' => array('hour', 'minute', 'second'),
322
    'zone' => array('timezone'),
323
  );
324
  $custom['limits'] = array(
325
    'date' => date_limit_format($format, $custom['granularities']['date']),
326
    'time' => date_limit_format($format, $custom['granularities']['time']),
327
    'zone' => date_limit_format($format, $custom['granularities']['zone']),
328
  );
329

    
330
  return array(
331
    'formatted' => date_format_date($date, 'custom', $format),
332
    'date'      => date_format_date($date, 'custom', $custom['limits']['date']),
333
    'time'      => date_format_date($date, 'custom', $custom['limits']['time']),
334
    'zone'      => date_format_date($date, 'custom', $custom['limits']['zone']),
335
  );
336
}
337

    
338
/**
339
 * Retrieves the granularity for a field.
340
 *
341
 * $field['settings']['granularity'] will contain an array like
342
 * ('hour' => 'hour', 'month' => 0) where the values turned on return their own
343
 * names and the values turned off return a zero need to reconfigure this into
344
 * simple array of the turned on values.
345
 *
346
 * @param array $field
347
 *   The field array.
348
 */
349
function date_granularity(array $field) {
350
  if (!is_array($field) || !is_array($field['settings']['granularity'])) {
351
    $granularity = drupal_map_assoc(array('year', 'month', 'day'));
352
    $field['settings']['granularity'] = $granularity;
353
  }
354
  return array_values(array_filter($field['settings']['granularity']));
355
}
356

    
357
/**
358
 * Create an array of the date values in a field that need to be processed.
359
 *
360
 * @param array $field
361
 *   The field being processed.
362
 *
363
 * @return array
364
 *   The date values which need to be processed.
365
 */
366
function date_process_values(array $field) {
367
  return $field['settings']['todate'] ? array('value', 'value2') : array('value');
368
}
369

    
370
/**
371
 * Implements hook_form_FORM_ID_alter() for field_ui_field_edit_form().
372
 */
373
function date_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
374
  $field = $form['#field'];
375
  $instance = $form['#instance'];
376

    
377
  if (!in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
378
    return;
379
  }
380

    
381
  // Reorganize the instance settings and widget settings sections into a more
382
  // intuitive combined fieldset.
383
  $form['instance']['defaults'] = array(
384
    '#type' => 'fieldset',
385
    '#title' => t('More settings and values'),
386
    '#collapsible' => TRUE,
387
    '#collapsed' => TRUE,
388
  );
389
  $form['instance']['date_format'] = array(
390
    '#type' => 'fieldset',
391
    '#title' => t('Date entry'),
392
    '#collapsible' => TRUE,
393
    '#collapsed' => FALSE,
394
  );
395
  $form['instance']['default_values'] = array(
396
    '#type' => 'fieldset',
397
    '#title' => t('Default values'),
398
    '#collapsible' => TRUE,
399
    '#collapsed' => FALSE,
400
  );
401
  $form['instance']['years_back_and_forward'] = array(
402
    '#type' => 'fieldset',
403
    '#title' => t('Starting and ending year'),
404
    '#collapsible' => TRUE,
405
    '#collapsed' => FALSE,
406
  );
407

    
408
  $form['instance']['#pre_render'][] = 'date_field_ui_field_edit_form_pre_render';
409
}
410

    
411
/**
412
 * Rearrange form elements into fieldsets for presentation only.
413
 */
414
function date_field_ui_field_edit_form_pre_render($form) {
415
  foreach ($form as $name => $element) {
416
    if (is_array($element) && isset($element['#fieldset'])) {
417
      $fieldset = $element['#fieldset'];
418
      $form[$fieldset][$name] = $element;
419
      unset($form[$name]);
420
    }
421
  }
422
  foreach (array('date_format', 'default_values', 'years_back_and_forward') as $name) {
423
    if (element_children($form[$name])) {
424
      // Force the items in the fieldset to be resorted now that the instance
425
      // and widget settings are combined.
426
      $form[$name]['#sorted'] = FALSE;
427
      $form['defaults'][$name] = $form[$name];
428
    }
429
    unset($form[$name]);
430
  }
431
  return $form;
432
}
433

    
434
/**
435
 * Implements hook_field_widget_error().
436
 */
437
function date_field_widget_error($element, $error, $form, &$form_state) {
438
  form_error($element[$error['error']], $error['message']);
439
}
440

    
441
/**
442
 * Retrieve a date format string from formatter settings.
443
 */
444
function date_formatter_format($formatter, $settings, $granularity = array(), $langcode = NULL) {
445
  $format_type = !empty($settings['format_type']) ? $settings['format_type'] : 'format_interval';
446

    
447
  switch ($formatter) {
448
    case 'format_interval':
449
      return 'format_interval';
450

    
451
    case 'date_plain':
452
      return 'date_plain';
453

    
454
    default:
455
      if ($format_type == 'custom') {
456
        $format = $settings['custom_date_format'];
457
      }
458
      else {
459
        $format = date_format_type_format($format_type, $langcode);
460
      }
461
  }
462

    
463
  // A selected format might include timezone information.
464
  array_push($granularity, 'timezone');
465
  return date_limit_format($format, $granularity);
466
}
467

    
468
/**
469
 * Helper function to get the right format for a format type.
470
 *
471
 * Checks for locale-based format first.
472
 */
473
function date_format_type_format($format_type, $langcode = NULL) {
474
  $static = &drupal_static(__FUNCTION__);
475
  if (!isset($static[$langcode][$format_type])) {
476
    $format = system_date_format_locale($langcode, $format_type);
477

    
478
    // If locale enabled and $format_type inexistent in {date_format_locale}
479
    // we receive (due to inconsistency of core api) an array of all (other)
480
    // formats available for $langcode in locale table.
481
    // However there's no guarantee that the key $format_type exists.
482
    // @see http://drupal.org/node/1302358
483
    if (!is_string($format)) {
484
      // If the configuration page at admin/config/regional/date-time was
485
      // never saved, the default core date format variables
486
      // ('date_format_short', 'date_format_medium', and 'date_format_long')
487
      // will not be stored in the database, so we have to define their
488
      // expected defaults here.
489
      switch ($format_type) {
490
        case 'short':
491
          $default = 'm/d/Y - H:i';
492
          break;
493

    
494
        case 'long':
495
          $default = 'l, F j, Y - H:i';
496
          break;
497

    
498
        // If it's not one of the core date types and isn't stored in the
499
        // database, we'll fall back on using the same default format as the
500
        // 'medium' type.
501
        case 'medium':
502
        default:
503
          // @todo: If a non-core module provides a date type and does not
504
          // variable_set() a default for it, the default assumed here may not
505
          // be correct (since the default format used by 'medium' may not even
506
          // be one of the allowed formats for the date type in question). To
507
          // fix this properly, we should really call
508
          // system_get_date_formats($format_type) and take the first format
509
          // from that list as the default. However, this function is called
510
          // often (on many different page requests), so calling
511
          // system_get_date_formats() from here would be a performance hit
512
          // since that function writes several records to the database during
513
          // each page request that calls it.
514
          $default = 'D, m/d/Y - H:i';
515
      }
516
      $format = variable_get('date_format_' . $format_type, $default);
517
    }
518
    $static[$langcode][$format_type] = $format;
519
  }
520
  return $static[$langcode][$format_type];
521
}
522

    
523
/**
524
 * Helper function to adapt entity date fields to formatter settings.
525
 */
526
function date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
527
  // If there are options to limit multiple values,
528
  // alter the entity values to match.
529
  $field_name = $field['field_name'];
530
  $options = $display['settings'];
531
  $max_count = $options['multiple_number'];
532

    
533
  // If no results should be shown, empty the values and return.
534
  if (is_numeric($max_count) && $max_count == 0) {
535
    $entity->{$field_name} = array();
536
    return $entity;
537
  }
538

    
539
  // Otherwise removed values that should not be displayed.
540
  if (!empty($options['multiple_from']) || !empty($options['multiple_to']) || !empty($max_count)) {
541
    $format = date_type_format($field['type']);
542
    include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
543
    $date_handler = new date_sql_handler($field);
544
    $arg0 = !empty($options['multiple_from']) ? $date_handler->arg_replace($options['multiple_from']) : variable_get('date_min_year', 100) . '-01-01T00:00:00';
545
    $arg1 = !empty($options['multiple_to']) ? $date_handler->arg_replace($options['multiple_to']) : variable_get('date_max_year', 4000) . '-12-31T23:59:59';
546
    if (!empty($arg0) && !empty($arg1)) {
547
      $arg = $arg0 . '--' . $arg1;
548
    }
549
    elseif (!empty($arg0)) {
550
      $arg = $arg0;
551
    }
552
    elseif (!empty($arg1)) {
553
      $arg = $arg1;
554
    }
555
    if (!empty($arg)) {
556
      $range = $date_handler->arg_range($arg);
557
      $start = date_format($range[0], $format);
558
      $end = date_format($range[1], $format);
559
      // Empty out values we don't want to see.
560
      $count = 0;
561
      foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
562
        if (!empty($entity->date_repeat_show_all)) {
563
          break;
564
        }
565
        elseif ((!empty($max_count) && is_numeric($max_count) && $count >= $max_count) ||
566
          (!empty($value['value'])  && $value['value'] < $start) ||
567
          (!empty($value['value2']) && $value['value2'] > $end)) {
568
          unset($entity->{$field_name}[$langcode][$delta]);
569
        }
570
        else {
571
          $count++;
572
        }
573
      }
574
    }
575
  }
576

    
577
  return $entity;
578
}
579

    
580
/**
581
 * Callback to alter the property info of date fields.
582
 *
583
 * @see date_field_info()
584
 */
585
function date_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
586
  $name = $field['field_name'];
587
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
588

    
589
  if ($field['type'] != 'datestamp' || $field['settings']['timezone_db'] != 'UTC') {
590
    // Add a getter callback to convert the date into the right format.
591
    $property['getter callback'] = 'date_entity_metadata_field_getter';
592
    $property['setter callback'] = 'date_entity_metadata_field_setter';
593
    unset($property['query callback']);
594
  }
595
  if (!empty($field['settings']['todate'])) {
596
    // Define a simple data structure containing both dates.
597
    $property['type'] = ($field['cardinality'] != 1) ? 'list<struct>' : 'struct';
598
    $property['auto creation'] = 'date_entity_metadata_struct_create';
599
    $property['getter callback'] = 'entity_metadata_field_verbatim_get';
600
    $property['setter callback'] = 'entity_metadata_field_verbatim_set';
601
    $property['property info'] = array(
602
      'value' => array(
603
        'type' => 'date',
604
        'label' => t('Start date'),
605
        'getter callback' => 'date_entity_metadata_struct_getter',
606
        'setter callback' => 'date_entity_metadata_struct_setter',
607
        // The getter and setter callbacks for 'value' and 'value2'
608
        // will not provide the field name as $name, we'll add it to $info.
609
        'field_name' => $field['field_name'],
610
        // Alert Microdata module that this value can be exposed in microdata.
611
        'microdata' => TRUE,
612
      ),
613
      'value2' => array(
614
        'type' => 'date',
615
        'label' => t('End date'),
616
        'getter callback' => 'date_entity_metadata_struct_getter',
617
        'setter callback' => 'date_entity_metadata_struct_setter',
618
        // The getter and setter callbacks for 'value' and 'value2'
619
        // will not provide the field name as $name, we'll add it to $info.
620
        'field_name' => $field['field_name'],
621
        // Alert Microdata module that this value can be exposed in microdata.
622
        'microdata' => TRUE,
623
      ),
624
      'duration' => array(
625
        'type' => 'duration',
626
        'label' => t('Duration'),
627
        'desription' => t('The duration of the time period given by the dates.'),
628
        'getter callback' => 'date_entity_metadata_duration_getter',
629
        // No setter callback for duration.
630
        // The getter callback for duration will not provide the field name
631
        // as $name, we'll add it to $info.
632
        'field_name' => $field['field_name'],
633
      ),
634
    );
635
    unset($property['query callback']);
636
  }
637
  else {
638
    // If this doesn't have a todate, it is handled as a date rather than a
639
    // struct. Enable microdata on the field itself rather than the properties.
640
    $property['microdata'] = TRUE;
641
  }
642
}
643

    
644
/**
645
 * Getter callback to return date values as datestamp in UTC from the field.
646
 */
647
function date_entity_metadata_field_getter($entity, array $options, $name, $entity_type, &$context) {
648
  $return = entity_metadata_field_verbatim_get($entity, $options, $name, $entity_type, $context);
649
  $items = ($context['field']['cardinality'] == 1) ? array($return) : $return;
650
  foreach ($items as $key => $item) {
651
    $items[$key] = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $context);
652
  }
653
  return ($context['field']['cardinality'] == 1) ? $items[0] : $items;
654
}
655

    
656
/**
657
 * Getter callback to return date values as datestamp in UTC.
658
 */
659
function date_entity_metadata_struct_getter($item, array $options, $name, $type, $info) {
660
  $value = trim($item[$name]);
661
  if (empty($value)) {
662
    return NULL;
663
  }
664

    
665
  $timezone_db = date_get_timezone_db($info['field']['settings']['tz_handling']);
666
  $date = new DateObject($value, $timezone_db);
667
  return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
668
}
669

    
670
/**
671
 * Getter callback to return the duration of the time period given by the dates.
672
 */
673
function date_entity_metadata_duration_getter($item, array $options, $name, $type, $info) {
674
  $value = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $info);
675
  $value2 = date_entity_metadata_struct_getter($item, $options, 'value2', 'struct', $info);
676
  if ($value && $value2) {
677
    return $value2 - $value;
678
  }
679
}
680

    
681
/**
682
 * Callback for setting field property values.
683
 *
684
 * Based on entity_metadata_field_property_set(), the original property setter,
685
 * adapted to transform non-timestamp date values to timestamps.
686
 */
687
function date_entity_metadata_field_setter(&$entity, $name, $value, $langcode, $entity_type, $info) {
688
  $field = field_info_field($name);
689
  if (!isset($langcode)) {
690
    // Try to figure out the default language used by the entity.
691
    // @todo: Update once http://drupal.org/node/1260640 has been fixed.
692
    $langcode = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
693
  }
694
  $values = $field['cardinality'] == 1 ? array($value) : (array) $value;
695

    
696
  $items = array();
697
  foreach ($values as $delta => $value) {
698
    // Make use of the struct setter to convert the date back to a timestamp.
699
    $info['field_name'] = $name;
700
    date_entity_metadata_struct_setter($items[$delta], 'value', $value, $langcode, 'struct', $info);
701
  }
702
  $entity->{$name}[$langcode] = $items;
703
  // Empty the static field language cache, so the field system picks up any
704
  // possible new languages.
705
  drupal_static_reset('field_language');
706
}
707

    
708
/**
709
 * Auto creation callback for fields which contain two date values in one.
710
 */
711
function date_entity_metadata_struct_create($name, $property_info) {
712
  return array(
713
    'date_type' => $property_info['field']['columns'][$name]['type'],
714
    'timezone_db' => $property_info['field']['settings']['timezone_db'],
715
  );
716
}
717

    
718
/**
719
 * Callback for setting an individual field value if a to-date may be there too.
720
 *
721
 * Based on entity_property_verbatim_set().
722
 *
723
 * The passed in unix timestamp (UTC) is converted to the right value and format
724
 * dependent on the field.
725
 *
726
 * $name is either 'value' or 'value2'.
727
 */
728
function date_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) {
729
  if (!isset($value)) {
730
    $item[$name] = NULL;
731
  }
732
  else {
733
    $field = field_info_field($info['field_name']);
734
    $format = date_type_format($field['type']);
735
    $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
736

    
737
    $date = new DateObject($value, 'UTC');
738
    if ($timezone_db != 'UTC') {
739
      date_timezone_set($date, timezone_open($timezone_db));
740
    }
741
    $item[$name] = $date->format($format);
742
  }
743
}
744

    
745
/**
746
 * Duplicate of what is now date_all_day_field() in the Date All Day module.
747
 *
748
 * Left here to avoid breaking other modules that use this function.
749
 *
750
 * DEPRECATED! Will be removed at some time in the future.
751
 */
752
function date_field_all_day($field, $instance, $date1, $date2 = NULL) {
753
  if (empty($date1) || !is_object($date1)) {
754
    return FALSE;
755
  }
756
  elseif (!date_has_time($field['settings']['granularity'])) {
757
    return FALSE;
758
  }
759
  // The master setting for the 'all day' functionality.
760
  elseif (empty($instance['settings']['widget']['settings']['display_all_day'])) {
761
    return FALSE;
762
  }
763

    
764
  if (empty($date2)) {
765
    $date2 = $date1;
766
  }
767

    
768
  // Use central logic for determining the granularity.
769
  $granularity = date_granularity_precision($field['settings']['granularity']);
770

    
771
  // The increment is 1 by default, but it can be overridden.
772
  $increment = 1;
773
  if (isset($instance['widget']['settings']['increment'])) {
774
    $increment = $instance['widget']['settings']['increment'];
775
  }
776

    
777
  return date_is_all_day(date_format($date1, DATE_FORMAT_DATETIME), date_format($date2, DATE_FORMAT_DATETIME), $granularity, $increment);
778
}
779

    
780
/**
781
 * Generates a Date API SQL handler for the given date field.
782
 *
783
 * The handler will be set up to make the correct timezone adjustments
784
 * for the field settings.
785
 *
786
 * @param array $field
787
 *   The $field array.
788
 * @param string $compare_tz
789
 *   The timezone used for comparison values in the SQL.
790
 *
791
 * DEPRECATED!, will be removed at some time in the future.
792
 */
793
function date_field_get_sql_handler(array $field, $compare_tz = NULL) {
794
  module_load_include('inc', 'date_api', 'date_api_sql');
795

    
796
  $db_info = date_api_database_info($field);
797

    
798
  // Create a DateAPI SQL handler class for this field type.
799
  $handler = new date_sql_handler($field['type']);
800

    
801
  // If this date field stores a timezone in the DB, tell the handler about it.
802
  if ($field['settings']['tz_handling'] == 'date') {
803
    $handler->db_timezone_field = $db_info['columns']['timezone']['column'];
804
  }
805
  else {
806
    $handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
807
  }
808

    
809
  if (empty($compare_tz)) {
810
    $compare_tz = date_get_timezone($field['settings']['tz_handling']);
811
  }
812
  $handler->local_timezone = $compare_tz;
813

    
814
  // Now that the handler is properly initialized, force the DB
815
  // to use UTC so no timezone conversions get added to things like
816
  // NOW() or FROM_UNIXTIME().
817
  $handler->set_db_timezone();
818

    
819
  return $handler;
820
}
821

    
822
/**
823
 * Implements hook_field_widget_properties_alter().
824
 */
825
function date_field_widget_properties_alter(array &$widget, $context) {
826
  // Alters the widget properties of a field instance before it gets displayed.
827
  // Used here to flag new entities so we can later tell if they need default
828
  // values.
829
  if (in_array($widget['type'], array('date_select', 'date_text', 'date_popup'))) {
830
    $entity_type = $context['entity_type'];
831
    $entity = $context['entity'];
832
    $info = entity_get_info($entity_type);
833
    $id = $info['entity keys']['id'];
834
    $widget['is_new'] = FALSE;
835
    if (empty($entity->$id)) {
836
      $widget['is_new'] = TRUE;
837
    }
838
  }
839
}
840

    
841
/**
842
 * Implements hook_migrate_api().
843
 */
844
function date_migrate_api() {
845
  $api = array(
846
    'api' => 2,
847
    'field handlers' => array('DateMigrateFieldHandler'),
848
  );
849
  return $api;
850
}