You are here

function calendar_systems_date_validate in Calendar Systems 8

Same name and namespace in other branches
  1. 6.3 calendar_systems.module \calendar_systems_date_validate()
  2. 6 calendar_systems.module \calendar_systems_date_validate()
  3. 7.3 calendar_systems.module \calendar_systems_date_validate()
  4. 7 calendar_systems.module \calendar_systems_date_validate()
  5. 7.2 calendar_systems.module \calendar_systems_date_validate()

Element validation callback for date fields.

Parameters

$element: Form element.

$form_state: Known form state array.

2 string references to 'calendar_systems_date_validate'
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 437
Contains Calendar Systems hook implementations and helpers.

Code

function calendar_systems_date_validate(&$element, &$form_state) {
  if (@$element['#calendar_system'] != 'default') {
    if (!empty($element['#value']) && is_array($element['#value'])) {
      $format = NULL;
      if (isset($element['value']['#date_format'])) {
        $format = $element['value']['#date_format'];
        if ($element['value']['#instance']['widget']['type'] == 'date_popup') {
          $format = preg_replace('%( \\- [a-zA-Z].*)%', '', $format);
        }
      }
      if (isset($element['#name'])) {
        $field_name = $element['#name'];
      }
      elseif (isset($element['#field_name'])) {
        $field_name = $element['#field_name'];
      }
      if (isset($element['#value']['value2']) || preg_match('%value2%', $field_name)) {
        $item_name = 'value2';
      }
      else {
        $item_name = 'value';
      }
      if ($field_name) {
        $field_name = preg_replace('%\\[.*%', '', $field_name);
      }
      $input_exists = NULL;
      $date_format = NULL;
      if (isset($element['#date_format'])) {
        $date_format = $element['#date_format'];
      }
      $value_path = $element['#parents'];
      if (isset($form_state['exposed_form_plugin']) && isset($form_state['view'])) {
        if ($element['#type'] == 'date_popup') {
          $value_path[] = 'date';
          $field_value =& $element['#value']['date'];
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $value_path, $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $value_path, $input_exists);
          $field_real_value = $field_value_in_form_input;
        }
        if ($element['#type'] == 'date_select') {
          $field_value =& $element['#value'][$item_name];
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);
          $field_real_value = $field_value_in_form_input;
          if (@empty($field_real_value['year'])) {
            $field_real_value = NULL;
          }
        }
        if ($element['#type'] == 'date_text') {
          $value_path[] = 'date';
          $field_value =& $element['#value']['date'];
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $value_path, $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $value_path, $input_exists);
          $field_real_value = $field_value_in_form_input;
        }
      }
      else {
        if ($element['#type'] == 'date_popup') {
          $field_value =& $element['#value']['date'];
          $value_path[] = 'date';
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $value_path, $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $value_path, $input_exists);
          $field_real_value = $field_value_in_form_input;
          $date_format = _calendar_systems_get_format_part('date', $date_format);
        }
        if ($element['#type'] == 'date_select') {
          $field_value =& $element['#value'];
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $value_path, $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $value_path, $input_exists);
          $field_real_value = $field_value_in_form_input;
          if (@empty($field_real_value['year'])) {
            $field_real_value = NULL;
          }
        }
        if ($element['#type'] == 'date_text') {
          $value_path[] = 'date';
          $field_value =& $element['#value']['date'];
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $value_path, $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $value_path, $input_exists);
          $field_real_value = $field_value_in_form_input;
        }
        if ($element['#type'] == 'date' || $element['#type'] == 'text') {
          $field_value =& $element['#value'];

          //$field_value_second = &$element[$item_name]['date']['value'];
          $field_value_in_form_state = drupal_array_get_nested_value($form_state['values'], $value_path, $input_exists);
          $field_value_in_form_input = drupal_array_get_nested_value($form_state['input'], $value_path, $input_exists);
          $field_real_value = $field_value_in_form_input;
          if (isset($element['#field']) && isset($element['#field']['#translatable'])) {
            $field_real_value_converted = _calendar_systems_date_convert($field_real_value, $date_format, $element['#calendar_system']);
          }
          else {
            $field_real_value_converted = $field_real_value;
          }
        }
      }
      $field_real_value_converted = _calendar_systems_date_convert($field_real_value, $date_format, $element['#calendar_system']);
      if (!empty($field_real_value)) {
        if ($field_real_value_converted) {
          drupal_array_set_nested_value($form_state['values'], $value_path, $field_real_value_converted);
          drupal_array_set_nested_value($form_state['input'], $value_path, $field_real_value_converted);
          if (isset($field_value_second)) {
            $field_value_second = $field_real_value_converted;
          }

          //form_set_value($element, $field_real_value_converted, $form_state);
          if (isset($element['#webform_component']) && $element['#type'] == 'date') {
            $element['#value'] = $field_real_value_converted;
            $element['#calendar_system'] = 'gregorian';
            if (isset($element['year'])) {
              $_value_path = $value_path + array(
                'year',
              );
              $element['year']['#value'] = $field_real_value_converted['year'];
            }
            if (isset($element['month'])) {
              $_value_path = $value_path + array(
                'month',
              );
              $element['month']['#value'] = $field_real_value_converted['month'];
            }
            if (isset($element['day'])) {
              $_value_path = $value_path + array(
                'day',
              );
              $element['day']['#value'] = $field_real_value_converted['day'];
            }
          }
        }
        else {
          form_set_error($element['#name'], t('You have to specify a valid date.'));
        }
      }
    }
    elseif (!empty($element['#value'])) {
      $value = _calendar_systems_date_convert($element['#value']);
      if ($value) {
        form_set_value($element, $value, $form_state);
      }
      else {
        form_set_error($element['#name'], t('You have to specify a valid date.'));
      }
    }
  }
}