Index: vocabindex.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/vocabindex/vocabindex.module,v
retrieving revision 1.1.2.5.2.85
diff -u -r1.1.2.5.2.85 vocabindex.module
--- vocabindex.module	17 Sep 2009 11:36:01 -0000	1.1.2.5.2.85
+++ vocabindex.module	9 Oct 2009 01:45:36 -0000
@@ -45,15 +45,12 @@
  * A VI page.
  */
 define('VOCABINDEX_VI_PAGE', 2);
-

-/**

- * Implementation of hook_perm().

- */

-function vocabindex_perm() {

-  return array(
-    'administer vocabulary index',
-    'view vocabulary index pages',
-  );

+
+/**
+ * Implementation of hook_perm().
+ */
+function vocabindex_perm() {
+  return array('administer vocabulary index');
 }
 
 /**
@@ -64,102 +61,103 @@
     $path = base_path() . 'update.php';
     drupal_set_message(t("Please proceed to !update.php to update your database to Vocabulary Index 2.", array('!update.php' => '<a href="' . $path . '">update.php</a>')), 'error');
   }
-}

-

-/**

- * Implementation of hook_menu().

- */

+}
+
+/**
+ * Implementation of hook_menu().
+ */
 function vocabindex_menu() {
   if (vocabindex_update_status() == FALSE) {
     return array();
   }
 
-  $items[_vocabindex_menu_paths('admin_main')] = array(

+  $items[_vocabindex_menu_paths('admin_main')] = array(
     'title' => 'Vocabulary Index',
-    'description' => 'Create index pages for vocabularies.',

-    'access arguments' => array('administer vocabulary index'),

+    'description' => 'Create index pages for vocabularies.',
+    'access arguments' => array('administer vocabulary index'),
     'page callback' => 'vocabindex_admin_vi',
     'page arguments' => array((string) VOCABINDEX_VI_PAGE),
-    'file' => 'vocabindex.admin.inc',

-  );

+    'file' => 'vocabindex.admin.inc',
+  );
 
   $items[_vocabindex_menu_paths('admin_pages')] = array(
-    'title' => 'Pages',

+    'title' => 'Pages',
     'access arguments' => array('administer vocabulary index'),
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'file' => 'vocabindex.admin.inc',
     'weight' => 0,
-  );

+  );
 
   $items[_vocabindex_menu_paths('admin_blocks')] = array(
     'title' => 'Blocks',
-    'description' => 'Create index blocks for vocabularies.',

-    'access arguments' => array('administer vocabulary index'),

+    'description' => 'Create index blocks for vocabularies.',
+    'access arguments' => array('administer vocabulary index'),
     'page callback' => 'vocabindex_admin_vi',
     'page arguments' => array((string) VOCABINDEX_VI_BLOCK),
     'type' => MENU_LOCAL_TASK,
     'file' => 'vocabindex.admin.inc',
     'weight' => 1,
-  );

+  );
 
