View source
<?php
module_load_include('theme', 'date', 'date');
module_load_include('inc', 'date', 'date.field');
module_load_include('inc', 'date', 'date_elements');
function date_get_entity_bundle($entity_type, $entity) {
switch ($entity_type) {
case 'field_collection_item':
$bundle = $entity->field_name;
break;
default:
$bundle = field_extract_bundle($entity_type, $entity);
}
if (empty($bundle)) {
$bundle = $entity_type;
}
return $bundle;
}
function date_default_format($type) {
$default_format = str_replace('i', 'i:s', variable_get('date_format_short', 'm/d/Y - H:i'));
return $default_format;
}
function date_input_date($field, $instance, $element, $input) {
if (isset($input['date'])) {
$input['date'] = trim($input['date']);
}
switch ($instance['widget']['type']) {
case 'date_text':
$function = 'date_text_input_date';
break;
case 'date_popup':
$function = 'date_popup_input_date';
break;
default:
$function = 'date_select_input_date';
}
return $function($element, $input);
}
function date_theme() {
$path = drupal_get_path('module', 'date');
module_load_include('theme', 'date', 'date');
$base = array(
'file' => 'date.theme',
'path' => "{$path}",
);
$themes = array(
'date_combo' => $base + array(
'render element' => 'element',
),
'date_form_element' => $base + array(
'render element' => 'element',
),
'date_text_parts' => $base + array(
'render element' => 'element',
),
'date' => $base + array(
'render element' => 'element',
),
'date_display_single' => $base + array(
'variables' => array(
'date' => NULL,
'timezone' => NULL,
'dates' => NULL,
'attributes' => array(),
'rdf_mapping' => NULL,
'add_rdf' => NULL,
'microdata' => NULL,
'add_microdata' => NULL,
),
),
'date_display_range' => $base + array(
'variables' => array(
'date1' => NULL,
'date2' => NULL,
'timezone' => NULL,
'dates' => NULL,
'attributes' => array(),
'attributes_start' => array(),
'attributes_end' => array(),
'rdf_mapping' => NULL,
'add_rdf' => NULL,
'microdata' => NULL,
'add_microdata' => NULL,
),
),
'date_display_remaining' => $base + array(
'variables' => array(
'remaining_days' => NULL,
),
),
'date_display_combination' => $base + array(
'variables' => array(
'entity_type' => NULL,
'entity' => NULL,
'field' => NULL,
'instance' => NULL,
'langcode' => NULL,
'item' => NULL,
'delta' => NULL,
'display' => NULL,
'dates' => NULL,
'attributes' => array(),
'rdf_mapping' => NULL,
'add_rdf' => NULL,
'microdata' => NULL,
'add_microdata' => NULL,
),
),
'date_display_interval' => $base + array(
'variables' => array(
'entity_type' => NULL,
'entity' => NULL,
'field' => NULL,
'instance' => NULL,
'langcode' => NULL,
'item' => NULL,
'delta' => NULL,
'display' => NULL,
'dates' => NULL,
'attributes' => array(),
'rdf_mapping' => NULL,
'add_rdf' => NULL,
),
),
);
return $themes;
}
function date_element_info() {
$type = array();
$type['date_combo'] = array(
'#input' => TRUE,
'#delta' => 0,
'#columns' => array(
'value',
'value2',
'timezone',
'offset',
'offset2',
),
'#process' => array(
'date_combo_element_process',
),
'#element_validate' => array(
'date_combo_validate',
),
'#theme_wrappers' => array(
'date_combo',
),
'#attached' => array(
'css' => array(
drupal_get_path('module', 'date_api') . '/date.css',
),
),
);
if (module_exists('ctools')) {
$type['date_combo']['#pre_render'] = array(
'ctools_dependent_pre_render',
);
}
return $type;
}
function date_formatter_process($formatter, $entity_type, $entity, array $field, array $instance, $langcode, array $item, array $display) {
$dates = array();
$timezone = date_default_timezone();
if (empty($timezone)) {
return $dates;
}
$granularity = date_granularity($field);
$settings = $display['settings'];
$field_name = $field['field_name'];
$format = date_formatter_format($formatter, $settings, $granularity, $langcode);
if (!isset($field['settings']['tz_handling']) || $field['settings']['tz_handling'] !== 'utc') {
$timezone = isset($item['timezone']) ? $item['timezone'] : '';
$timezone = date_get_timezone($field['settings']['tz_handling'], $timezone);
}
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
$db_format = date_type_format($field['type']);
$process = date_process_values($field);
foreach ($process as $processed) {
if (empty($item[$processed])) {
$dates[$processed] = NULL;
}
else {
$dates[$processed] = array();
if (isset($item['db'][$processed])) {
$date = $item['db'][$processed];
}
else {
$date = new DateObject($item[$processed], $timezone_db, $db_format);
$date
->limitGranularity($field['settings']['granularity']);
}
$dates[$processed]['db']['object'] = $date;
$dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
$dates[$processed]['db']['all_day'] = FALSE;
date_timezone_set($date, timezone_open($timezone));
$dates[$processed]['local']['object'] = $date;
$dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
$dates[$processed]['local']['timezone'] = $timezone;
$dates[$processed]['local']['offset'] = date_offset_get($date);
$dates[$processed]['local']['all_day'] = FALSE;
if (!empty($instance['widget']['settings']['display_all_day'])) {
$dates[$processed]['db']['all_day'] = !empty($item['all_day']);
$dates[$processed]['local']['all_day'] = !empty($item['all_day']);
}
$dates[$processed]['formatted'] = '';
$dates[$processed]['formatted_iso'] = date_format_date($date, 'custom', 'c');
if (is_object($date)) {
if ($format == 'format_interval') {
$dates[$processed]['interval'] = date_format_interval($date);
}
elseif ($format == 'format_calendar_day') {
$dates[$processed]['calendar_day'] = date_format_calendar_day($date);
}
elseif ($format == 'U' || $format == 'r' || $format == 'c') {
$dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
$dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
$dates[$processed]['formatted_time'] = '';
$dates[$processed]['formatted_timezone'] = '';
}
elseif (!empty($format)) {
$formats = _get_custom_date_format($date, $format);
$dates[$processed]['formatted'] = $formats['formatted'];
$dates[$processed]['formatted_date'] = $formats['date'];
$dates[$processed]['formatted_time'] = $formats['time'];
$dates[$processed]['formatted_timezone'] = $formats['zone'];
}
}
}
}
if (empty($dates['value2'])) {
$dates['value2'] = $dates['value'];
}
$context = array(
'field' => $field,
'instance' => $instance,
'format' => $format,
'entity_type' => $entity_type,
'entity' => $entity,
'langcode' => $langcode,
'item' => $item,
'display' => $display,
);
drupal_alter('date_formatter_dates', $dates, $context);
$dates['format'] = $format;
return $dates;
}
function _get_custom_date_format($date, $format) {
$custom = array();
$custom['granularities'] = array(
'date' => array(
'year',
'month',
'day',
),
'time' => array(
'hour',
'minute',
'second',
),
'zone' => array(
'timezone',
),
);
$custom['limits'] = array(
'date' => date_limit_format($format, $custom['granularities']['date']),
'time' => date_limit_format($format, $custom['granularities']['time']),
'zone' => date_limit_format($format, $custom['granularities']['zone']),
);
return array(
'formatted' => date_format_date($date, 'custom', $format),
'date' => date_format_date($date, 'custom', $custom['limits']['date']),
'time' => date_format_date($date, 'custom', $custom['limits']['time']),
'zone' => date_format_date($date, 'custom', $custom['limits']['zone']),
);
}
function date_granularity(array $field) {
if (!is_array($field) || !is_array($field['settings']['granularity'])) {
$granularity = drupal_map_assoc(array(
'year',
'month',
'day',
));
$field['settings']['granularity'] = $granularity;
}
return array_values(array_filter($field['settings']['granularity']));
}
function date_process_values(array $field) {
return $field['settings']['todate'] ? array(
'value',
'value2',
) : array(
'value',
);
}
function date_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
$field = $form['#field'];
$instance = $form['#instance'];
if (!in_array($field['type'], array(
'date',
'datetime',
'datestamp',
))) {
return;
}
$form['instance']['defaults'] = array(
'#type' => 'fieldset',
'#title' => t('More settings and values'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['instance']['date_format'] = array(
'#type' => 'fieldset',
'#title' => t('Date entry'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['instance']['default_values'] = array(
'#type' => 'fieldset',
'#title' => t('Default values'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['instance']['years_back_and_forward'] = array(
'#type' => 'fieldset',
'#title' => t('Starting and ending year'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['instance']['#pre_render'][] = 'date_field_ui_field_edit_form_pre_render';
}
function date_field_ui_field_edit_form_pre_render($form) {
foreach ($form as $name => $element) {
if (is_array($element) && isset($element['#fieldset'])) {
$fieldset = $element['#fieldset'];
$form[$fieldset][$name] = $element;
unset($form[$name]);
}
}
foreach (array(
'date_format',
'default_values',
'years_back_and_forward',
) as $name) {
if (element_children($form[$name])) {
$form[$name]['#sorted'] = FALSE;
$form['defaults'][$name] = $form[$name];
}
unset($form[$name]);
}
return $form;
}
function date_field_widget_error($element, $error, $form, &$form_state) {
form_error($element[$error['error']], $error['message']);
}
function date_formatter_format($formatter, $settings, $granularity = array(), $langcode = NULL) {
$format_type = !empty($settings['format_type']) ? $settings['format_type'] : 'format_interval';
switch ($formatter) {
case 'format_interval':
return 'format_interval';
case 'date_plain':
return 'date_plain';
default:
if ($format_type == 'custom') {
$format = $settings['custom_date_format'];
}
else {
$format = date_format_type_format($format_type, $langcode);
}
}
array_push($granularity, 'timezone');
return date_limit_format($format, $granularity);
}
function date_format_type_format($format_type, $langcode = NULL) {
$static =& drupal_static(__FUNCTION__);
if (!isset($static[$langcode][$format_type])) {
$format = system_date_format_locale($langcode, $format_type);
if (!is_string($format)) {
switch ($format_type) {
case 'short':
$default = 'm/d/Y - H:i';
break;
case 'long':
$default = 'l, F j, Y - H:i';
break;
case 'medium':
default:
$default = 'D, m/d/Y - H:i';
}
$format = variable_get('date_format_' . $format_type, $default);
}
$static[$langcode][$format_type] = $format;
}
return $static[$langcode][$format_type];
}
function date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
$field_name = $field['field_name'];
$options = $display['settings'];
$max_count = $options['multiple_number'];
if (is_numeric($max_count) && $max_count == 0) {
$entity->{$field_name} = array();
return $entity;
}
if (!empty($options['multiple_from']) || !empty($options['multiple_to']) || !empty($max_count)) {
$format = date_type_format($field['type']);
include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
$date_handler = new date_sql_handler($field);
$arg0 = !empty($options['multiple_from']) ? $date_handler
->arg_replace($options['multiple_from']) : variable_get('date_min_year', 100) . '-01-01T00:00:00';
$arg1 = !empty($options['multiple_to']) ? $date_handler
->arg_replace($options['multiple_to']) : variable_get('date_max_year', 4000) . '-12-31T23:59:59';
if (!empty($arg0) && !empty($arg1)) {
$arg = $arg0 . '--' . $arg1;
}
elseif (!empty($arg0)) {
$arg = $arg0;
}
elseif (!empty($arg1)) {
$arg = $arg1;
}
if (!empty($arg)) {
$range = $date_handler
->arg_range($arg);
$start = date_format($range[0], $format);
$end = date_format($range[1], $format);
$count = 0;
foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
if (!empty($entity->date_repeat_show_all)) {
break;
}
elseif (!empty($max_count) && is_numeric($max_count) && $count >= $max_count || !empty($value['value']) && $value['value'] < $start || !empty($value['value2']) && $value['value2'] > $end) {
unset($entity->{$field_name}[$langcode][$delta]);
}
else {
$count++;
}
}
}
}
return $entity;
}
function date_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
$name = $field['field_name'];
$property =& $info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
if ($field['type'] != 'datestamp' || $field['settings']['timezone_db'] != 'UTC') {
$property['getter callback'] = 'date_entity_metadata_field_getter';
$property['setter callback'] = 'date_entity_metadata_field_setter';
unset($property['query callback']);
}
if (!empty($field['settings']['todate'])) {
$property['type'] = $field['cardinality'] != 1 ? 'list<struct>' : 'struct';
$property['auto creation'] = 'date_entity_metadata_struct_create';
$property['getter callback'] = 'entity_metadata_field_verbatim_get';
$property['setter callback'] = 'entity_metadata_field_verbatim_set';
$property['property info'] = array(
'value' => array(
'type' => 'date',
'label' => t('Start date'),
'getter callback' => 'date_entity_metadata_struct_getter',
'setter callback' => 'date_entity_metadata_struct_setter',
'field_name' => $field['field_name'],
'microdata' => TRUE,
),
'value2' => array(
'type' => 'date',
'label' => t('End date'),
'getter callback' => 'date_entity_metadata_struct_getter',
'setter callback' => 'date_entity_metadata_struct_setter',
'field_name' => $field['field_name'],
'microdata' => TRUE,
),
'duration' => array(
'type' => 'duration',
'label' => t('Duration'),
'desription' => t('The duration of the time period given by the dates.'),
'getter callback' => 'date_entity_metadata_duration_getter',
'field_name' => $field['field_name'],
),
);
unset($property['query callback']);
}
else {
$property['microdata'] = TRUE;
}
}
function date_entity_metadata_field_getter($entity, array $options, $name, $entity_type, &$context) {
$return = entity_metadata_field_verbatim_get($entity, $options, $name, $entity_type, $context);
$items = $context['field']['cardinality'] == 1 ? array(
$return,
) : $return;
foreach ($items as $key => $item) {
$items[$key] = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $context);
}
return $context['field']['cardinality'] == 1 ? $items[0] : $items;
}
function date_entity_metadata_struct_getter($item, array $options, $name, $type, $info) {
if (!isset($item[$name])) {
return NULL;
}
$value = trim($item[$name]);
if (empty($value)) {
return NULL;
}
if (isset($info['field']['settings']['tz_handling'])) {
$timezone_db = date_get_timezone_db($info['field']['settings']['tz_handling']);
}
elseif (!empty($item['timezone_db'])) {
$timezone_db = $item['timezone_db'];
}
else {
$timezone_db = 'UTC';
}
$date = new DateObject($value, $timezone_db);
return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
}
function date_entity_metadata_duration_getter($item, array $options, $name, $type, $info) {
$value = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $info);
$value2 = date_entity_metadata_struct_getter($item, $options, 'value2', 'struct', $info);
if ($value && $value2) {
return $value2 - $value;
}
}
function date_entity_metadata_field_setter(&$entity, $name, $value, $langcode, $entity_type, $info) {
$field = field_info_field($name);
if (!isset($langcode)) {
$langcode = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
}
$values = $field['cardinality'] == 1 ? array(
$value,
) : (array) $value;
$items = array();
foreach ($values as $delta => $value) {
$info['field_name'] = $name;
date_entity_metadata_struct_setter($items[$delta], 'value', $value, $langcode, 'struct', $info);
}
$entity->{$name}[$langcode] = $items;
drupal_static_reset('field_language');
}
function date_entity_metadata_struct_create($name, $property_info) {
return array(
'date_type' => $property_info['field']['columns'][$name]['type'],
'timezone_db' => $property_info['field']['settings']['timezone_db'],
);
}
function date_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) {
if (!isset($value)) {
$item[$name] = NULL;
}
else {
$field = field_info_field($info['field_name']);
$format = date_type_format($field['type']);
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
$date = new DateObject($value, 'UTC');
if ($timezone_db != 'UTC') {
date_timezone_set($date, timezone_open($timezone_db));
}
$item[$name] = $date
->format($format);
}
}
function date_field_all_day($field, $instance, $date1, $date2 = NULL) {
if (empty($value)) {
return FALSE;
}
elseif (empty($instance['widget']['settings']['display_all_day'])) {
return FALSE;
}
return !empty($value['value']['local']['all_day']);
}
function date_field_get_sql_handler(array $field, $compare_tz = NULL) {
module_load_include('inc', 'date_api', 'date_api_sql');
$db_info = date_api_database_info($field);
$handler = new date_sql_handler($field['type']);
if ($field['settings']['tz_handling'] == 'date') {
$handler->db_timezone_field = $db_info['columns']['timezone']['column'];
}
else {
$handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
}
if (empty($compare_tz)) {
$compare_tz = date_get_timezone($field['settings']['tz_handling']);
}
$handler->local_timezone = $compare_tz;
$handler
->set_db_timezone();
return $handler;
}
function date_field_widget_properties_alter(array &$widget, $context) {
if (in_array($widget['type'], array(
'date_select',
'date_text',
'date_popup',
))) {
$entity_type = $context['entity_type'];
$entity = $context['entity'];
$info = entity_get_info($entity_type);
$id = $info['entity keys']['id'];
$widget['is_new'] = FALSE;
if (empty($entity->{$id})) {
$widget['is_new'] = TRUE;
}
}
}
function date_migrate_api() {
$api = array(
'api' => 2,
'field handlers' => array(
'DateMigrateFieldHandler',
),
);
return $api;
}