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

Diff of /contributions/modules/taxonomy_browser/taxonomy_browser.module

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

revision 1.19.2.11.2.23, Sat Feb 7 02:20:02 2009 UTC revision 1.19.2.33, Sat Feb 7 02:20:24 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: taxonomy_browser.module,v 1.19.2.11.2.22 2008/09/25 20:43:33 nancyw Exp $  // $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)  // Original by Moshe Weitzman (weitzmna@tejasa.com)
4    
5  /**  /**
6   * @file   * @file
7   * Enables users to construct their own view of content from terms across   *   Enables users to construct their own view of content from terms across
8   * multiple vocabularies.   *   multiple vocabularies.
9   */   */
10    
11  //*******************************************************************  //*******************************************************************
# Line 15  Line 15 
15  /**  /**
16   * Implementation of hook_menu().   * Implementation of hook_menu().
17   */   */
18  function taxonomy_browser_menu() {  function taxonomy_browser_menu($may_cache) {
19    $items = array();    $items = array();
20    
21    $items['taxonomy_browser'] = array(    if ($may_cache) {
22      'title' => 'Category Browser',      $items[] = array(
23      'page callback' => 'taxonomy_browser_page',        'path' => 'admin/settings/taxonomy-browser',
24      'access arguments' => array('access content'),        'title' => t('Taxonomy Browser'),
25      'description' => 'Find content on your own terms.',        'description' => t('Set usage guidelines and included vocabularies.'),
26      );        'callback' => 'drupal_get_form',
27          'callback arguments' => 'taxonomy_browser_admin_settings',
28    $items['admin/settings/taxonomy-browser'] = array(        'access' => user_access('administer site configuration'),
29      'title' => 'Taxonomy Browser',        );
30      'description' => 'Set usage guidelines and included vocabularies.',    }
31      'page callback' => 'drupal_get_form',    else {
32      'page arguments' => array('taxonomy_browser_admin_settings'),      $items[] = array(
33      'access arguments' => array('administer site configuration'),        '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    return $items;      drupal_add_css(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.css');
42  }    }
43    
44  /**    return $items;
  * Implementation of hook_init().  
  */  
 function taxonomy_browser_init() {  
   drupal_add_css(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.css');  
45  }  }
46    
47  /**  /**
48   * Implementation of hook_help().   * Implementation of hook_help().
49   */   */
50  function taxonomy_browser_help($path, $arg) {  function taxonomy_browser_help($section = '') {
51    switch ($path) {    switch ($section) {
52      case 'admin/modules#description':      case 'admin/modules#description':
53        return t('An interface for viewing content grouped by arbitrary taxonomy terms.');        return t('An interface for viewing content grouped by arbitrary taxonomy terms.');
54      case 'taxonomy_browser':      case 'taxonomy_browser':
55        $output = check_markup(variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()));        $output = check_markup(variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()));
56        if (user_access('administer site configuration')) {        if (user_access('administer site configuration')) {
57          $output .= '<p class="links">'. l(t('Go to Taxonomy Browser settings'), 'admin/settings/taxonomy-browser', array('query' => drupal_get_destination())) .'</p>';          $output .= '<p class="links">'. l(t('Go to Taxonomy Browser settings'), 'admin/settings/taxonomy-browser', array(), drupal_get_destination()) .'</p>';
58        }        }
59        return $output;        return $output;
60    }    }
# Line 71  function taxonomy_browser_perm() { Line 72  function taxonomy_browser_perm() {
72    }    }
73  }  }
74    
 /**  
  * Implementation of hook_menu_alter().  
  */  
 function taxonomy_browser_menu_alter(&$callbacks) {  
   $callbacks['taxonomy_browser']['access arguments'] = array(variable_get('taxonomy_browser_need_perm', FALSE) ? 'access taxonomy browser' : 'access content');  
 }  
   
