From b15634d5006be239b7c2f77304ef350b57fa863d Mon Sep 17 00:00:00 2001
From: Pasqualle <csuthyb@gmail.com>
Date: Sun, 22 Jan 2012 15:43:10 +0100
Subject: #1413208 apply view access

---
 taxonomy_views_switcher.info    |    2 +-
 taxonomy_views_switcher.install |   11 ++-
 taxonomy_views_switcher.module  |  199 ++++++++++++++++++++++-----------------
 3 files changed, 123 insertions(+), 89 deletions(-)

diff --git a/taxonomy_views_switcher.info b/taxonomy_views_switcher.info
index a31a2b4..bb50c36 100644
--- a/taxonomy_views_switcher.info
+++ b/taxonomy_views_switcher.info
@@ -1,7 +1,7 @@
-
 name = Taxonomy Views Switcher
 description = Allows different views to be set for different taxonomy terms.
 core = 7.x
 dependencies[] = views
 dependencies[] = taxonomy
 
+;http://drupal.org/sandbox/simg/1106944
\ No newline at end of file
diff --git a/taxonomy_views_switcher.install b/taxonomy_views_switcher.install
index b6f4e7f..0ab18cd 100644
--- a/taxonomy_views_switcher.install
+++ b/taxonomy_views_switcher.install
@@ -1,4 +1,12 @@
 <?php
