From 658f8cdd29784e0ce2d88ad5a62d8b2d79609080 Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Tue, 15 Feb 2011 10:41:00 -0500
Subject: [PATCH] - Re-work render to use fields.
 - Let operation revision, person and attribution views field handlers output plain text instead of html markup based on an field handler option.

---
 ...control_handler_field_operation_attribution.inc |    6 +-
 ...rsioncontrol_handler_field_operation_person.inc |   21 ++++-
 ...ioncontrol_handler_field_operation_revision.inc |   19 +++
 .../versioncontrol_plugin_row_operation_rss.inc    |  121 ++++++++++++--------
 includes/views/versioncontrol.views.inc            |    2 +-
 5 files changed, 118 insertions(+), 51 deletions(-)

diff --git includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc
index 7a70d67..ec0d55f 100644
--- includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc
+++ includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc
@@ -57,7 +57,11 @@ class versioncontrol_handler_field_operation_attribution extends versioncontrol_
     $this->aliases['person_username'] = 'committer_username';
     $values->{$this->aliases['person_username']} = $this->usernames[$committer_uid];
     $committer = parent::render($values);
-    return t('<span class="authored-by">authored by !author</span>, <span class="committed-by">committed by !committer</span>', array(
+    if (empty($this->options['plain_text_output'])) {
+      return t('<span class="authored-by">authored by !author</span>, <span class="committed-by">committed by !committer</span>', array(
+        '!author' => $author, '!committer' => $committer));
+    }
+    return t('authored by !author, committed by !committer', array(
       '!author' => $author, '!committer' => $committer));
   }
 
diff --git includes/views/handlers/versioncontrol_handler_field_operation_person.inc includes/views/handlers/versioncontrol_handler_field_operation_person.inc
index 5668aad..1dd5aab 100644
--- includes/views/handlers/versioncontrol_handler_field_operation_person.inc
+++ includes/views/handlers/versioncontrol_handler_field_operation_person.inc
@@ -7,6 +7,22 @@
  */
 class versioncontrol_handler_field_operation_person extends views_handler_field {
 
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['plain_text_output'] = array('default' => 0, 'translatable' => TRUE);
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    $form['plain_text_output'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Plain text output'),
+      '#default_value' => $this->options['plain_text_output'],
+      '#description' => t('Avoid markup and links on this field output.'),
+    );
+  }
+
   function render($values) {
     if (empty($values->{$this->aliases['person_uid']})) {
       return $values->{$this->field_alias};
@@ -26,7 +42,10 @@ class versioncontrol_handler_field_operation_person extends views_handler_field
           $account->name = $values->{$this->aliases['person_username']};
         }
       }
-      return theme('username', $account);
+      if (empty($this->options['plain_text_output'])) {
+        return theme('username', $account);
+      }
+      return $account->name;
     }
   }
 
