| 1 |
jaza |
1.17 |
<?php
|
| 2 |
weitzman |
1.19 |
// $Id: taxonomy_browser.module,v 1.18 2007/02/01 18:43:57 weitzman Exp $
|
| 3 |
jaza |
1.17 |
// Original by Moshe Weitzman (weitzmna@tejasa.com)
|
| 4 |
|
|
|
| 5 |
|
|
/**
|
| 6 |
|
|
* @file
|
| 7 |
|
|
* Enables users to construct their own view of content from terms across
|
| 8 |
|
|
* multiple vocabularies.
|
| 9 |
|
|
*/
|
| 10 |
|
|
|
| 11 |
|
|
/********************************************************************
|
| 12 |
|
|
* Drupal Hooks :: General Overview
|
| 13 |
|
|
********************************************************************/
|
| 14 |
|
|
|
| 15 |
|
|
/**
|
| 16 |
|
|
* Implementation of hook_menu().
|
| 17 |
|
|
*/
|
| 18 |
|
|
function taxonomy_browser_menu($may_cache) {
|
| 19 |
|
|
$items = array();
|
| 20 |
|
|
|
| 21 |
|
|
if ($may_cache) {
|
| 22 |
|
|
$items[] = array('path' => 'taxonomy_browser', 'title' => t('category browser'),
|
| 23 |
|
|
'access' => user_access('access content'),
|
| 24 |
|
|
'callback' => 'taxonomy_browser_page',
|
| 25 |
|
|
'weight' => 7,
|
| 26 |
|
|
'type' => MENU_NORMAL_ITEM);
|
| 27 |
|
|
}
|
| 28 |
|
|
|
| 29 |
|
|
return $items;
|
| 30 |
|
|
}
|
| 31 |
|
|
|
| 32 |
|
|
/**
|
| 33 |
|
|
* Implementation of hook_help().
|
| 34 |
|
|
*/
|
| 35 |
|
|
function taxonomy_browser_help($section = '') {
|
| 36 |
|
|
switch ($section) {
|
| 37 |
|
|
case 'admin/modules#description':
|
| 38 |
|
|
return t('An interface for viewing content grouped by arbitrary taxonomy terms.');
|
| 39 |
|
|
case 'taxonomy_browser':
|
| 40 |
|
|
return variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default());
|
| 41 |
|
|
}
|
| 42 |
|
|
}
|
| 43 |
|
|
|
| 44 |
|
|
/********************************************************************
|
| 45 |
|
|
* Drupal Hooks :: Core
|
| 46 |
|
|
********************************************************************/
|
| 47 |
|
|
|
| 48 |
|
|
/**
|
| 49 |
|
|
* Implementation of hook_settings().
|
| 50 |
|
|
*/
|
| 51 |
|
|
function taxonomy_browser_settings() {
|
| 52 |
|
|
if (!module_exist('node_type_filter') && !drupal_set_message()) {
|
| 53 |
|
|
drupal_set_message(t('You do not have the node_type_filter module installed. This means that the \'restrict search by content type\' option will not be available on the category browser page.'));
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
$form['taxonomy_browser_guidelines'] = array(
|
| 57 |
|
|
'#title' => t('Guidelines'),
|
| 58 |
|
|
'#type' => 'textarea',
|
| 59 |
|
|
'#default_value' => variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()),
|
| 60 |
|
|
'#rows' => 6,
|
| 61 |
|
|
'#description' => t('Instructions which should appear at top of the category browser main page'),
|
| 62 |
|
|
);
|
| 63 |
|
|
|
| 64 |
|
|
$vocabularies = taxonomy_get_vocabularies();
|
| 65 |
|
|
foreach ($vocabularies as $vocabulary) {
|
| 66 |
|
|
$select[$vocabulary->vid] = $vocabulary->name;
|
| 67 |
|
|
}
|
| 68 |
|
|
$form['taxonomy_browser_vocabularies'] = array(
|
| 69 |
|
|
'#title' => t('Included Vocabularies'),
|
| 70 |
|
|
'#type' => 'select',
|
| 71 |
|
|
'#default_value' => variable_get('taxonomy_browser_vocabularies', array()),
|
| 72 |
|
|
'#options' => $select,
|
| 73 |
|
|
'#description' => t('Select the vocabularies the user can select from on the category browser page'),
|
| 74 |
|
|
'#multiple' => TRUE,
|
| 75 |
weitzman |
1.19 |
'#size' => 6,
|
| 76 |
jaza |
1.17 |
);
|
| 77 |
|
|
|
| 78 |
|
|
return $form;
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
/********************************************************************
|
| 82 |
|
|
* Module Functions :: Public
|
| 83 |
|
|
********************************************************************/
|
| 84 |
|
|
|
| 85 |
|
|
/**
|
| 86 |
|
|
* Menu callback: the query building interface for nodes selected based on
|
| 87 |
|
|
* taxonomy terms.
|
| 88 |
|
|
*/
|
| 89 |
|
|
function taxonomy_browser_page() {
|
| 90 |
|
|
$form = array();
|
| 91 |
|
|
|
| 92 |
|
|
$form['scope'] = array(
|
| 93 |
|
|
'#type' => 'fieldset',
|
| 94 |
|
|
'#title' => t('Scope'),
|
| 95 |
weitzman |
1.18 |
'#collapsible' => FALSE,
|
| 96 |
jaza |
1.17 |
);
|
| 97 |
|
|
|
| 98 |
|
|
if (module_exist('node_type_filter')) {
|
| 99 |
|
|
$filter_options['all'] = t('all');
|
| 100 |
|
|
foreach (node_get_types() as $type => $name) {
|
| 101 |
|
|
$filter_options[$type] = $name;
|
| 102 |
|
|
}
|
| 103 |
|
|
$form['scope']['node_filter'] = array(
|
| 104 |
|
|
'#type' => 'radios',
|
| 105 |
|
|
'#title' => t('Restrict search by content type'),
|
| 106 |
|
|
'#options' => $filter_options,
|
| 107 |
|
|
'#default_value' => 'all',
|
| 108 |
|
|
);
|
| 109 |
|
|
}
|
| 110 |
|
|
|
| 111 |
|
|
$form['scope']['operator'] = array(
|
| 112 |
|
|
'#type' => 'radios',
|
| 113 |
|
|
'#title' => t('Items containing'),
|
| 114 |
|
|
'#options' => array(t('<strong>all</strong> terms'), t('<strong>any</strong> terms')),
|
| 115 |
|
|
'#default_value' => 0,
|
| 116 |
|
|
);
|
| 117 |
|
|
|
| 118 |
|
|
$vocabularies = variable_get('taxonomy_browser_vocabularies', array());
|
| 119 |
|
|
|
| 120 |
|
|
$form['taxonomy'] = array(
|
| 121 |
|
|
'#type' => 'fieldset',
|
| 122 |
|
|
'#title' => t('Categories'),
|
| 123 |
|
|
'#tree' => TRUE,
|
| 124 |
|
|
);
|
| 125 |
weitzman |
1.19 |
|
| 126 |
|
|
$i = 0;
|
| 127 |
jaza |
1.17 |
foreach ($vocabularies as $v) {
|
| 128 |
|
|
$form['taxonomy'][$v] = taxonomy_form($v);
|
| 129 |
weitzman |
1.19 |
$form['taxonomy'][$v]['#weight'] = $i;
|
| 130 |
|
|
$i++;
|
| 131 |
jaza |
1.17 |
}
|
| 132 |
|
|
|
| 133 |
|
|
$form['submit'] = array(
|
| 134 |
|
|
'#type' => 'submit',
|
| 135 |
|
|
'#value' => t('Search'),
|
| 136 |
|
|
);
|
| 137 |
|
|
|
| 138 |
|
|
return drupal_get_form('taxonomy_browser_page', $form);
|
| 139 |
|
|
}
|
| 140 |
|
|
|
| 141 |
|
|
/**
|
| 142 |
|
|
* Themable form output for the category browser page.
|
| 143 |
|
|
*/
|
| 144 |
|
|
function theme_taxonomy_browser_page($form) {
|
| 145 |
|
|
$output = '';
|
| 146 |
|
|
|
| 147 |
|
|
$vocabularies = variable_get('taxonomy_browser_vocabularies', array());
|
| 148 |
|
|
if (empty($vocabularies)) {
|
| 149 |
|
|
form_set_error('taxonomy_browser_page', t('You must select the vocabularies to display from the <a href="%link">taxonomy browser settings page</a>.', array ('%link' => url('admin/settings/taxonomy_browser'))));
|
| 150 |
|
|
return ' ';
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
$output .= form_render($form);
|
| 154 |
|
|
return $output;
|
| 155 |
|
|
}
|
| 156 |
|
|
|
| 157 |
|
|
/**
|
| 158 |
|
|
* Implementation of hook_form_validate().
|
| 159 |
|
|
*/
|
| 160 |
|
|
function taxonomy_browser_page_validate($form_id, $form_values) {
|
| 161 |
|
|
$tids = _taxonomy_browser_get_tid_list($form_values['taxonomy']);
|
| 162 |
|
|
|
| 163 |
|
|
if (empty($tids)) {
|
| 164 |
|
|
form_set_error('taxonomy', t('You must select at least one category in your search.'));
|
| 165 |
|
|
}
|
| 166 |
|
|
else {
|
| 167 |
|
|
$operator = $form_values['operator'] ? 'or' : 'and';
|
| 168 |
|
|
$node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
|
| 169 |
|
|
if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
|
| 170 |
|
|
form_set_error('taxonomy', t('No posts match your criteria.'));
|
| 171 |
|
|
}
|
| 172 |
|
|
}
|
| 173 |
|
|
}
|
| 174 |
|
|
|
| 175 |
|
|
/**
|
| 176 |
|
|
* Implementation of hook_form_submit().
|
| 177 |
|
|
*/
|
| 178 |
|
|
function taxonomy_browser_page_submit($form_id, $form_values) {
|
| 179 |
|
|
$tids = _taxonomy_browser_get_tid_list();
|
| 180 |
|
|
$operator = $form_values['operator'] ? 'or' : 'and';
|
| 181 |
|
|
$str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
|
| 182 |
|
|
$node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
|
| 183 |
|
|
|
| 184 |
|
|
return array('taxonomy/term/'. $str_tids, (isset($node_type) ? 'type='. $node_type : ''));
|
| 185 |
|
|
}
|
| 186 |
|
|
|
| 187 |
|
|
/********************************************************************
|
| 188 |
|
|
* Module Functions :: Private
|
| 189 |
|
|
********************************************************************/
|
| 190 |
|
|
|
| 191 |
|
|
/**
|
| 192 |
|
|
* Private function to count the number of nodes found by the user's query.
|
| 193 |
|
|
*/
|
| 194 |
|
|
function taxonomy_browser_count_nodes($tids = array(), $operator = 'or', $depth = 0, $nodetype = NULL) {
|
| 195 |
|
|
if (count($tids) > 0) {
|
| 196 |
|
|
// For each term ID, generate an array of descendant term IDs to the right depth.
|
| 197 |
|
|
$descendant_tids = array();
|
| 198 |
|
|
if ($depth === 'all') {
|
| 199 |
|
|
$depth = NULL;
|
| 200 |
|
|
}
|
| 201 |
|
|
foreach ($tids as $index => $tid) {
|
| 202 |
|
|
$term = taxonomy_get_term($tid);
|
| 203 |
|
|
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
|
| 204 |
|
|
$descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
|
| 205 |
|
|
}
|
| 206 |
|
|
|
| 207 |
|
|
$type_where = $nodetype ? "n.type = '". db_escape_string($nodetype). "'" : 1;
|
| 208 |
|
|
if ($operator == 'or') {
|
| 209 |
|
|
$str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
|
| 210 |
|
|
|
| 211 |
|
|
$sql_count = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE $type_where AND tn.tid IN ($str_tids) ORDER BY n.sticky DESC, n.title ASC";
|
| 212 |
|
|
}
|
| 213 |
|
|
else {
|
| 214 |
|
|
$joins = '';
|
| 215 |
|
|
$wheres = array();
|
| 216 |
|
|
$wheres[] = $type_where;
|
| 217 |
|
|
foreach ($descendant_tids as $index => $tids) {
|
| 218 |
|
|
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
|
| 219 |
|
|
$wheres[] = 'tn'. $index .'.tid IN ('. implode(',', $tids) .')';
|
| 220 |
|
|
}
|
| 221 |
|
|
$sql_count = "SELECT COUNT(n.nid) FROM {node} n $joins WHERE ". implode(' AND ', $wheres);
|
| 222 |
|
|
}
|
| 223 |
|
|
|
| 224 |
|
|
return db_result(db_query(db_rewrite_sql($sql_count)));
|
| 225 |
|
|
}
|
| 226 |
|
|
|
| 227 |
|
|
return 0;
|
| 228 |
|
|
}
|
| 229 |
|
|
|
| 230 |
|
|
/**
|
| 231 |
|
|
* Transforms an unpredictably and irregularly nested set of tids (as returned
|
| 232 |
|
|
* from a taxonomy form) into a linear array of tids.
|
| 233 |
|
|
*/
|
| 234 |
|
|
function _taxonomy_browser_get_tid_list($tids = NULL) {
|
| 235 |
|
|
static $tid_list;
|
| 236 |
|
|
|
| 237 |
|
|
if (isset($tids) && is_array($tids)) {
|
| 238 |
|
|
$tid_list = array();
|
| 239 |
|
|
foreach ($tids as $key => $tid) {
|
| 240 |
|
|
if (!empty($tid)) {
|
| 241 |
|
|
if (is_array($tid)) {
|
| 242 |
|
|
foreach ($tid as $key2 => $tid2) {
|
| 243 |
|
|
if (!empty($tid2)) {
|
| 244 |
|
|
$tid_list[] = $tid2;
|
| 245 |
|
|
}
|
| 246 |
|
|
}
|
| 247 |
|
|
}
|
| 248 |
|
|
else {
|
| 249 |
|
|
$tid_list[] = $tid;
|
| 250 |
|
|
}
|
| 251 |
|
|
}
|
| 252 |
|
|
}
|
| 253 |
|
|
}
|
| 254 |
|
|
|
| 255 |
|
|
return $tid_list;
|
| 256 |
|
|
}
|
| 257 |
|
|
|
| 258 |
|
|
/**
|
| 259 |
|
|
* Provides default guideline text.
|
| 260 |
|
|
*/
|
| 261 |
|
|
function _taxonomy_browser_guidelines_default() {
|
| 262 |
|
|
return t('<p>You may select multiple items from each list by holding down the <code>Ctrl</code> (Mac: <code>command</code>) key while left-clicking each item.</p>');
|
| 263 |
|
|
}
|
| 264 |
|
|
|
| 265 |
|
|
?>
|