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

Contents of /contributions/modules/vocabulary_list/vocabulary_list.module

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


Revision 1.5 - (show annotations) (download) (as text)
Sat Nov 6 05:58:14 2004 UTC (5 years ago) by wazdog
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +128 -42 lines
File MIME type: text/x-php
updating (rewriting) for 4.5 to use new url schemes
will not tag a 4.5 version until I handle legacy urls.
1 <?php
2 // $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) {
8 switch ($section) {
9 case 'admin/modules#description':
10 return t('Lists all nodes in a given vocabulary.');
11 case 'admin/help#vocabulary_list':
12 return t('
13 <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 /**
23 * Implementation of hook_menu().
24 */
25 function vocabulary_list_menu($may_cache) {
26 $items = array();
27
28 if ($may_cache) {
29 $items[] = array('path' => 'taxonomy/vocabulary', 'title' => t('vocabulary'),
30 'callback' => 'vocabulary_term_page',
31 'access' => user_access('access content'),
32 'type' => MENU_CALLBACK);
33 }
34
35 return $items;
36 }
37
38 /**
39 * Menu callback; displays all nodes associated with a vocabulary.
40 */
41 function vocabulary_term_page($str_vids = '', $depth = 0, $op = 'page') {
42 if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_vids)) {
43 $operator = 'or';
44 // The '+' character in a query string may be parsed as ' '.
45 $vids = preg_split('/[+ ]/', $str_vids);
46 }
47 else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_vids)) {
48 $operator = 'and';
49 $vids = explode(',', $str_vids);
50 }
51 else {
52 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 switch ($op) {
65 case 'page':
66 drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS - '. $title .'" href="'. url('taxonomy/vocabulary/'. $str_vids .'/'. $depth .'/feed') .'" />');
67
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 }
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 ?>

  ViewVC Help
Powered by ViewVC 1.1.2