You are here

function _calendar_systems_element_date_process in Calendar Systems 7

Same name and namespace in other branches
  1. 8 calendar_systems.module \_calendar_systems_element_date_process()
  2. 7.2 calendar_systems.module \_calendar_systems_element_date_process()

Support drupal's core date field

Parameters

$element:

Return value

array

2 string references to '_calendar_systems_element_date_process'
calendar_systems_element_info_alter in ./calendar_systems.module
calendar_systems_form_alter in ./calendar_systems.module
Implements hook_form_alter().

File

./calendar_systems.module, line 1126
Contains Calendar Systems hook implementations and helpers.

Code

function _calendar_systems_element_date_process(&$element, &$form_state) {
  $element = form_process_date($element);
  $year_range = '-100:+100';
  if (isset($element['#webform_component']) && isset($element['#webform_component']['extra']) && isset($element['#webform_component']['extra']['start_date'])) {
    $start = str_replace(' years', '', $element['#webform_component']['extra']['start_date']);
    $end = str_replace(' years', '', $element['#webform_component']['extra']['end_date']);
    $year_range = "{$start}:{$end}";
  }
  if (!isset($element['#calendar_system'])) {
    $element['#calendar_system'] = calendar_systems_get_calendar_system_name();
  }
  if ($element['#calendar_system'] != 'default') {
    $calendar = calendar_systems_get_calendar_instance($element['#calendar_system']);
    $date_array = array();
    $value_path = $element['#parents'];
    $submitted_value = drupal_array_get_nested_value($form_state['input'], $value_path);
    $element_value = $element['#value'];
    if (@empty($element['#value']['year'])) {
      $element_value = $element['#default_value'];
    }
    else {
      $date_array = $element['#value'];
    }
    if (!empty($element_value) && empty($submitted_value) && !isset($element['#webform_component'])) {
      if (is_array($element_value)) {
        $element_value = "{$element_value['year']}-{$element_value['month']}-{$element_value['day']}";
      }
      $date = new DateTime($element_value);
      if ($date) {
        $timestamp = $date
          ->format("U");
        $element['#default_value'] = $calendar
          ->smartGet("Y-m-d\\TH:i:s", $timestamp);
        $date_array = $calendar
          ->timestampToInfoArray($timestamp);
        $element['#value'] = $date_array;
      }
    }
    else {
      $date_array = $element['#value'];
    }
    if (isset($element['year'])) {
      if ($year_range) {
        if ($date_array) {
          $element['year']['#value'] = $date_array['year'];
        }
        list($from, $to) = explode(':', $year_range);
        $from = intval($from);
        $to = intval($to);
        $current_year = $calendar
          ->date('Y');
        $from_year = $current_year + $from;
        if ($element['year']['#value'] < $from_year) {
          $from_year = $element['year']['#value'];
        }
        $to_year = $current_year + $to;
        if ($element['year']['#value'] > $to_year) {
          $to_year = $element['year']['#value'];
        }
        $years = array();
        foreach (range($from_year, $to_year) as $year) {
          $years[$year] = $year;
        }
        $element['year']['#options'] = $years;
      }
    }
    if (isset($element['month'])) {
      if ($date_array) {
        $element['month']['#value'] = $date_array['month'];
      }
      $months = array();
      $_months = $calendar
        ->getOption('monthsName');
      foreach ($_months as $n => $month) {
        $months[$n] = html_entity_decode($month, ENT_QUOTES, 'UTF-8');
      }
      $element['month']['#options'] = $months;
    }
    if (isset($element['day'])) {
      if ($date_array) {
        $element['day']['#value'] = $date_array['day'];
      }
    }
  }
  return $element;
}