+/**
+ * @file taxonomy_views_switcher.install
+ * Contains install and update functions for taxonomy_views_switcher.
+ */
+
+/**
+ * Implements hook_install().
+ */
 function taxonomy_views_switcher_install() {
   // Taxonomy views switcher should run after Views,
   // so that TVS hook_menu_alter will override 'taxonomy/term/%' view
@@ -9,8 +17,7 @@ function taxonomy_views_switcher_install() {
     ->fetchField();
 
   db_update('system')
-    ->fields(array('weight' => $views_weight+1))
+    ->fields(array('weight' => $views_weight +1))
     ->condition('name', 'taxonomy_views_switcher')
     ->execute();
 }
-?>
diff --git a/taxonomy_views_switcher.module b/taxonomy_views_switcher.module
index 64f0efb..5a4c3dc 100644
--- a/taxonomy_views_switcher.module
+++ b/taxonomy_views_switcher.module
@@ -1,129 +1,156 @@
 <?php
 
+/**
+ * Implements hook_menu_alter().
+ */
 function taxonomy_views_switcher_menu_alter(&$items) {
-  $items['taxonomy/term/%']['page callback']    = 'taxonomy_views_switcher_render_view';
-  $items['taxonomy/term/%']['page arguments']   = array(2);
-  $items['taxonomy/term/%']['access callback']  = 'taxonomy_views_switcher_view_access';
-  $items['taxonomy/term/%']['access arguments'] = array(2);
+  $items['taxonomy/term/%']['page callback'] = 'taxonomy_views_switcher_render_view';
+  $items['taxonomy/term/%']['page arguments'] = array(2);
+  $items['taxonomy/term/%']['access callback'] = 'user_access';
+  $items['taxonomy/term/%']['access arguments'] = array('access content');
 }
 
-function taxonomy_views_switcher_render_view($tid = '', $depth = 0, $op = 'page'){
+/**
+ * Implements hook_views_pre_build().
+ */
+function taxonomy_views_switcher_views_pre_build(&$view) {
+  // Disable direct access to our switch views.
+  if (arg(0) != 'switch') {
+    return;
+  }
+  //$view->disabled = TRUE;
+  drupal_not_found();
+  drupal_goto();
+}
 
+/**
+ * Render taxonomy listing page as a view.
+ */
+function taxonomy_views_switcher_render_view($tid = '', $depth = 0, $op = 'page') {
   $target = taxonomy_views_switcher_get_view();
   if (isset($target['view'])) {
-  	$view = views_get_view($target['view']);
+    $view = views_get_view($target['view']);
+    if (!$view || !$view->access($target['display'])) {
+      drupal_access_denied();
+      return;
+    }
     $view->set_display($target['display']);
     $view->set_arguments(array($tid, $depth));
     $view->build();
-    drupal_set_title(str_replace('&amp;','&',$view->get_title()));
+    drupal_set_title(str_replace('&amp;', '&', $view->get_title()));
     return $view->preview();
   }
 
-  // if no suitable view is found, use core drupal taxonomy/term/% page
+  // If no suitable view is found, use core drupal taxonomy/term/% page
   module_load_include('inc', 'taxonomy', 'taxonomy.pages');
   return taxonomy_term_page(taxonomy_term_load($tid));
 }
 
-function taxonomy_views_switcher_view_access($term){
-  //TODO: add access control if necessary
-  return true;
-}
-
-
-function taxonomy_views_switcher_get_view(){
-  $view_path = request_path(); //use the request path, not the aliased path and escape the forward slashes
-
+/**
+ * Get suitable view to replace the taxonomy listing page.
+ */
+function taxonomy_views_switcher_get_view() {
+  // Use the request path, not the aliased path and escape the forward slashes.
+  $view_path = request_path();
 
   if ($cached = cache_get("taxonomy_views_switcher:$view_path")) {
     return $cached->data;
-  }   
-  
+  }
+
   $target_view = array(); // default target view;
   $default_view = array(); // default view if no suitable target found
 
   $views = views_get_all_views();
   foreach ($views as $view) {
-    if (isset($view->disabled) && $view->disabled) continue;
+    if (isset($view->disabled) && $view->disabled) {
+      continue;
+    }
 
-    foreach ($view->display as $name => $display) 
-    {
+    foreach ($view->display as $name => $display) {
       // Each view can have multiple displays. These may have a path specified.
       $plugin = $display->display_plugin;
 
-      // It has a path or it is not eligable.
-      if (!in_array($plugin, array('block', 'feed', 'default'))) {
-        if (!isset($display->display_options['path'])) continue;
+      // Wrong display type.
+      if (in_array($plugin, array('block', 'feed', 'default'))) {
+        continue;
+      }
+
+      // Does not have path.
+      if (!isset($display->display_options['path'])) {
+        continue;
+      }
 
-        $path = $display->display_options['path']; // this display might be suitable
-        if (!(strpos($path,'switch/') == 0)) continue; // path must start with "switch/" otherwise display is of no use
-        $path = preg_replace('/^(switch)?\//','',$path);
-        $path = preg_replace('/(\/\%)?$/','',$path);
+      $path = $display->display_options['path']; // this display might be suitable
+      if (!(strpos($path, 'switch/') == 0)) {
+        // Path must start with "switch/" otherwise display is of no use.
+        continue;
+      }
+      $path = preg_replace('/^(switch)?\//', '', $path);
+      $path = preg_replace('/(\/\%)?$/', '', $path);
+
+      // Set default view - check if default taxonomy/term view is found
+      if ($path == 'taxonomy/term') {
+        $default_view = array(
+          'view' => $view->name,
+          'display' => $name,
+          'path' => $path
+        );
+      }
 
-        // Set default view - check if default taxonomy/term view is found
-        if ($path == 'taxonomy/term') {
-          $default_view = array(
-            'view'    => $view->name,
-            'display' => $name,
-            'path' => $path
-          );
+      // Set target view - match the view to the current request_path
+      if (preg_match('/' . str_replace('/', '\/', $path) . '/', $view_path)) {
+        // This path matches, now we want to see if it's a more specific (ie deeper) path than any existing ones)
+        if (!isset($target_view['view']) || substr_count($path, '/') > substr_count($target_view['path'], '/')) {
+          if (empty($target_view['match_priority']) || $target_view['match_priority'] <= 1) {
+            $target_view = array(
+              'view' => $view->name,
+              'display' => $name,
+              'path' => $path,
+              'match_priority' => '1'
+            );
+          }
         }
-
-        // Set target view - match the view to the current request_path
-        if (preg_match('/'.str_replace('/','\/',$path).'/', $view_path)) {
-          // this path matches, now we want to see if it's a more specific (ie deeper) path than any existing ones)
-          if (!isset($target_view['view']) || substr_count($path,'/') > substr_count($target_view['path'],'/')) {
-            if (empty($target_view['match_priority']) || $target_view['match_priority'] <= 1){
-              $target_view = array(
-                'view'    => $view->name,
-                'display' => $name,
-                'path' => $path,
-                'match_priority' => '1'
-              );
-            }
+      }
+      // See if there is a path matching taxonomy vid/machine_name.
+      elseif (substr($path, 0, 4) == 'vid/') {
+        $term = taxonomy_term_load(arg(2));
+        // Match vid or vocabulary machine name.
+        if ($path == 'vid/' . $term->vid || $path == 'vid/' . $term->vocabulary_machine_name) {
+          if (empty($target_view['match_priority']) || $target_view['match_priority'] <= 2) {
+            $target_view = array(
+              'view' => $view->name,
+              'display' => $name,
+              'path' => $path,
+              'match_priority' => '2'
+            );
           }
-        } 
-        // see if there is a path matching taxonomy vid/machine_name
-        elseif (substr($path,0,4) == 'vid/') {
-          $term = taxonomy_term_load(arg(2));
-          // match vid or vocabulary machine name
-          if ($path == 'vid/'.$term->vid || $path == 'vid/'.$term->vocabulary_machine_name) {
-            if (empty($target_view['match_priority']) || $target_view['match_priority'] <= 2){
-              $target_view = array(
-                'view'    => $view->name,
-                'display' => $name,
-                'path' => $path,
-                'match_priority' => '2'
-              );
-            }
-          }        
         }
-        // see if there is a path matching taxonomy tid/term name
-        elseif (substr($path,0,4) == 'tid/') {
-          $term = taxonomy_term_load(arg(2));
-          // match tid or taxonomy term name
-          if ($path == 'tid/'.$term->tid || $path == 'tid/'.$term->name) {
-            if (empty($target_view['match_priority']) || $target_view['match_priority'] <= 3){
-              $target_view = array(
-                'view'    => $view->name,
-                'display' => $name,
-                'path' => $path,
-                'match_priority' => '3'
-              );
-            }
-            break; // for now, a term match is totally specific, so end matching process 
-          }        
+      }
+      // See if there is a path matching taxonomy tid/term name
+      elseif (substr($path, 0, 4) == 'tid/') {
+        $term = taxonomy_term_load(arg(2));
+        // Match tid or taxonomy term name.
+        if ($path == 'tid/' . $term->tid || $path == 'tid/' . $term->name) {
+          if (empty($target_view['match_priority']) || $target_view['match_priority'] <= 3) {
+            $target_view = array(
+              'view' => $view->name,
+              'display' => $name,
+              'path' => $path,
+              'match_priority' => '3'
+            );
+          }
         }
+        break; // For now, a term match is totally specific, so end matching process.
       }
     }
   }
-  if (isset($target_view['view']))
-  {
-    cache_set("taxonomy_views_switcher:$view_path",$target_view);
+
+  if (isset($target_view['view'])) {
+    cache_set("taxonomy_views_switcher:$view_path", $target_view);
     return $target_view;
-  } else {
-    cache_set("taxonomy_views_switcher:$view_path",$default_view);
+  }
+  else {
+    cache_set("taxonomy_views_switcher:$view_path", $default_view);
     return $default_view;
   }
 }
-
-
-- 

