You are here

class FeedsDateTimeElement in Feeds 7

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

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

Hierarchy

Expanded class hierarchy of FeedsDateTimeElement

File

plugins/FeedsParser.inc, line 282

View source
class FeedsDateTimeElement extends FeedsElement {

  // Start date and end date.
  public $start;
  public $end;

  /**
   * Constructor.
   *
   * @param $start
   *   A FeedsDateTime object or a date as accepted by FeedsDateTime.
   * @param $end
   *   A FeedsDateTime object or a date as accepted by FeedsDateTime.
   * @param $tz
   *   A PHP DateTimeZone object.
   */
  public function __construct($start = NULL, $end = NULL, $tz = NULL) {
    $this->start = !isset($start) || $start instanceof FeedsDateTime ? $start : new FeedsDateTime($start, $tz);
    $this->end = !isset($end) || $end instanceof FeedsDateTime ? $end : new FeedsDateTime($end, $tz);
  }

  /**
   * Override FeedsElement::getValue().
   */
  public function getValue() {
    return $this->start;
  }

  /**
   * Implements toString magic php method.
   */
  public function __toString() {
    $val = $this
      ->getValue();
    if ($val) {
      return $val
        ->format('U');
    }
    return '';
  }

  /**
   * Merge this field with another. Most stuff goes down when merging the two
   * sub-dates.
   *
   * @see FeedsDateTime
   */
  public function merge(FeedsDateTimeElement $other) {
    $this2 = clone $this;
    if ($this->start && $other->start) {
      $this2->start = $this->start
        ->merge($other->start);
    }
    elseif ($other->start) {
      $this2->start = clone $other->start;
    }
    elseif ($this->start) {
      $this2->start = clone $this->start;
    }
    if ($this->end && $other->end) {
      $this2->end = $this->end
        ->merge($other->end);
    }
    elseif ($other->end) {
      $this2->end = clone $other->end;
    }
    elseif ($this->end) {
      $this2->end = clone $this->end;
    }
    return $this2;
  }

  /**
   * Helper method for buildDateField(). Build a FeedsDateTimeElement object
   * from a standard formatted node.
   */
  protected static function readDateField($node, $field_name) {
    $field = content_fields($field_name);
    $ret = new FeedsDateTimeElement();
    if (isset($node->{$field_name}[0]['date']) && $node->{$field_name}[0]['date'] instanceof FeedsDateTime) {
      $ret->start = $node->{$field_name}[0]['date'];
    }
    if (isset($node->{$field_name}[0]['date2']) && $node->{$field_name}[0]['date2'] instanceof FeedsDateTime) {
      $ret->end = $node->{$field_name}[0]['date2'];
    }
    return $ret;
  }

  /**
   * Build a node's date CCK field from our object.
   *
   * @param $node
   *   The node to build the date field on.
   * @param $field_name
   *   The name of the field to build.
   */
  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']));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsDateTimeElement::$end public property
FeedsDateTimeElement::$start public property
FeedsDateTimeElement::buildDateField public function Build a node's date CCK field from our object.
FeedsDateTimeElement::getValue public function Override FeedsElement::getValue(). Overrides FeedsElement::getValue
FeedsDateTimeElement::merge public function Merge this field with another. Most stuff goes down when merging the two sub-dates.
FeedsDateTimeElement::readDateField protected static function Helper method for buildDateField(). Build a FeedsDateTimeElement object from a standard formatted node.
FeedsDateTimeElement::__construct public function Constructor. Overrides FeedsElement::__construct
FeedsDateTimeElement::__toString public function Implements toString magic php method. Overrides FeedsElement::__toString
FeedsElement::$value protected property