| 1 |
<?php
|
| 2 |
// $Id: clickpath.module,v 1.3 2008/12/17 23:45:45 coltrane Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Saves and displays the user's most recently visited links in the site.
|
| 7 |
*
|
| 8 |
* The goal is to track the path a user has taken through the site, and
|
| 9 |
* display it in a sidebar block or (optionally) the breadcrumb trail.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_perm.
|
| 14 |
*
|
| 15 |
* Exposes permission to see one's own clickpath.
|
| 16 |
*/
|
| 17 |
function clickpath_perm() {
|
| 18 |
return array('view clickpath block');
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_menu.
|
| 23 |
*
|
| 24 |
* Exposes the Clickpath administration page.
|
| 25 |
*/
|
| 26 |
function clickpath_menu() {
|
| 27 |
$items['admin/settings/clickpath'] = array(
|
| 28 |
'title' => 'Clickpath settings',
|
| 29 |
'description' => 'Configure what information will be displayed about the paths users take when clicking through the site.',
|
| 30 |
'page callback' => 'drupal_get_form',
|
| 31 |
'page arguments' => array('clickpath_admin_settings'),
|
| 32 |
'access arguments' => array('administer site configuration'),
|
| 33 |
'type' => MENU_NORMAL_ITEM,
|
| 34 |
);
|
| 35 |
|
| 36 |
return $items;
|
| 37 |
}
|
| 38 |
|
| 39 |
function clickpath_admin_settings() {
|
| 40 |
$form = array();
|
| 41 |
|
| 42 |
$form['clickpath_count'] = array(
|
| 43 |
'#type' => 'textfield',
|
| 44 |
'#title' => t('Number of paths to save'),
|
| 45 |
'#default_value' => variable_get('clickpath_count', 5),
|
| 46 |
);
|
| 47 |
|
| 48 |
$form['clickpath_title_length'] = array(
|
| 49 |
'#type' => 'textfield',
|
| 50 |
'#title' => t('Limit titles to a specific length'),
|
| 51 |
'#default_value' => variable_get('clickpath_title_length', 20),
|
| 52 |
);
|
| 53 |
|
| 54 |
$description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
|
| 55 |
$form['clickpath_ignore_list'] = array(
|
| 56 |
'#type' => 'textarea',
|
| 57 |
'#title' => t('Paths to ignore'),
|
| 58 |
'#default_value' => variable_get('clickpath_ignore_list', "admin*\nnode/*/*"),
|
| 59 |
'#description' => $description,
|
| 60 |
);
|
| 61 |
|
| 62 |
$form['clickpath_breadcrumb'] = array(
|
| 63 |
'#type' => 'checkbox',
|
| 64 |
'#title' => t('Override breacrumb trail'),
|
| 65 |
'#default_value' => variable_get('clickpath_breadcrumb', FALSE),
|
| 66 |
'#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.'),
|
| 67 |
);
|
| 68 |
|
| 69 |
$form['#validate'][] = 'clickpath_admin_settings_validate';
|
| 70 |
|
| 71 |
return system_settings_form($form);
|
| 72 |
}
|
| 73 |
|
| 74 |
function clickpath_admin_settings_validate($form, &$form_state) {
|
| 75 |
if (!is_numeric($form_state['values']['clickpath_count'])) {
|
| 76 |
form_set_error('clickpath_count', t('Number of paths to save must be a positive number.'));
|
| 77 |
}
|
| 78 |
if (!empty($form_state['values']['clickpath_title_length']) && !is_numeric($form_state['values']['clickpath_title_length'])) {
|
| 79 |
form_set_error('clickpath_title_length', t('Title length must be a positive number.'));
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
* Implementation of hook_block.
|
| 85 |
*
|
| 86 |
* Exposes a block containing the current user's most recently visited
|
| 87 |
* pages -- the path they've taken through the site.
|
| 88 |
*/
|
| 89 |
function clickpath_block($op = 'list', $delta = 0) {
|
| 90 |
global $user;
|
| 91 |
if ($op == 'list') {
|
| 92 |
$block[0]['info'] = t('Recently visited pages');
|
| 93 |
return $block;
|
| 94 |
}
|
| 95 |
else if ($op == 'view') {
|
| 96 |
if (user_access('view clickpath block') && $links = clickpath_get_paths()) {
|
| 97 |
$list = array();
|
| 98 |
$links = array_reverse($links, TRUE);
|
| 99 |
foreach ($links as $path => $title) {
|
| 100 |
$list[] = l(_clickpath_truncate_title($title), $path, array('html' => TRUE));
|
| 101 |
}
|
| 102 |
|
| 103 |
$block['subject'] = t('Recently visited pages');
|
| 104 |
$block['content'] = theme('item_list', $list);
|
| 105 |
return $block;
|
| 106 |
}
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
function clickpath_preprocess_page(&$variables) {
|
| 111 |
if (variable_get('clickpath_breadcrumb', FALSE)) {
|
| 112 |
$list = array();
|
| 113 |
$links = clickpath_get_paths();
|
| 114 |
foreach ($links as $path => $title) {
|
| 115 |
$list[] = l(_clickpath_truncate_title($title), $path, array('html' => TRUE));
|
| 116 |
}
|
| 117 |
drupal_set_breadcrumb($list);
|
| 118 |
$variables['breadcrumb'] = theme('breadcrumb', $list);
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* Implementation of hook_exit.
|
| 124 |
*
|
| 125 |
* Saves the path a user visited on page exit.
|
| 126 |
*/
|
| 127 |
function clickpath_exit() {
|
| 128 |
global $user;
|
| 129 |
if ($user->uid) {
|
| 130 |
clickpath_save_path($_GET['q']);
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
| 134 |
function clickpath_get_paths() {
|
| 135 |
return empty($_SESSION['clickpath']) ? array() : $_SESSION['clickpath'];
|
| 136 |
}
|
| 137 |
|
| 138 |
function _clickpath_path_is_frontpage($path) {
|
| 139 |
return $path == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
|
| 140 |
}
|
| 141 |
|
| 142 |
function _clickpath_truncate_title($title) {
|
| 143 |
$length = variable_get('clickpath_title_length', 20);
|
| 144 |
if ($length > 0 && strlen($title) > $length) {
|
| 145 |
$title = substr($title, 0, $length) . '…';
|
| 146 |
}
|
| 147 |
return $title;
|
| 148 |
}
|
| 149 |
|
| 150 |
function clickpath_save_path($path) {
|
| 151 |
$path_pattern = variable_get('clickpath_ignore_list', "admin*\nnode/*/*");
|
| 152 |
|
| 153 |
// Match path if necessary
|
| 154 |
if (!empty($path_pattern)) {
|
| 155 |
$aliased_path = drupal_get_path_alias($path);
|
| 156 |
// Compare with the internal and path alias (if any).
|
| 157 |
$page_match = drupal_match_path($aliased_path, $path_pattern);
|
| 158 |
if ($aliased_path != $_GET['q']) {
|
| 159 |
$page_match = $page_match || drupal_match_path($path, $path_pattern);
|
| 160 |
}
|
| 161 |
|
| 162 |
// Since it's a list of patterns to ignore, flip it.
|
| 163 |
$page_match = !$page_match;
|
| 164 |
}
|
| 165 |
else {
|
| 166 |
$page_match = TRUE;
|
| 167 |
}
|
| 168 |
|
| 169 |
if ($page_match) {
|
| 170 |
$title = _clickpath_path_is_frontpage($path) ? t('Home') : drupal_get_title();
|
| 171 |
$clickpath = clickpath_get_paths();
|
| 172 |
if (empty($clickpath[$path])) {
|
| 173 |
$clickpath[$path] = $title;
|
| 174 |
while (count($clickpath) > variable_get('clickpath_count', 5)) {
|
| 175 |
array_shift($clickpath);
|
| 176 |
}
|
| 177 |
}
|
| 178 |
else {
|
| 179 |
unset($clickpath[$path]);
|
| 180 |
$clickpath[$path] = $title;
|
| 181 |
}
|
| 182 |
$_SESSION['clickpath'] = $clickpath;
|
| 183 |
}
|
| 184 |
}
|