You are here

function views_xml_opml_render in Views Datasource 5

1 call to views_xml_opml_render()
theme_views_xml_opml in ./views_xml.module

File

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

Code

function views_xml_opml_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 .= '<opml version="1.0">' . "\n";
  $xml .= '<head>' . "\n";
  $xml .= '  <title>' . variable_get('site_name', 'drupal') . '-' . $view->name . '</title>' . "\n";
  $xml .= '  <ownerName>' . $user->name . '</ownerName>' . "\n";
  $xml .= '  <ownerEmail>' . $user->mail . '</ownerEmail>' . "\n";
  $xml .= '  <dateCreated>' . date(DATE_ISO8601, time()) . '</dateCreated>' . "\n";
  $xml .= '</head>' . "\n";
  $xml .= '<body>' . "\n";
  foreach ($nodes as $node) {
    $xml .= '  <outline ';
    $field_count = 0;
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        $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)));
        if (is_null($value) || $value === '') {
          continue;
        }
        if (strtolower($label) == 'text' || strtolower($label) == 'nodebody') {
          $label = "text";
        }
        if (strtolower($label) == 'type' || strtolower($label) == 'nodetype') {
          $label = "type";
        }
        if (strtolower($label) == 'id' || strtolower($label) == 'nid' || strtolower($label) == 'nodeid') {

          //if a nid is given construct the url attribute
          $url = 'http://' . $_SERVER['HTTP_HOST'] . $base_path . 'index.php?q=node/' . $value;
        }
        if (strtolower($label) == 'published' || strtolower($label) == 'node_created' || strtolower($label) == 'nodecreatedtime') {
          if ($node->node_created) {
            $label = 'created';
            $value = date(DATE_RFC822, $node->node_created);
          }
        }
        $xml .= $label . '="' . preg_replace('/[^A-Za-z0-9 :\\/\\-_\\.\\?\\=]/', '', $value) . '" ';
      }
    }
    if ($url) {
      $xml .= ' ' . 'url="' . $url . '"';
    }
    $xml .= "  />" . "\n";
  }
  $xml .= '</body>' . "\n";
  $xml .= '</opml>' . "\n";
  drupal_set_header('Content-Type: text/xml');

  //drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
  print $xml;
  module_invoke_all('exit');
  exit;
}