You are here

function views_xml_atom_render in Views Datasource 5

1 call to views_xml_atom_render()
theme_views_xml_atom in ./views_xml.module

File

./views_xml.module, line 228
views_xml.module - provides Views plugin for rendering node content as XML.

Code

function views_xml_atom_render($vid, $nodes, $type) {
  global $user;
  global $base_path;
  $view = views_load_view($vid);
  $result = views_build_view('items', $view);
  $fields = _views_get_fields();
  $xml .= '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
  $xml .= '<!-- generator="Drupal Views_Datasource.Module" -->' . "\n";
  $xml .= '<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">' . "\n";
  $xml .= '  <title>' . $view->name . '</title>' . "\n";
  $xml .= '  <link rel="alternate" type="text/html" href="' . ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . base_path() . '"/>' . "\n";
  $xml .= '  <link rel ="self" type="application/atom+xml" href="' . ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/' . $view->real_url . '"/>' . "\n";
  $xml .= '  <id>' . ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/' . $view->real_url . '</id>' . "\n";

  //use path as id
  $xml .= '  <updated>###feed_updated###</updated>' . "\n";

  //will set later
  $xml .= '  <generator>Views Datasource module</generator>' . "\n";
  $feed_last_updated = 0;
  foreach ($nodes as $node) {
    if ($node->nid) {
      $entry['nid'] = $node->nid;
    }
    if ($node->title) {
      $entry['title'] = $node->title;
    }
    if ($node->node_created) {
      $entry['published'] = $node->node_created;
    }
    if ($node->node_changed) {
      $entry['updated'] = $node->node_changed;
    }
    if ($node->users_name) {
      $entry['author'] = $node->users_name;
    }
    if ($node->users_email) {
      $entry['email'] = $node->users_mail;
    }
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] === false) {
        continue;
      }
      $label = preg_replace('/\\W/', '', views_xml_strip_illegal_chars($field['label'] ? $field['label'] : $fields[$field['fullname']]['name']));
      $value = views_xml_strip_illegal_chars(views_xml_is_date(views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view)));
      $label = str_replace("Profile", '', $label);

      //strip out Profile: from profile fields
      if (is_null($value) || $value === '') {
        continue;
      }
      if (!isset($entry['nid']) && (strtolower($label) == 'nid' || strtolower($label) == 'node_nid' || strtolower($label) == 'nodeid')) {
        $entry['nid'] = $value;
      }
      if (!isset($entry['updated']) && (strtolower($label) == 'updated' || strtolower($label) == 'updated date' || strtolower($label) == 'nodeupdatedtime')) {
        if (is_int($value)) {

          //timestamp
          $entry['updated'] = intval($value);
        }
        else {
          if (getdate($value)) {

            //string date
            $entry['updated'] = strtotime($value);
          }
          else {
            $entry['updated'] = $value;
          }
        }
      }
      if (!isset($entry['title']) && strtolower($label) == 'title' || strtolower($label) == 'nodetitle') {
        $entry['title'] = $value;
      }
      if (strtolower($label) == 'link') {
        $entry['link'] = $value;
      }
      if (!isset($entry['published']) && (strtolower($label) == 'published' || strtolower($label) == 'nodecreatedtime')) {
        if (intval($value)) {

          //timestamp
          $entry['published'] = intval($value);
        }
        else {
          if (getdate($value)) {

            //string date
            $entry['published'] = strtotime($value);
          }
        }
      }
      if (!isset($entry['author']) && (strtolower($label) == 'author' || strtolower($label) == 'nodeauthorname')) {
        $entry['author'] = $value;
      }
      if (!isset($entry['email']) && strtolower($label) == 'email' || strtolower($label) == 'users_mail') {
        $entry['email'] = $value;
      }
      if (strtolower($label) == 'content' || strtolower($label) == 'nodebody') {
        $entry['content'] = $value;
      }
      if (strtolower($label) == 'summary' || strtolower($label) == 'nodeteaser' || strtolower($label) == 'node_revisions_teaser') {
        $entry['summary'] = $value;
      }
      if (isset($entry['nid']) && isset($entry['updated']) && isset($entry['link']) && isset($entry['title']) && isset($entry['published'])) {
        if (parse_url($entry['link'])) {
          $link = $entry['link'];
        }
        else {
          drupal_set_message('The link URL is not valid.', 'error');
          return;
        }
      }
      elseif (isset($entry['nid']) && isset($entry['updated']) && isset($entry['title']) && isset($entry['published'])) {

        //make the entry path with base_path + nid {
        $entry['link'] = 'http://' . $_SERVER['HTTP_HOST'] . $base_path . 'index.php?q=node/' . $entry['nid'];
      }
      else {
        drupal_set_message('The fields "nid", "title", "published", and "updated" must exist.', 'error');
        return;
      }
    }
    $link = $entry['link'];
    $link_url = parse_url($link);
    $nid = $entry['nid'];
    $updated = $entry['updated'];
    if ($updated > $feed_last_updated) {
      $feed_last_updated = $updated;
    }

    //Overall feed updated is the most recent node updated timestamp
    $title = $entry['title'];
    $published = $entry['published'];
    $author = $entry['author'];
    $email = $entry['email'];
    $content = $entry['content'];
    $summary = $entry['summary'];

    //Create an id for the entry using tag URIs
    $id = 'tag:' . $link_url['host'] . ',' . date('Y-m-d', $updated) . ':' . $link_url['path'] . '?' . $link_url['query'];
    $xml .= '  <entry>' . "\n";
    $xml .= '    <id>' . $id . '</id>' . "\n";
    $xml .= '    <updated>' . date(DATE_ATOM, $updated) . '</updated>' . "\n";
    $xml .= '    <title type="text">' . $title . '</title>' . "\n";
    $xml .= '    <link rel="alternate" type="text/html" href="' . $link . '"/>' . "\n";
    $xml .= '    <published>' . date(DATE_ATOM, $published) . '</published>' . "\n";
    if ($author) {
      if ($email) {
        $xml .= '    <author><name>' . $author . '</name><email>' . $email . '</email></author>' . "\n";
      }
      else {
        $xml .= '    <author><name>' . $author . '</name></author>' . "\n";
      }
    }
    if ($content) {
      $xml .= '    <content type="html" xml:base="' . ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . base_path() . '"><![CDATA[' . $content . ']]></content>' . "\n";
    }
    if ($summary) {
      $xml .= '    <summary type="html" xml:base="' . ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . base_path() . '"><![CDATA[' . $summary . ']]></summary>' . "\n";
    }
    $xml .= '  </entry>' . "\n";
  }
  $xml .= '</feed>' . "\n";
  $xml = str_replace('###feed_updated###', date(DATE_ATOM, $feed_last_updated), $xml);
  drupal_set_header('Content-Type: application/atom+xml');
  print $xml;

  //var_dump($view);
  module_invoke_all('exit');
  exit;
}