You are here

class FeedsDateTimeElement in Feeds 7.2

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

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

Hierarchy

Expanded class hierarchy of FeedsDateTimeElement

Deprecated

This is no longer in use and will not be maintained.

File

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

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().
   *
   * @return
   *   The UNIX timestamp of this object's start date. Return value is
   *   technically a string but will only contain numeric values.
   */
  public function getValue() {
    if ($this->start) {
      return $this->start
        ->format('U');
    }
    return '0';
  }

  /**
   * 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($entity, $field_name, $delta = 0, $language = LANGUAGE_NONE) {
    $ret = new FeedsDateTimeElement();
    if (isset($entity->{$field_name}[$language][$delta]['date']) && $entity->{$field_name}[$language][$delta]['date'] instanceof FeedsDateTime) {
      $ret->start = $entity->{$field_name}[$language][$delta]['date'];
    }
    if (isset($entity->{$field_name}[$language][$delta]['date2']) && $entity->{$field_name}[$language][$delta]['date2'] instanceof FeedsDateTime) {
      $ret->end = $entity->{$field_name}[$language][$delta]['date2'];
    }
    return $ret;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsDateTimeElement::$end public property
FeedsDateTimeElement::$start public property Start date and end date.
FeedsDateTimeElement::buildDateField public function Build a entity's date 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
FeedsElement::$value protected property The standard value of this element. This value can contain be a simple type, a FeedsElement or an array of either.
FeedsElement::__toString public function Magic method __toString() for printing and string conversion of this object.