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

Diff of /contributions/modules/vocabulary_list/vocabulary_list.module

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

revision 1.4, Tue Aug 31 19:15:49 2004 UTC revision 1.5, Sat Nov 6 05:58:14 2004 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: vocabulary_list.module,v 1.2 2004/07/04 16:05:17 goba Exp $  // $Id: vocabulary_list.module,v 1.4 2004/08/31 19:15:49 LacKac Exp $
3    
4    /**
5     * Implementation of hook_help().
6     */
7  function vocabulary_list_help($section) {  function vocabulary_list_help($section) {
8    $help = t('Provides pages and feeds for vocabularies');    switch ($section) {
9    if ($section == "admin/modules#description") {      case 'admin/modules#description':
10      return $help;        return t('Lists all nodes in a given vocabulary.');
11    }      case 'admin/help#vocabulary_list':
12    elseif ($section == "admin/help#vocabulary_list") {        return t('
13      return "<p>$help</p>";        <h3><a id="vocabulary-url"></a>Displaying content within specific vocabularies</h3>
14              <p>In order to view the content associated with a vocabulary, or a collection of vocabularies, you should browse to a properly formed Vocabulary URL. For example, <a href="%vocab-example">taxonomy/vocabulary/1+2</a>. Vocabulary URLs always contain one or more vocabulary IDs at the end of the URL. You may learn the vocabulary ID for a given vocabulary by hovering over "edit vocabulary" in the <a href="%taxo-overview">taxonomy overview</a> page, and noting the number at the end of the URL.</p>
15          <p>To build a Vocabulary URL, start with "taxonomy/vocabulary/". Then list the vocabulary IDs, separated by "+" to choose content tagged with terms within <strong>any</strong> of the given vocabulary IDs, or separated by "," to choose content tagged with terms within <strong>all</strong> of the given vocabulary IDs. In other words, "+" is less specific than ",". You may optionally specify a "depth" in the taxonomy tree hierarchy. This defaults to "1", which means only nodes tagged with top-level terms within the vocabulary are listed. A positive number indicates the number of additional levels of the taxonomy tree to search. You may also use the value "all", which means that all terms within a vocabulary are searched.</p>
16          <h3>RSS feeds</h3>
17          <p>Every vocabulary, or collection of vocabularies, provides an <a href="%userland-rss">RSS</a> feed to which interested users may subscribe. The URL format for a sample RSS feed is <a href="%sample-rss">taxonomy/vocabulary/1+2/0/feed</a>. These are built just like
18    <a href="%vocab-help">Vocabulary URLs</a>, but are followed by the word "feed".</p>', array('%vocab-example' => url('taxonomy/vocabulary/1+2'), '%taxo-overview' => url('admin/taxonomy'), '%userland-rss' => 'http://backend.userland.com/stories/rss', '%sample-rss' => url('taxonomy/vocabulary/1+2/feed'), '%vocab-help' => url('admin/help/vocabulary_list', NULL, 'vocabulary-url')));
19    }    }
20  }  }
21    
22  function vocabulary_list_menu() {  /**
23     * Implementation of hook_menu().
24     */
25    function vocabulary_list_menu($may_cache) {
26    $items = array();    $items = array();
27    
28    $items[] = array('path' => 'taxonomy/page/vocab', 'title' => t('vocabulary'),    if ($may_cache) {
29      'callback' => 'vocabulary_list_page',      $items[] = array('path' => 'taxonomy/vocabulary', 'title' => t('vocabulary'),
30      'access' => user_access('access content'),        'callback' => 'vocabulary_term_page',
31      'type' => MENU_CALLBACK);        'access' => user_access('access content'),
32    $items[] = array('path' => 'taxonomy/feed/vocab', 'title' => t('vocabulary'),        'type' => MENU_CALLBACK);
33      'callback' => 'vocabulary_list_page',    }
     'access' => user_access('access content'),  
     'type' => MENU_CALLBACK);  
34    
35    return $items;    return $items;
36  }  }
37    
38  function vocabulary_list_page() {  /**
39    $vocabid = (intval(arg(3)) ? intval(arg(3)) : 1);   * Menu callback; displays all nodes associated with a vocabulary.
40    $isfeed = (arg(1) == 'feed');   */
41    function vocabulary_term_page($str_vids = '', $depth = 0, $op = 'page') {
42    $vocab = taxonomy_get_vocabulary($vocabid);    if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_vids)) {
43    $SQL = "SELECT DISTINCT(n.nid), n.type, n.created, n.changed, n.uid, u.name FROM {node} n, {term_node} tn, {term_data} td, {users} u WHERE n.nid = tn.nid AND tn.tid = td.tid AND n.uid = u.uid AND n.status = '1' AND td.vid = '$vocabid' ORDER BY n.sticky DESC, created DESC";      $operator = 'or';
44        // The '+' character in a query string may be parsed as ' '.
45    // Handle output to RSS feed via node_feed()      $vids = preg_split('/[+ ]/', $str_vids);
46    if ($isfeed) {    }
47      $result = db_query_range($SQL, 0, 15);    else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_vids)) {
48      $channel = array(      $operator = 'and';
49        "link"        => url("taxonomy/page/vocab/$vocabid", NULL, NULL, TRUE),      $vids = explode(',', $str_vids);
       "title"       => variable_get("site_name", "drupal") . " - ". $vocab->name,  
       "description" => $vocab->description  
     );  
     node_feed($result, $channel);  
50    }    }
   
   // Handle normal themed output  
