From 6a351ab0f77c55f27048a355d07e657b35871d6a Mon Sep 17 00:00:00 2001
From: pmcmonagle <pmcmonagle@tvo.org>
Date: Fri, 1 Apr 2011 15:53:17 -0400
Subject: [PATCH 1/2] Issue #123456 by pmcmonagle : Added feature that allows
 nested fieldsets to be treated as tags, with each field
 within that fieldset acting as an attribute for that
 tag.

---
 views/views-rss-fields-item.tpl.php       |    6 ++--
 views/views-view-views-rss-fields.tpl.php |    2 +-
 views/views_plugin_style_rss_fields.inc   |   19 +++++++++++++++--
 views/views_rss_views_fields.theme.inc    |   31 ++++++++++++++++++++++++++++-
 4 files changed, 50 insertions(+), 8 deletions(-)

diff --git a/views/views-rss-fields-item.tpl.php b/views/views-rss-fields-item.tpl.php
index 47ca842..cb19837 100644
--- a/views/views-rss-fields-item.tpl.php
+++ b/views/views-rss-fields-item.tpl.php
@@ -1,4 +1,4 @@
 <?php // $Id$ ?>
-<item>
-  <?php print $row ?>
-</item>
+    <item>
+<?php print $row ?>
+    </item>
\ No newline at end of file
diff --git a/views/views-view-views-rss-fields.tpl.php b/views/views-view-views-rss-fields.tpl.php
index fb9ee1e..675836e 100644
--- a/views/views-view-views-rss-fields.tpl.php
+++ b/views/views-view-views-rss-fields.tpl.php
@@ -6,6 +6,6 @@
     <title><?php print $viewtitle; ?></title>
     <description><?php print $description; ?></description>
     <link><?php print $link ?></link>
-    <?php print $rows ?>
+<?php print $rows ?>
   </channel>
 </rss>
