You are here

function _partial_date_field_validate in Partial Date 7

Implements hook_field_validate().

Possible error codes:

  • 'xxxx': The partial_date year is not valid

See also

partial_date_field_widget_error().

1 call to _partial_date_field_validate()
partial_date_field_validate in ./partial_date.module
Implements hook_field_validate().

File

./partial_date.admin.inc, line 364
Less freq. functions for field administration.

Code

function _partial_date_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $has_range = strpos($field['type'], 'range');
  $date_components = partial_date_components();
  $has_data = FALSE;
  $minimum_components = FALSE;
  if (!empty($field['settings']['minimum_components'])) {
    $minimum_components = array_filter($field['settings']['minimum_components']);
    $widget_settings = $instance['widget']['settings'];
    $widget_components = array();
    foreach (array_filter($widget_settings['granularity']['from']) as $key) {
      $widget_components['from_granularity_' . $key] = $has_range ? t('From @component', array(
        '@component' => $date_components[$key],
      )) : $date_components[$key];
    }
    foreach (array_filter($widget_settings['estimates']['from']) as $key) {
      $widget_components['from_estimates_' . $key] = $has_range ? t('From @component Estimate', array(
        '@component' => $date_components[$key],
      )) : $date_components[$key];
    }
    if ($has_range) {
      foreach (array_filter($widget_settings['granularity']['to']) as $key) {
        $widget_components['to_granularity_' . $key] = t('To @component', array(
          '@component' => $date_components[$key],
        ));
      }
      foreach (array_filter($widget_settings['estimates']['to']) as $key) {
        $widget_components['to_estimates_' . $key] = t('To @component Estimate', array(
          '@component' => $date_components[$key],
        ));
      }
    }
    if (!empty($widget_settings['theme_overrides']['txt_short'])) {
      $widget_components['txt_short'] = t('Short date description');
    }
    if (!empty($widget_settings['theme_overrides']['txt_long'])) {
      $widget_components['txt_long'] = t('Long date description');
    }
    $minimum_components = array_intersect_key($widget_components, $minimum_components);
  }
  foreach ($items as $delta => $item) {
    if (!partial_date_field_is_empty($item, $field)) {
      $has_data = TRUE;
      $incomplete = array();
      if ($minimum_components) {
        foreach ($minimum_components as $key => $label) {
          if (strpos($key, 'from_granularity_') === 0) {
            $component = str_replace('from_granularity_', '', $key);
            if (empty($item['from'][$component])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'partial_date_incomplete_from',
                'partial_date_component' => $component,
                'message' => t('@component is required', array(
                  '@component' => $label,
                )),
              );
            }
          }
          elseif (strpos($key, 'from_estimates_') === 0) {
            $component = str_replace('from_estimates_', '', $key) . '_estimate';
            if (empty($item['from'][$component])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'partial_date_incomplete_from',
                'partial_date_component' => $component,
                'message' => t('@component is required', array(
                  '@component' => $label,
                )),
              );
            }
          }
          elseif (strpos($key, 'to_granularity_') === 0) {
            $component = str_replace('to_granularity_', '', $key);
            if (empty($item['to'][$component])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'partial_date_incomplete_to',
                'partial_date_component' => $component,
                'message' => t('@component is required', array(
                  '@component' => $label,
                )),
              );
            }
          }
          elseif (strpos($key, 'to_estimates_') === 0) {
            $component = str_replace('to_estimates_', '', $key) . '_estimate';
            if (empty($item['to'][$component])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'partial_date_incomplete_to',
                'partial_date_component' => $component,
                'message' => t('@component is required', array(
                  '@component' => $label,
                )),
              );
            }
          }
          else {
            if (empty($item[$key])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'partial_date_incomplete_' . $key,
                'partial_date_component' => $component,
                'message' => t('@component is required', array(
                  '@component' => $label,
                )),
              );
            }
          }
        }
      }
    }
  }
  if ($entity_type && $entity && !$has_data && $instance['required']) {
    if (!empty($minimum_components)) {
      $errors[$field['field_name']][$langcode][0][] = array(
        'error' => 'partial_date_is_required',
        'message' => t('@label requires at least one item to be completed. The following components are required: @components', array(
          '@label' => $instance['label'],
          '@components' => implode(', ', $minimum_components),
        )),
      );
    }
    else {
      $errors[$field['field_name']][$langcode][0][] = array(
        'error' => 'partial_date_is_required',
        'message' => t('@label requires at least one item to be completed.', array(
          '@label' => $instance['label'],
        )),
      );
    }
  }

  // @todo - ensure that the estimates match the date.
  $estimate_options = partial_date_field_estimates($field);
  return;

  // TODO
  foreach ($items as $delta => $item) {
    foreach (array(
      'from',
      'to',
    ) as $position) {
      if (!$has_range && $position == 'to') {
        break;
      }
      $item = $item[$position];
      foreach (partial_date_components() as $key => $label) {
        $value = strlen($item[$key]) ? $item[$key] : FALSE;
        $estimate = strlen($item[$key . '_estimate']) ? $item[$key . '_estimate'] : FALSE;
        if ($value && $estimate) {
          list($start, $end) = explode('|', $estimate);
          $empty_start = FALSE;
          $empty_end = FALSE;
          if (!strlen($start)) {
            $start = $value;
            $empty_start = TRUE;
          }
          if (!strlen($end)) {
            $empty_end = TRUE;
            $end = $value;
          }

          /**
           * Helper function to determine the best error message given that we are
           * validating against a range that may or may not have a start or ending value.
           */
          function _estimates_error_message($label, $estimate_label, $start, $end) {
            $e = max(array(
              $start,
              $end,
            ));
            if ($start !== FALSE && $end !== FALSE) {
              return t('%label fields do not match. %label must be greater than %start and less than %end if you select %estimate', array(
                '%start' => $start,
                '%end' => $end,
                '%estimate' => $estimate_label,
                '%label' => $label,
              ));
            }
            elseif ($start !== FALSE) {
              return t('%label fields do not match. %label must be greater than %start if you select %estimate', array(
                '%start' => $start,
                '%estimate' => $estimate_label,
                '%label' => $label,
              ));
            }
            elseif ($end !== FALSE) {
              return t('%label fields do not match. %label must be less than %end if you select %estimate', array(
                '%end' => $end,
                '%estimate' => $estimate_label,
                '%label' => $label,
              ));
            }
          }
          $message = FALSE;
          switch ($key) {
            case 'year':
              if ($value < $start) {
                $message = _estimates_error_message($label, $estimate_options[$key][$estimate], $start, $end);
                $message = t('%label fields do not match. %label must be greater than %start if you select %estimate', array(
                  '%start' => $start,
                  '%estimate' => $estimate_options[$key][$estimate],
                  '%label' => $label,
                ));
              }
              elseif ($value > $end) {
                $message = t('%label fields do not match. %label must be less than %end if you select %estimate', array(
                  '%end' => $end,
                  '%estimate' => $estimate_options[$key][$estimate],
                  '%label' => $label,
                ));
              }
              break;
            default:

              // If range is x to y && x < y, value must be between x and y
              if ($end > $start) {
                if (!($value >= $start && $value <= $end)) {
                  $message = t('%label fields do not match. %label must be between %start and %end if you select %estimate', array(
                    '%start' => $start,
                    '%estimate' => $estimate_options[$key][$estimate],
                    '%label' => $label,
                  ));
                }
              }
              elseif ($end > $start) {
                if (!($value >= $start || $value <= $end)) {
                }
              }
              elseif ($value != $start) {
              }
              if (!($internal_match || $external_match)) {
                $message = t('%label fields do not match. %label must be greater than %start if you select %estimate', array(
                  '%start' => $start,
                  '%estimate' => $estimate_options[$key][$estimate],
                  '%label' => $label,
                ));
              }
              elseif ($value > $end) {
                $message = t('%label fields do not match. %label must be less than %end if you select %estimate', array(
                  '%end' => $end,
                  '%estimate' => $estimate_options[$key][$estimate],
                  '%label' => $label,
                ));
              }
          }
          if ($message) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'partial_date_invalid_' . $key,
              'message' => $message,
            );
          }
        }
      }
      $year;
    }
  }
  foreach ($items as $delta => $item) {

    // Validate we actually have valid year as an integer value.
    if ($message = partial_date_field_validate_year($item, 'year')) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'partial_date_invalid_year_estimate',
        'message' => $message,
      );
    }
    if (!empty($item['year_estimate']) && !empty($item['year'])) {

      // Search and validate the first match.
      foreach ($year_estimates as $line) {
        if ($line[0] == $item['year_estimate']) {
          break;
        }
      }
      if ($item['year'] < $line[0] || $item['year'] > $line[1]) {
        $message = t('Year fields do not match. Year must be between %start and %end if you select %label', array(
          '%start' => $line[0],
          '%end' => $line[1],
          '%label' => $line[2],
        ));
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'partial_date_invalid_year_estimate',
          'message' => $message,
        );
      }
    }
    if (!empty($item['year_estimate_to']) && !empty($item['year_to'])) {

      // Search and validate the first match.
      foreach ($year_estimates as $line) {
        if ($line[1] == $item['year_estimate_to']) {
          break;
        }
      }
      if ($item['year_to'] < $line[0] || $item['year_to'] > $line[1]) {
        $message = t('Year fields do not match. Year must be between %start and %end if you select %label', array(
          '%start' => $line[0],
          '%end' => $line[1],
          '%label' => $line[2],
        ));
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'partial_date_invalid_year_estimate_to',
          'message' => $message,
        );
      }
    }
    if (!empty($item['month_estimate']) && !empty($item['month'])) {

      // Search and validate the first match.
      foreach ($month_estimates as $line) {
        if ($line[0] == $item['month_estimate']) {
          break;
        }
      }
      if (0) {
        $message = t('Month fields do not match. Year must be between %start and %end if you select %label', array(
          '%start' => $line[0],
          '%end' => $line[1],
          '%label' => $line[2],
        ));
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'partial_date_invalid_year_estimate',
          'message' => $message,
        );
      }
    }
    if (!empty($item['year_estimate_to']) && !empty($item['year_to'])) {

      // Search and validate the first match.
      foreach ($year_estimates as $line) {
        if ($line[1] == $item['year_estimate_to']) {
          break;
        }
      }
      if ($item['year_to'] < $line[0] || $item['year_to'] > $line[1]) {
        $message = t('Year fields do not match. Year must be between %start and %end if you select %label', array(
          '%start' => $line[0],
          '%end' => $line[1],
          '%label' => $line[2],
        ));
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'partial_date_invalid_year_estimate_to',
          'message' => $message,
        );
      }
    }

    /*
    if ($item['partial_date'] != '' && !valid_partial_date_address(trim($item['partial_date']))) {
    }
    */
  }
}