51    else {    else {
52      drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS" href="'. url("taxonomy/feed/vocab/$vocabid") .'" />');      drupal_not_found();
53      }
54    
55      if ($vids) {
56        // Build title:
57        $result = db_query('SELECT name FROM {vocabulary} WHERE vid IN (%s)', implode(',', $vids));
58        $names = array();
59        while ($vocab = db_fetch_object($result)) {
60          $names[] = $vocab->name;
61        }
62        $title = implode(', ', $names);
63    
64      $result = pager_query($SQL, variable_get("default_nodes_main", 10));      switch ($op) {
65      $content = '';        case 'page':
66      while ($node = db_fetch_object($result)) {          drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS - '. $title .'" href="'. url('taxonomy/vocabulary/'. $str_vids .'/'. $depth .'/feed') .'" />');
67        $content .= node_view(node_load(array("nid" => $node->nid, "type" => $node->type)), 1);  
68            $output = taxonomy_render_nodes(_vocabulary_select_nodes($vids, $operator, $depth, TRUE));
69            print theme('page', $output, $title);
70            break;
71    
72          case 'feed':
73            $vocab = taxonomy_get_vocabulary($vids[0]);
74            $channel['link'] = url('taxonomy/vocabulary/'. $str_vids .'/'. $depth, NULL, NULL, TRUE);
75            $channel['title'] = variable_get('site_name', 'drupal') .' - '. $title;
76            $channel['description'] = $vocab->description;
77    
78            $result = _vocabulary_select_nodes($vids, $operator, $depth, FALSE);
79            node_feed($result, $channel);
80            break;
81          default:
82            drupal_not_found();
83      }      }
     $content .= theme("pager", NULL, variable_get("default_nodes_main", 10));  
     print theme("page", $content, $vocab->name, array(l(t('Home'), NULL)));  
84    }    }
85  }  }
86    
87    /**
88     * Finds all nodes that match selected taxonomy conditions.
89     *
90     * @param $vids
91     *   An array of vocabulary IDs to match.
92     * @param $operator
93     *   How to interpret multiple IDs in the array. Can be "or" or "and".
94     * @param $depth
95     *   How many levels deep to traverse the taxonomy tree. Can be a nonnegative
96     *   integer or "all".
97     * @param $pager
98     *   Whether the nodes are to be used with a pager (the case on most Drupal
99     *   pages) or not (in an XML feed, for example).
100     * @return
101     *   A resource identifier pointing to the query results.
102     */
103    function _vocabulary_select_nodes($vids = array(), $operator = 'or', $depth = 0, $pager = TRUE) {
104      if (count($vids) > 0) {
105        // For each vocabulary ID, generate an array of descendant taxonomy term IDs to the right depth.
106        $descendant_tids = array();
107        if ($depth === 'all') {
108          $depth = NULL;
109        }
110        else if ($depth === 0) {
111          $depth = 1;
112        }
113        foreach ($vids as $index => $vid) {
114          $tree = taxonomy_get_tree($vid, 0, -1, $depth);
115          $descendant_tids[] = array_merge(array($vid), array_map('_taxonomy_get_tid_from_term', $tree));
116        }
117    
118        if ($operator == 'or') {
119          $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
120          $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.created FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql() .' ORDER BY n.sticky DESC, n.created DESC';
121          $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql();
122        }
123        else {
124          $joins = '';
125          $wheres = '';
126          foreach ($descendant_tids as $index => $tids) {
127            $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
128            $wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
129          }
130          $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.created FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres .' ORDER BY n.sticky DESC, n.created DESC';
131          $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres;
132        }
133    
134        if ($pager) {
135          $result = pager_query($sql, variable_get('default_nodes_main', 10) , 0, $sql_count);
136        }
137        else {
138          $result = db_query_range($sql, 0, 15);
139        }
140      }
141    
142      return $result;
143    }
144    
145    ?>

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2