diff --git includes/views/handlers/versioncontrol_handler_field_operation_revision.inc includes/views/handlers/versioncontrol_handler_field_operation_revision.inc
index f86613f..da22a39 100644
--- includes/views/handlers/versioncontrol_handler_field_operation_revision.inc
+++ includes/views/handlers/versioncontrol_handler_field_operation_revision.inc
@@ -24,12 +24,31 @@ class versioncontrol_handler_field_operation_revision extends views_handler_fiel
     return $this->repos[$repo_id];
   }
 
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['plain_text_output'] = array('default' => 0, 'translatable' => TRUE);
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    $form['plain_text_output'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Plain text output'),
+      '#default_value' => $this->options['plain_text_output'],
+      '#description' => t('Avoid markup and links on this field output.'),
+    );
+  }
+
   function render($values) {
     $revision = $values->{$this->field_alias};
     $vcs = $values->{$this->aliases['vcs']};
     $repo_id = $values->{$this->aliases['repo_id']};
     $revision = $this->backends[$vcs]->formatRevisionIdentifier($revision, 'short');
     $repo = $this->getRepository($vcs, $repo_id);
+    if (!empty($this->options['plain_text_output'])) {
+      return check_plain($revision);
+    }
     $url_handler = $repo->getUrlHandler();
     $link = $url_handler->getCommitViewUrl($revision);
     if (!empty($link)) {
diff --git includes/views/plugins/versioncontrol_plugin_row_operation_rss.inc includes/views/plugins/versioncontrol_plugin_row_operation_rss.inc
index 65f3322..fe1e359 100644
--- includes/views/plugins/versioncontrol_plugin_row_operation_rss.inc
+++ includes/views/plugins/versioncontrol_plugin_row_operation_rss.inc
@@ -10,81 +10,88 @@
  * object and formats it as an RSS item.
  */
 class versioncontrol_plugin_row_operation_rss extends views_plugin_row {
-  public $backends = NULL;
-  public $operations = array();
   // Basic properties that let the row style follow relationships.
   public $base_table = 'versioncontrol_operations';
   public $base_field = 'vc_op_id';
   public $revision_alias = '';
   public $repo_id_alias = '';
-
-  function construct() {
-    parent::construct();
-    $this->backends = versioncontrol_get_backends();
-  }
-
-  function pre_render($results) {
-    $vc_op_ids = array();
-    foreach ($results as $result) {
-      $vc_op_ids[] = $result->{$this->base_field};
-    }
-    $this->operations = versioncontrol_operation_load_multiple($vc_op_ids);
-  }
-
   function query() {
     parent::query();
     $this->revision_alias = $this->view->query->add_field($this->base_table, 'revision');
     $this->repo_id_alias = $this->view->query->add_field($this->base_table, 'repo_id');
   }
 
-  function option_definition() {
-    $options = parent::option_definition();
-
-    $set = versioncontrol_get_views_set('individual_commit_view');
-    $view_name = $set->getViewName();
-
-    $options['single_operation_view'] = array('default' => $view_name ? $view_name : '');
-
-    return $options;
-  }
-
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
-    $all_views = drupal_map_assoc(array_keys(views_get_all_views()));
 
-    $form['single_operation_view'] = array(
+    $view_fields = array();
+    foreach ($this->display->handler->get_handlers('field') as $field => $handler) {
+      $view_fields[$field] = $handler->ui_name();
+    }
+
+    $form['title_field'] = array(
+      '#type' => 'select',
+      '#title' => t('Title field'),
+      '#description' => t('The field that is going to be used as the RSS item title for each row.'),
+      '#options' => $view_fields,
+      '#default_value' => $this->options['title_field'],
+    );
+    $form['description_field'] = array(
       '#type' => 'select',
-      '#title' => t('Display type'),
-      '#description' => t('The view that is going to be used on the description for of the RSS item.'),
-      '#options' => $all_views,
-      '#default_value' => $this->options['single_operation_view'],
+      '#title' => t('Description field'),
+      '#description' => t('The field that is going to be used as the RSS item description for each row.'),
+      '#options' => $view_fields,
+      '#default_value' => $this->options['description_field'],
     );
+    $form['creator_field'] = array(
+      '#type' => 'select',
+      '#title' => t('Creator field'),
+      '#description' => t('The field that is going to be used as the RSS item creator for each row.'),
+      '#options' => $view_fields,
+      '#default_value' => $this->options['creator_field'],
+    );
+    $form['date_field'] = array(
+      '#type' => 'select',
+      '#title' => t('Publication date field'),
+      '#description' => t('The field that is going to be used as the RSS item pubDate for each row. It need to be in RFC 2822 format.'),
+      '#options' => $view_fields,
+      '#default_value' => $this->options['date_field'],
+    );
+  }
+
+  function validate() {
+    $errors = parent::validate();
+    $required_options = array('title_field', 'description_field', 'creator_field', 'date_field');
+    foreach ($required_options as $required_option) {
+      if (empty($this->options[$required_option])) {
+        $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
+        break;
+      }
+    }
+    return $errors;
   }
 
   function render($row) {
     global $base_url;
-    $vc_op_id = $row->{$this->field_alias};
-    $operation = $this->operations[$vc_op_id];
-    $description_view = $this->options['single_operation_view'];
-
-    $view = views_get_view($description_view);
-    // Basic logic here borrowed from views_embed_view().
-    if (!$view || !$view->access('default')) {
-      return '';
+    //TODO make sure this is the right way to deal with row_index
+    static $row_index;
+    if (!isset($row_index)) {
+      $row_index = 0;
     }
-    $repo_id = $row->{$this->repo_id_alias};
-    $revision = $row->{$this->revision_alias};
-    $view_output = $view->preview('default', array($repo_id, $revision));
+    $vc_op_id  = $row->{$this->field_alias};
+    $repo_id   = $row->{$this->repo_id_alias};
+    $revision  = $row->{$this->revision_alias};
 
     $item = new stdClass();
-    $item->title = $view->get_title();
+    $item->title = $this->get_field($row_index, $this->options['title_field']);
+    //TODO convert into another expected field
     $item->link = url(sprintf('commitlog/commit/%s/%s', $repo_id, $revision), array('absolute' => TRUE));
-    $item->description = $view_output;
+    $item->description = $this->get_field($row_index, $this->options['description_field']);
     $item->elements = array(
-      array('key' => 'pubDate', 'value' => gmdate('r', $operation->date)),
+      array('key' => 'pubDate', 'value' => $this->get_field($row_index, $this->options['date_field'])),
       array(
         'key' => 'dc:creator',
-        'value' => $operation->author,
+        'value' => $this->get_field($row_index, $this->options['creator_field']),
         'namespace' => array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'),
       ),
       array(
@@ -94,6 +101,8 @@ class versioncontrol_plugin_row_operation_rss extends views_plugin_row {
       ),
     );
 
+    $row_index++;
+
     foreach ($item->elements as $element) {
       if (isset($element['namespace'])) {
         $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $element['namespace']);
@@ -102,4 +111,20 @@ class versioncontrol_plugin_row_operation_rss extends views_plugin_row {
 
     return theme($this->theme_functions(), $this->view, $this->options, $item, $this->field_alias);
   }
+
+  /**
+   * Convinience method  to retrieve field output.
+   *
+   * @param $index
+   *   The index count of the row as expected by views_plugin_style::get_field().
+   * @param $field_id
+   *   The ID assigned to the required field in the display.
+   */
+  function get_field($index, $field_id) {
+    if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
+      return '';
+    }
+    return $this->view->style_plugin->get_field($index, $field_id);
+  }
+
 }
diff --git includes/views/versioncontrol.views.inc includes/views/versioncontrol.views.inc
index c472935..a12aef5 100755
--- includes/views/versioncontrol.views.inc
+++ includes/views/versioncontrol.views.inc
@@ -664,7 +664,7 @@ function versioncontrol_views_plugins() {
         'theme path' => drupal_get_path('module', 'versioncontrol') . '/includes',
         'base' => array('versioncontrol_operations'), // only works with 'versioncontrol_operations' as base.
         'uses options' => TRUE,
-        'uses fields' => FALSE,
+        'uses fields' => TRUE,
         'type' => 'feed',
         'help topic' => 'style-node-rss',
       ),
-- 
1.7.2.3