75  //********************************************************************  //********************************************************************
76  //* Drupal Hooks : Core  //* Drupal Hooks : Core
77  //********************************************************************  //********************************************************************
# Line 99  function taxonomy_browser_block($op = 'l Line 93  function taxonomy_browser_block($op = 'l
93            break;            break;
94        }        }
95        return $block;        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   * Settings form.   * Implementation of hook_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()) {
# Line 111  function taxonomy_browser_admin_settings Line 111  function taxonomy_browser_admin_settings
111    }    }
112    
113    drupal_add_js(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.js', 'module');    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',
# Line 119  function taxonomy_browser_admin_settings Line 119  function taxonomy_browser_admin_settings
119      '#rows' => 2,      '#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',
# Line 149  function taxonomy_browser_admin_settings Line 149  function taxonomy_browser_admin_settings
149      '#prefix' => '<div class="taxonomy_browser_radios">',      '#prefix' => '<div class="taxonomy_browser_radios">',
150      '#suffix' => '</div>',      '#suffix' => '</div>',
151      );      );
152    
153    $form['taxonomy_browser_count_nodes'] = array(    $form['taxonomy_browser_count_nodes'] = array(
154      '#title' => t('Show node count'),      '#title' => t('Show node count'),
155      '#type' => 'checkbox',      '#type' => 'checkbox',
156      '#default_value' => variable_get('taxonomy_browser_count_nodes', FALSE),      '#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.'),      '#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(    $form['taxonomy_browser_show_unused'] = array(
161      '#title' => t('Show unused terms'),      '#title' => t('Show unused terms'),
162      '#type' => 'checkbox',      '#type' => 'checkbox',
163      '#default_value' => variable_get('taxonomy_browser_show_unused', FALSE),      '#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.'),      '#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(    $form['taxonomy_browser_allow_children'] = array(
168      '#title' => t('Allow child terms to be included'),      '#title' => t('Allow child terms to be included'),
169      '#type' => 'checkbox',      '#type' => 'checkbox',
# Line 175  function taxonomy_browser_admin_settings Line 175  function taxonomy_browser_admin_settings
175      '#title' => t('Requires permission'),      '#title' => t('Requires permission'),
176      '#type' => 'checkbox',      '#type' => 'checkbox',
177      '#default_value' => variable_get('taxonomy_browser_need_perm', FALSE),      '#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'))),      '#description' => t('Do you want to require permission to see the browser page?'),
179      );      );
180    
181    $form['taxonomy_browser_show_types'] = array(    $form['taxonomy_browser_show_types'] = array(
# Line 191  function taxonomy_browser_admin_settings Line 191  function taxonomy_browser_admin_settings
191      $select[$vocabulary->vid] = $vocabulary->name;      $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(    $form['taxonomy_browser_vocabularies'] = array(
199      '#title' => t('Included Vocabularies'),      '#title' => t('Included Vocabularies'),
200      '#type' => 'checkboxes',      '#type' => 'checkboxes',
201      '#default_value' => variable_get('taxonomy_browser_vocabularies', array()),      '#default_value' => $current_vocabs,
202      '#options' => $select,      '#options' => $select,
203      '#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.'),
204      '#prefix' => '<div class="taxonomy_browser_checkboxes">',      '#prefix' => '<div class="taxonomy_browser_checkboxes">',
# Line 217  function taxonomy_browser_admin_settings Line 221  function taxonomy_browser_admin_settings
221    return system_settings_form($form);    return system_settings_form($form);
222  }  }
223    
224  function taxonomy_browser_admin_settings_validate($form, &$form_state) {  function taxonomy_browser_admin_settings_validate($form_id, &$form_values) {
225    if ($form_state['values']['taxonomy_browser_count_nodes'] == FALSE    if ($form_values['taxonomy_browser_count_nodes'] == FALSE
226      && $form_state['values']['taxonomy_browser_show_unused'] == TRUE) {      && $form_values['taxonomy_browser_show_unused'] == TRUE) {
227      form_set_error('taxonomy_browser_show_unused', t('"Show unused" requires "count nodes."'));      form_set_error('taxonomy_browser_show_unused', t('"Show unused" requires "count nodes."'));
228    }    }
229    if ($form_state['values']['taxonomy_browser_collapse'] != 0    if ($form_values['taxonomy_browser_collapse'] != 0
230      && $form_state['values']['taxonomy_browser_select_type'] != 1) {      && $form_values['taxonomy_browser_select_type'] != 1) {
231      form_set_error('taxonomy_browser_collapse', t('"Make vocabularies collapsible" requires "Check boxes."'));      form_set_error('taxonomy_browser_collapse', t('"Make vocabularies collapsible" requires "Check boxes."'));
232    }    }
233  }  }
234    
235    
236  //********************************************************************  //********************************************************************
237  //* Module Functions : Public  //* Module Functions : Public
238  //********************************************************************  //********************************************************************
# Line 250  function taxonomy_browser_form() { Line 255  function taxonomy_browser_form() {
255    $show_unused = variable_get('taxonomy_browser_show_unused', FALSE);    $show_unused = variable_get('taxonomy_browser_show_unused', FALSE);
256    $allow_children = variable_get('taxonomy_browser_allow_children', FALSE);    $allow_children = variable_get('taxonomy_browser_allow_children', FALSE);
257    $node_types = node_get_types('names');    $node_types = node_get_types('names');
258    
259    $form['scope'] = array(    $form['scope'] = array(
260      '#type' => 'fieldset',      '#type' => 'fieldset',
261      '#title' => t('Scope'),      '#title' => t('Scope'),
# Line 287  function taxonomy_browser_form() { Line 292  function taxonomy_browser_form() {
292        $desc .= ' '. t('The total count of all types is !count.', array('!count' => $total_count));        $desc .= ' '. t('The total count of all types is !count.', array('!count' => $total_count));
293      }      }
294      $form['scope']['node_filter'] = array(      $form['scope']['node_filter'] = array(
295    //      '#type' => 'checkboxes',
296        '#type' => $select_type,        '#type' => $select_type,
297        '#title' => t('Restrict search by content type'),        '#title' => t('Restrict search by content type'),
298        '#options' => $node_types,        '#options' => $node_types,
# Line 302  function taxonomy_browser_form() { Line 308  function taxonomy_browser_form() {
308        '#value' => array(),        '#value' => array(),
309        );        );
310    }    }
311    
312    $form['scope']['operator'] = array(    $form['scope']['operator'] = array(
313      '#type' => 'radios',      '#type' => 'radios',
314      '#title' => t('Items containing'),      '#title' => t('Items containing'),
# Line 313  function taxonomy_browser_form() { Line 319  function taxonomy_browser_form() {
319      );      );
320    
321    $vocabularies = array_filter(variable_get('taxonomy_browser_vocabularies', array()));    $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?    // Has the admin selected any vocabs?
326    if (count($vocabularies) == 0) {    if (count($vocabularies) == 0) {
327      $vocabs = taxonomy_get_vocabularies();      $vocabs = taxonomy_get_vocabularies();
# Line 320  function taxonomy_browser_form() { Line 329  function taxonomy_browser_form() {
329        $vocabularies[$vocabulary->vid] = 1;        $vocabularies[$vocabulary->vid] = 1;
330      }      }
331    }    }
332    
333    if ($allow_children) {    if ($allow_children) {
334      $form['children'] = array(      $form['children'] = array(
335        '#type' => 'fieldset',        '#type' => 'fieldset',
# Line 352  function taxonomy_browser_form() { Line 361  function taxonomy_browser_form() {
361    $selection_types = array('select', 'checkboxes');    $selection_types = array('select', 'checkboxes');
362    $i = 0;    $i = 0;
363    foreach ($vocabularies as $v => $sel) {    foreach ($vocabularies as $v => $sel) {
364      $voc = taxonomy_vocabulary_load($v);      $voc = taxonomy_get_vocabulary($v);
365    
366      $voc_node_types = array();      $voc_node_types = array();
367      if (isset($voc->nodes) && !empty($voc->nodes)) {      if (isset($voc->nodes) && !empty($voc->nodes[0])) {
368        foreach ($voc->nodes as $key => $type) {        foreach ($voc->nodes as $key => $type) {
369          $voc_node_types[] = $node_types[$type];          $voc_node_types[] = $node_types[$type];
370        }        }
371      }      }
372      else {      else {
373        drupal_set_message(t('The %name vocabulary does not appear to be associated with any content types.', array('%name' => $voc->name)), 'warning');        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);      $count_types = count($voc_node_types);
377      if (count($voc_node_types) == 1) {      $node_type_list = implode(', ', $voc_node_types);
       $node_type_list = $voc_node_types[0];  
     }  
     else {  
       $node_type_list = implode(', ', $voc_node_types);  
     }  
378    
379      $tree = taxonomy_get_tree($v);      $tree = taxonomy_get_tree($v);
380      $term_opts = array();      $term_opts = array();
# Line 379  function taxonomy_browser_form() { Line 383  function taxonomy_browser_form() {
383        foreach ($tree as $term) {        foreach ($tree as $term) {
384          $opt_string = NULL;          $opt_string = NULL;
385          if ($count_nodes) {          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);            $count = taxonomy_term_count_nodes($term->tid);
388            if ($count > 0 || $show_unused) {            if ($count > 0 || $show_unused) {
389              $opt_string = decode_entities(check_plain($term->name)) .' ('. $count .')';              $opt_string = decode_entities(check_plain($term->name)) .' ('. $count .')';
# Line 429  function taxonomy_browser_form() { Line 434  function taxonomy_browser_form() {
434        ++$i;        ++$i;
435      }      }
436    }    }
437    
438    $form['submit'] = array(    $form['submit'] = array(
439      '#type' => 'submit',      '#type' => 'submit',
440      '#value' => t('Search'),      '#value' => t('Search'),
441  //    '#submit' => TRUE,      '#submit' => TRUE,
442      );      );
   
   return $form;  
 }  
443    
444  /**    return $form;
  * Implementation of hook_theme().  
  */  
 function taxonomy_browser_theme() {  
   return array(  
     'taxonomy_browser_page' => array(  
       'arguments' => array('form'),  
     ),  
   );  
445  }  }
446    
447  /**  /**
# Line 469  function theme_taxonomy_browser_page($fo Line 463  function theme_taxonomy_browser_page($fo
463  /**  /**
464   * Implementation of hook_form_validate().   * Implementation of hook_form_validate().
465   */   */
466  function taxonomy_browser_form_validate($form, &$form_state) {  function taxonomy_browser_form_validate($form_id, &$form_values) {
467    
468    $include_children = $form_state['values']['include_children'];    $include_children = $form_values['include_children'];
469    $tids = _taxonomy_browser_get_tid_list($form_state['values']['taxonomy'], $include_children);    $tids = _taxonomy_browser_get_tid_list($form_values['taxonomy'], $include_children);
470    
471    $operator = $form_state['values']['operator'] ? 'or' : 'and';    $operator = $form_values['operator'] ? 'or' : 'and';
472    
473    if ($operator == 'and' && $include_children == TRUE) {    if ($operator == 'and' && $include_children == TRUE) {
474      form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));      form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));
# Line 485  function taxonomy_browser_form_validate( Line 479  function taxonomy_browser_form_validate(
479    }    }
480    else {    else {
481  //    $node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;  //    $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_state['values']['node_filter']));      $node_type = str_replace(',0', '', implode(',', $form_values['node_filter']));
483    
484      if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {      if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
485        form_set_error('taxonomy', t('No posts match your criteria.'));        form_set_error('taxonomy', t('No posts match your criteria.'));
# Line 496  function taxonomy_browser_form_validate( Line 490  function taxonomy_browser_form_validate(
490  /**  /**
491   * Implementation of hook_form_submit().   * Implementation of hook_form_submit().
492   */   */
493  function taxonomy_browser_form_submit($form, &$form_state) {  function taxonomy_browser_form_submit($form_id, &$form_values) {
494    $tids = _taxonomy_browser_get_tid_list();    $tids = _taxonomy_browser_get_tid_list();
495    
496    $operator = $form_state['values']['operator'] ? 'or' : 'and';    $operator = $form_values['operator'] ? 'or' : 'and';
497    $str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);    $str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
498    
499    $types = array_filter($form_state['values']['node_filter']);    $types = array_filter($form_values['node_filter']);
500    $node_type = str_replace(',0', '', implode(',', $types));    $node_type = str_replace(',0', '', implode(',', $types));
501    
502    if ($types) {    if ($types) {
503      $redir = array('taxonomy/term/'. $str_tids, (isset($node_type) ? 'type='. $node_type : ''));      return array('taxonomy/term/'. $str_tids, 'type='. $node_type);
504    }    }
505    else {    else {
506      $redir = 'taxonomy/term/'. $str_tids;      return 'taxonomy/term/'. $str_tids;
507    }    }
508    
   $form_state['redirect'] = $redir;  
509  }  }
510    
511  //********************************************************************  //********************************************************************
# Line 564  function taxonomy_browser_count_nodes($t Line 557  function taxonomy_browser_count_nodes($t
557    
558      if ($operator == 'or') {      if ($operator == 'or') {
559        $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));        $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";        $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 {      else {
# Line 580  function taxonomy_browser_count_nodes($t Line 572  function taxonomy_browser_count_nodes($t
572        $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);
573      }      }
574    
575    //    drupal_set_message('Count query: '. $sql_count);
576      return db_result(db_query(db_rewrite_sql($sql_count)));      return db_result(db_query(db_rewrite_sql($sql_count)));
577    }    }
578    

Legend:
Removed from v.1.19.2.11.2.23  
changed lines
  Added in v.1.19.2.33

  ViewVC Help
Powered by ViewVC 1.1.2