You are here

function _partial_date_field_presave in Partial Date 7

Implements hook_field_presave().

This assumes data in the format: from year year_estimate month month_estimate etc to year year_estimate month month_estimate etc

1 call to _partial_date_field_presave()
partial_date_field_presave in ./partial_date.module
Implements hook_field_presave().

File

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

Code

function _partial_date_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $has_range = strpos($field['type'], '_range');
  foreach ($items as $delta => $item) {
    $items[$delta] = array();
    $items[$delta]['txt_short'] = isset($item['txt_short']) ? $item['txt_short'] : NULL;
    $items[$delta]['txt_long'] = isset($item['txt_long']) ? $item['txt_long'] : NULL;
    $item['from'] = empty($item['from']) ? array() : $item['from'];
    $items[$delta] += partial_date_field_presave_generate_storage_date($item['from']);
    if ($has_range) {
      $item['to'] = empty($item['to']) ? array() : $item['to'];
      $items[$delta] += partial_date_field_presave_generate_storage_date($item['to'], TRUE);
    }

    // Populate empty components with the estimate components. On load, these
    // should be cleared.
    $items[$delta]['data'] = array(
      'check_approximate' => empty($item['check_approximate']) ? 0 : 1,
    );
    foreach (partial_date_components(array(
      'timezone',
    )) as $key => $label) {
      $items[$delta]['data'][$key . '_estimate'] = '';
      $items[$delta]['data'][$key . '_estimate_from_used'] = 0;
      $items[$delta]['data'][$key . '_estimate_to_used'] = 0;
      $from = NULL;
      $to = NULL;
      if (!empty($item['from'][$key . '_estimate'])) {
        $items[$delta]['data'][$key . '_estimate'] = $item['from'][$key . '_estimate'];
        list($from, $to) = explode('|', $item['from'][$key . '_estimate']);
        if (!isset($item['from'][$key]) || !strlen($item['from'][$key])) {
          $items[$delta][$key] = $from;
          $items[$delta]['data'][$key . '_estimate_from_used'] = 1;
        }
        if ($has_range && (!isset($item['to'][$key]) || !strlen($item['to'][$key]))) {
          $items[$delta][$key . '_to'] = $to;
          $items[$delta]['data'][$key . '_estimate_to_used'] = 1;
        }
      }
      if ($has_range) {
        $items[$delta]['data'][$key . '_to_estimate'] = '';
        if (!empty($item['to'][$key . '_estimate'])) {

          // We use the to estimate if not set
          $items[$delta]['data'][$key . '_to_estimate'] = $item['to'][$key . '_estimate'];
          list($from_to, $to_to) = explode('|', $item['to'][$key . '_estimate']);
          if (!isset($item['from'][$key]) || !strlen($item['from'][$key]) || $items[$delta]['data'][$key . '_estimate_from_used']) {
            $items[$delta][$key] = is_numeric($from) ? min(array(
              $from_to,
              $from,
            )) : $from_to;
            $items[$delta]['data'][$key . '_estimate_from_used'] = 1;
          }
          if (!isset($item['to'][$key]) || !strlen($item['to'][$key]) || $items[$delta]['data'][$key . '_estimate_to_used']) {
            $items[$delta][$key . '_to'] = is_numeric($to) ? max(array(
              $to_to,
              $to,
            )) : $to_to;
            $items[$delta]['data'][$key . '_estimate_to_used'] = 1;
          }
        }
      }
    }

    // This is done after the estimates are expanded out.
    $items[$delta]['timestamp'] = partial_date_float(partial_date_field_populate_components($items[$delta]));
    if ($has_range) {
      $items[$delta]['timestamp_to'] = partial_date_float(partial_date_field_populate_components($items[$delta], 0));
    }
    $items[$delta]['data'] = serialize($items[$delta]['data']);
  }
}