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

  ViewVC Help
Powered by ViewVC 1.1.2