Projet

Général

Profil

Paste
Télécharger (4,22 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_style_rss.inc @ 5d12d676

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_style_rss.
6
 */
7

    
8
/**
9
 * Default style plugin to render an RSS feed.
10
 *
11
 * @ingroup views_style_plugins
12
 */
13
class views_plugin_style_rss extends views_plugin_style {
14

    
15
  /**
16
   *
17
   */
18
  public function attach_to($display_id, $path, $title) {
19
    $display = $this->view->display[$display_id]->handler;
20
    $url_options = array();
21
    $input = $this->view->get_exposed_input();
22
    if ($input) {
23
      $url_options['query'] = $input;
24
    }
25
    $url_options['absolute'] = TRUE;
26

    
27
    $url = url($this->view->get_url(NULL, $path), $url_options);
28
    if ($display->has_path()) {
29
      if (empty($this->preview)) {
30
        drupal_add_feed($url, $title);
31
      }
32
    }
33
    else {
34
      if (empty($this->view->feed_icon)) {
35
        $this->view->feed_icon = '';
36
      }
37

    
38
      $this->view->feed_icon .= theme('feed_icon', array('url' => $url, 'title' => $title));
39
      drupal_add_html_head_link(array(
40
        'rel' => 'alternate',
41
        'type' => 'application/rss+xml',
42
        'title' => $title,
43
        'href' => $url,
44
      ));
45
    }
46
  }
47

    
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function option_definition() {
52
    $options = parent::option_definition();
53

    
54
    $options['description'] = array('default' => '', 'translatable' => TRUE);
55

    
56
    return $options;
57
  }
58

    
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function options_form(&$form, &$form_state) {
63
    parent::options_form($form, $form_state);
64

    
65
    $form['description'] = array(
66
      '#type' => 'textfield',
67
      '#title' => t('RSS description'),
68
      '#default_value' => $this->options['description'],
69
      '#description' => t('This will appear in the RSS feed itself.'),
70
      '#maxlength' => 1024,
71
    );
72
  }
73

    
74
  /**
75
   * Return an array of additional XHTML elements to add to the channel.
76
   *
77
   * @return
78
   *   An array that can be passed to format_xml_elements().
79
   */
80
  public function get_channel_elements() {
81
    return array();
82
  }
83

    
84
  /**
85
   * Return an atom:link XHTML element to add to the channel to comply with
86
   * the RSS 2.0 specification.
87
   *
88
   * @see http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html
89
   *
90
   * @return
91
   *   An array that can be passed to format_xml_elements().
92
   */
93
  public function get_channel_elements_atom_link() {
94
    $url_options = array('absolute' => TRUE);
95
    $input = $this->view->get_exposed_input();
96
    if ($input) {
97
      $url_options['query'] = $input;
98
    }
99
    $url = url($this->view->get_url(), $url_options);
100

    
101
    return array(
102
      array(
103
        'namespace' => array('xmlns:atom' => 'http://www.w3.org/2005/Atom'),
104
        'key' => 'atom:link',
105
        'attributes' => array(
106
          'href' => $url,
107
          'rel' => 'self',
108
          'type' => 'application/rss+xml',
109
        ),
110
      ),
111
    );
112
  }
113

    
114
  /**
115
   * Get RSS feed description.
116
   *
117
   * @return string
118
   *   The string containing the description with the tokens replaced.
119
   */
120
  public function get_description() {
121
    $description = $this->options['description'];
122

    
123
    // Allow substitutions from the first row.
124
    $description = $this->tokenize_value($description, 0);
125

    
126
    return $description;
127
  }
128

    
129
  /**
130
   * {@inheritdoc}
131
   */
132
  public function render() {
133
    if (empty($this->row_plugin)) {
134
      vpr('views_plugin_style_default: Missing row plugin');
135
      return;
136
    }
137
    $rows = '';
138

    
139
    // This will be filled in by the row plugin and is used later on in the
140
    // theming output.
141
    $this->namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/');
142

    
143
    // Fetch any additional elements for the channel and merge in their
144
    // namespaces.
145
    $this->channel_elements = array_merge(
146
      $this->get_channel_elements(),
147
      $this->get_channel_elements_atom_link()
148
    );
149
    foreach ($this->channel_elements as $element) {
150
      if (isset($element['namespace'])) {
151
        $this->namespaces = array_merge($this->namespaces, $element['namespace']);
152
      }
153
    }
154

    
155
    foreach ($this->view->result as $row_index => $row) {
156
      $this->view->row_index = $row_index;
157
      $rows .= $this->row_plugin->render($row);
158
    }
159

    
160
    $output = theme($this->theme_functions(),
161
      array(
162
        'view' => $this->view,
163
        'options' => $this->options,
164
        'rows' => $rows,
165
      ));
166
    unset($this->view->row_index);
167
    return $output;
168
  }
169

    
170
}