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