views_plugin_style_rss.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Contains the RSS style plugin.
  5. */
  6. /**
  7. * Default style plugin to render an RSS feed.
  8. *
  9. * @ingroup views_style_plugins
  10. */
  11. class views_plugin_style_rss extends views_plugin_style {
  12. function attach_to($display_id, $path, $title) {
  13. $display = $this->view->display[$display_id]->handler;
  14. $url_options = array();
  15. $input = $this->view->get_exposed_input();
  16. if ($input) {
  17. $url_options['query'] = $input;
  18. }
  19. $url_options['absolute'] = TRUE;
  20. $url = url($this->view->get_url(NULL, $path), $url_options);
  21. if ($display->has_path()) {
  22. if (empty($this->preview)) {
  23. drupal_add_feed($url, $title);
  24. }
  25. }
  26. else {
  27. if (empty($this->view->feed_icon)) {
  28. $this->view->feed_icon = '';
  29. }
  30. $this->view->feed_icon .= theme('feed_icon', array('url' => $url, 'title' => $title));
  31. drupal_add_html_head_link(array(
  32. 'rel' => 'alternate',
  33. 'type' => 'application/rss+xml',
  34. 'title' => $title,
  35. 'href' => $url
  36. ));
  37. }
  38. }
  39. function option_definition() {
  40. $options = parent::option_definition();
  41. $options['description'] = array('default' => '', 'translatable' => TRUE);
  42. return $options;
  43. }
  44. function options_form(&$form, &$form_state) {
  45. parent::options_form($form, $form_state);
  46. $form['description'] = array(
  47. '#type' => 'textfield',
  48. '#title' => t('RSS description'),
  49. '#default_value' => $this->options['description'],
  50. '#description' => t('This will appear in the RSS feed itself.'),
  51. '#maxlength' => 1024,
  52. );
  53. }
  54. /**
  55. * Return an array of additional XHTML elements to add to the channel.
  56. *
  57. * @return
  58. * An array that can be passed to format_xml_elements().
  59. */
  60. function get_channel_elements() {
  61. return array();
  62. }
  63. /**
  64. * Return an atom:link XHTML element to add to the channel to comply with
  65. * the RSS 2.0 specification.
  66. *
  67. * @see http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html
  68. *
  69. * @return
  70. * An array that can be passed to format_xml_elements().
  71. */
  72. function get_channel_elements_atom_link() {
  73. $url_options = array('absolute' => TRUE);
  74. $input = $this->view->get_exposed_input();
  75. if ($input) {
  76. $url_options['query'] = $input;
  77. }
  78. $url = url($this->view->get_url(), $url_options);
  79. return array(
  80. array(
  81. 'namespace' => array('xmlns:atom' => 'http://www.w3.org/2005/Atom'),
  82. 'key' => 'atom:link',
  83. 'attributes' => array(
  84. 'href' => $url,
  85. 'rel' => 'self',
  86. 'type' => 'application/rss+xml',
  87. ),
  88. ),
  89. );
  90. }
  91. /**
  92. * Get RSS feed description.
  93. *
  94. * @return string
  95. * The string containing the description with the tokens replaced.
  96. */
  97. function get_description() {
  98. $description = $this->options['description'];
  99. // Allow substitutions from the first row.
  100. $description = $this->tokenize_value($description, 0);
  101. return $description;
  102. }
  103. function render() {
  104. if (empty($this->row_plugin)) {
  105. vpr('views_plugin_style_default: Missing row plugin');
  106. return;
  107. }
  108. $rows = '';
  109. // This will be filled in by the row plugin and is used later on in the
  110. // theming output.
  111. $this->namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/');
  112. // Fetch any additional elements for the channel and merge in their
  113. // namespaces.
  114. $this->channel_elements = array_merge(
  115. $this->get_channel_elements(),
  116. $this->get_channel_elements_atom_link()
  117. );
  118. foreach ($this->channel_elements as $element) {
  119. if (isset($element['namespace'])) {
  120. $this->namespaces = array_merge($this->namespaces, $element['namespace']);
  121. }
  122. }
  123. foreach ($this->view->result as $row_index => $row) {
  124. $this->view->row_index = $row_index;
  125. $rows .= $this->row_plugin->render($row);
  126. }
  127. $output = theme($this->theme_functions(),
  128. array(
  129. 'view' => $this->view,
  130. 'options' => $this->options,
  131. 'rows' => $rows
  132. ));
  133. unset($this->view->row_index);
  134. return $output;
  135. }
  136. }