| 1 |
<?php
|
| 2 |
// $Id: taxonomy_explorer.module,v 1.5 2007/01/22 14:06:30 arto Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides a hierarchical browser, similar to Windows Explorer or OS X
|
| 7 |
* Finder, for browsing nodes organized in a taxonomy tree.
|
| 8 |
*/
|
| 9 |
|
| 10 |
define('TAXONOMY_EXPLORER_PATH', drupal_get_path('module', 'taxonomy_explorer'));
|
| 11 |
define('TAXONOMY_EXPLORER_HIDE_EMPTY', variable_get('taxonomy_explorer_hide_empty', FALSE));
|
| 12 |
|
| 13 |
//////////////////////////////////////////////////////////////////////////////
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_help().
|
| 17 |
*/
|
| 18 |
function taxonomy_explorer_help($section) {
|
| 19 |
switch ($section) {
|
| 20 |
case 'admin/modules#name':
|
| 21 |
return t('taxonomy explorer');
|
| 22 |
case 'admin/modules#description':
|
| 23 |
return t('Provides a hierarchical browser for nodes organized in a taxonomy tree.');
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Implementation of hook_perm().
|
| 29 |
*/
|
| 30 |
function taxonomy_explorer_perm() {
|
| 31 |
return array('browse categories');
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_menu().
|
| 36 |
*/
|
| 37 |
function taxonomy_explorer_menu($may_cache) {
|
| 38 |
$items = array();
|
| 39 |
if ($may_cache) {
|
| 40 |
$items[] = array(
|
| 41 |
'path' => 'taxonomy_explorer',
|
| 42 |
'title' => t('browse categories'),
|
| 43 |
'callback' => 'taxonomy_explorer_page',
|
| 44 |
'access' => user_access('browse categories'),
|
| 45 |
'type' => MENU_CALLBACK,
|
| 46 |
);
|
| 47 |
}
|
| 48 |
return $items;
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_block().
|
| 53 |
*/
|
| 54 |
function taxonomy_explorer_block($op = 'list', $delta = 0, $edit = array()) {
|
| 55 |
if (!user_access('browse categories'))
|
| 56 |
return;
|
| 57 |
|
| 58 |
switch ($op) {
|
| 59 |
case 'list':
|
| 60 |
$vocabularies = taxonomy_get_vocabularies();
|
| 61 |
foreach ($vocabularies as $vid => $vocabulary) {
|
| 62 |
$block_info = t('%title explorer', array('%title' => theme('placeholder', $vocabulary->name)));
|
| 63 |
$block['taxonomy_explorer-' . $vocabulary->vid]['info'] = $block_info;
|
| 64 |
}
|
| 65 |
return $block;
|
| 66 |
case 'configure':
|
| 67 |
return ''; // TODO
|
| 68 |
case 'view':
|
| 69 |
list($type, $vid) = explode('-', $delta);
|
| 70 |
if ($vocabulary = taxonomy_get_vocabulary($vid)) {
|
| 71 |
$block['subject'] = check_plain($vocabulary->name);
|
| 72 |
$block['content'] = theme('taxonomy_explorer_block', $vid);
|
| 73 |
}
|
| 74 |
return $block;
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Implementation of hook_settings().
|
| 80 |
*/
|
| 81 |
function taxonomy_explorer_settings() {
|
| 82 |
// Display settings
|
| 83 |
$form['display'] = array('#type' => 'fieldset', '#title' => t('Display settings'));
|
| 84 |
$form['display']['taxonomy_explorer_hide_empty'] = array(
|
| 85 |
'#type' => 'radios',
|
| 86 |
'#title' => t('Hide empty categories'),
|
| 87 |
'#default_value' => TAXONOMY_EXPLORER_HIDE_EMPTY,
|
| 88 |
'#options' => array(FALSE => t('Disabled'), TRUE => t('Enabled')),
|
| 89 |
'#description' => t('Whether taxonomy terms that have no sub-terms or nodes associated with them should be hidden. This can be useful to reduce load time in situations where the vocabulary is very large (e.g. for free-tagging vocabularies).'),
|
| 90 |
);
|
| 91 |
return $form;
|
| 92 |
}
|
| 93 |
|
| 94 |
//////////////////////////////////////////////////////////////////////////////
|
| 95 |
// MENU CALLBACKS
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Menu callback; displays a taxonomy explorer page.
|
| 99 |
*/
|
| 100 |
function taxonomy_explorer_page($vid = NULL, $tid = NULL) {
|
| 101 |
if (!$vid || !is_numeric($vid)) {
|
| 102 |
drupal_set_message('No vocabulary specified.', 'error');
|
| 103 |
return '';
|
| 104 |
}
|
| 105 |
$tids = empty($tid) ? array() : array((integer)$tid);
|
| 106 |
print theme('page', theme('taxonomy_explorer_page', $vid, $tids));
|
| 107 |
}
|
| 108 |
|
| 109 |
//////////////////////////////////////////////////////////////////////////////
|
| 110 |
// THEME LAYER
|
| 111 |
|
| 112 |
/**
|
| 113 |
*
|
| 114 |
*/
|
| 115 |
function theme_taxonomy_explorer_block($vid, $expanded_tids = array()) {
|
| 116 |
_taxonomy_explorer_add_headers();
|
| 117 |
|
| 118 |
$output = '<div class="explorer" id="explorer-' . $vid . '">';
|
| 119 |
foreach (taxonomy_explorer_tree($vid) as $term)
|
| 120 |
$output .= theme('taxonomy_explorer_term', $term, TRUE, in_array($term->tid, $expanded_tids), FALSE);
|
| 121 |
$output .= '</div>';
|
| 122 |
$output .= '<div class="more-link">' . l(t('more'), 'taxonomy_explorer/' . $vid, array('title' => t('View category browser')));
|
| 123 |
return $output;
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
*
|
| 128 |
*/
|
| 129 |
function theme_taxonomy_explorer_page($vid, $expanded_tids = array()) {
|
| 130 |
_taxonomy_explorer_add_headers();
|
| 131 |
|
| 132 |
$output = '<div class="explorer" id="explorer-' . $vid . '">';
|
| 133 |
foreach (taxonomy_explorer_tree($vid) as $term)
|
| 134 |
$output .= theme('taxonomy_explorer_term', $term, TRUE, in_array($term->tid, $expanded_tids));
|
| 135 |
$output .= '</div>';
|
| 136 |
return $output;
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
*
|
| 141 |
*/
|
| 142 |
function theme_taxonomy_explorer_term($term, $visible = TRUE, $expanded = FALSE, $columns = TRUE) {
|
| 143 |
if (TAXONOMY_EXPLORER_HIDE_EMPTY && empty($term->terms) && empty($term->nodes))
|
| 144 |
return '';
|
| 145 |
|
| 146 |
$count = is_array($term->nodes) ? count($term->nodes) : 0;
|
| 147 |
$icon_path = base_path() . TAXONOMY_EXPLORER_PATH . '/icons/folder.png';
|
| 148 |
|
| 149 |
$output = '<div class="explorer-folder' . ($expanded ? ' expanded' : '') . '" id="explorer-folder-' . $term->tid . '" style="display: ' . ($visible ? 'block' : 'none') .';">';
|
| 150 |
$output .= '<span class="explorer-folder-toggle"></span>';
|
| 151 |
$output .= '<span class="explorer-folder-icon"' . (_taxonomy_explorer_icon_exists('folder') ? ' style="background-image: url(' . $icon_path . ');"' : '') . '></span>';
|
| 152 |
$output .= '<span class="explorer-folder-title">' . check_plain($term->name) . '</span>';
|
| 153 |
if ($columns) {
|
| 154 |
$output .= '<span class="explorer-folder-info">' . ($count == 0 ? '—' : $count . ' item' . ($count == 1 ? '' : 's')) . '</span>';
|
| 155 |
}
|
| 156 |
foreach ($term->terms as $subterm)
|
| 157 |
$output .= theme('taxonomy_explorer_term', $subterm, FALSE, FALSE, $columns);
|
| 158 |
foreach ($term->nodes as $node)
|
| 159 |
$output .= theme('taxonomy_explorer_node', $node, $expanded, $columns);
|
| 160 |
$output .= '</div>';
|
| 161 |
return $output;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
*
|
| 166 |
*/
|
| 167 |
function theme_taxonomy_explorer_node($node, $visible = TRUE, $columns = TRUE) {
|
| 168 |
$link = l($node->title, 'node/' . $node->nid, array('title' => strip_tags($node->teaser)));
|
| 169 |
$date = format_date($node->changed, 'small');
|
| 170 |
$icon_path = base_path() . TAXONOMY_EXPLORER_PATH . '/icons/' . $node->type . '.png';
|
| 171 |
|
| 172 |
$output = '<div class="explorer-node ' . $node->type . '" id="explorer-node-' . $node->nid . '" style="display: ' . ($visible ? 'block' : 'none') .';">';
|
| 173 |
$output .= '<span class="explorer-node-icon"' . (_taxonomy_explorer_icon_exists($node->type) ? ' style="background-image: url(' . $icon_path . ');"' : '') . '></span>';
|
| 174 |
$output .= '<span class="exploder-node-title">' . $link . '</span>';
|
| 175 |
if ($columns) {
|
| 176 |
$output .= '<span class="explorer-node-info">' . $date . '</span>';
|
| 177 |
}
|
| 178 |
$output .= '</div>';
|
| 179 |
return $output;
|
| 180 |
}
|
| 181 |
|
| 182 |
//////////////////////////////////////////////////////////////////////////////
|
| 183 |
// HELPERS
|
| 184 |
|
| 185 |
/**
|
| 186 |
*
|
| 187 |
*/
|
| 188 |
function _taxonomy_explorer_icon_exists($type) {
|
| 189 |
// The result of file_exists() is cached by PHP, so we don't need to
|
| 190 |
// perform additional caching ourselves.
|
| 191 |
return file_exists(TAXONOMY_EXPLORER_PATH . '/icons/' . $type . '.png');
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
*
|
| 196 |
*/
|
| 197 |
function _taxonomy_explorer_add_headers() {
|
| 198 |
static $initialized = FALSE;
|
| 199 |
if (!$initialized) {
|
| 200 |
$initialized = TRUE;
|
| 201 |
|
| 202 |
drupal_add_js(TAXONOMY_EXPLORER_PATH . '/jquery.js');
|
| 203 |
theme_add_style(TAXONOMY_EXPLORER_PATH . '/taxonomy_explorer.css');
|
| 204 |
drupal_add_js(TAXONOMY_EXPLORER_PATH . '/taxonomy_explorer.js');
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Builds a taxonomy/node tree based on the given vocabulary.
|
| 210 |
*/
|
| 211 |
function taxonomy_explorer_tree($vid, $tid = 0) {
|
| 212 |
$terms = taxonomy_get_tree($vid, $tid, -1, 1);
|
| 213 |
foreach ($terms as $index => $term) {
|
| 214 |
$term->terms = taxonomy_explorer_tree($vid, $term->tid);
|
| 215 |
$term->nodes = array();
|
| 216 |
if ($nodes = taxonomy_select_nodes(array($term->tid), 'or', 0, FALSE, 'n.sticky DESC, n.title ASC, n.created DESC')) {
|
| 217 |
if (db_num_rows($nodes) > 0) {
|
| 218 |
while ($node = db_fetch_object($nodes)) {
|
| 219 |
$term->nodes[] = node_load($node->nid);
|
| 220 |
}
|
| 221 |
}
|
| 222 |
}
|
| 223 |
$terms[$index] = $term;
|
| 224 |
}
|
| 225 |
return $terms;
|
| 226 |
}
|