From c994303fdfd0fd433696a80c123cec4804b6d47e Mon Sep 17 00:00:00 2001
From: Mark Carver <mark.carver@me.com>
Date: Wed, 9 May 2012 13:13:55 -0500
Subject: [PATCH] Added filter.

---
 plugins/views_plugin_argument_default_context.inc  |   49 ------
 views/views_arg_context.views.inc                  |   59 ++++++++
 views/views_arg_context_handler_filter.inc         |  160 ++++++++++++++++++++
 .../views_arg_context_plugin_argument_default.inc  |   49 ++++++
 views_arg_context.info                             |    7 +-
 views_arg_context.module                           |   18 +--
 6 files changed, 273 insertions(+), 69 deletions(-)
 delete mode 100644 plugins/views_plugin_argument_default_context.inc
 create mode 100644 views/views_arg_context.views.inc
 create mode 100644 views/views_arg_context_handler_filter.inc
 create mode 100644 views/views_arg_context_plugin_argument_default.inc

diff --git a/plugins/views_plugin_argument_default_context.inc b/plugins/views_plugin_argument_default_context.inc
deleted file mode 100644
index 2062b9f..0000000
--- a/plugins/views_plugin_argument_default_context.inc
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-// $Id: views_plugin_argument_default_context.inc,v 1.1.2.1 2010/10/25 20:27:58 brynbellomy Exp $
-/**
- * @file
- * Contains the active context argument default plugin.
- */
-
-/**
- * Default argument plugin to extract a value from the active context
- */
-class views_plugin_argument_default_context extends views_plugin_argument_default {
-  var $option_name = 'views_arg_context';
-  
-  function option_definition() {
-    $options = parent::option_definition();
-    $options['namespace'] = array('default' => '');
-    $options['attribute'] = array('default' => '');
-
-    return $options;
-  }
-
-  function options_form(&$form, &$form_state) {
-    $form['namespace'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Namespace'),
-      '#default_value' => $this->options['namespace'],
-    );
-    $form['attribute'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Attribute'),
-      '#default_value' => $this->options['attribute'],
-    );
-    $this->check_access($form);
-  }
-
-  function check_access(&$form) {
-    if (!$this->access()) {
-      foreach(array('namespace','attribute') as $elem) {
-        $form[$elem]['#disabled'] = TRUE;
-        $form[$elem]['#value'] = $form[$elem]['#default_value'];
-        $form[$elem]['#description'] .= ' <strong>' . t('Note: you do not have permission to modify this. If you change the default argument type, this setting will be lost and you will NOT be able to get it back.') . '</strong>';
-      }
-    }
-  }
-
-  function get_argument() {
-    return context_get($this->options['namespace'], $this->options['attribute']);
-  }
-}
\ No newline at end of file
diff --git a/views/views_arg_context.views.inc b/views/views_arg_context.views.inc
new file mode 100644
index 0000000..b936ecb
--- /dev/null
+++ b/views/views_arg_context.views.inc
@@ -0,0 +1,59 @@
+<?php
+
+/**
+ * Implementation of hook_views_handlers().
+ */
+function views_arg_context_views_handlers() {
+  return array(
+    'handlers' => array(
+      'views_arg_context_handler_filter' => array(
+        'parent' => 'views_handler_filter_string',
+        'path' => drupal_get_path('module', 'views_arg_context') . '/views',
+      ),
+    ),
+  );
+}
+
+/**
+ * Implements hook_views_data().
+ */
+function views_arg_context_views_data() {
+  $data['views']['context'] = array(
+    'group' => t('Context'),
+    'title' => t('Active Context'),
+    'filter' => array(
+      'help' => t('Use an active context to filter the result of the view.'),
+      'handler' => 'views_arg_context_handler_filter',
+      'allow empty' => FALSE,
+    ),
+  );
+  return $data;
+}
+
+/**
+ * Implements of hook_views_plugins
+ */
+function views_arg_context_views_plugins() {
+  return array(
+    'module' => 'views_arg_context', // This just tells our themes are elsewhere.
+    'argument default' => array(
+      'context' => array(
+        'title' => t('Active context'),
+        'handler' => 'views_arg_context_plugin_argument_default',
+        'path' => drupal_get_path('module', 'views_arg_context') . '/views',
+        'parent' => 'fixed',
+      ),
+    ),
+  );
+}
+
+/**
+ * Implements hook_views_pre_execute().
+ */
+function views_arg_context_views_pre_execute($view) {
+  // Seize control over the query plugin if a views handler requested so.
+  if (!empty($view->active_context)) {
+    // $query = new views_php_plugin_query();
+    // $query->php_wrap($view->query);
+  }
+}
diff --git a/views/views_arg_context_handler_filter.inc b/views/views_arg_context_handler_filter.inc
new file mode 100644
index 0000000..dfecaf6
--- /dev/null
+++ b/views/views_arg_context_handler_filter.inc
@@ -0,0 +1,160 @@
+<?php
+
+/**
+ * A handler to filter a view using PHP defined by the administrator.
+ *
+ * @ingroup views_filter_handlers
+ */
+class views_arg_context_handler_filter extends views_handler_filter_string {
+
+  /**
+   * Implements views_handler#can_expose().
+   */
+  function can_expose() {
+    return FALSE;
+  }
+
+  /**
+   * Implements views_object#option_definition().
+   */
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['namespace'] = array('default' => '');
+    $options['attribute'] = array('default' => '');
+    return $options;
+  }
+  
+  function ui_name($short = FALSE) {
+    if (!empty($this->options['ui_name'])) {
+      $title = check_plain($this->options['ui_name']);
+      return $title;
+    }
+    if (!empty($this->options['namespace']) && !empty($this->options['attribute'])) {
+      $title = $this->options['namespace'] . '|' . $this->options['attribute'];
+    }
+    else {
+      $title = ($short && isset($this->definition['title short'])) ? $this->definition['title short'] : $this->definition['title'];
+    }
+    return t('!group: !title', array('!group' => $this->definition['group'], '!title' => $title));
+  }
+  
+  function options_form(&$form, &$form_state) {
+    if ($this->can_expose()) {
+      $this->show_expose_button($form, $form_state);
+    }
+    $form['namespace'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Namespace'),
+      '#size' => 30,
+      '#default_value' => $this->options['namespace'],
+    );
+    $form['attribute'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Attribute'),
+      '#size' => 30,
+      '#default_value' => $this->options['attribute'],
+    );
+    $this->check_access($form);
+    
+    $form['clear_markup_start'] = array(
+      '#markup' => '<div class="clearfix">',
+    );
+    // Add the subform from operator_form().
+    $this->show_operator_form($form, $form_state);
+    // Add the subform from value_form().
+    $this->show_value_form($form, $form_state);
+    $form['clear_markup_end'] = array(
+      '#markup' => '</div>',
+    );
+    if ($this->can_expose()) {
+      // Add the subform from expose_form().
+      $this->show_expose_form($form, $form_state);
+    }
+  }
+        
+  function check_access(&$form, $option_name) {
+    if (!$this->access()) {
+      foreach(array('namespace', 'attribute', 'value') as $elem) {
+        $form[$elem]['#disabled'] = TRUE;
+        $form[$elem]['#value'] = $form[$elem]['#default_value'];
+        $form[$elem]['#description'] .= ' <strong>' . t('Note: you do not have permission to modify this. If you change the default argument type, this setting will be lost and you will NOT be able to get it back.') . '</strong>';
+      }
+    }
+  }
+  
+  function query() {
+    $info = $this->operators();
+    if (!empty($info[$this->operator]['method'])) {
+      $context = context_get($this->options['namespace'], $this->options['attribute']);
+      // The context should return a string value, if false, fail the view.
+      if ($context === FALSE || !$this->{$info[$this->operator]['method']}($context)) {
+        // Operator comparison failed, hide the view.
+        $this->view->build_info['fail'] = TRUE;
+      }
+    }
+  }
+  
+  function op_equal($context) {
+    return $this->value == $context;
+  }
+
+  function op_contains($context) {
+    return strpos($context, $this->value) !== FALSE;
+  }
+
+  function op_word($context) {
+    // Match any words
+    $words = explode(' ', $this->value);
+    if ($this->operator == 'word') {
+      foreach ($words as $word) {
+        if ($word == $context) {
+          return TRUE;
+        }
+      }
+    }
+    // Match all words
+    else {
+      $all = array();
+      foreach ($words as $word) {
+        $all[] = $word == $context;
+      }
+      return !in_array(FALSE, $all, TRUE);
+    }
+  }
+
+  function op_starts($context) {
+    return (bool) preg_match("/^$this->value/i", $context);
+  }
+
+  function op_not_starts($context) {
+    return (bool) preg_match("/^(?!$this->value).*/i", $context);
+  }
+
+  function op_ends($context) {
+    return (bool) preg_match("/$this->value$/i", $context);
+  }
+
+  function op_not_ends($context) {
+    return (bool) preg_match("/^(?!.*$this->value$).*$/i", $context);
+  }
+
+  function op_not($context) {
+    return strpos($context, $this->value) === FALSE;
+  }
+
+  function op_shorter($context) {
+    return strlen($this->value) < strlen($context);
+  }
+
+  function op_longer($context) {
+    return strlen($this->value) > strlen($context);
+  }
+
+  function op_regex($context) {
+    $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
+  }
+
+  function op_empty($context) {
+    return $this->operator == 'empty' ? empty($context) : !empty($context);
+  }
+}
\ No newline at end of file
diff --git a/views/views_arg_context_plugin_argument_default.inc b/views/views_arg_context_plugin_argument_default.inc
new file mode 100644
index 0000000..d91a5d7
--- /dev/null
+++ b/views/views_arg_context_plugin_argument_default.inc
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * @file
+ * Contains the active context argument default plugin.
+ */
+
+/**
+ * Default argument plugin to extract a value from the active context
+ */
+class views_arg_context_plugin_argument_default extends views_plugin_argument_default {
+  var $option_name = 'views_arg_context';
+  
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['namespace'] = array('default' => '');
+    $options['attribute'] = array('default' => '');
+
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    $form['namespace'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Namespace'),
+      '#default_value' => $this->options['namespace'],
+    );
+    $form['attribute'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Attribute'),
+      '#default_value' => $this->options['attribute'],
+    );
+    $this->check_access($form);
+  }
+
+  function check_access(&$form, $option_name) {
+    if (!$this->access()) {
+      foreach(array('namespace','attribute') as $elem) {
+        $form[$elem]['#disabled'] = TRUE;
+        $form[$elem]['#value'] = $form[$elem]['#default_value'];
+        $form[$elem]['#description'] .= ' <strong>' . t('Note: you do not have permission to modify this. If you change the default argument type, this setting will be lost and you will NOT be able to get it back.') . '</strong>';
+      }
+    }
+  }
+
+  function get_argument() {
+    return context_get($this->options['namespace'], $this->options['attribute']);
+  }
+}
\ No newline at end of file
diff --git a/views_arg_context.info b/views_arg_context.info
index c353a42..2052d82 100644
--- a/views_arg_context.info
+++ b/views_arg_context.info
@@ -3,10 +3,11 @@ name = Views Default Argument from Context
 description = Allows the default value for a Views argument to be specified through Context module data set using context_set.
 core = 7.x
 files[] = views_arg_context.module
-files[] = plugins/views_plugin_argument_default_context.inc
+; Views handlers
+files[] = views/views_arg_context_handler_filter.inc
+; Views plugins
+files[] = views/views_arg_context_plugin_argument_default.inc
 
 package = Views
 dependencies[] = views
 dependencies[] = context
-
-
diff --git a/views_arg_context.module b/views_arg_context.module
index 27081d0..e55f878 100644
--- a/views_arg_context.module
+++ b/views_arg_context.module
@@ -7,26 +7,10 @@
 function views_arg_context_views_api() {
   return array(
     'api' => 2.0,
+    'path' => drupal_get_path('module', 'views_arg_context') . '/views',
   );
 }
 
-/**
- * Implements of hook_views_plugins
- */
-function views_arg_context_views_plugins() {
-  $path = drupal_get_path('module', 'views_arg_context') . '/includes';
-  return array(
-    'module' => 'views_arg_context', // This just tells our themes are elsewhere.
-    'argument default' => array(
-      'context' => array(
-        'title' => t('Active context'),
-        'handler' => 'views_plugin_argument_default_context',
-        'path' => $path,
-        'parent' => 'fixed',
-      ),
-    ),
-  );
-}
 
 /**
  * Implements of hook_node_view
-- 
1.7.10

