You are here

function calendar_systems_date_select_process_alter in Calendar Systems 7

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

Supporing date module's date_select element

hook_date_select_process_alter

Parameters

$element:

$form_state:

$context:

File

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

Code

function calendar_systems_date_select_process_alter(&$element, &$form_state, $context) {
  if (!isset($element['#calendar_system'])) {
    if (!empty($element['#field']['settings']['calendar_system'])) {
      $element['#calendar_system'] = $element['#field']['settings']['calendar_system'];
    }
    else {
      $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'];

    //$value_path[] = 'year';
    $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)) {
      if (is_string($element_value)) {
        $date = new DateObject($element_value);
      }
      else {
        $date = date_select_input_date($element, $element_value);
      }
      if ($date) {
        $timestamp = $date
          ->format("U");
        $element['#default_value'] = $calendar
          ->smartGet(DATE_FORMAT_ISO, $timestamp);
        $date_array = $calendar
          ->timestampToInfoArray($timestamp);
        $element['#value'] = $date_array;
      }
    }
    if (isset($element['year'])) {
      if ($element['#date_year_range']) {
        if ($date_array) {
          $element['year']['#default_value'] = $date_array['year'];
        }
        list($from, $to) = explode(':', $element['#date_year_range']);
        $from = intval($from);
        $to = intval($to);
        $current_year = $calendar
          ->date('Y');
        $from_year = $current_year + $from;
        if ($element['year']['#default_value'] < $from_year && !empty($element['year']['#default_value'])) {
          $from_year = $element['year']['#default_value'];
        }
        $to_year = $current_year + $to;
        if ($element['year']['#default_value'] > $to_year && !empty($element['year']['#default_value'])) {
          $to_year = $element['year']['#default_value'];
        }
        $years = array();
        if (isset($element['year']['#options'][''])) {
          $years[''] = $element['year']['#options'][''];
        }
        foreach (range($from_year, $to_year) as $year) {
          $years[$year] = $year;
        }
        $element['year']['#options'] = $years;
      }
    }
    if (isset($element['month'])) {
      if ($date_array) {
        $element['month']['#default_value'] = $date_array['month'];
      }
      $months = array();
      if (isset($element['month']['#options'][''])) {
        $months[''] = $element['month']['#options'][''];
      }
      $_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']['#default_value'] = $date_array['day'];
      }
    }

    //$element['day']['#options'] = ;
  }
}