diff --git a/views/views_plugin_style_rss_fields.inc b/views/views_plugin_style_rss_fields.inc
index 878901b..a8af2c1 100644
--- a/views/views_plugin_style_rss_fields.inc
+++ b/views/views_plugin_style_rss_fields.inc
@@ -113,7 +113,7 @@ class views_plugin_style_rss_fields extends views_plugin_style {
       }
       $form['fields'] = array(
         '#type' => 'fieldset',
-        '#title' => 'Field usage',
+        '#title' => 'RSS Field usage',
         '#description' => t('Select the fields that relevant data for each element of the feed'),
         '#collapsible' => TRUE,
         '#collapsed' => TRUE,
@@ -131,7 +131,7 @@ class views_plugin_style_rss_fields extends views_plugin_style {
       // GeoRSS
       $form['georss'] = array(
         '#type' => 'fieldset',
-        '#title' => t('GeoRSS'),
+        '#title' => t('GeoRSS Fields'),
         '#description' => t('Select fields that provide the latitude and longitude values'),
         '#collapsible' => TRUE,
         '#collapsed' => TRUE,
@@ -175,7 +175,18 @@ class views_plugin_style_rss_fields extends views_plugin_style {
     $items = array();
     foreach ($renders as $id => $row) {
       $item = array();
-      foreach (array_merge($this->options['fields'], $this->options['georss']) as $key => $val) {
+      //re-written to support nested arrays
+      foreach (array_merge($this->options['fields'], $this->options['mrss'], $this->options['brightcove'], $this->options['georss']) as $key => $val) {
+        //Nested arrays are treated as parameters
+        if (is_array($val)) {
+          if(isset($val)) {
+            foreach($val as $subkey => $subval){
+              $item[$key][$subkey] = $row[$subval];
+            }
+          }
+          continue;
+        }
+        //normal items
         if (!isset($this->view->field[$val])) continue;
         $item[$key] = $row[$val];
       }
@@ -230,6 +241,7 @@ class views_plugin_style_rss_fields extends views_plugin_style {
   function xml_namespaces() {
     return array(
       'dc' => 'http://purl.org/dc/elements/1.1/',
+      'dcterms' => 'http://purl.org/dc/terms/',
       'georss' => 'http://www.georss.org/georss',
     );
   }
@@ -258,4 +270,5 @@ class views_plugin_style_rss_fields extends views_plugin_style {
     }
     return FALSE;
   }
+  
 }
\ No newline at end of file
diff --git a/views/views_rss_views_fields.theme.inc b/views/views_rss_views_fields.theme.inc
index f81bd0d..157b4dc 100644
--- a/views/views_rss_views_fields.theme.inc
+++ b/views/views_rss_views_fields.theme.inc
@@ -72,7 +72,29 @@ function template_preprocess_views_rss_fields_item(&$vars) {
   $row = '';
   foreach ($item as $key => $value) {
     if ($value) {
-      $row .= "<$key>". check_plain(htmlspecialchars_decode($value)) ."</$key>\n";
+      //Nested arrays are treated as self-closing tags with attributes
+      if(is_array($value)){
+        if(is_not_empty($value)) {
+          $row .= "      <$key";
+          $self_closing = true;
+          foreach($value as $subkey => $subval){
+            if($subval){
+              if($subkey == 'value') {
+                $self_closing = false;
+                continue;
+              }
+              $row .= " ".$subkey.'="'.check_plain(htmlspecialchars_decode($subval)).'"';
+            }
+          }
+          if($self_closing) {
+            $row .= " />\n";
+          } else {
+            $row .= ">".$value['value']."</$key>\n";
+          }
+        }
+        continue;
+      }
+      $row .= "      <$key>". check_plain(htmlspecialchars_decode($value)) ."</$key>\n";
     }
   }
 
@@ -94,3 +116,10 @@ function theme_views_rss_feed_icon($url, $title, $icon) {
 function theme_views_rss_feed_description($description, $view) {
   return $description;
 }
+
+function is_not_empty($array) {
+  foreach($array as $key => $value) {
+    if($value) return true;
+  }
+  return false;
+}
\ No newline at end of file
-- 
1.7.4


From e3194d4c162186792c3435af6957cdade06efbf8 Mon Sep 17 00:00:00 2001
From: pmcmonagle <pmcmonagle@tvo.org>
Date: Fri, 1 Apr 2011 16:27:20 -0400
Subject: [PATCH 2/2] by pmcmonagle : Added Feature allowing nested fieldsets
 to represent RSS elements with many attributes. Fields
 within a nested fieldset will represent attributes of
 that element, rather than becoming their own seperate
 elements.

---
 views/views_plugin_style_rss_fields.inc |  154 +++++++++++++++++++++++-------
 1 files changed, 118 insertions(+), 36 deletions(-)

diff --git a/views/views_plugin_style_rss_fields.inc b/views/views_plugin_style_rss_fields.inc
index a8af2c1..480b7c1 100644
--- a/views/views_plugin_style_rss_fields.inc
+++ b/views/views_plugin_style_rss_fields.inc
@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Extend the view_plugin_style class to provide an RSS view style.
  */
@@ -50,7 +49,7 @@ class views_plugin_style_rss_fields extends views_plugin_style {
     );
     
     $field_options = array();
-    foreach ($this->xml_fields() as $k => $option) {
+    foreach ($this->rss_fieldsets() as $k => $option) {
       $field_options[$k] = array('default' => '');
     }
     
@@ -111,50 +110,33 @@ class views_plugin_style_rss_fields extends views_plugin_style {
           $field_names[$field] = $handler->ui_name();
         }
       }
-      $form['fields'] = array(
+      
+      /**
+       * RSS Fieldsets
+       */
+      $rss_outer = array(
         '#type' => 'fieldset',
-        '#title' => 'RSS Field usage',
+        '#title' => 'RSS Fields',
         '#description' => t('Select the fields that relevant data for each element of the feed'),
         '#collapsible' => TRUE,
         '#collapsed' => TRUE,
         '#weight' => 0,
       );
-      foreach ($this->xml_fields() as $k => $option) {
-        $form['fields'][$k] = array(
-          '#type' => 'select',
-          '#title' => $option['title'],
-          '#description' => isset($option['description']) ? $option['description'] : '',
-          '#options' => $field_names,
-          '#default_value' => $this->options['fields'][$k],
-        );
-      }
-      // GeoRSS
-      $form['georss'] = array(
+      $form['fields'] = $this->buildform('fields', $rss_outer, $this->rss_fieldsets(), $field_names);
+      
+      /**
+       * GeoRSS Fields
+       */
+      $georss_outer = array(
         '#type' => 'fieldset',
         '#title' => t('GeoRSS Fields'),
         '#description' => t('Select fields that provide the latitude and longitude values'),
         '#collapsible' => TRUE,
         '#collapsed' => TRUE,
-        '#weight' => 5,
-      );
-      $form['georss']['lat'] = array(
-        '#type' => 'select',
-        '#title' => t('Latitude'),
-        '#options' => $field_names,
-        '#default_value' => $this->options['georss']['lat'],
-      );
-      $form['georss']['lon'] = array(
-        '#type' => 'select',
-        '#title' => t('Longitude'),
-        '#options' => $field_names,
-        '#default_value' => $this->options['georss']['lon'],
-      );
-      $form['georss']['featureName'] = array(
-        '#type' => 'select',
-        '#title' => t('Feature Name'),
-        '#options' => $field_names,
-        '#default_value' => $this->options['georss']['featureName'],
+        '#weight' => 15,
       );
+      $form['georss'] = $this->buildform('georss', $georss_outer, $this->georss_fieldsets(), $field_names);
+      
     }
   }
 
@@ -195,11 +177,16 @@ class views_plugin_style_rss_fields extends views_plugin_style {
     return $items;
   }
 
+  
   /**
-   * An array of available XML fields to map to.
+   * An array of available RSS fields to map to.
    */
-  function xml_fields() {
+  function rss_fieldsets() {
     return array(
+      //Nested Field-Sets
+      // -- none
+      
+      //Non-Nested - no attributes supported
       'title' => array(
         'title' => t('Title'),
         'description' => t('RSS 2.0 title element'),
@@ -234,6 +221,31 @@ class views_plugin_style_rss_fields extends views_plugin_style {
       ),
     );
   }
+  
+  /**
+   * An array of available GeoRSS fields to map to.
+   */
+  function georss_fieldsets() {
+    return array(
+      //Nested Field-Sets
+      // -- none
+      
+      //Non-Nested - no attributes supported
+      'lat' => array(
+        'title' => t('Latitude'),
+        'description' => t('Latitude value for your location.'),
+      ),
+      'lon' => array(
+        'title' => t('Longitude'),
+        'description' => t('Latitude value for your location.'),
+      ),
+      'featureName' => array(
+        'title' => t('Feature Name'),
+        'description' => t('Descriptive name that will help identify your location.'),
+      ),
+    );
+  }
+  
 
   /**
    * Return an array of all known XML namespaces.
@@ -270,5 +282,75 @@ class views_plugin_style_rss_fields extends views_plugin_style {
     }
     return FALSE;
   }
+
+  
+  /**
+   * Method to reduce code redundancy
+   * This function should be called to create a form for each set of RSS Specifications / Namespaces in this document
+   *
+   * @param $fieldset_string
+   *   A string that matches the key of your outer fieldset
+   *   eg. 'fields' or 'georss' or 'mrss'
+   *
+   * @param array $fieldset_options
+   *   An associative array of options for the outer fieldset
+   *   Should contain #type, #title, #description, #collapsible, #collapsed and #weight as keys
+   *
+   * @param array $fieldset_arr
+   *   An associative array containing further arrays that represent either nested fieldsets
+   *   or fields within the outer fieldset
+   *
+   * @param array $options
+   *   Reference to the array of content fields that can be mapped to each element
+   *   In this case, it should always be $field_names - as defined above
+   *
+   * @return
+   *   An array representing an outer fieldset, with several nested arrays representing
+   *   nested fieldsets or fields within that outer fieldset
+   */
+  function buildform($fieldset_string, $fieldset_options, $fieldset_arr, $options) {
+    $temp_arr = $fieldset_options;
+    foreach ($fieldset_arr as $k_fieldset => $fieldset_option) {
+        //TO-DO: Make $formtype work properly - currently produces "illegal choice has been detected" for any value other than "select"
+        //This is probably due to the #options key, as other form types do not have lists of "choices" associated with them
+        $formtype = 'select';//($fieldset_option['formtype']) ? $fieldset_option['formtype'] : 'select'; // <-- don't use this yet
+        if($fieldset_option['attributes']) {
+          $temp_arr[$k_fieldset] = array(
+            '#type' => 'fieldset',
+            '#title' => $fieldset_option['title'],
+            '#description' => $fieldset_option['description'],
+            '#collapsible' => TRUE,
+            '#collapsed' => TRUE,
+            '#weight' => 0,
+          );
+          $temp_arr[$k_fieldset]['value'] = array(
+            '#type' => $formtype,
+            '#title' => 'value',
+            '#description' => '&lt;tag&gt;value&lt;/tag&gt; - If left blank, the tag will be self-closing.',
+            '#options' => $options,
+            '#default_value' => $this->options[$fieldset_string][$k_fieldset]['value'],
+          );
+          foreach ($fieldset_option['attributes'] as $k_field => $field_option) {
+            $subformtype = 'select';//($field_option['formtype']) ? $field_option['formtype'] : 'select'; //same as above
+            $temp_arr[$k_fieldset][$k_field] = array(
+              '#type' => $subformtype,
+              '#title' => $field_option['title'],
+              '#description' => isset($field_option['description']) ? $field_option['description'] : '',
+              '#options' => $options,
+              '#default_value' => $this->options[$fieldset_string][$k_fieldset][$k_field],
+            );
+          }
+        } else {
+          $temp_arr[$k_fieldset] = array(
+            '#type' => $formtype,
+            '#title' => $fieldset_option['title'],
+            '#description' => isset($fieldset_option['description']) ? $fieldset_option['description'] : '',
+            '#options' => $options,
+            '#default_value' => $this->options[$fieldset_string][$k_fieldset],
+          );
+        }
+      }
+      return $temp_arr;
+  }//end function
   
 }
\ No newline at end of file
-- 
1.7.4

