Projet

Général

Profil

Paste
Télécharger (6,92 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
/**
9
 * Renders an RSS item based on fields.
10
 */
11
class views_plugin_row_rss_fields extends views_plugin_row {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function option_definition() {
17
    $options = parent::option_definition();
18
    $options['title_field'] = array('default' => '');
19
    $options['link_field'] = array('default' => '');
20
    $options['description_field'] = array('default' => '');
21
    $options['creator_field'] = array('default' => '');
22
    $options['date_field'] = array('default' => '');
23
    $options['guid_field_options']['guid_field'] = array('default' => '');
24
    $options['guid_field_options']['guid_field_is_permalink'] = array('default' => TRUE, 'bool' => TRUE);
25
    return $options;
26
  }
27

    
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function options_form(&$form, &$form_state) {
32
    parent::options_form($form, $form_state);
33

    
34
    $initial_labels = array('' => t('- None -'));
35
    $view_fields_labels = $this->display->handler->get_field_labels();
36
    $view_fields_labels = array_merge($initial_labels, $view_fields_labels);
37

    
38
    $form['title_field'] = array(
39
      '#type' => 'select',
40
      '#title' => t('Title field'),
41
      '#description' => t('The field that is going to be used as the RSS item title for each row.'),
42
      '#options' => $view_fields_labels,
43
      '#default_value' => $this->options['title_field'],
44
      '#required' => TRUE,
45
    );
46
    $form['link_field'] = array(
47
      '#type' => 'select',
48
      '#title' => t('Link field'),
49
      '#description' => t('The field that is going to be used as the RSS item link for each row. This must be a drupal relative path.'),
50
      '#options' => $view_fields_labels,
51
      '#default_value' => $this->options['link_field'],
52
      '#required' => TRUE,
53
    );
54
    $form['description_field'] = array(
55
      '#type' => 'select',
56
      '#title' => t('Description field'),
57
      '#description' => t('The field that is going to be used as the RSS item description for each row.'),
58
      '#options' => $view_fields_labels,
59
      '#default_value' => $this->options['description_field'],
60
      '#required' => TRUE,
61
    );
62
    $form['creator_field'] = array(
63
      '#type' => 'select',
64
      '#title' => t('Creator field'),
65
      '#description' => t('The field that is going to be used as the RSS item creator for each row.'),
66
      '#options' => $view_fields_labels,
67
      '#default_value' => $this->options['creator_field'],
68
      '#required' => TRUE,
69
    );
70
    $form['date_field'] = array(
71
      '#type' => 'select',
72
      '#title' => t('Publication date field'),
73
      '#description' => t('The field that is going to be used as the RSS item pubDate for each row. It needs to be in RFC 2822 format.'),
74
      '#options' => $view_fields_labels,
75
      '#default_value' => $this->options['date_field'],
76
      '#required' => TRUE,
77
    );
78
    $form['guid_field_options'] = array(
79
      '#type' => 'fieldset',
80
      '#title' => t('GUID settings'),
81
      '#collapsible' => FALSE,
82
      '#collapsed' => FALSE,
83
    );
84
    $form['guid_field_options']['guid_field'] = array(
85
      '#type' => 'select',
86
      '#title' => t('GUID field'),
87
      '#description' => t('The globally unique identifier of the RSS item.'),
88
      '#options' => $view_fields_labels,
89
      '#default_value' => $this->options['guid_field_options']['guid_field'],
90
      '#required' => TRUE,
91
    );
92
    $form['guid_field_options']['guid_field_is_permalink'] = array(
93
      '#type' => 'checkbox',
94
      '#title' => t('GUID is permalink'),
95
      '#description' => t('The RSS item GUID is a permalink.'),
96
      '#default_value' => $this->options['guid_field_options']['guid_field_is_permalink'],
97
    );
98
  }
99

    
100
  /**
101
   *
102
   */
103
  public function validate() {
104
    $errors = parent::validate();
105
    $required_options = array('title_field', 'link_field', 'description_field', 'creator_field', 'date_field');
106
    foreach ($required_options as $required_option) {
107
      if (empty($this->options[$required_option])) {
108
        $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
109
        break;
110
      }
111
    }
112
    // Once more for guid.
113
    if (empty($this->options['guid_field_options']['guid_field'])) {
114
      $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
115
    }
116
    return $errors;
117
  }
118

    
119
  /**
120
   * {@inheritdoc}
121
   */
122
  public function render($row) {
123
    static $row_index;
124
    if (!isset($row_index)) {
125
      $row_index = 0;
126
    }
127
    if (function_exists('rdf_get_namespaces')) {
128
      // Merge RDF namespaces in the XML namespaces in case they are used
129
      // further in the RSS content.
130
      $xml_rdf_namespaces = array();
131
      foreach (rdf_get_namespaces() as $prefix => $uri) {
132
        $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
133
      }
134
      $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
135
    }
136

    
137
    // Create the RSS item object.
138
    $item = new stdClass();
139
    $item->title = $this->get_field($row_index, $this->options['title_field']);
140
    $item->link = url($this->get_field($row_index, $this->options['link_field']), array('absolute' => TRUE));
141
    $item->description = $this->get_field($row_index, $this->options['description_field']);
142
    $item->elements = array(
143
      array('key' => 'pubDate', 'value' => $this->get_field($row_index, $this->options['date_field'])),
144
      array(
145
        'key' => 'dc:creator',
146
        'value' => $this->get_field($row_index, $this->options['creator_field']),
147
        'namespace' => array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'),
148
      ),
149
    );
150
    $guid_is_permalink_string = 'false';
151
    $item_guid = $this->get_field($row_index, $this->options['guid_field_options']['guid_field']);
152
    if ($this->options['guid_field_options']['guid_field_is_permalink']) {
153
      $guid_is_permalink_string = 'true';
154
      $item_guid = url($item_guid, array('absolute' => TRUE));
155
    }
156
    $item->elements[] = array(
157
      'key' => 'guid',
158
      'value' => $item_guid,
159
      'attributes' => array('isPermaLink' => $guid_is_permalink_string),
160
    );
161

    
162
    $row_index++;
163

    
164
    foreach ($item->elements as $element) {
165
      if (isset($element['namespace'])) {
166
        $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $element['namespace']);
167
      }
168
    }
169

    
170
    return theme($this->theme_functions(),
171
      array(
172
        'view' => $this->view,
173
        'options' => $this->options,
174
        'row' => $item,
175
        'field_alias' => isset($this->field_alias) ? $this->field_alias : '',
176
      ));
177
  }
178

    
179
  /**
180
   * Retrieves a views field value from the style plugin.
181
   *
182
   * @param int $index
183
   *   The index of the row as expected by views_plugin_style::get_field().
184
   * @param string $field_id
185
   *   The ID assigned to the required field in the display.
186
   */
187
  public function get_field($index, $field_id) {
188
    if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
189
      return '';
190
    }
191
    return $this->view->style_plugin->get_field($index, $field_id);
192
  }
193

    
194
}