You are here

class NodeResourceFeedModelItem in Services 6.3

Same name and namespace in other branches
  1. 6.2 services/node_service/node_resource.models.inc \NodeResourceFeedModelItem
  2. 7 services/node_service/node_resource.models.inc \NodeResourceFeedModelItem

Hierarchy

Expanded class hierarchy of NodeResourceFeedModelItem

File

servers/rest_server/includes/node_resource.models.inc, line 51
This file will parse a node resource feed And will also provide the necessary manip functions

View source
class NodeResourceFeedModelItem implements ResourceTimeFeedModelItem {
  protected $node = NULL;
  protected $mode = 'raw';
  protected $item_length = 'fulltext';
  public function __construct($data, $mode = 'raw', $item_length = 'fulltext') {
    $this->node = $data;
    $this->mode = $mode;
    $this->item_length = $item_length;
  }

  /**
   * Returns the raw node title
   *
   * @return string
   *  The title of the node
   */
  public function getName() {
    return $this->node->title;
  }

  /**
   * Calls node_build_content to create a teaser
   */
  public function getDescription() {
    $description = '';
    if ($this->mode == 'render') {

      // Don't bother with the rendering process if only
      // the title was requested.
      if ($this->item_length != 'title') {
        $description = $this
          ->renderAsNodeFeed($this->node->nid, $this->item_length);
      }
    }
    else {

      // Serve the raw teaser or body
      switch ($this->item_length) {
        case 'teaser':
          if (isset($this->node->teaser)) {
            $description = $this->node->teaser;
          }
          break;
        case 'fulltext':
          if (isset($this->node->body)) {
            $description = $this->node->body;
          }
          break;
      }
    }
    return $description;
  }

  /**
   * This function consists entirely of code copy pasted from node_feed()
   * to make the node resource feed model consistent with normal feeds.
   *
   * @param int $nid
   *  The id of the node that should be rendered
   * @param string $item_length
   *  The length of the content to generate: title, teaser or fulltext
   * @return string
   *  The text that should be used for the feed item description
   */
  protected function renderAsNodeFeed($nid, $item_length) {

    // Load the specified node:
    $item = node_load($nid);
    $item->build_mode = NODE_BUILD_RSS;
    $item->link = url("node/{$nid}", array(
      'absolute' => TRUE,
    ));
    if ($item_length != 'title') {
      $teaser = $item_length == 'teaser' ? TRUE : FALSE;

      // Filter and prepare node teaser
      if (node_hook($item, 'view')) {
        $item = node_invoke($item, 'view', $teaser, FALSE);
      }
      else {
        $item = node_prepare($item, $teaser);
      }

      // Allow modules to change $node->content before the node is rendered.
      node_invoke_nodeapi($item, 'view', $teaser, FALSE);

      // Set the proper node property, then unset unused $node property so that a
      // bad theme can not open a security hole.
      $content = drupal_render($item->content);
      if ($teaser) {
        $item->teaser = $content;
        unset($item->body);
      }
      else {
        $item->body = $content;
        unset($item->teaser);
      }

      // Allow modules to modify the fully-built node.
      node_invoke_nodeapi($item, 'alter', $teaser, FALSE);
    }

    // Allow modules to add additional item fields and/or modify $item
    $extra = node_invoke_nodeapi($item, 'rss item');
    $extra = array_merge($extra, array(
      array(
        'key' => 'pubDate',
        'value' => gmdate('r', $item->created),
      ),
      array(
        'key' => 'dc:creator',
        'value' => $item->name,
      ),
      array(
        'key' => 'guid',
        'value' => $item->nid . ' at ' . $base_url,
        'attributes' => array(
          'isPermaLink' => 'false',
        ),
      ),
    ));
    foreach ($extra as $element) {
      if (isset($element['namespace'])) {
        $namespaces = array_merge($namespaces, $element['namespace']);
      }
    }

    // Prepare the item description
    switch ($item_length) {
      case 'fulltext':
        $item_text = $item->body;
        break;
      case 'teaser':
        $item_text = $item->teaser;
        if (!empty($item->readmore)) {
          $item_text .= '<p>' . l(t('read more'), 'node/' . $item->nid, array(
            'absolute' => TRUE,
            'attributes' => array(
              'target' => '_blank',
            ),
          )) . '</p>';
        }
        break;
      case 'title':
        $item_text = '';
        break;
    }
    return $item_text;
  }

  /**
   * Returns the absolute url to the node
   *
   * @return string
   *  The node url
   */
  public function getUrl() {
    return url('node/' . $this->node->nid, array(
      'absolute' => TRUE,
    ));
  }

  /**
   * Gets the created time for the node
   *
   * @return int
   *  The created time of the node as a timestamp
   */
  public function getCreated() {
    return $this->node->created;
  }

  /**
   * Gets the created time for the node
   *
   * @return int
   *  The created time of the node as a timestamp
   */
  public function getStarts() {
    return $this->node->created;
  }

  /**
   * Gets the created time for the node
   *
   * @return int
   *  The created time of the node as a timestamp
   */
  public function getEnds() {
    return $this->node->created;
  }

  /**
   * Gets a associative array containing extra properties for the item.
   *
   * @return array
   *  The extra properties of the item as an array
   */
  public function getProperties() {
    return get_object_vars($this->node);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeResourceFeedModelItem::$item_length protected property
NodeResourceFeedModelItem::$mode protected property
NodeResourceFeedModelItem::$node protected property
NodeResourceFeedModelItem::getCreated public function Gets the created time for the node Overrides ResourceFeedModelItem::getCreated
NodeResourceFeedModelItem::getDescription public function Calls node_build_content to create a teaser Overrides ResourceFeedModelItem::getDescription
NodeResourceFeedModelItem::getEnds public function Gets the created time for the node Overrides ResourceTimeFeedModelItem::getEnds
NodeResourceFeedModelItem::getName public function Returns the raw node title Overrides ResourceFeedModelItem::getName
NodeResourceFeedModelItem::getProperties public function Gets a associative array containing extra properties for the item. Overrides ResourceFeedModelItem::getProperties
NodeResourceFeedModelItem::getStarts public function Gets the created time for the node Overrides ResourceTimeFeedModelItem::getStarts
NodeResourceFeedModelItem::getUrl public function Returns the absolute url to the node Overrides ResourceFeedModelItem::getUrl
NodeResourceFeedModelItem::renderAsNodeFeed protected function This function consists entirely of code copy pasted from node_feed() to make the node resource feed model consistent with normal feeds.
NodeResourceFeedModelItem::__construct public function