You are here

public function FeedsDateTimeElement::buildDateField in Feeds 7

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

Build a node's date CCK field from our object.

Parameters

$node: The node to build the date field on.

$field_name: The name of the field to build.

File

plugins/FeedsParser.inc, line 375

Class

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

Code

public function buildDateField($node, $field_name) {
  $field = content_fields($field_name);
  $oldfield = FeedsDateTimeElement::readDateField($node, $field_name);

  // 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($field['tz_handling'], date_default_timezone_name());
  $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($field['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($field['tz_handling'], $use_end
        ->getTimezone()
        ->getName());
    }
  }
  if (!$db_tz) {
    return;
  }
  $db_tz = new DateTimeZone($db_tz);
  if (!isset($node->{$field_name})) {
    $node->{$field_name} = array();
  }
  if ($use_start) {
    $node->{$field_name}[0]['timezone'] = $use_start
      ->getTimezone()
      ->getName();
    $node->{$field_name}[0]['offset'] = $use_start
      ->getOffset();
    $use_start
      ->setTimezone($db_tz);
    $node->{$field_name}[0]['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));
     */
    $node->{$field_name}[0]['value'] = $use_start
      ->format(date_type_format($field['type']));
  }
  if ($use_end) {

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