/[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.33 - (show annotations) (download) (as text)
Sat Feb 7 02:20:24 2009 UTC (9 months, 2 weeks ago) by nancyw
Branch: DRUPAL-5
Changes since 1.19.2.32: +17 -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.32 2008/09/25 20:43:50 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($may_cache) {
19 $items = array();
20
21 if ($may_cache) {
22 $items[] = array(
23 '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 $items[] = array(
33 'path' => 'taxonomy_browser',
34 'title' => t('Category Browser'),
35 'access' => user_access(variable_get('taxonomy_browser_need_perm', FALSE) ? 'access taxonomy browser' : 'access content'),
36 'callback' => 'taxonomy_browser_page',
37 'description' => t('Find content on your own terms.'),
38 'type' => MENU_NORMAL_ITEM,
39 );
40
41 drupal_add_css(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.css');
42 }
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 $output = check_markup(variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()));
56 if (user_access('administer site configuration')) {
57 $output .= '<p class="links">'. l(t('Go to Taxonomy Browser settings'), 'admin/settings/taxonomy-browser', array(), drupal_get_destination()) .'</p>';
58 }
59 return $output;
60 }
61 }
62
63 /**
64 * Implementation of hook_perm().
65 */
66 function taxonomy_browser_perm() {
67 if (variable_get('taxonomy_browser_need_perm', FALSE)) {
68 return array('access taxonomy browser');
69 }
70 else {
71 return array();
72 }
73 }
74
75 //********************************************************************
76 //* Drupal Hooks : Core
77 //********************************************************************
78
79 /**
80 * 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 switch ($delta) {
91 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 * Implementation of hook_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?'),
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 $current_vocabs = variable_get('taxonomy_browser_vocabularies', array());
195 // Occasionally we get a 0 vid in the array.
196 unset($current_vocabs[0]);
197
198 $form['taxonomy_browser_vocabularies'] = array(
199 '#title' => t('Included Vocabularies'),
200 '#type' => 'checkboxes',
201 '#default_value' => $current_vocabs,
202 '#options' => $select,
203 '#description' => t('Select the vocabularies the user can select from on the category browser page.'),
204 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
205 '#suffix' => '</div>',
206 );
207
208 if (module_exists('node_type_filter')) {
209 $filter_options = node_get_types('names');
210 $form['taxonomy_browser_omit'] = array(
211 '#type' => 'checkboxes',
212 '#title' => t('Omit content types'),
213 '#options' => $filter_options,
214 '#default_value' => variable_get('taxonomy_browser_omit', array('')),
215 '#description' => t('If any of these types is selected, it will be omitted from the list on the "Category Browser" page.'),
216 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
217 '#suffix' => '</div>',
218 );
219 }
220
221 return system_settings_form($form);
222 }
223
224 function taxonomy_browser_admin_settings_validate($form_id, &$form_values) {
225 if ($form_values['taxonomy_browser_count_nodes'] == FALSE
226 && $form_values['taxonomy_browser_show_unused'] == TRUE) {
227 form_set_error('taxonomy_browser_show_unused', t('"Show unused" requires "count nodes."'));
228 }
229 if ($form_values['taxonomy_browser_collapse'] != 0
230 && $form_values['taxonomy_browser_select_type'] != 1) {
231 form_set_error('taxonomy_browser_collapse', t('"Make vocabularies collapsible" requires "Check boxes."'));
232 }
233 }
234
235
236 //********************************************************************
237 //* Module Functions : Public
238 //********************************************************************
239
240 /**
241 * Menu callback: the query building interface for nodes selected based on
242 * taxonomy terms.
243 */
244 function taxonomy_browser_page() {
245 $output .= drupal_get_form('taxonomy_browser_form');
246 return $output;
247 }
248
249 function taxonomy_browser_form() {
250 $form = array();
251 $selection_types = array('select', 'checkboxes');
252 $select_type = $selection_types[variable_get('taxonomy_browser_select_type', 1)];
253 $collapsible = variable_get('taxonomy_browser_collapse', 0);
254 $count_nodes = variable_get('taxonomy_browser_count_nodes', FALSE);
255 $show_unused = variable_get('taxonomy_browser_show_unused', FALSE);
256 $allow_children = variable_get('taxonomy_browser_allow_children', FALSE);
257 $node_types = node_get_types('names');
258
259 $form['scope'] = array(
260 '#type' => 'fieldset',
261 '#title' => t('Scope'),
262 '#collapsible' => TRUE,
263 '#collapsed' => FALSE,
264 '#attributes' => array('class' => 'taxonomy_browser_scope'),
265 );
266
267 if (module_exists('node_type_filter')) {
268 if ($count_nodes) {
269 $total_count = 0;
270 $result = db_query('SELECT DISTINCT(type), COUNT(nid) AS count FROM {node} WHERE status=1 GROUP BY type ORDER BY type');
271 while ($counter = db_fetch_array($result)) {
272 // Check if we know about this type - a disabled module could have orphans.
273 if (isset($node_types[$counter['type']])) {
274 $node_types[$counter['type']] .= ' ('. $counter['count'] .')';
275 }
276 else {
277 $node_types['unknown'] .= $counter['type'] .' ??? ('. $counter['count'] .') ';
278 watchdog('Taxonomy Browser', 'Unknown content type found: @type', array('@type' => $counter['type']), WATCHDOG_WARNING);
279 }
280 $total_count += $counter['count'];
281 }
282 }
283
284 $omit = array_filter(variable_get('taxonomy_browser_omit', array()));
285 if (!empty($omit)) {
286 foreach ($omit as $omit_type) {
287 unset($node_types[$omit_type]);
288 }
289 }
290 $desc = t('Not selecting any type is the same as selecting all types.');
291 if ($count_nodes) {
292 $desc .= ' '. t('The total count of all types is !count.', array('!count' => $total_count));
293 }
294 $form['scope']['node_filter'] = array(
295 // '#type' => 'checkboxes',
296 '#type' => $select_type,
297 '#title' => t('Restrict search by content type'),
298 '#options' => $node_types,
299 '#multiple' => TRUE,
300 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
301 '#suffix' => '</div>',
302 '#description' => $desc,
303 );
304 }
305 else {
306 $form['scope']['node_filter'] = array(
307 '#type' => value,
308 '#value' => array(),
309 );
310 }
311
312 $form['scope']['operator'] = array(
313 '#type' => 'radios',
314 '#title' => t('Items containing'),
315 '#options' => array(t('<strong>all</strong> terms'), t('<strong>any</strong> terms')),
316 '#default_value' => variable_get('taxonomy_browser_default_op', 0),
317 '#prefix' => '<div class="taxonomy_browser_radios">',
318 '#suffix' => '</div>',
319 );
320
321 $vocabularies = array_filter(variable_get('taxonomy_browser_vocabularies', array()));
322 // Occasionally we get a 0 vid in the array.
323 unset($vocabularies[0]);
324
325 // Has the admin selected any vocabs?
326 if (count($vocabularies) == 0) {
327 $vocabs = taxonomy_get_vocabularies();
328 foreach ($vocabs as $vocabulary) {
329 $vocabularies[$vocabulary->vid] = 1;
330 }
331 }
332
333 if ($allow_children) {
334 $form['children'] = array(
335 '#type' => 'fieldset',
336 '#title' => t('Include Children'),
337 '#collapsible' => TRUE,
338 '#collapsed' => FALSE,
339 );
340 $form['children']['include_children'] = array(
341 '#type' => 'checkbox',
342 '#title' => t('Automatically include children (sub-terms)'),
343 '#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."'),
344 );
345 }
346 else {
347 $form['include_children'] = array(
348 '#type' => 'value',
349 '#value' => FALSE,
350 );
351 }
352
353 $form['taxonomy'] = array(
354 '#type' => 'fieldset',
355 '#title' => t('Categories'),
356 '#collapsible' => TRUE,
357 '#collapsed' => FALSE,
358 '#tree' => TRUE,
359 );
360
361 $selection_types = array('select', 'checkboxes');
362 $i = 0;
363 foreach ($vocabularies as $v => $sel) {
364 $voc = taxonomy_get_vocabulary($v);
365
366 $voc_node_types = array();
367 if (isset($voc->nodes) && !empty($voc->nodes[0])) {
368 foreach ($voc->nodes as $key => $type) {
369 $voc_node_types[] = $node_types[$type];
370 }
371 }
372 else {
373 drupal_set_message(t('The %name vocabulary does not appear to be associated with any content types.', array('%name' => $voc->name)), 'error');
374 }
375
376 $count_types = count($voc_node_types);
377 $node_type_list = implode(', ', $voc_node_types);
378
379 $tree = taxonomy_get_tree($v);
380 $term_opts = array();
381
382 if ($tree) {
383 foreach ($tree as $term) {
384 $opt_string = NULL;
385 if ($count_nodes) {
386 // $count = db_result(db_query('SELECT COUNT(nid) FROM {term_node} WHERE tid=%d', $term->tid));
387 $count = taxonomy_term_count_nodes($term->tid);
388 if ($count > 0 || $show_unused) {
389 $opt_string = decode_entities(check_plain($term->name)) .' ('. $count .')';
390 }
391 }
392 else {
393 $opt_string = decode_entities(check_plain($term->name));
394 }
395 if ($opt_string) {
396 $term_opts[$term->tid] = str_repeat('-', $term->depth) . $opt_string;
397 }
398 }
399 }
400
401 $vocname = check_plain($voc->name);
402 $description = $voc->description ? check_markup($voc->description) : NULL;
403 if (variable_get('taxonomy_browser_show_types', FALSE)) {
404 $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)));
405 }
406
407 if (!empty($term_opts)) {
408 $voc_element = array(
409 '#type' => $select_type,
410 '#title' => $vocname,
411 '#options' => $term_opts,
412 '#multiple' => TRUE,
413 '#description' => $collapsible ? $used_for : $description . $used_for,
414 '#prefix' => '<div class="taxonomy_browser_'. $select_type .'">',
415 '#suffix' => '</div>',
416 '#field_suffix' => $node_type_list,
417 '#weight' => $i,
418 );
419
420 if ($collapsible) {
421 $fld_set = 'set'. $voc->vid;
422 $form['taxonomy'][$fld_set] = array(
423 '#type' => 'fieldset',
424 '#title' => $vocname,
425 '#collapsible' => TRUE,
426 '#collapsed' => $collapsible == 1,
427 '#description' => $description,
428 );
429 $form['taxonomy'][$fld_set][$v] = $voc_element;
430 }
431 else {
432 $form['taxonomy'][$v] = $voc_element;
433 }
434 ++$i;
435 }
436 }
437
438 $form['submit'] = array(
439 '#type' => 'submit',
440 '#value' => t('Search'),
441 '#submit' => TRUE,
442 );
443
444 return $form;
445 }
446
447 /**
448 * Themable form output for the category browser page.
449 */
450 function theme_taxonomy_browser_page($form) {
451 $output = '';
452
453 $vocabularies = variable_get('taxonomy_browser_vocabularies', array());
454 if (empty($vocabularies)) {
455 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'))));
456 return ' ';
457 }
458
459 $output .= drupal_render($form);
460 return $output;
461 }
462
463 /**
464 * Implementation of hook_form_validate().
465 */
466 function taxonomy_browser_form_validate($form_id, &$form_values) {
467
468 $include_children = $form_values['include_children'];
469 $tids = _taxonomy_browser_get_tid_list($form_values['taxonomy'], $include_children);
470
471 $operator = $form_values['operator'] ? 'or' : 'and';
472
473 if ($operator == 'and' && $include_children == TRUE) {
474 form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));
475 }
476
477 if (empty($tids)) {
478 form_set_error('taxonomy', t('You must select at least one category in your search.'));
479 }
480 else {
481 // $node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
482 $node_type = str_replace(',0', '', implode(',', $form_values['node_filter']));
483
484 if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
485 form_set_error('taxonomy', t('No posts match your criteria.'));
486 }
487 }
488 }
489
490 /**
491 * Implementation of hook_form_submit().
492 */
493 function taxonomy_browser_form_submit($form_id, &$form_values) {
494 $tids = _taxonomy_browser_get_tid_list();
495
496 $operator = $form_values['operator'] ? 'or' : 'and';
497 $str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
498
499 $types = array_filter($form_values['node_filter']);
500 $node_type = str_replace(',0', '', implode(',', $types));
501
502 if ($types) {
503 return array('taxonomy/term/'. $str_tids, 'type='. $node_type);
504 }
505 else {
506 return 'taxonomy/term/'. $str_tids;
507 }
508
509 }
510
511 //********************************************************************
512 //* Module Functions : Private
513 //********************************************************************
514
515 /**
516 * Get the output to be displayed by the block.
517 *
518 * @param
519 * $delta - integer for the block number.
520 *
521 * @return
522 * array containing the title ("subject") and content of the block.
523 */
524 function _taxonomy_browser_block_view($delta) {
525 $block = array();
526 switch ($delta) {
527 case 0:
528 $block = array(
529 'content' => drupal_get_form('taxonomy_browser_form'),
530 );
531 break;
532 }
533 return $block;
534 }
535
536 /**
537 * Private function to count the number of nodes found by the user's query.
538 */
539 function taxonomy_browser_count_nodes($tids = array(), $operator = 'or', $depth = 0, $nodetype = NULL) {
540 if (count($tids) > 0) {
541 // For each term ID, generate an array of descendant term IDs to the right depth.
542 $descendant_tids = array();
543 if ($depth === 'all') {
544 $depth = NULL;
545 }
546 foreach ($tids as $index => $tid) {
547 $term = taxonomy_get_term($tid);
548 $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
549 $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
550 }
551
552 $type_where = NULL;
553 if ($nodetype) {
554 // $type_where = "n.type = '". db_escape_string($nodetype) ."'";
555 $type_where = "n.type IN ('". implode("', '", explode(',', db_escape_string($nodetype))) ."')";
556 }
557
558 if ($operator == 'or') {
559 $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
560 $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";
561 }
562 else {
563 $joins = '';
564 $wheres = array();
565 if ($type_where) {
566 $wheres[] = $type_where;
567 }
568 foreach ($descendant_tids as $index => $tids) {
569 $joins .= 'INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid ';
570 $wheres[] = 'tn'. $index .'.tid IN ('. implode(',', $tids) .')';
571 }
572 $sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE '. implode(' AND ', $wheres);
573 }
574
575 // drupal_set_message('Count query: '. $sql_count);
576 return db_result(db_query(db_rewrite_sql($sql_count)));
577 }
578
579 return 0;
580 }
581
582 /**
583 * Transforms an unpredictably and irregularly nested set of tids (as returned
584 * from a taxonomy form) into a linear array of tids.
585 */
586 function _taxonomy_browser_get_tid_list($tids = NULL, $include_children = FALSE) {
587 static $tid_list;
588
589 if (isset($tids) && is_array($tids)) {
590 $tid_list = array();
591 foreach ($tids as $key => $tid) {
592 if (!empty($tid)) {
593 if (is_array($tid)) {
594 foreach ($tid as $key2 => $tid2) {
595 if (!empty($tid2)) {
596 if (is_array($tid2)) {
597 foreach ($tid2 as $key3 => $tid3) {
598 if (!empty($tid3)) {
599 $tid_list[$tid3] = $tid3;
600 }
601 }
602 }
603 else {
604 $tid_list[$tid2] = $tid2;
605 }
606 }
607 }
608 }
609 else {
610 $tid_list[$tid] = $tid;
611 }
612 }
613 }
614 }
615
616 if ($include_children) {
617 foreach ($tid_list as $tid) {
618 _taxonomy_browser_get_kids($tid_list, $tid);
619 }
620 }
621
622 return $tid_list;
623 }
624
625 function _taxonomy_browser_get_kids(&$tid_list, $tid) {
626 $children = taxonomy_get_children($tid);
627 if ($children) {
628 foreach ($children as $child_tid => $child_term) {
629 _taxonomy_browser_get_kids($tid_list, $child_tid);
630 }
631 }
632 else {
633 $tid_list[$tid] = $tid;
634 }
635 }
636
637 /**
638 * Provides default guideline text.
639 */
640 function _taxonomy_browser_guidelines_default() {
641 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>');
642 }

  ViewVC Help
Powered by ViewVC 1.1.2