You are here

function partial_date_format in Partial Date 7

1 call to partial_date_format()
theme_partial_date in ./partial_date.module
The partial date theme callback.

File

./partial_date.module, line 1546
Defines a date element that allows for any combination of date granularity settings.

Code

function partial_date_format($item, array $settings = array()) {
  $components = array();
  uasort($settings['components'], 'partial_date_sort');

  // Skip unless we have a 12 hour format, then enforce it.
  if (isset($settings['components']['hour']) && ($settings['components']['hour'] == 'h' || $settings['components']['hour'] == 'g')) {
    if (empty($settings['meridiem'])) {
      $settings['meridiem'] = 'a';
    }
  }
  else {
    $settings['meridiem'] = '';
  }

  // Hide year designation if no valid year.
  if (empty($item['year'])) {
    $settings['year_designation'] = '';
  }
  elseif (!isset($settings['year_designation'])) {
    $settings['year_designation'] = 'ce';
  }
  if (empty($settings['is_approximate']) || !isset($settings['is_approximate'])) {
    $settings['components']['approx'] = '';
  }
  $valid_components = partial_date_components();
  $last_type = FALSE;
  foreach ($settings['components'] as $type => $component) {
    if (isset($valid_components[$type])) {
      $markup = '';

      // Value is determined by the $settings['display]
      // If estimate, use this other use value
      $display_type = empty($settings['display'][$type]) ? 'estimate_label' : $settings['display'][$type];
      $estimate = empty($item[$type . '_estimate']) ? FALSE : $item[$type . '_estimate'];
      $value = isset($item[$type]) && strlen($item[$type]) ? $item[$type] : FALSE;

      // If no estimate, switch to the date only formating option.
      if (!$estimate && ($display_type == 'date_or' || strpos($display_type, 'estimate_') === 0)) {
        $display_type = 'date_only';
      }
      switch ($display_type) {
        case 'none':

          // We need to avoid adding an empty option.
          continue;
        case 'date_only':
          if ($value !== FALSE) {
            $markup = partial_date_format_component($value, $component['format'], $item, $settings);
          }
          break;
        case 'date_or':
          if ($value !== FALSE) {
            $markup = partial_date_format_component($value, $component['format'], $item, $settings);
            break;
          }

        // Fall through
        case 'estimate_label':
          $markup = $item[$type . '_estimate_label'];

          // We no longer have a date / time like value.
          $type = 'other';
          break;
        case 'estimate_range':
          list($start, $end) = explode('|', $item[$type . '_estimate']);
          $start = partial_date_format_component($start, $component['format'], $item, $settings);
          $end = partial_date_format_component($end, $component['format'], $item, $settings);
          if (strlen($start) && strlen($end)) {
            $markup = t('@estimate_start to @estimate_end', array(
              '@estimate_start' => $start,
              '@estimate_end' => $end,
            ));
          }
          elseif (strlen($start) xor strlen($end)) {
            $markup = strlen($start) ? $start : $end;
          }
          break;
        case 'estimate_component':
          $markup = partial_date_format_component($item[$type . '_estimate_value'], $component['format'], $item, $settings);
          break;
      }
      if (!strlen($markup)) {
        if (isset($component['empty']) && strlen($component['empty'])) {

          // What do we get? If numeric, assume a date / time component, otherwise
          // we can assume that we no longer have a date / time like value.
          $markup = $component['empty'];
          if (!is_numeric($markup)) {
            $type = 'other';
          }
        }
      }
      if (strlen($markup)) {
        if ($separator = _partial_date_component_separator($last_type, $type, $settings['separator'])) {
          $components[] = $separator;
        }
        $components[] = $markup;
        $last_type = $type;
      }
    }
    elseif (isset($component['value']) && strlen($component['value'])) {
      if ($separator = _partial_date_component_separator($last_type, $type, $settings['separator'])) {
        $components[] = $separator;
      }
      $components[] = $component['value'];
      $last_type = $type;
    }
  }
  return implode('', $components);
}