-  $items[_vocabindex_menu_paths('admin_settings')] = array(

+  $items[_vocabindex_menu_paths('admin_settings')] = array(
     'title' => 'Settings',
-    'description' => 'General settings.',

+    'description' => 'General settings.',
     'access arguments' => array('administer vocabulary index'),
-    'page callback' => 'drupal_get_form',

+    'page callback' => 'drupal_get_form',
     'page arguments' => array('vocabindex_admin'),
     'type' => MENU_LOCAL_TASK,
     'file' => 'vocabindex.admin.inc',
-    'weight' => 2,

+    'weight' => 2,
   );
 
   // VI pages.
-  $vis = vocabindex_vi_load(VOCABINDEX_VI_PAGE);

+  $vis = vocabindex_vi_load(VOCABINDEX_VI_PAGE);
   foreach ($vis as $vi) {
-    $item = vocabindex_menu_base() + array(
-      'title' => $vi->name,
-      'description' => check_plain($vi->description),
-      // We need to cast out the constant to a string, because the integer will
-      // otherwise be replaced with the corresponding part of the menu path.

-      'page arguments' => array($vi->vid, (string) VOCABINDEX_VOC),

+    $item = array(
+       'title' => $vi->name,
+       'description' => check_plain($vi->description),
+       'page callback' => 'vocabindex_view_page',
+       // We need to cast out the constant to a string, because the integer will
+       // otherwise be replaced with the corresponding part of the menu path.
+      'page arguments' => array($vi->vid, (string) VOCABINDEX_VOC),
+      'access arguments' => array('access content'),
+      'type' => MENU_CALLBACK,
+      'file' => 'vocabindex.view.inc',
     );
     // The menu item for alphabetical VIs using the letter wildcard.
     if ($vi->view == VOCABINDEX_VIEW_ALPHABETICAL) {
       $arg = (int) substr_count($vi->path, '/') + 1;
       $path = $vi->path . '/%vocabindex_letter';
       $item['page arguments'][2] = $arg;
-    }

-    $items[$vi->path] = $item;

+    }
+    $items[$vi->path] = $item;
   }
-

+
   return $items;
 }
 
 /**
  * Implementation of hook_menu_alter().
- *
- * Overrides taxonomy.module's default /taxonomy/term/tid callback for term
- * index pages.
  */
 function vocabindex_menu_alter(&$items) {
-  $vis = vocabindex_vi_load(VOCABINDEX_VI_PAGE);

+  $vis = vocabindex_vi_load(VOCABINDEX_VI_PAGE);
   foreach ($vis as $vi) {
     if ($vi->view == VOCABINDEX_VIEW_FLAT) {
-      $tree = taxonomy_get_tree($vi->vid);
-      $children = vocabindex_get_children($tree);
-      foreach ($tree as $term) {
-        // Check if the current term is a parent.
-        if (isset($children[$term->tid])) {
-          $items['taxonomy/term/' . $term->tid] = vocabindex_menu_base() + array(
-            'title' => $term->name,
-            'description' => check_plain($term->description),
-            // We need to cast our integer constant to a string, because the
-            // integer will otherwise be replaced with the corresponding part
-            // of the menu path.
-            'page arguments' => array($term->tid, (string) VOCABINDEX_TERM),
-            'file path' => drupal_get_path('module', 'vocabindex'),
          );
-        }
-      }
+      $item = &$items['taxonomy/term/%'];
+
+      // Store the original configuration, so we can pass it on to our own
+      // callback.
+      $callback = $item['page callback'];
+      $arguments = $item['page arguments'];
+      $file = $item['file'];
+      $filepath = isset($item['file path']) ? $item['file path'] : drupal_get_path('module', $item['module']);
+
+      // Alter the original callback.
+      $item['page callback'] = 'vocabindex_term_page';
+      $item['page arguments'] = array_merge(array(2, $callback, $file, $filepath), $arguments);
+      $item['file'] = 'vocabindex.module';
+      $item['file path'] = drupal_get_path('module', 'vocabindex');
+      break;
     }
   }
 }
@@ -364,18 +362,6 @@
 }
 
 /**
- * Returns the basics for a VI menu item.
- */
-function vocabindex_menu_base() {
-  return array(
-    'access arguments' => array('view vocabulary index pages'),

-    'page callback' => 'vocabindex_view_page',

-    'type' => MENU_CALLBACK,
-    'file' => 'vocabindex.view.inc',

-  );
-}
-
-/**
  * Check if the latest DB updates for this module have been performed.
  */
 function vocabindex_update_status() {
@@ -395,6 +381,38 @@
 }
 
 /**
+ * Route a term page request to the right callback function.
+ *
+ * @param $tid
+ *   The TID of the term to call a callback for.
+ * @param $callback
+ *   The name of the original callback function.
+ * @param $file
+ *   The file the original term callback is in.
+ * @param $filepath
+ *   The path to $file.
+ * @param ...
+ *   Additional arguments to pass on to the callback.
+ *
+ * @return
+ *   Either an index page or the output of the original callback.
+ */
+function vocabindex_term_page($tid, $callback, $file, $filepath) {
+  $term = taxonomy_get_term($tid);
+  $vis = vocabindex_vi_load(VOCABINDEX_VI_PAGE);
+  foreach ($vis as $vi) {
+    if ($vi->vid == $term->vid) {
+      module_load_include('inc', 'vocabindex', 'vocabindex.view');
+      return vocabindex_view_page($tid, VOCABINDEX_TERM);
+    }
+  }
+
+  require_once($filepath .'/'. $file);
+  $arguments = array_slice(func_get_args(), 4);
+  return call_user_func_array($callback, $arguments);
+}
+
+/**
  * Menu path wildcard for alphabetical VIs.
  *
  * @param $letter
