Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_row_rss_fields.inc @ 7547bb19

1
<?php
2
/**
3
 * @file
4
 * Contains an implementation of RSS items based on fields on a row plugin.
5
 */
6

    
7
/**
8
 * Renders an RSS item based on fields.
9
 */
10
class views_plugin_row_rss_fields extends views_plugin_row {
11
  function option_definition() {
12
    $options = parent::option_definition();
13
    $options['title_field'] = array('default' => '');
14
    $options['link_field'] = array('default' => '');
15
    $options['description_field'] = array('default' => '');
16
    $options['creator_field'] = array('default' => '');
17
    $options['date_field'] = array('default' => '');
18
    $options['guid_field_options']['guid_field'] = array('default' => '');
19
    $options['guid_field_options']['guid_field_is_permalink'] = array('default' => TRUE, 'bool' => TRUE);
20
    return $options;
21
  }
22

    
23
  function options_form(&$form, &$form_state) {
24
    parent::options_form($form, $form_state);
25

    
26
    $initial_labels = array('' => t('- None -'));
27
    $view_fields_labels = $this->display->handler->get_field_labels();
28
    $view_fields_labels = array_merge($initial_labels, $view_fields_labels);
29

    
30
    $form['title_field'] = array(
31
      '#type' => 'select',
32
      '#title' => t('Title field'),
33
      '#description' => t('The field that is going to be used as the RSS item title for each row.'),
34
      '#options' => $view_fields_labels,
35
      '#default_value' => $this->options['title_field'],
36
      '#required' => TRUE,
37
    );
38
    $form['link_field'] = array(
39
      '#type' => 'select',
40
      '#title' => t('Link field'),
41
      '#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.'),
42
      '#options' => $view_fields_labels,
43
      '#default_value' => $this->options['link_field'],
44
      '#required' => TRUE,
45
    );
46
    $form['description_field'] = array(
47
      '#type' => 'select',
48
      '#title' => t('Description field'),
49
      '#description' => t('The field that is going to be used as the RSS item description for each row.'),
50
      '#options' => $view_fields_labels,
51
      '#default_value' => $this->options['description_field'],
52
      '#required' => TRUE,
53
    );
54
    $form['creator_field'] = array(
55
      '#type' => 'select',
56
      '#title' => t('Creator field'),
57
      '#description' => t('The field that is going to be used as the RSS item creator for each row.'),
58
      '#options' => $view_fields_labels,
59
      '#default_value' => $this->options['creator_field'],
60
      '#required' => TRUE,
61
    );
62
    $form['date_field'] = array(
63
      '#type' => 'select',
64
      '#title' => t('Publication date field'),
65
      '#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.'),
66
      '#options' => $view_fields_labels,
67
      '#default_value' => $this->options['date_field'],
68
      '#required' => TRUE,
69
    );
70
    $form['guid_field_options'] = array(
71
      '#type' => 'fieldset',
72
      '#title' => t('GUID settings'),
73
      '#collapsible' => FALSE,
74
      '#collapsed' => FALSE,
75
    );
76
    $form['guid_field_options']['guid_field'] = array(
77
      '#type' => 'select',
78
      '#title' => t('GUID field'),
79
      '#description' => t('The globally unique identifier of the RSS item.'),
80
      '#options' => $view_fields_labels,
81
      '#default_value' => $this->options['guid_field_options']['guid_field'],
82
      '#required' => TRUE,
83
    );
84
    $form['guid_field_options']['guid_field_is_permalink'] = array(
85
      '#type' => 'checkbox',
86
      '#title' => t('GUID is permalink'),
87
      '#description' => t('The RSS item GUID is a permalink.'),
88
      '#default_value' => $this->options['guid_field_options']['guid_field_is_permalink'],
89
    );
90
  }
91

    
92
  function validate() {
93
    $errors = parent::validate();
94
    $required_options = array('title_field', 'link_field', 'description_field', 'creator_field', 'date_field');
95
    foreach ($required_options as $required_option) {
96
      if (empty($this->options[$required_option])) {
97
        $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
98
        break;
99
      }
100
    }
101
    // Once more for guid.
102
    if (empty($this->options['guid_field_options']['guid_field'])) {
103
      $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
104
    }
105
    return $errors;
106
  }
107

    
108
  function render($row) {
109
    static $row_index;
110
    if (!isset($row_index)) {
111
      $row_index = 0;
112
    }
113
    if (function_exists('rdf_get_namespaces')) {
114
      // Merge RDF namespaces in the XML namespaces in case they are used
115
      // further in the RSS content.
116
      $xml_rdf_namespaces = array();
117
      foreach (rdf_get_namespaces() as $prefix => $uri) {
118
        $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
119
      }
120
      $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
121
    }
122

    
123
    // Create the RSS item object.
124
    $item = new stdClass();
125
    $item->title = $this->get_field($row_index, $this->options['title_field']);
126
    $item->link = url($this->get_field($row_index, $this->options['link_field']), array('absolute' => TRUE));
127
    $item->description = $this->get_field($row_index, $this->options['description_field']);
128
    $item->elements = array(
129
      array('key' => 'pubDate', 'value' => $this->get_field($row_index, $this->options['date_field'])),
130
      array(
131
        'key' => 'dc:creator',
132
        'value' => $this->get_field($row_index, $this->options['creator_field']),
133
        'namespace' => array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'),
134
      ),
135
    );
136
    $guid_is_permalink_string = 'false';
137
    $item_guid = $this->get_field($row_index, $this->options['guid_field_options']['guid_field']);
138
    if ($this->options['guid_field_options']['guid_field_is_permalink']) {
139
      $guid_is_permalink_string = 'true';
140
      $item_guid = url($item_guid, array('absolute' => TRUE));
141
    }
142
    $item->elements[] = array(
143
      'key' => 'guid',
144
      'value' => $item_guid,
145
      'attributes' => array('isPermaLink' => $guid_is_permalink_string),
146
    );
147

    
148
    $row_index++;
149

    
150
    foreach ($item->elements as $element) {
151
      if (isset($element['namespace'])) {
152
        $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $element['namespace']);
153
      }
154
    }
155

    
156
    return theme($this->theme_functions(),
157
      array(
158
        'view' => $this->view,
159
        'options' => $this->options,
160
        'row' => $item,
161
        'field_alias' => isset($this->field_alias) ? $this->field_alias : '',
162
      ));
163
  }
164

    
165
  /**
166
   * Retrieves a views field value from the style plugin.
167
   *
168
   * @param $index
169
   *   The index count of the row as expected by views_plugin_style::get_field().
170
   * @param $field_id
171
   *   The ID assigned to the required field in the display.
172
   */
173
  function get_field($index, $field_id) {
174
    if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
175
      return '';
176
    }
177
    return $this->view->style_plugin->get_field($index, $field_id);
178
  }
179

    
180
}