You are here

public function FeedsDateTimeElement::buildDateField in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsParser.inc \FeedsDateTimeElement::buildDateField()
  2. 7 plugins/FeedsParser.inc \FeedsDateTimeElement::buildDateField()

Build a entity's date field from our object.

Parameters

object $entity: The entity to build the date field on.

string $field_name: The name of the field to build.

int $delta: The delta in the field.

File

plugins/FeedsParser.inc, line 629
Contains FeedsParser and related classes.

Class

FeedsDateTimeElement
Defines a date element of a parsed result (including ranges, repeat).

Code

public function buildDateField($entity, $field_name, $delta = 0, $language = LANGUAGE_NONE) {
  $info = field_info_field($field_name);
  $oldfield = FeedsDateTimeElement::readDateField($entity, $field_name, $delta, $language);

  // Merge with any preexisting objects on the field; we take precedence.
  $oldfield = $this
    ->merge($oldfield);
  $use_start = $oldfield->start;
  $use_end = $oldfield->end;

  // Set timezone if not already in the FeedsDateTime object.
  $to_tz = date_get_timezone($info['settings']['tz_handling'], date_default_timezone());
  $temp = new FeedsDateTime(NULL, new DateTimeZone($to_tz));
  $db_tz = '';
  if ($use_start) {
    $use_start = $use_start
      ->merge($temp);
    if (!date_timezone_is_valid($use_start
      ->getTimezone()
      ->getName())) {
      $use_start
        ->setTimezone(new DateTimeZone("UTC"));
    }
    $db_tz = date_get_timezone_db($info['settings']['tz_handling'], $use_start
      ->getTimezone()
      ->getName());
  }
  if ($use_end) {
    $use_end = $use_end
      ->merge($temp);
    if (!date_timezone_is_valid($use_end
      ->getTimezone()
      ->getName())) {
      $use_end
        ->setTimezone(new DateTimeZone("UTC"));
    }
    if (!$db_tz) {
      $db_tz = date_get_timezone_db($info['settings']['tz_handling'], $use_end
        ->getTimezone()
        ->getName());
    }
  }
  if (!$db_tz) {
    return;
  }
  $db_tz = new DateTimeZone($db_tz);
  if (!isset($entity->{$field_name})) {
    $entity->{$field_name} = array(
      $language => array(),
    );
  }
  if ($use_start) {
    $entity->{$field_name}[$language][$delta]['timezone'] = $use_start
      ->getTimezone()
      ->getName();
    $entity->{$field_name}[$language][$delta]['offset'] = $use_start
      ->getOffset();
    $use_start
      ->setTimezone($db_tz);
    $entity->{$field_name}[$language][$delta]['date'] = $use_start;

    /**
     * @todo the date_type_format line could be simplified based upon a patch
     *   DO issue #259308 could affect this, follow up on at some point.
     *   Without this, all granularity info is lost.
     *   $use_start->format(date_type_format($field['type'], $use_start->granularity));
     */
    $entity->{$field_name}[$language][$delta]['value'] = $use_start
      ->format(date_type_format($info['type']));
  }
  if ($use_end) {

    // Don't ever use end to set timezone (for now)
    $entity->{$field_name}[$language][$delta]['offset2'] = $use_end
      ->getOffset();
    $use_end
      ->setTimezone($db_tz);
    $entity->{$field_name}[$language][$delta]['date2'] = $use_end;
    $entity->{$field_name}[$language][$delta]['value2'] = $use_end
      ->format(date_type_format($info['type']));
  }
}