| 1 |
jaza |
1.17 |
<?php
|
| 2 |
nancyw |
1.19.2.31 |
// $Id: taxonomy_browser.module,v 1.19.2.30 2008/07/22 00:47:13 nancyw Exp $
|
| 3 |
jaza |
1.17 |
// Original by Moshe Weitzman (weitzmna@tejasa.com)
|
| 4 |
|
|
|
| 5 |
|
|
/**
|
| 6 |
|
|
* @file
|
| 7 |
nancyw |
1.19.2.19 |
* Enables users to construct their own view of content from terms across
|
| 8 |
|
|
* multiple vocabularies.
|
| 9 |
jaza |
1.17 |
*/
|
| 10 |
|
|
|
| 11 |
nancyw |
1.19.2.13 |
//*******************************************************************
|
| 12 |
nancyw |
1.19.2.21 |
//* Drupal Hooks : General Overview
|
| 13 |
nancyw |
1.19.2.13 |
//*******************************************************************
|
| 14 |
jaza |
1.17 |
|
| 15 |
|
|
/**
|
| 16 |
|
|
* Implementation of hook_menu().
|
| 17 |
|
|
*/
|
| 18 |
|
|
function taxonomy_browser_menu($may_cache) {
|
| 19 |
|
|
$items = array();
|
| 20 |
|
|
|
| 21 |
|
|
if ($may_cache) {
|
| 22 |
nancyw |
1.19.2.14 |
$items[] = array(
|
| 23 |
nancyw |
1.19.2.5 |
'path' => 'admin/settings/taxonomy-browser',
|
| 24 |
|
|
'title' => t('Taxonomy Browser'),
|
| 25 |
|
|
'description' => t('Set usage guidelines and included vocabularies.'),
|
| 26 |
|
|
'callback' => 'drupal_get_form',
|
| 27 |
|
|
'callback arguments' => 'taxonomy_browser_admin_settings',
|
| 28 |
|
|
'access' => user_access('administer site configuration'),
|
| 29 |
|
|
);
|
| 30 |
|
|
}
|
| 31 |
|
|
else {
|
| 32 |
nancyw |
1.19.2.15 |
$items[] = array(
|
| 33 |
|
|
'path' => 'taxonomy_browser',
|
| 34 |
|
|
'title' => t('Category Browser'),
|
| 35 |
nancyw |
1.19.2.31 |
'access' => user_access(variable_get('taxonomy_browser_need_perm', FALSE) ? 'access taxonomy browser' : 'access content'),
|
| 36 |
nancyw |
1.19.2.15 |
'callback' => 'taxonomy_browser_page',
|
| 37 |
|
|
'description' => t('Find content on your own terms.'),
|
| 38 |
nancyw |
1.19.2.20 |
'type' => MENU_NORMAL_ITEM,
|
| 39 |
|
|
);
|
| 40 |
|
|
|
| 41 |
nancyw |
1.19.2.5 |
drupal_add_css(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.css');
|
| 42 |
jaza |
1.17 |
}
|
| 43 |
|
|
|
| 44 |
|
|
return $items;
|
| 45 |
|
|
}
|
| 46 |
|
|
|
| 47 |
|
|
/**
|
| 48 |
|
|
* Implementation of hook_help().
|
| 49 |
|
|
*/
|
| 50 |
|
|
function taxonomy_browser_help($section = '') {
|
| 51 |
|
|
switch ($section) {
|
| 52 |
|
|
case 'admin/modules#description':
|
| 53 |
|
|
return t('An interface for viewing content grouped by arbitrary taxonomy terms.');
|
| 54 |
|
|
case 'taxonomy_browser':
|
| 55 |
nancyw |
1.19.2.15 |
$output = check_markup(variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()));
|
| 56 |
|
|
if (user_access('administer site configuration')) {
|
| 57 |
nancyw |
1.19.2.23 |
$output .= '<p class="links">'. l(t('Go to Taxonomy Browser settings'), 'admin/settings/taxonomy-browser', array(), drupal_get_destination()) .'</p>';
|
| 58 |
nancyw |
1.19.2.15 |
}
|
| 59 |
|
|
return $output;
|
| 60 |
|
|
}
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
/**
|
| 64 |
|
|
* Implementation of hook_perm().
|
| 65 |
|
|
*/
|
| 66 |
|
|
function taxonomy_browser_perm() {
|
| 67 |
nancyw |
1.19.2.31 |
if (variable_get('taxonomy_browser_need_perm', FALSE)) {
|
| 68 |
nancyw |
1.19.2.15 |
return array('access taxonomy browser');
|
| 69 |
|
|
}
|
| 70 |
|
|
else {
|
| 71 |
|
|
return array();
|
| 72 |
jaza |
1.17 |
}
|
| 73 |
|
|
}
|
| 74 |
|
|
|
| 75 |
nancyw |
1.19.2.13 |
//********************************************************************
|
| 76 |
nancyw |
1.19.2.15 |
//* Drupal Hooks : Core
|
| 77 |
nancyw |
1.19.2.13 |
//********************************************************************
|
| 78 |
jaza |
1.17 |
|
| 79 |
|
|
/**
|
| 80 |
nancyw |
1.19.2.21 |
* Implementation of hook_block().
|
| 81 |
|
|
*/
|
| 82 |
|
|
function taxonomy_browser_block($op = 'list', $delta = 0, $edit = array()) {
|
| 83 |
|
|
$block = array();
|
| 84 |
|
|
switch ($op) {
|
| 85 |
|
|
case 'list':
|
| 86 |
|
|
$block[0]['info'] = t('Category browser');
|
| 87 |
|
|
return $block;
|
| 88 |
|
|
|
| 89 |
|
|
case 'view':
|
| 90 |
nancyw |
1.19.2.23 |
switch ($delta) {
|
| 91 |
nancyw |
1.19.2.21 |
case 0:
|
| 92 |
|
|
$block = _taxonomy_browser_block_view($delta);
|
| 93 |
|
|
break;
|
| 94 |
|
|
}
|
| 95 |
|
|
return $block;
|
| 96 |
|
|
//
|
| 97 |
|
|
// case 'configure':
|
| 98 |
|
|
// return _taxonomy_browser_block_configure($delta);
|
| 99 |
|
|
//
|
| 100 |
|
|
// case 'save':
|
| 101 |
|
|
// _taxonomy_browser_block_save($delta, $edit);
|
| 102 |
|
|
}
|
| 103 |
|
|
}
|
| 104 |
|
|
|
| 105 |
|
|
/**
|
| 106 |
nancyw |
1.19.2.15 |
* Implementation of hook_form().
|
| 107 |
jaza |
1.17 |
*/
|
| 108 |
nancyw |
1.19.2.1 |
function taxonomy_browser_admin_settings() {
|
| 109 |
|
|
if (!module_exists('node_type_filter') && !drupal_set_message()) {
|
| 110 |
nancyw |
1.19.2.13 |
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.'), 'status');
|
| 111 |
jaza |
1.17 |
}
|
| 112 |
nancyw |
1.19.2.27 |
|
| 113 |
|
|
drupal_add_js(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.js', 'module');
|
| 114 |
jaza |
1.17 |
|
| 115 |
|
|
$form['taxonomy_browser_guidelines'] = array(
|
| 116 |
|
|
'#title' => t('Guidelines'),
|
| 117 |
|
|
'#type' => 'textarea',
|
| 118 |
|
|
'#default_value' => variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()),
|
| 119 |
nancyw |
1.19.2.15 |
'#rows' => 2,
|
| 120 |
jaza |
1.17 |
'#description' => t('Instructions which should appear at top of the category browser main page'),
|
| 121 |
nancyw |
1.19.2.13 |
);
|
| 122 |
jaza |
1.17 |
|
| 123 |
nancyw |
1.19.2.5 |
$form['taxonomy_browser_select_type'] = array(
|
| 124 |
|
|
'#title' => t('Selection type'),
|
| 125 |
|
|
'#type' => 'radios',
|
| 126 |
nancyw |
1.19.2.23 |
'#default_value' => variable_get('taxonomy_browser_select_type', 1),
|
| 127 |
nancyw |
1.19.2.7 |
'#options' => array(t('Selection box'), t('Check boxes')),
|
| 128 |
nancyw |
1.19.2.9 |
'#description' => t('This option determines whether the user will see a selection list or check boxes.'),
|
| 129 |
nancyw |
1.19.2.23 |
'#prefix' => '<div class="taxonomy_browser_radios">',
|
| 130 |
|
|
'#suffix' => '</div>',
|
| 131 |
nancyw |
1.19.2.13 |
);
|
| 132 |
nancyw |
1.19.2.16 |
|
| 133 |
nancyw |
1.19.2.31 |
$form['taxonomy_browser_collapse'] = array(
|
| 134 |
|
|
'#title' => t('Make vocabularies collapsible'),
|
| 135 |
|
|
'#type' => 'checkbox',
|
| 136 |
|
|
'#default_value' => variable_get('taxonomy_browser_collapse', FALSE),
|
| 137 |
|
|
'#description' => t('Do you want to display of the terms within a vocabulary to be collapsible? Requires "Check boxes" above.'),
|
| 138 |
|
|
);
|
| 139 |
|
|
|
| 140 |
nancyw |
1.19.2.16 |
$form['taxonomy_browser_default_op'] = array(
|
| 141 |
|
|
'#title' => t('"Items containing" default'),
|
| 142 |
|
|
'#type' => 'radios',
|
| 143 |
|
|
'#default_value' => variable_get('taxonomy_browser_default_op', 0),
|
| 144 |
|
|
'#options' => array(t('All'), t('Any')),
|
| 145 |
|
|
'#description' => t('This option determines which "Items containing" choice is the default.'),
|
| 146 |
|
|
'#prefix' => '<div class="taxonomy_browser_radios">',
|
| 147 |
|
|
'#suffix' => '</div>',
|
| 148 |
|
|
);
|
| 149 |
nancyw |
1.19.2.10 |
|
| 150 |
|
|
$form['taxonomy_browser_count_nodes'] = array(
|
| 151 |
|
|
'#title' => t('Show node count'),
|
| 152 |
|
|
'#type' => 'checkbox',
|
| 153 |
nancyw |
1.19.2.31 |
'#default_value' => variable_get('taxonomy_browser_count_nodes', FALSE),
|
| 154 |
nancyw |
1.19.2.10 |
'#description' => t('Do you want to display the count of nodes tagged with each term? This can be SQL-intensive.'),
|
| 155 |
nancyw |
1.19.2.13 |
);
|
| 156 |
nancyw |
1.19.2.10 |
|
| 157 |
|
|
$form['taxonomy_browser_show_unused'] = array(
|
| 158 |
|
|
'#title' => t('Show unused terms'),
|
| 159 |
|
|
'#type' => 'checkbox',
|
| 160 |
nancyw |
1.19.2.31 |
'#default_value' => variable_get('taxonomy_browser_show_unused', FALSE),
|
| 161 |
nancyw |
1.19.2.23 |
'#description' => t('Do you want to display the term if no nodes are tagged with that term? This requires "Show node count" to be selected; if it is not seleted, all terms will be shown.'),
|
| 162 |
nancyw |
1.19.2.13 |
);
|
| 163 |
|
|
|
| 164 |
|
|
$form['taxonomy_browser_allow_children'] = array(
|
| 165 |
|
|
'#title' => t('Allow child terms to be included'),
|
| 166 |
|
|
'#type' => 'checkbox',
|
| 167 |
nancyw |
1.19.2.31 |
'#default_value' => variable_get('taxonomy_browser_allow_children', FALSE),
|
| 168 |
nancyw |
1.19.2.13 |
'#description' => t('Do you want the user to see a check box to include child terms (sub-terms)?'),
|
| 169 |
|
|
);
|
| 170 |
nancyw |
1.19.2.5 |
|
| 171 |
nancyw |
1.19.2.15 |
$form['taxonomy_browser_need_perm'] = array(
|
| 172 |
|
|
'#title' => t('Requires permission'),
|
| 173 |
|
|
'#type' => 'checkbox',
|
| 174 |
nancyw |
1.19.2.31 |
'#default_value' => variable_get('taxonomy_browser_need_perm', FALSE),
|
| 175 |
nancyw |
1.19.2.15 |
'#description' => t('Do you want to require permission to see the browser page?'),
|
| 176 |
|
|
);
|
| 177 |
|
|
|
| 178 |
nancyw |
1.19.2.31 |
$form['taxonomy_browser_show_types'] = array(
|
| 179 |
|
|
'#title' => t('Show content types with vocabulary'),
|
| 180 |
|
|
'#type' => 'checkbox',
|
| 181 |
|
|
'#default_value' => variable_get('taxonomy_browser_show_types', FALSE),
|
| 182 |
|
|
'#description' => t('If checked, this option displays a list of the content types for which this vocabulary may be used.'),
|
| 183 |
|
|
);
|
| 184 |
|
|
|
| 185 |
nancyw |
1.19.2.23 |
$select = array();
|
| 186 |
jaza |
1.17 |
$vocabularies = taxonomy_get_vocabularies();
|
| 187 |
|
|
foreach ($vocabularies as $vocabulary) {
|
| 188 |
|
|
$select[$vocabulary->vid] = $vocabulary->name;
|
| 189 |
|
|
}
|
| 190 |
nancyw |
1.19.2.13 |
|
| 191 |
nancyw |
1.19.2.29 |
$current_vocabs = variable_get('taxonomy_browser_vocabularies', array());
|
| 192 |
|
|
// Occasionally we get a 0 vid in the array.
|
| 193 |
|
|
unset($current_vocabs[0]);
|
| 194 |
|
|
|
| 195 |
jaza |
1.17 |
$form['taxonomy_browser_vocabularies'] = array(
|
| 196 |
|
|
'#title' => t('Included Vocabularies'),
|
| 197 |
nancyw |
1.19.2.23 |
'#type' => 'checkboxes',
|
| 198 |
nancyw |
1.19.2.29 |
'#default_value' => $current_vocabs,
|
| 199 |
jaza |
1.17 |
'#options' => $select,
|
| 200 |
nancyw |
1.19.2.8 |
'#description' => t('Select the vocabularies the user can select from on the category browser page.'),
|
| 201 |
nancyw |
1.19.2.23 |
'#prefix' => '<div class="taxonomy_browser_checkboxes">',
|
| 202 |
|
|
'#suffix' => '</div>',
|
| 203 |
nancyw |
1.19.2.13 |
);
|
| 204 |
nancyw |
1.19.2.18 |
|
| 205 |
nancyw |
1.19.2.8 |
if (module_exists('node_type_filter')) {
|
| 206 |
nancyw |
1.19.2.23 |
$filter_options = node_get_types('names');
|
| 207 |
nancyw |
1.19.2.8 |
$form['taxonomy_browser_omit'] = array(
|
| 208 |
nancyw |
1.19.2.23 |
'#type' => 'checkboxes',
|
| 209 |
nancyw |
1.19.2.8 |
'#title' => t('Omit content types'),
|
| 210 |
|
|
'#options' => $filter_options,
|
| 211 |
nancyw |
1.19.2.13 |
'#default_value' => variable_get('taxonomy_browser_omit', array('')),
|
| 212 |
nancyw |
1.19.2.8 |
'#description' => t('If any of these types is selected, it will be omitted from the list on the "Category Browser" page.'),
|
| 213 |
nancyw |
1.19.2.23 |
'#prefix' => '<div class="taxonomy_browser_checkboxes">',
|
| 214 |
|
|
'#suffix' => '</div>',
|
| 215 |
nancyw |
1.19.2.13 |
);
|
| 216 |
nancyw |
1.19.2.8 |
}
|
| 217 |
jaza |
1.17 |
|
| 218 |
nancyw |
1.19.2.12 |
return system_settings_form($form);
|
| 219 |
jaza |
1.17 |
}
|
| 220 |
|
|
|
| 221 |
nancyw |
1.19.2.13 |
function taxonomy_browser_admin_settings_validate($form_id, &$form_values) {
|
| 222 |
nancyw |
1.19.2.31 |
if ($form_values['taxonomy_browser_count_nodes'] == FALSE
|
| 223 |
|
|
&& $form_values['taxonomy_browser_show_unused'] == TRUE) {
|
| 224 |
nancyw |
1.19.2.13 |
form_set_error('taxonomy_browser_show_unused', t('"Show unused" requires "count nodes."'));
|
| 225 |
|
|
}
|
| 226 |
|
|
}
|
| 227 |
|
|
|
| 228 |
|
|
//********************************************************************
|
| 229 |
|
|
//* Module Functions : Public
|
| 230 |
|
|
//********************************************************************
|
| 231 |
jaza |
1.17 |
|
| 232 |
|
|
/**
|
| 233 |
|
|
* Menu callback: the query building interface for nodes selected based on
|
| 234 |
|
|
* taxonomy terms.
|
| 235 |
|
|
*/
|
| 236 |
|
|
function taxonomy_browser_page() {
|
| 237 |
nancyw |
1.19.2.1 |
$output .= drupal_get_form('taxonomy_browser_form');
|
| 238 |
|
|
return $output;
|
| 239 |
|
|
}
|
| 240 |
|
|
|
| 241 |
|
|
function taxonomy_browser_form() {
|
| 242 |
jaza |
1.17 |
$form = array();
|
| 243 |
nancyw |
1.19.2.15 |
$selection_types = array('select', 'checkboxes');
|
| 244 |
nancyw |
1.19.2.23 |
$select_type = $selection_types[variable_get('taxonomy_browser_select_type', 1)];
|
| 245 |
nancyw |
1.19.2.31 |
$collapsible = variable_get('taxonomy_browser_collapse', FALSE) && ($select_type == 'checkboxes');
|
| 246 |
|
|
$count_nodes = variable_get('taxonomy_browser_count_nodes', FALSE);
|
| 247 |
|
|
$show_unused = variable_get('taxonomy_browser_show_unused', FALSE);
|
| 248 |
|
|
$allow_children = variable_get('taxonomy_browser_allow_children', FALSE);
|
| 249 |
nancyw |
1.19.2.14 |
$node_types = node_get_types('names');
|
| 250 |
jaza |
1.17 |
|
| 251 |
|
|
$form['scope'] = array(
|
| 252 |
|
|
'#type' => 'fieldset',
|
| 253 |
|
|
'#title' => t('Scope'),
|
| 254 |
nancyw |
1.19.2.31 |
'#collapsible' => TRUE,
|
| 255 |
|
|
'#collapsed' => FALSE,
|
| 256 |
nancyw |
1.19.2.15 |
'#attributes' => array('class' => 'taxonomy_browser_scope'),
|
| 257 |
|
|
);
|
| 258 |
nancyw |
1.19.2.13 |
|
| 259 |
nancyw |
1.19.2.1 |
if (module_exists('node_type_filter')) {
|
| 260 |
nancyw |
1.19.2.11 |
if ($count_nodes) {
|
| 261 |
|
|
$total_count = 0;
|
| 262 |
nancyw |
1.19.2.20 |
$result = db_query('SELECT DISTINCT(type), COUNT(nid) AS count FROM {node} WHERE status=1 GROUP BY type ORDER BY type');
|
| 263 |
nancyw |
1.19.2.11 |
while ($counter = db_fetch_array($result)) {
|
| 264 |
nancyw |
1.19.2.14 |
// Check if we know about this type - a disabled module could have orphans.
|
| 265 |
nancyw |
1.19.2.20 |
if (isset($node_types[$counter['type']])) {
|
| 266 |
|
|
$node_types[$counter['type']] .= ' ('. $counter['count'] .')';
|
| 267 |
nancyw |
1.19.2.14 |
}
|
| 268 |
|
|
else {
|
| 269 |
nancyw |
1.19.2.31 |
$node_types['unknown'] .= $counter['type'] .' ??? ('. $counter['count'] .') ';
|
| 270 |
nancyw |
1.19.2.20 |
watchdog('Taxonomy Browser', 'Unknown content type found: @type', array('@type' => $counter['type']), WATCHDOG_WARNING);
|
| 271 |
nancyw |
1.19.2.14 |
}
|
| 272 |
nancyw |
1.19.2.11 |
$total_count += $counter['count'];
|
| 273 |
|
|
}
|
| 274 |
|
|
}
|
| 275 |
nancyw |
1.19.2.20 |
|
| 276 |
nancyw |
1.19.2.24 |
$omit = array_filter(variable_get('taxonomy_browser_omit', array()));
|
| 277 |
nancyw |
1.19.2.10 |
if (!empty($omit)) {
|
| 278 |
nancyw |
1.19.2.13 |
foreach ($omit as $omit_type) {
|
| 279 |
nancyw |
1.19.2.20 |
unset($node_types[$omit_type]);
|
| 280 |
nancyw |
1.19.2.10 |
}
|
| 281 |
|
|
}
|
| 282 |
nancyw |
1.19.2.22 |
$desc = t('Not selecting any type is the same as selecting all types.');
|
| 283 |
|
|
if ($count_nodes) {
|
| 284 |
|
|
$desc .= ' '. t('The total count of all types is !count.', array('!count' => $total_count));
|
| 285 |
|
|
}
|
| 286 |
jaza |
1.17 |
$form['scope']['node_filter'] = array(
|
| 287 |
nancyw |
1.19.2.6 |
// '#type' => 'checkboxes',
|
| 288 |
nancyw |
1.19.2.15 |
'#type' => $select_type,
|
| 289 |
jaza |
1.17 |
'#title' => t('Restrict search by content type'),
|
| 290 |
nancyw |
1.19.2.15 |
'#options' => $node_types,
|
| 291 |
nancyw |
1.19.2.31 |
'#multiple' => TRUE,
|
| 292 |
nancyw |
1.19.2.15 |
'#prefix' => '<div class="taxonomy_browser_checkboxes">',
|
| 293 |
|
|
'#suffix' => '</div>',
|
| 294 |
nancyw |
1.19.2.22 |
'#description' => $desc,
|
| 295 |
nancyw |
1.19.2.14 |
);
|
| 296 |
jaza |
1.17 |
}
|
| 297 |
nancyw |
1.19.2.17 |
else {
|
| 298 |
|
|
$form['scope']['node_filter'] = array(
|
| 299 |
|
|
'#type' => value,
|
| 300 |
|
|
'#value' => array(),
|
| 301 |
|
|
);
|
| 302 |
|
|
}
|
| 303 |
|
|
|
| 304 |
jaza |
1.17 |
$form['scope']['operator'] = array(
|
| 305 |
|
|
'#type' => 'radios',
|
| 306 |
|
|
'#title' => t('Items containing'),
|
| 307 |
|
|
'#options' => array(t('<strong>all</strong> terms'), t('<strong>any</strong> terms')),
|
| 308 |
nancyw |
1.19.2.16 |
'#default_value' => variable_get('taxonomy_browser_default_op', 0),
|
| 309 |
nancyw |
1.19.2.14 |
'#prefix' => '<div class="taxonomy_browser_radios">',
|
| 310 |
nancyw |
1.19.2.15 |
'#suffix' => '</div>',
|
| 311 |
nancyw |
1.19.2.14 |
);
|
| 312 |
jaza |
1.17 |
|
| 313 |
nancyw |
1.19.2.23 |
$vocabularies = array_filter(variable_get('taxonomy_browser_vocabularies', array()));
|
| 314 |
nancyw |
1.19.2.29 |
// Occasionally we get a 0 vid in the array.
|
| 315 |
|
|
unset($vocabularies[0]);
|
| 316 |
|
|
|
| 317 |
nancyw |
1.19.2.18 |
// Has the admin selected any vocabs?
|
| 318 |
|
|
if (count($vocabularies) == 0) {
|
| 319 |
nancyw |
1.19.2.23 |
$vocabs = taxonomy_get_vocabularies();
|
| 320 |
|
|
foreach ($vocabs as $vocabulary) {
|
| 321 |
|
|
$vocabularies[$vocabulary->vid] = 1;
|
| 322 |
|
|
}
|
| 323 |
nancyw |
1.19.2.18 |
}
|
| 324 |
nancyw |
1.19.2.17 |
|
| 325 |
nancyw |
1.19.2.13 |
if ($allow_children) {
|
| 326 |
|
|
$form['children'] = array(
|
| 327 |
|
|
'#type' => 'fieldset',
|
| 328 |
|
|
'#title' => t('Include Children'),
|
| 329 |
nancyw |
1.19.2.31 |
'#collapsible' => TRUE,
|
| 330 |
|
|
'#collapsed' => FALSE,
|
| 331 |
nancyw |
1.19.2.13 |
);
|
| 332 |
|
|
$form['children']['include_children'] = array(
|
| 333 |
|
|
'#type' => 'checkbox',
|
| 334 |
|
|
'#title' => t('Automatically include children (sub-terms)'),
|
| 335 |
|
|
'#description' => t('If you select a term with children (sub-terms), do you want those child terms automatically included in the search? This requires that "Items containing" be "any."'),
|
| 336 |
|
|
);
|
| 337 |
|
|
}
|
| 338 |
|
|
else {
|
| 339 |
nancyw |
1.19.2.14 |
$form['include_children'] = array(
|
| 340 |
nancyw |
1.19.2.13 |
'#type' => 'value',
|
| 341 |
nancyw |
1.19.2.31 |
'#value' => FALSE,
|
| 342 |
nancyw |
1.19.2.13 |
);
|
| 343 |
|
|
}
|
| 344 |
jaza |
1.17 |
|
| 345 |
|
|
$form['taxonomy'] = array(
|
| 346 |
|
|
'#type' => 'fieldset',
|
| 347 |
|
|
'#title' => t('Categories'),
|
| 348 |
nancyw |
1.19.2.31 |
'#collapsible' => TRUE,
|
| 349 |
|
|
'#collapsed' => FALSE,
|
| 350 |
jaza |
1.17 |
'#tree' => TRUE,
|
| 351 |
|
|
);
|
| 352 |
weitzman |
1.19 |
|
| 353 |
nancyw |
1.19.2.13 |
$selection_types = array('select', 'checkboxes');
|
| 354 |
weitzman |
1.19 |
$i = 0;
|
| 355 |
nancyw |
1.19.2.23 |
foreach ($vocabularies as $v => $sel) {
|
| 356 |
nancyw |
1.19.2.10 |
$voc = taxonomy_get_vocabulary($v);
|
| 357 |
nancyw |
1.19.2.14 |
|
| 358 |
|
|
$voc_node_types = array();
|
| 359 |
nancyw |
1.19.2.30 |
if (isset($voc->nodes) && !empty($voc->nodes[0])) {
|
| 360 |
|
|
foreach ($voc->nodes as $key => $type) {
|
| 361 |
|
|
$voc_node_types[] = $node_types[$type];
|
| 362 |
|
|
}
|
| 363 |
nancyw |
1.19.2.14 |
}
|
| 364 |
|
|
else {
|
| 365 |
nancyw |
1.19.2.30 |
drupal_set_message(t('The %name vocabulary does not appear to be associated with any content types.', array('%name' => $voc->name)), 'error');
|
| 366 |
nancyw |
1.19.2.14 |
}
|
| 367 |
|
|
|
| 368 |
nancyw |
1.19.2.30 |
$count_types = count($voc_node_types);
|
| 369 |
|
|
$node_type_list = implode(', ', $voc_node_types);
|
| 370 |
|
|
|
| 371 |
nancyw |
1.19.2.5 |
$tree = taxonomy_get_tree($v);
|
| 372 |
|
|
$term_opts = array();
|
| 373 |
|
|
|
| 374 |
|
|
if ($tree) {
|
| 375 |
|
|
foreach ($tree as $term) {
|
| 376 |
nancyw |
1.19.2.31 |
$opt_string = NULL;
|
| 377 |
nancyw |
1.19.2.10 |
if ($count_nodes) {
|
| 378 |
nancyw |
1.19.2.25 |
// $count = db_result(db_query('SELECT COUNT(nid) FROM {term_node} WHERE tid=%d', $term->tid));
|
| 379 |
|
|
$count = taxonomy_term_count_nodes($term->tid);
|
| 380 |
nancyw |
1.19.2.10 |
if ($count > 0 || $show_unused) {
|
| 381 |
nancyw |
1.19.2.28 |
$opt_string = decode_entities(check_plain($term->name)) .' ('. $count .')';
|
| 382 |
nancyw |
1.19.2.10 |
}
|
| 383 |
|
|
}
|
| 384 |
|
|
else {
|
| 385 |
nancyw |
1.19.2.28 |
$opt_string = decode_entities(check_plain($term->name));
|
| 386 |
nancyw |
1.19.2.13 |
}
|
| 387 |
|
|
if ($opt_string) {
|
| 388 |
|
|
$term_opts[$term->tid] = str_repeat('-', $term->depth) . $opt_string;
|
| 389 |
nancyw |
1.19.2.10 |
}
|
| 390 |
nancyw |
1.19.2.5 |
}
|
| 391 |
|
|
}
|
| 392 |
|
|
|
| 393 |
nancyw |
1.19.2.30 |
$vocname = check_plain($voc->name);
|
| 394 |
nancyw |
1.19.2.31 |
$description = $voc->description ? decode_entities(check_markup($voc->description)) : NULL;
|
| 395 |
|
|
if (variable_get('taxonomy_browser_show_types', FALSE)) {
|
| 396 |
|
|
$description .= t('"!name" is used for: !types.', array('!name' => '<strong>'. $vocname .'</strong>', '!types' => (empty($node_type_list) ? '<em>'. t('nothing') .'</em>' : $node_type_list)));
|
| 397 |
|
|
}
|
| 398 |
|
|
|
| 399 |
nancyw |
1.19.2.10 |
if (!empty($term_opts)) {
|
| 400 |
nancyw |
1.19.2.31 |
$voc_element = array(
|
| 401 |
nancyw |
1.19.2.13 |
'#type' => $select_type,
|
| 402 |
nancyw |
1.19.2.21 |
'#title' => $vocname,
|
| 403 |
nancyw |
1.19.2.10 |
'#options' => $term_opts,
|
| 404 |
nancyw |
1.19.2.31 |
'#multiple' => TRUE,
|
| 405 |
nancyw |
1.19.2.30 |
'#description' => $description,
|
| 406 |
nancyw |
1.19.2.13 |
'#prefix' => '<div class="taxonomy_browser_'. $select_type .'">',
|
| 407 |
nancyw |
1.19.2.15 |
'#suffix' => '</div>',
|
| 408 |
nancyw |
1.19.2.14 |
'#field_suffix' => $node_type_list,
|
| 409 |
nancyw |
1.19.2.10 |
'#weight' => $i,
|
| 410 |
|
|
);
|
| 411 |
nancyw |
1.19.2.31 |
|
| 412 |
|
|
if ($collapsible) {
|
| 413 |
|
|
$fld_set = 'set'. $voc->vid;
|
| 414 |
|
|
$form['taxonomy'][$fld_set] = array(
|
| 415 |
|
|
'#type' => 'fieldset',
|
| 416 |
|
|
'#title' => $vocname,
|
| 417 |
|
|
'#collapsible' => TRUE,
|
| 418 |
|
|
'#collapsed' => TRUE,
|
| 419 |
|
|
);
|
| 420 |
|
|
$form['taxonomy'][$fld_set][$v] = $voc_element;
|
| 421 |
|
|
}
|
| 422 |
|
|
else {
|
| 423 |
|
|
$form['taxonomy'][$v] = $voc_element;
|
| 424 |
|
|
}
|
| 425 |
nancyw |
1.19.2.14 |
++$i;
|
| 426 |
nancyw |
1.19.2.10 |
}
|
| 427 |
jaza |
1.17 |
}
|
| 428 |
nancyw |
1.19.2.30 |
|
| 429 |
jaza |
1.17 |
$form['submit'] = array(
|
| 430 |
|
|
'#type' => 'submit',
|
| 431 |
|
|
'#value' => t('Search'),
|
| 432 |
nancyw |
1.19.2.5 |
'#submit' => TRUE,
|
| 433 |
nancyw |
1.19.2.14 |
);
|
| 434 |
nancyw |
1.19.2.30 |
|
| 435 |
nancyw |
1.19.2.1 |
return $form;
|
| 436 |
jaza |
1.17 |
}
|
| 437 |
|
|
|
| 438 |
|
|
/**
|
| 439 |
|
|
* Themable form output for the category browser page.
|
| 440 |
|
|
*/
|
| 441 |
|
|
function theme_taxonomy_browser_page($form) {
|
| 442 |
|
|
$output = '';
|
| 443 |
|
|
|
| 444 |
|
|
$vocabularies = variable_get('taxonomy_browser_vocabularies', array());
|
| 445 |
|
|
if (empty($vocabularies)) {
|
| 446 |
nancyw |
1.19.2.2 |
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'))));
|
| 447 |
jaza |
1.17 |
return ' ';
|
| 448 |
|
|
}
|
| 449 |
|
|
|
| 450 |
nancyw |
1.19.2.2 |
$output .= drupal_render($form);
|
| 451 |
jaza |
1.17 |
return $output;
|
| 452 |
|
|
}
|
| 453 |
|
|
|
| 454 |
|
|
/**
|
| 455 |
|
|
* Implementation of hook_form_validate().
|
| 456 |
|
|
*/
|
| 457 |
nancyw |
1.19.2.13 |
function taxonomy_browser_form_validate($form_id, &$form_values) {
|
| 458 |
|
|
|
| 459 |
|
|
$include_children = $form_values['include_children'];
|
| 460 |
|
|
$tids = _taxonomy_browser_get_tid_list($form_values['taxonomy'], $include_children);
|
| 461 |
|
|
|
| 462 |
|
|
$operator = $form_values['operator'] ? 'or' : 'and';
|
| 463 |
|
|
|
| 464 |
nancyw |
1.19.2.31 |
if ($operator == 'and' && $include_children == TRUE) {
|
| 465 |
nancyw |
1.19.2.13 |
form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));
|
| 466 |
|
|
}
|
| 467 |
|
|
|
| 468 |
jaza |
1.17 |
if (empty($tids)) {
|
| 469 |
|
|
form_set_error('taxonomy', t('You must select at least one category in your search.'));
|
| 470 |
|
|
}
|
| 471 |
|
|
else {
|
| 472 |
nancyw |
1.19.2.15 |
// $node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
|
| 473 |
|
|
$node_type = str_replace(',0', '', implode(',', $form_values['node_filter']));
|
| 474 |
nancyw |
1.19.2.18 |
|
| 475 |
jaza |
1.17 |
if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
|
| 476 |
|
|
form_set_error('taxonomy', t('No posts match your criteria.'));
|
| 477 |
|
|
}
|
| 478 |
|
|
}
|
| 479 |
|
|
}
|
| 480 |
|
|
|
| 481 |
|
|
/**
|
| 482 |
|
|
* Implementation of hook_form_submit().
|
| 483 |
|
|
*/
|
| 484 |
nancyw |
1.19.2.13 |
function taxonomy_browser_form_submit($form_id, &$form_values) {
|
| 485 |
jaza |
1.17 |
$tids = _taxonomy_browser_get_tid_list();
|
| 486 |
nancyw |
1.19.2.1 |
|
| 487 |
jaza |
1.17 |
$operator = $form_values['operator'] ? 'or' : 'and';
|
| 488 |
|
|
$str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
|
| 489 |
|
|
|
| 490 |
nancyw |
1.19.2.15 |
$types = array_filter($form_values['node_filter']);
|
| 491 |
|
|
$node_type = str_replace(',0', '', implode(',', $types));
|
| 492 |
|
|
|
| 493 |
|
|
if ($types) {
|
| 494 |
|
|
return array('taxonomy/term/'. $str_tids, 'type='. $node_type);
|
| 495 |
|
|
}
|
| 496 |
|
|
else {
|
| 497 |
|
|
return 'taxonomy/term/'. $str_tids;
|
| 498 |
|
|
}
|
| 499 |
|
|
|
| 500 |
jaza |
1.17 |
}
|
| 501 |
|
|
|
| 502 |
nancyw |
1.19.2.13 |
//********************************************************************
|
| 503 |
nancyw |
1.19.2.21 |
//* Module Functions : Private
|
| 504 |
nancyw |
1.19.2.13 |
//********************************************************************
|
| 505 |
jaza |
1.17 |
|
| 506 |
|
|
/**
|
| 507 |
nancyw |
1.19.2.21 |
* Get the output to be displayed by the block.
|
| 508 |
|
|
*
|
| 509 |
|
|
* @param
|
| 510 |
|
|
* $delta - integer for the block number.
|
| 511 |
|
|
*
|
| 512 |
|
|
* @return
|
| 513 |
|
|
* array containing the title ("subject") and content of the block.
|
| 514 |
|
|
*/
|
| 515 |
|
|
function _taxonomy_browser_block_view($delta) {
|
| 516 |
|
|
$block = array();
|
| 517 |
|
|
switch ($delta) {
|
| 518 |
|
|
case 0:
|
| 519 |
|
|
$block = array(
|
| 520 |
|
|
'content' => drupal_get_form('taxonomy_browser_form'),
|
| 521 |
|
|
);
|
| 522 |
|
|
break;
|
| 523 |
|
|
}
|
| 524 |
|
|
return $block;
|
| 525 |
|
|
}
|
| 526 |
|
|
|
| 527 |
|
|
/**
|
| 528 |
jaza |
1.17 |
* Private function to count the number of nodes found by the user's query.
|
| 529 |
|
|
*/
|
| 530 |
|
|
function taxonomy_browser_count_nodes($tids = array(), $operator = 'or', $depth = 0, $nodetype = NULL) {
|
| 531 |
|
|
if (count($tids) > 0) {
|
| 532 |
|
|
// For each term ID, generate an array of descendant term IDs to the right depth.
|
| 533 |
|
|
$descendant_tids = array();
|
| 534 |
|
|
if ($depth === 'all') {
|
| 535 |
|
|
$depth = NULL;
|
| 536 |
|
|
}
|
| 537 |
|
|
foreach ($tids as $index => $tid) {
|
| 538 |
|
|
$term = taxonomy_get_term($tid);
|
| 539 |
|
|
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
|
| 540 |
|
|
$descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
|
| 541 |
|
|
}
|
| 542 |
|
|
|
| 543 |
nancyw |
1.19.2.31 |
$type_where = NULL;
|
| 544 |
nancyw |
1.19.2.3 |
if ($nodetype) {
|
| 545 |
nancyw |
1.19.2.15 |
// $type_where = "n.type = '". db_escape_string($nodetype) ."'";
|
| 546 |
|
|
$type_where = "n.type IN ('". implode("', '", explode(',', db_escape_string($nodetype))) ."')";
|
| 547 |
nancyw |
1.19.2.3 |
}
|
| 548 |
nancyw |
1.19.2.15 |
|
| 549 |
jaza |
1.17 |
if ($operator == 'or') {
|
| 550 |
|
|
$str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
|
| 551 |
nancyw |
1.19.2.31 |
$sql_count = 'SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn USING(nid) WHERE '. ($type_where ? $type_where .' AND ' : NULL) ."tn.tid IN ($str_tids) ORDER BY n.sticky DESC, n.title ASC";
|
| 552 |
jaza |
1.17 |
}
|
| 553 |
|
|
else {
|
| 554 |
|
|
$joins = '';
|
| 555 |
|
|
$wheres = array();
|
| 556 |
nancyw |
1.19.2.3 |
if ($type_where) {
|
| 557 |
|
|
$wheres[] = $type_where;
|
| 558 |
|
|
}
|
| 559 |
jaza |
1.17 |
foreach ($descendant_tids as $index => $tids) {
|
| 560 |
nancyw |
1.19.2.13 |
$joins .= 'INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid ';
|
| 561 |
jaza |
1.17 |
$wheres[] = 'tn'. $index .'.tid IN ('. implode(',', $tids) .')';
|
| 562 |
|
|
}
|
| 563 |
nancyw |
1.19.2.13 |
$sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE '. implode(' AND ', $wheres);
|
| 564 |
jaza |
1.17 |
}
|
| 565 |
|
|
|
| 566 |
nancyw |
1.19.2.15 |
// drupal_set_message('Count query: '. $sql_count);
|
| 567 |
jaza |
1.17 |
return db_result(db_query(db_rewrite_sql($sql_count)));
|
| 568 |
|
|
}
|
| 569 |
|
|
|
| 570 |
|
|
return 0;
|
| 571 |
|
|
}
|
| 572 |
|
|
|
| 573 |
|
|
/**
|
| 574 |
|
|
* Transforms an unpredictably and irregularly nested set of tids (as returned
|
| 575 |
|
|
* from a taxonomy form) into a linear array of tids.
|
| 576 |
|
|
*/
|
| 577 |
nancyw |
1.19.2.31 |
function _taxonomy_browser_get_tid_list($tids = NULL, $include_children = FALSE) {
|
| 578 |
jaza |
1.17 |
static $tid_list;
|
| 579 |
nancyw |
1.19.2.13 |
|
| 580 |
jaza |
1.17 |
if (isset($tids) && is_array($tids)) {
|
| 581 |
|
|
$tid_list = array();
|
| 582 |
|
|
foreach ($tids as $key => $tid) {
|
| 583 |
|
|
if (!empty($tid)) {
|
| 584 |
|
|
if (is_array($tid)) {
|
| 585 |
|
|
foreach ($tid as $key2 => $tid2) {
|
| 586 |
|
|
if (!empty($tid2)) {
|
| 587 |
nancyw |
1.19.2.13 |
$tid_list[$tid2] = $tid2;
|
| 588 |
jaza |
1.17 |
}
|
| 589 |
|
|
}
|
| 590 |
|
|
}
|
| 591 |
|
|
else {
|
| 592 |
nancyw |
1.19.2.13 |
$tid_list[$tid] = $tid;
|
| 593 |
jaza |
1.17 |
}
|
| 594 |
nancyw |
1.19.2.13 |
} /* end !empty */
|
| 595 |
|
|
} /* end foreach */
|
| 596 |
jaza |
1.17 |
}
|
| 597 |
|
|
|
| 598 |
nancyw |
1.19.2.13 |
if ($include_children) {
|
| 599 |
|
|
foreach ($tid_list as $tid) {
|
| 600 |
|
|
_taxonomy_browser_get_kids($tid_list, $tid);
|
| 601 |
|
|
}
|
| 602 |
|
|
}
|
| 603 |
|
|
|
| 604 |
jaza |
1.17 |
return $tid_list;
|
| 605 |
|
|
}
|
| 606 |
|
|
|
| 607 |
nancyw |
1.19.2.13 |
function _taxonomy_browser_get_kids(&$tid_list, $tid) {
|
| 608 |
|
|
$children = taxonomy_get_children($tid);
|
| 609 |
|
|
if ($children) {
|
| 610 |
|
|
foreach ($children as $child_tid => $child_term) {
|
| 611 |
|
|
_taxonomy_browser_get_kids($tid_list, $child_tid);
|
| 612 |
|
|
}
|
| 613 |
|
|
}
|
| 614 |
|
|
else {
|
| 615 |
|
|
$tid_list[$tid] = $tid;
|
| 616 |
|
|
}
|
| 617 |
|
|
}
|
| 618 |
|
|
|
| 619 |
jaza |
1.17 |
/**
|
| 620 |
|
|
* Provides default guideline text.
|
| 621 |
|
|
*/
|
| 622 |
|
|
function _taxonomy_browser_guidelines_default() {
|
| 623 |
|
|
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>');
|
| 624 |
|
|
}
|