From 964a03c9bfc8c5be5326821eb9fc7a32fcf9f251 Mon Sep 17 00:00:00 2001
From: Roberto Montero <roberto.montero@achieveinternet.com>
Date: Fri, 7 Jan 2011 17:52:41 -0800
Subject: [PATCH] Reapplied Nick's patch for clickpath allowing us to do reverse matching of paths

---
 .../all/modules/contrib/clickpath/clickpath.module |   91 +++++++++++++++-----
 1 files changed, 69 insertions(+), 22 deletions(-)

diff --git a/sites/all/modules/contrib/clickpath/clickpath.module b/sites/all/modules/contrib/clickpath/clickpath.module
index dba4dd7..fa3c2b0 100644
--- a/sites/all/modules/contrib/clickpath/clickpath.module
+++ b/sites/all/modules/contrib/clickpath/clickpath.module
@@ -36,6 +36,9 @@ function clickpath_menu() {
   return $items;
 }
 
+/**
+ * Form with administrative settings for clickpath.
+ */
 function clickpath_admin_settings() {
   $form = array();
 
@@ -44,7 +47,7 @@ function clickpath_admin_settings() {
     '#title' => t('Number of paths to save'),
     '#default_value' => variable_get('clickpath_count', 5),
   );
-  
+
   $form['clickpath_title_length'] = array(
     '#type' => 'textfield',
     '#title' => t('Limit titles to a specific length'),
@@ -59,6 +62,13 @@ function clickpath_admin_settings() {
     '#description' => $description,
   );
 
+  $form['clickpath_reverse_ignore'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Reverse ignore filter'),
+    '#default_value' => variable_get('clickpath_reverse_ignore', FALSE),
+    '#description' => t('Checking this box will reverse the ignore list above, thus only collecting the paths that match that pattern'),
+  );
+
   $form['clickpath_home_title'] = array(
     '#type' => 'textfield',
     '#title' => t('Title to display for frontpage'),
@@ -72,12 +82,15 @@ function clickpath_admin_settings() {
     '#default_value' => variable_get('clickpath_breadcrumb', FALSE),
     '#description' => t('Replace the standard Drupal breadcrumb trail with a list of the most recent pages a user has visited. If this option is used, ignoring paths is NOT recommended.'),
   );
-  
+
   $form['#validate'][] = 'clickpath_admin_settings_validate';
 
   return system_settings_form($form);
 }
 
+/**
+ * Administrative settings validation.
+ */
 function clickpath_admin_settings_validate($form, &$form_state) {
   if (!is_numeric($form_state['values']['clickpath_count'])) {
     form_set_error('clickpath_count', t('Number of paths to save must be a positive number.'));
@@ -89,31 +102,46 @@ function clickpath_admin_settings_validate($form, &$form_state) {
 
 /**
  * Implementation of hook_block.
- * 
+ *
  * Exposes a block containing the current user's most recently visited
  * pages -- the path they've taken through the site.
  */
 function clickpath_block($op = 'list', $delta = 0) {
-  global $user;
   if ($op == 'list') {
-    $block[0]['info'] = t('Recently visited pages');
-    return $block;
+    $blocks = array();
+    $blocks['clickpath'] = array(
+      'info' => t('Recently visited pages'),
+      'cache' => BLOCK_NO_CACHE,
+    );
+    return $blocks;
   }
   else if ($op == 'view') {
     if (user_access('view clickpath block') && $links = clickpath_get_paths()) {
+      $block = array();
       $list = array();
+      $links = clickpath_get_paths();
       $links = array_reverse($links, TRUE);
       foreach ($links as $path => $title) {
         $list[] = l(_clickpath_truncate_title($title), $path, array('html' => TRUE));
       }
 
       $block['subject'] = t('Recently visited pages');
-      $block['content'] = theme('item_list', $list);
+      if (empty($list)) {
+        $block['content'] = t('You have not browsed any documents yet');
+      }
+      else {
+        $block['content'] = theme('item_list', $list);
+      }
+
       return $block;
     }
   }
 }
 
+/**
+ * Function that replaces the default breadcrumb with clickpath's breadcrumb
+ * if the settings call for it.
+ */
 function clickpath_preprocess_page(&$variables) {
   if (variable_get('clickpath_breadcrumb', FALSE)) {
     $list = array();
@@ -127,35 +155,42 @@ function clickpath_preprocess_page(&$variables) {
 }
 
 /**
- * Implementation of hook_exit.
- *
- * Saves the path a user visited on page exit.
+ * Returns an array of visited pages.
+ * An empty array is returned if no pages have been collected.
+ * Array is stored in $_SESSION.
  */
-function clickpath_exit() {
-  global $user;
-  if (function_exists('drupal_get_path_alias')) {
-    clickpath_save_path($_GET['q']);
-  }
-}
-
 function clickpath_get_paths() {
   return empty($_SESSION['clickpath']) ? array() : $_SESSION['clickpath'];
 }
 
+/**
+ * Function that determines whether the current path is the frontpage. Boolean.
+ */
 function _clickpath_path_is_frontpage($path) {
   return $path == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
 }
 
+/**
+ * Function that truncates the maximum length of the titles to store.
+ */
 function _clickpath_truncate_title($title) {
   $length = variable_get('clickpath_title_length', 20);
+  $title = strip_tags($title);
   if ($length > 0 && strlen($title) > $length) {
     $title = substr($title, 0, $length) . '&hellip;';
   }
   return $title;
 }
 
-function clickpath_save_path($path) {
+/**
+ * Function that takes the current path and processes it for storage if valid.
+ */
+function clickpath_save_path($path = NULL) {
+  if ($path == NULL) {
+    return FALSE;
+  }
   $path_pattern = variable_get('clickpath_ignore_list', "admin*\nnode/*/*");
+  $reverse_pattern = (bool)variable_get('clickpath_reverse_ignore', FALSE);
 
   // Match path if necessary
   if (!empty($path_pattern)) {
@@ -165,25 +200,29 @@ function clickpath_save_path($path) {
     if ($aliased_path != $_GET['q']) {
       $page_match = $page_match || drupal_match_path($path, $path_pattern);
     }
-    
+
     // Since it's a list of patterns to ignore, flip it.
     $page_match = !$page_match;
   }
   else {
     $page_match = TRUE;
   }
-  
+
+    if ($reverse_pattern == TRUE) {
+      $page_match = !$page_match;
+    }
+
   if ($page_match) {
     if (_clickpath_path_is_frontpage($path)) {
       if ($home_title = variable_get('clickpath_home_title', 'Home')) {
-        $title = $home_title;    
+        $title = $home_title;
       } else {
         $title = variable_get('site_name', 'Home');
       }
     } else {
       $title = drupal_get_title();
     }
-    
+
     $clickpath = clickpath_get_paths();
     if (empty($clickpath[$path])) {
       $clickpath[$path] = $title;
@@ -197,4 +236,12 @@ function clickpath_save_path($path) {
     }
     $_SESSION['clickpath'] = $clickpath;
   }
+}
+
+/**
+ * Implementation of hook_exit().
+ * Saves the path a user visited on page exit.
+ */
+function clickpath_exit() {
+  clickpath_save_path($_GET['q']);
 }
\ No newline at end of file
-- 
1.7.2

