Added channel <lastBuildDate> element
authorMaciej Zgadzaj
Wed, 1 Feb 2012 21:50:23 +0000 (22:50 +0100)
committerMaciej Zgadzaj
Wed, 1 Feb 2012 21:50:23 +0000 (22:50 +0100)
modules/views_rss_core/views_rss_core.module
views/views_plugin_style_rss_fields.inc

index 2f49ac1..a87ed80 100644 (file)
@@ -82,7 +82,12 @@ function views_rss_core_views_rss_channel_elements_alter(&$elements) {
     'theme' => 'views_rss_core_element_skip',
     'help' => 'http://www.rssboard.org/rss-profile#element-channel-skipdays',
   );
-  // @TODO: add 'image', 'pubDate' and 'lastBuildDate', ignore 'textInput'.
+  $elements['lastBuildDate'] = array(
+    'configurable' => FALSE,
+    'preprocess functions' => array('views_rss_core_preprocess_channel_lastbuilddate'),
+    'help' => 'http://www.rssboard.org/rss-profile#element-channel-lastbuilddate',
+  );
+  // @TODO: add 'image' and 'pubDate', ignore 'textInput'.
 }
 
 /**
@@ -146,6 +151,19 @@ function views_rss_core_views_rss_item_elements_alter(&$elements) {
 }
 
 /**
+ * Implementation of hook_views_data_alter().
+ */
+function views_rss_core_views_query_alter(&$view, &$query) {
+  if ($view->style_plugin->definition['handler'] == 'views_plugin_style_rss_fields') {
+    // Select the most recent node creation date for <lastBuildDate> element.
+    $query->fields['views_rss_max_created'] = array(
+      'field' => '(SELECT MAX(created) FROM node)',
+      'alias' => 'views_rss_max_created',
+    );
+  }
+}
+
+/**
  * Preprocess function for channel <title> element.
  */
 function views_rss_core_preprocess_channel_title($vars) {
@@ -178,6 +196,15 @@ function views_rss_core_preprocess_channel_link($vars) {
 }
 
 /**
+ * Preprocess function for item <lastBuildDate> element.
+ */
+function views_rss_core_preprocess_channel_lastbuilddate($vars) {
+  if (isset($vars['view']->result[0]->views_rss_max_created)) {
+    return date('r', $vars['view']->result[0]->views_rss_max_created);
+  }
+}
+
+/**
  * Preprocess function for item <description> element.
  */
 function views_rss_core_preprocess_item_description($vars) {
index 2215e87..31e4e9b 100644 (file)
@@ -43,7 +43,7 @@ class views_plugin_style_rss_fields extends views_plugin_style {
     // Channel element defaults.
     $channel_elements = $this->xml_channel_elements();
     if (count($channel_elements)) {
-      foreach (array_keys($channel_elements) as $element_name) {
+      foreach (array_keys($channel_elements) as $element) {
         list($namespace, $element_name) = $this->extract_element_names($element, 'core');
         $options['channel'][$namespace][$element_name] = array('default' => '');
       }