/[drupal]/contributions/modules/taxonomy_browser/taxonomy_browser.module
ViewVC logotype

Contents of /contributions/modules/taxonomy_browser/taxonomy_browser.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.19.2.11.2.23 - (show annotations) (download) (as text)
Sat Feb 7 02:20:02 2009 UTC (9 months, 2 weeks ago) by nancyw
Branch: DRUPAL-6--1
Changes since 1.19.2.11.2.22: +16 -8 lines
File MIME type: text/x-php
#370196 by NancyDru for mathieso - Added ability to move vocab description and made sure vocab name starts on new line.
1 <?php
2 // $Id: taxonomy_browser.module,v 1.19.2.11.2.22 2008/09/25 20:43:33 nancyw Exp $
3 // 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() {
19 $items = array();
20
21 $items['taxonomy_browser'] = array(
22 'title' => 'Category Browser',
23 'page callback' => 'taxonomy_browser_page',
24 'access arguments' => array('access content'),
25 'description' => 'Find content on your own terms.',
26 );
27
28 $items['admin/settings/taxonomy-browser'] = array(
29 'title' => 'Taxonomy Browser',
30 'description' => 'Set usage guidelines and included vocabularies.',
31 'page callback' => 'drupal_get_form',
32 'page arguments' => array('taxonomy_browser_admin_settings'),
33 'access arguments' => array('administer site configuration'),
34 );
35
36 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().
48 */
49 function taxonomy_browser_help($path, $arg) {
50 switch ($path) {
51 case 'admin/modules#description':
52 return t('An interface for viewing content grouped by arbitrary taxonomy terms.');
53 case 'taxonomy_browser':
54 $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 * 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_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() {
109 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.'), 'status');
111 }
112
113 drupal_add_js(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.js', 'module');
114
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 '#rows' => 2,
120 '#description' => t('Instructions which should appear at top of the category browser main page'),
121 );
122
123 $form['taxonomy_browser_select_type'] = array(
124 '#title' => t('Selection type'),
125 '#type' => 'radios',
126 '#default_value' => variable_get('taxonomy_browser_select_type', 1),
127 '#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.'),
129 '#prefix' => '<div class="taxonomy_browser_radios">',
130 '#suffix' => '</div>',
131 );
132
133 $form['taxonomy_browser_collapse'] = array(
134 '#title' => t('Make vocabularies collapsible'),
135 '#type' => 'radios',
136 '#options' => array('Not collapsible', 'Collapsed by default', 'Collapsible, but not collapsed'),
137 '#default_value' => variable_get('taxonomy_browser_collapse', 0),
138 '#description' => t('Do you want to display of the terms within a vocabulary to be collapsible? Requires "Check boxes" above.'),
139 '#prefix' => '<div class="taxonomy_browser_radios">',
140 '#suffix' => '</div>',
141 );
142
143 $form['taxonomy_browser_default_op'] = array(
144 '#title' => t('"Items containing" default'),
145 '#type' => 'radios',
146 '#default_value' => variable_get('taxonomy_browser_default_op', 0),
147 '#options' => array(t('All'), t('Any')),
148 '#description' => t('This option determines which "Items containing" choice is the default.'),
149 '#prefix' => '<div class="taxonomy_browser_radios">',
150 '#suffix' => '</div>',
151 );
152
153 $form['taxonomy_browser_count_nodes'] = array(
154 '#title' => t('Show node count'),
155 '#type' => 'checkbox',
156 '#default_value' => variable_get('taxonomy_browser_count_nodes', FALSE),
157 '#description' => t('Do you want to display the count of nodes tagged with each term? This can be SQL-intensive.'),
158 );
159
160 $form['taxonomy_browser_show_unused'] = array(
161 '#title' => t('Show unused terms'),
162 '#type' => 'checkbox',
163 '#default_value' => variable_get('taxonomy_browser_show_unused', FALSE),
164 '#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.'),
165 );
166
167 $form['taxonomy_browser_allow_children'] = array(
168 '#title' => t('Allow child terms to be included'),
169 '#type' => 'checkbox',
170 '#default_value' => variable_get('taxonomy_browser_allow_children', FALSE),
171 '#description' => t('Do you want the user to see a check box to include child terms (sub-terms)?'),
172 );
173
174 $form['taxonomy_browser_need_perm'] = array(
175 '#title' => t('Requires permission'),
176 '#type' => 'checkbox',
177 '#default_value' => variable_get('taxonomy_browser_need_perm', FALSE),
178 '#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'))),
179 );
180
181 $form['taxonomy_browser_show_types'] = array(
182 '#title' => t('Show content types with vocabulary'),
183 '#type' => 'checkbox',
184 '#default_value' => variable_get('taxonomy_browser_show_types', FALSE),
185 '#description' => t('If checked, this option displays a list of the content types for which this vocabulary may be used.'),
186 );
187
188 $select = array();
189 $vocabularies = taxonomy_get_vocabularies();
190 foreach ($vocabularies as $vocabulary) {
191 $select[$vocabulary->vid] = $vocabulary->name;
192 }
193
194 $form['taxonomy_browser_vocabularies'] = array(
195 '#title' => t('Included Vocabularies'),
196 '#type' => 'checkboxes',
197 '#default_value' => variable_get('taxonomy_browser_vocabularies', array()),
198 '#options' => $select,
199 '#description' => t('Select the vocabularies the user can select from on the category browser page.'),
200 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
201 '#suffix' => '</div>',
202 );
203
204 if (module_exists('node_type_filter')) {
205 $filter_options = node_get_types('names');
206 $form['taxonomy_browser_omit'] = array(
207 '#type' => 'checkboxes',
208 '#title' => t('Omit content types'),
209 '#options' => $filter_options,
210 '#default_value' => variable_get('taxonomy_browser_omit', array('')),
211 '#description' => t('If any of these types is selected, it will be omitted from the list on the "Category Browser" page.'),
212 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
213 '#suffix' => '</div>',
214 );
215 }
216
217 return system_settings_form($form);
218 }
219
220 function taxonomy_browser_admin_settings_validate($form, &$form_state) {
221 if ($form_state['values']['taxonomy_browser_count_nodes'] == FALSE
222 && $form_state['values']['taxonomy_browser_show_unused'] == TRUE) {
223 form_set_error('taxonomy_browser_show_unused', t('"Show unused" requires "count nodes."'));
224 }
225 if ($form_state['values']['taxonomy_browser_collapse'] != 0
226 && $form_state['values']['taxonomy_browser_select_type'] != 1) {
227 form_set_error('taxonomy_browser_collapse', t('"Make vocabularies collapsible" requires "Check boxes."'));
228 }
229 }
230
231 //********************************************************************
232 //* Module Functions : Public
233 //********************************************************************
234
235 /**
236 * Menu callback: the query building interface for nodes selected based on
237 * taxonomy terms.
238 */
239 function taxonomy_browser_page() {
240 $output .= drupal_get_form('taxonomy_browser_form');
241 return $output;
242 }
243
244 function taxonomy_browser_form() {
245 $form = array();
246 $selection_types = array('select', 'checkboxes');
247 $select_type = $selection_types[variable_get('taxonomy_browser_select_type', 1)];
248 $collapsible = variable_get('taxonomy_browser_collapse', 0);
249 $count_nodes = variable_get('taxonomy_browser_count_nodes', FALSE);
250 $show_unused = variable_get('taxonomy_browser_show_unused', FALSE);
251 $allow_children = variable_get('taxonomy_browser_allow_children', FALSE);
252 $node_types = node_get_types('names');
253
254 $form['scope'] = array(
255 '#type' => 'fieldset',
256 '#title' => t('Scope'),
257 '#collapsible' => TRUE,
258 '#collapsed' => FALSE,
259 '#attributes' => array('class' => 'taxonomy_browser_scope'),
260 );
261
262 if (module_exists('node_type_filter')) {
263 if ($count_nodes) {
264 $total_count = 0;
265 $result = db_query('SELECT DISTINCT(type), COUNT(nid) AS count FROM {node} WHERE status=1 GROUP BY type ORDER BY type');
266 while ($counter = db_fetch_array($result)) {
267 // Check if we know about this type - a disabled module could have orphans.
268 if (isset($node_types[$counter['type']])) {
269 $node_types[$counter['type']] .= ' ('. $counter['count'] .')';
270 }
271 else {
272 $node_types['unknown'] .= $counter['type'] .' ??? ('. $counter['count'] .') ';
273 watchdog('Taxonomy Browser', 'Unknown content type found: @type', array('@type' => $counter['type']), WATCHDOG_WARNING);
274 }
275 $total_count += $counter['count'];
276 }
277 }
278
279 $omit = array_filter(variable_get('taxonomy_browser_omit', array()));
280 if (!empty($omit)) {
281 foreach ($omit as $omit_type) {
282 unset($node_types[$omit_type]);
283 }
284 }
285 $desc = t('Not selecting any type is the same as selecting all types.');
286 if ($count_nodes) {
287 $desc .= ' '. t('The total count of all types is !count.', array('!count' => $total_count));
288 }
289 $form['scope']['node_filter'] = array(
290 '#type' => $select_type,
291 '#title' => t('Restrict search by content type'),
292 '#options' => $node_types,
293 '#multiple' => TRUE,
294 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
295 '#suffix' => '</div>',
296 '#description' => $desc,
297 );
298 }
299 else {
300 $form['scope']['node_filter'] = array(
301 '#type' => value,
302 '#value' => array(),
303 );
304 }
305
306 $form['scope']['operator'] = array(
307 '#type' => 'radios',
308 '#title' => t('Items containing'),
309 '#options' => array(t('<strong>all</strong> terms'), t('<strong>any</strong> terms')),
310 '#default_value' => variable_get('taxonomy_browser_default_op', 0),
311 '#prefix' => '<div class="taxonomy_browser_radios">',
312 '#suffix' => '</div>',
313 );
314
315 $vocabularies = array_filter(variable_get('taxonomy_browser_vocabularies', array()));
316 // Has the admin selected any vocabs?
317 if (count($vocabularies) == 0) {
318 $vocabs = taxonomy_get_vocabularies();
319 foreach ($vocabs as $vocabulary) {
320 $vocabularies[$vocabulary->vid] = 1;
321 }
322 }
323
324 if ($allow_children) {
325 $form['children'] = array(
326 '#type' => 'fieldset',
327 '#title' => t('Include Children'),
328 '#collapsible' => TRUE,
329 '#collapsed' => FALSE,
330 );
331 $form['children']['include_children'] = array(
332 '#type' => 'checkbox',
333 '#title' => t('Automatically include children (sub-terms)'),
334 '#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."'),
335 );
336 }
337 else {
338 $form['include_children'] = array(
339 '#type' => 'value',
340 '#value' => FALSE,
341 );
342 }
343
344 $form['taxonomy'] = array(
345 '#type' => 'fieldset',
346 '#title' => t('Categories'),
347 '#collapsible' => TRUE,
348 '#collapsed' => FALSE,
349 '#tree' => TRUE,
350 );
351
352 $selection_types = array('select', 'checkboxes');
353 $i = 0;
354 foreach ($vocabularies as $v => $sel) {
355 $voc = taxonomy_vocabulary_load($v);
356
357 $voc_node_types = array();
358 if (isset($voc->nodes) && !empty($voc->nodes)) {
359 foreach ($voc->nodes as $key => $type) {
360 $voc_node_types[] = $node_types[$type];
361 }
362 }
363 else {
364 drupal_set_message(t('The %name vocabulary does not appear to be associated with any content types.', array('%name' => $voc->name)), 'warning');
365 }
366
367 $count_types = count($voc_node_types);
368 if (count($voc_node_types) == 1) {
369 $node_type_list = $voc_node_types[0];
370 }
371 else {
372 $node_type_list = implode(', ', $voc_node_types);
373 }
374
375 $tree = taxonomy_get_tree($v);
376 $term_opts = array();
377
378 if ($tree) {
379 foreach ($tree as $term) {
380 $opt_string = NULL;
381 if ($count_nodes) {
382 $count = taxonomy_term_count_nodes($term->tid);
383 if ($count > 0 || $show_unused) {
384 $opt_string = decode_entities(check_plain($term->name)) .' ('. $count .')';
385 }
386 }
387 else {
388 $opt_string = decode_entities(check_plain($term->name));
389 }
390 if ($opt_string) {
391 $term_opts[$term->tid] = str_repeat('-', $term->depth) . $opt_string;
392 }
393 }
394 }
395
396 $vocname = check_plain($voc->name);
397 $description = $voc->description ? check_markup($voc->description) : NULL;
398 if (variable_get('taxonomy_browser_show_types', FALSE)) {
399 $used_for = t('"!name" is used for: !types.', array('!name' => '<strong>'. $vocname .'</strong>', '!types' => (empty($node_type_list) ? '<em>'. t('nothing') .'</em>' : $node_type_list)));
400 }
401
402 if (!empty($term_opts)) {
403 $voc_element = array(
404 '#type' => $select_type,
405 '#title' => $vocname,
406 '#options' => $term_opts,
407 '#multiple' => TRUE,
408 '#description' => $collapsible ? $used_for : $description . $used_for,
409 '#prefix' => '<div class="taxonomy_browser_'. $select_type .'">',
410 '#suffix' => '</div>',
411 '#field_suffix' => $node_type_list,
412 '#weight' => $i,
413 );
414
415 if ($collapsible) {
416 $fld_set = 'set'. $voc->vid;
417 $form['taxonomy'][$fld_set] = array(
418 '#type' => 'fieldset',
419 '#title' => $vocname,
420 '#collapsible' => TRUE,
421 '#collapsed' => $collapsible == 1,
422 '#description' => $description,
423 );
424 $form['taxonomy'][$fld_set][$v] = $voc_element;
425 }
426 else {
427 $form['taxonomy'][$v] = $voc_element;
428 }
429 ++$i;
430 }
431 }
432
433 $form['submit'] = array(
434 '#type' => 'submit',
435 '#value' => t('Search'),
436 // '#submit' => TRUE,
437 );
438
439 return $form;
440 }
441
442 /**
443 * Implementation of hook_theme().
444 */
445 function taxonomy_browser_theme() {
446 return array(
447 'taxonomy_browser_page' => array(
448 'arguments' => array('form'),
449 ),
450 );
451 }
452
453 /**
454 * Themable form output for the category browser page.
455 */
456 function theme_taxonomy_browser_page($form) {
457 $output = '';
458
459 $vocabularies = variable_get('taxonomy_browser_vocabularies', array());
460 if (empty($vocabularies)) {
461 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'))));
462 return ' ';
463 }
464
465 $output .= drupal_render($form);
466 return $output;
467 }
468
469 /**
470 * Implementation of hook_form_validate().
471 */
472 function taxonomy_browser_form_validate($form, &$form_state) {
473
474 $include_children = $form_state['values']['include_children'];
475 $tids = _taxonomy_browser_get_tid_list($form_state['values']['taxonomy'], $include_children);
476
477 $operator = $form_state['values']['operator'] ? 'or' : 'and';
478
479 if ($operator == 'and' && $include_children == TRUE) {
480 form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));
481 }
482
483 if (empty($tids)) {
484 form_set_error('taxonomy', t('You must select at least one category in your search.'));
485 }
486 else {
487 // $node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
488 $node_type = str_replace(',0', '', implode(',', $form_state['values']['node_filter']));
489
490 if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
491 form_set_error('taxonomy', t('No posts match your criteria.'));
492 }
493 }
494 }
495
496 /**
497 * Implementation of hook_form_submit().
498 */
499 function taxonomy_browser_form_submit($form, &$form_state) {
500 $tids = _taxonomy_browser_get_tid_list();
501
502 $operator = $form_state['values']['operator'] ? 'or' : 'and';
503 $str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
504
505 $types = array_filter($form_state['values']['node_filter']);
506 $node_type = str_replace(',0', '', implode(',', $types));
507
508 if ($types) {
509 $redir = array('taxonomy/term/'. $str_tids, (isset($node_type) ? 'type='. $node_type : ''));
510 }
511 else {
512 $redir = 'taxonomy/term/'. $str_tids;
513 }
514
515 $form_state['redirect'] = $redir;
516 }
517
518 //********************************************************************
519 //* Module Functions : Private
520 //********************************************************************
521
522 /**
523 * Get the output to be displayed by the block.
524 *
525 * @param
526 * $delta - integer for the block number.
527 *
528 * @return
529 * array containing the title ("subject") and content of the block.
530 */
531 function _taxonomy_browser_block_view($delta) {
532 $block = array();
533 switch ($delta) {
534 case 0:
535 $block = array(
536 'content' => drupal_get_form('taxonomy_browser_form'),
537 );
538 break;
539 }
540 return $block;
541 }
542
543 /**
544 * Private function to count the number of nodes found by the user's query.
545 */
546 function taxonomy_browser_count_nodes($tids = array(), $operator = 'or', $depth = 0, $nodetype = NULL) {
547 if (count($tids) > 0) {
548 // For each term ID, generate an array of descendant term IDs to the right depth.
549 $descendant_tids = array();
550 if ($depth === 'all') {
551 $depth = NULL;
552 }
553 foreach ($tids as $index => $tid) {
554 $term = taxonomy_get_term($tid);
555 $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
556 $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
557 }
558
559 $type_where = NULL;
560 if ($nodetype) {
561 // $type_where = "n.type = '". db_escape_string($nodetype) ."'";
562 $type_where = "n.type IN ('". implode("', '", explode(',', db_escape_string($nodetype))) ."')";
563 }
564
565 if ($operator == 'or') {
566 $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
567
568 $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";
569 }
570 else {
571 $joins = '';
572 $wheres = array();
573 if ($type_where) {
574 $wheres[] = $type_where;
575 }
576 foreach ($descendant_tids as $index => $tids) {
577 $joins .= 'INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid ';
578 $wheres[] = 'tn'. $index .'.tid IN ('. implode(',', $tids) .')';
579 }
580 $sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE '. implode(' AND ', $wheres);
581 }
582
583 return db_result(db_query(db_rewrite_sql($sql_count)));
584 }
585
586 return 0;
587 }
588
589 /**
590 * Transforms an unpredictably and irregularly nested set of tids (as returned
591 * from a taxonomy form) into a linear array of tids.
592 */
593 function _taxonomy_browser_get_tid_list($tids = NULL, $include_children = FALSE) {
594 static $tid_list;
595
596 if (isset($tids) && is_array($tids)) {
597 $tid_list = array();
598 foreach ($tids as $key => $tid) {
599 if (!empty($tid)) {
600 if (is_array($tid)) {
601 foreach ($tid as $key2 => $tid2) {
602 if (!empty($tid2)) {
603 if (is_array($tid2)) {
604 foreach ($tid2 as $key3 => $tid3) {
605 if (!empty($tid3)) {
606 $tid_list[$tid3] = $tid3;
607 }
608 }
609 }
610 else {
611 $tid_list[$tid2] = $tid2;
612 }
613 }
614 }
615 }
616 else {
617 $tid_list[$tid] = $tid;
618 }
619 }
620 }
621 }
622
623 if ($include_children) {
624 foreach ($tid_list as $tid) {
625 _taxonomy_browser_get_kids($tid_list, $tid);
626 }
627 }
628
629 return $tid_list;
630 }
631
632 function _taxonomy_browser_get_kids(&$tid_list, $tid) {
633 $children = taxonomy_get_children($tid);
634 if ($children) {
635 foreach ($children as $child_tid => $child_term) {
636 _taxonomy_browser_get_kids($tid_list, $child_tid);
637 }
638 }
639 else {
640 $tid_list[$tid] = $tid;
641 }
642 }
643
644 /**
645 * Provides default guideline text.
646 */
647 function _taxonomy_browser_guidelines_default() {
648 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>');
649 }

  ViewVC Help
Powered by ViewVC 1.1.2