/[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.26 - (show annotations) (download) (as text)
Wed Jun 11 18:20:50 2008 UTC (17 months, 2 weeks ago) by nancyw
Branch: DRUPAL-5
Changes since 1.19.2.25: +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.25 2008/06/10 18:29:47 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 $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?'),
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_id, &$form_values) {
202 if ($form_values['taxonomy_browser_count_nodes'] == false
203 && $form_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' => 'checkboxes',
268 '#type' => $select_type,
269 '#title' => t('Restrict search by content type'),
270 '#options' => $node_types,
271 '#multiple' => true,
272 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
273 '#suffix' => '</div>',
274 '#description' => $desc,
275 );
276 }
277 else {
278 $form['scope']['node_filter'] = array(
279 '#type' => value,
280 '#value' => array(),
281 );
282 }
283
284 $form['scope']['operator'] = array(
285 '#type' => 'radios',
286 '#title' => t('Items containing'),
287 '#options' => array(t('<strong>all</strong> terms'), t('<strong>any</strong> terms')),
288 '#default_value' => variable_get('taxonomy_browser_default_op', 0),
289 '#prefix' => '<div class="taxonomy_browser_radios">',
290 '#suffix' => '</div>',
291 );
292
293 $vocabularies = array_filter(variable_get('taxonomy_browser_vocabularies', array()));
294 // drupal_set_message('variable: '. print_r($vocabularies, true));
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 // drupal_set_message('all: '. print_r($vocabularies, true));
302 }
303
304 if ($allow_children) {
305 $form['children'] = array(
306 '#type' => 'fieldset',
307 '#title' => t('Include Children'),
308 '#collapsible' => true,
309 '#collapsed' => false,
310 );
311 $form['children']['include_children'] = array(
312 '#type' => 'checkbox',
313 '#title' => t('Automatically include children (sub-terms)'),
314 '#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."'),
315 );
316 }
317 else {
318 $form['include_children'] = array(
319 '#type' => 'value',
320 '#value' => false,
321 );
322 }
323
324 $form['taxonomy'] = array(
325 '#type' => 'fieldset',
326 '#title' => t('Categories'),
327 '#collapsible' => true,
328 '#collapsed' => false,
329 '#tree' => TRUE,
330 );
331
332 $selection_types = array('select', 'checkboxes');
333 $i = 0;
334 foreach ($vocabularies as $v => $sel) {
335 $voc = taxonomy_get_vocabulary($v);
336
337 $voc_node_types = array();
338 foreach ($voc->nodes as $key => $type) {
339 $voc_node_types[] = $node_types[$type];
340 }
341 $count_types = count($voc_node_types);
342 if (count($voc_node_types) == 1) {
343 $node_type_list = $voc_node_types[0];
344 }
345 else {
346 $node_type_list = implode(', ', $voc_node_types);
347 }
348
349 $tree = taxonomy_get_tree($v);
350 $term_opts = array();
351
352 if ($tree) {
353 foreach ($tree as $term) {
354 $opt_string = null;
355 if ($count_nodes) {
356 // $count = db_result(db_query('SELECT COUNT(nid) FROM {term_node} WHERE tid=%d', $term->tid));
357 $count = taxonomy_term_count_nodes($term->tid);
358 if ($count > 0 || $show_unused) {
359 $opt_string = htmlspecialchars_decode(check_plain($term->name), ENT_QUOTES) .' ('. $count .')';
360 }
361 }
362 else {
363 $opt_string = htmlspecialchars_decode(check_plain($term->name), ENT_QUOTES);
364 }
365 if ($opt_string) {
366 $term_opts[$term->tid] = str_repeat('-', $term->depth) . $opt_string;
367 }
368 }
369 }
370
371 $vocname = filter_xss($voc->name);
372 if (!empty($term_opts)) {
373 $form['taxonomy'][$v] = array(
374 '#type' => $select_type,
375 '#title' => $vocname,
376 '#options' => $term_opts,
377 '#multiple' => true,
378 '#description' => $voc->description
379 .' &nbsp;'. t('"!name" is used for: !types.', array('!name' => '<strong>'. $vocname .'</strong>', '!types' => $node_type_list)),
380 '#size' => min(10, count($term_opts)),
381 '#prefix' => '<div class="taxonomy_browser_'. $select_type .'">',
382 '#suffix' => '</div>',
383 '#field_suffix' => $node_type_list,
384 '#weight' => $i,
385 );
386 ++$i;
387 }
388 }
389
390 $form['submit'] = array(
391 '#type' => 'submit',
392 '#value' => t('Search'),
393 '#submit' => TRUE,
394 );
395
396 return $form;
397 }
398
399 /**
400 * Themable form output for the category browser page.
401 */
402 function theme_taxonomy_browser_page($form) {
403 $output = '';
404
405 $vocabularies = variable_get('taxonomy_browser_vocabularies', array());
406 if (empty($vocabularies)) {
407 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'))));
408 return ' ';
409 }
410
411 $output .= drupal_render($form);
412 return $output;
413 }
414
415 /**
416 * Implementation of hook_form_validate().
417 */
418 function taxonomy_browser_form_validate($form_id, &$form_values) {
419
420 $include_children = $form_values['include_children'];
421 $tids = _taxonomy_browser_get_tid_list($form_values['taxonomy'], $include_children);
422
423 $operator = $form_values['operator'] ? 'or' : 'and';
424
425 if ($operator == 'and' && $include_children == true) {
426 form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));
427 }
428
429 if (empty($tids)) {
430 form_set_error('taxonomy', t('You must select at least one category in your search.'));
431 }
432 else {
433 // $node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
434 $node_type = str_replace(',0', '', implode(',', $form_values['node_filter']));
435
436 if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
437 form_set_error('taxonomy', t('No posts match your criteria.'));
438 }
439 }
440 }
441
442 /**
443 * Implementation of hook_form_submit().
444 */
445 function taxonomy_browser_form_submit($form_id, &$form_values) {
446 $tids = _taxonomy_browser_get_tid_list();
447
448 $operator = $form_values['operator'] ? 'or' : 'and';
449 $str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
450
451 $types = array_filter($form_values['node_filter']);
452 $node_type = str_replace(',0', '', implode(',', $types));
453
454 if ($types) {
455 return array('taxonomy/term/'. $str_tids, 'type='. $node_type);
456 }
457 else {
458 return 'taxonomy/term/'. $str_tids;
459 }
460
461 }
462
463 //********************************************************************
464 //* Module Functions : Private
465 //********************************************************************
466
467 /**
468 * Get the output to be displayed by the block.
469 *
470 * @param
471 * $delta - integer for the block number.
472 *
473 * @return
474 * array containing the title ("subject") and content of the block.
475 */
476 function _taxonomy_browser_block_view($delta) {
477 $block = array();
478 switch ($delta) {
479 case 0:
480 $block = array(
481 'content' => drupal_get_form('taxonomy_browser_form'),
482 );
483 break;
484 }
485 return $block;
486 }
487
488 /**
489 * Private function to count the number of nodes found by the user's query.
490 */
491 function taxonomy_browser_count_nodes($tids = array(), $operator = 'or', $depth = 0, $nodetype = NULL) {
492 if (count($tids) > 0) {
493 // For each term ID, generate an array of descendant term IDs to the right depth.
494 $descendant_tids = array();
495 if ($depth === 'all') {
496 $depth = NULL;
497 }
498 foreach ($tids as $index => $tid) {
499 $term = taxonomy_get_term($tid);
500 $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
501 $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
502 }
503
504 $type_where = null;
505 if ($nodetype) {
506 // $type_where = "n.type = '". db_escape_string($nodetype) ."'";
507 $type_where = "n.type IN ('". implode("', '", explode(',', db_escape_string($nodetype))) ."')";
508 }
509
510 if ($operator == 'or') {
511 $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
512 $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";
513 }
514 else {
515 $joins = '';
516 $wheres = array();
517 if ($type_where) {
518 $wheres[] = $type_where;
519 }
520 foreach ($descendant_tids as $index => $tids) {
521 $joins .= 'INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid ';
522 $wheres[] = 'tn'. $index .'.tid IN ('. implode(',', $tids) .')';
523 }
524 $sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE '. implode(' AND ', $wheres);
525 }
526
527 // drupal_set_message('Count query: '. $sql_count);
528 return db_result(db_query(db_rewrite_sql($sql_count)));
529 }
530
531 return 0;
532 }
533
534 /**
535 * Transforms an unpredictably and irregularly nested set of tids (as returned
536 * from a taxonomy form) into a linear array of tids.
537 */
538 function _taxonomy_browser_get_tid_list($tids = null, $include_children = false) {
539 static $tid_list;
540
541 if (isset($tids) && is_array($tids)) {
542 $tid_list = array();
543 foreach ($tids as $key => $tid) {
544 if (!empty($tid)) {
545 if (is_array($tid)) {
546 foreach ($tid as $key2 => $tid2) {
547 if (!empty($tid2)) {
548 $tid_list[$tid2] = $tid2;
549 }
550 }
551 }
552 else {
553 $tid_list[$tid] = $tid;
554 }
555 } /* end !empty */
556 } /* end foreach */
557 }
558
559 if ($include_children) {
560 foreach ($tid_list as $tid) {
561 _taxonomy_browser_get_kids($tid_list, $tid);
562 }
563 }
564
565 return $tid_list;
566 }
567
568 function _taxonomy_browser_get_kids(&$tid_list, $tid) {
569 $children = taxonomy_get_children($tid);
570 if ($children) {
571 foreach ($children as $child_tid => $child_term) {
572 _taxonomy_browser_get_kids($tid_list, $child_tid);
573 }
574 }
575 else {
576 $tid_list[$tid] = $tid;
577 }
578 }
579
580 /**
581 * Provides default guideline text.
582 */
583 function _taxonomy_browser_guidelines_default() {
584 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>');
585 }

  ViewVC Help
Powered by ViewVC 1.1.2