/[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.21 - (hide annotations) (download) (as text)
Thu Aug 14 14:27:30 2008 UTC (15 months, 1 week ago) by nancyw
Branch: DRUPAL-6--1
CVS Tags: DRUPAL-6--1-4
Changes since 1.19.2.11.2.20: +62 -32 lines
File MIME type: text/x-php
#293041 by NancyDru for Gerben Zaagsma - option to suppress content type assoc. with vocabs.
1 jaza 1.17 <?php
2 nancyw 1.19.2.11.2.21 // $Id: taxonomy_browser.module,v 1.19.2.11.2.20 2008/07/22 00:47:02 nancyw Exp $
3 jaza 1.17 // 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 nancyw 1.19.2.11.2.4 //*******************************************************************
12 nancyw 1.19.2.11.2.7 //* Drupal Hooks : General Overview
13 nancyw 1.19.2.11.2.4 //*******************************************************************
14 jaza 1.17
15     /**
16     * Implementation of hook_menu().
17     */
18 nancyw 1.19.2.11.2.1 function taxonomy_browser_menu() {
19 jaza 1.17 $items = array();
20    
21 nancyw 1.19.2.11.2.1 $items['taxonomy_browser'] = array(
22     'title' => 'Category Browser',
23     'page callback' => 'taxonomy_browser_page',
24     'access arguments' => array('access content'),
25 nancyw 1.19.2.11.2.5 'description' => 'Find content on your own terms.',
26 nancyw 1.19.2.11.2.1 );
27 nancyw 1.19.2.1
28 nancyw 1.19.2.11.2.1 $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 jaza 1.17 return $items;
37     }
38    
39     /**
40 nancyw 1.19.2.11.2.2 * 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 jaza 1.17 * Implementation of hook_help().
48     */
49 nancyw 1.19.2.11.2.1 function taxonomy_browser_help($path, $arg) {
50     switch ($path) {
51 jaza 1.17 case 'admin/modules#description':
52     return t('An interface for viewing content grouped by arbitrary taxonomy terms.');
53     case 'taxonomy_browser':
54 nancyw 1.19.2.11.2.7 $output = check_markup(variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()));
55     if (user_access('administer site configuration')) {
56 nancyw 1.19.2.11.2.14 $output .= '<p class="links">'. l(t('Go to Taxonomy Browser settings'), 'admin/settings/taxonomy-browser', array('query' => drupal_get_destination())) .'</p>';
57 nancyw 1.19.2.11.2.7 }
58     return $output;
59     }
60     }
61    
62     /**
63     * Implementation of hook_perm().
64     */
65     function taxonomy_browser_perm() {
66 nancyw 1.19.2.11.2.21 if (variable_get('taxonomy_browser_need_perm', FALSE)) {
67 nancyw 1.19.2.11.2.7 return array('access taxonomy browser');
68     }
69     else {
70     return array();
71 jaza 1.17 }
72     }
73    
74 nancyw 1.19.2.11.2.7 /**
75     * Implementation of hook_menu_alter().
76     */
77     function taxonomy_browser_menu_alter(&$callbacks) {
78 nancyw 1.19.2.11.2.21 $callbacks['taxonomy_browser']['access arguments'] = array(variable_get('taxonomy_browser_need_perm', FALSE) ? 'access taxonomy browser' : 'access content');
79 nancyw 1.19.2.11.2.7 }
80    
81 nancyw 1.19.2.11.2.4 //********************************************************************
82 nancyw 1.19.2.11.2.7 //* Drupal Hooks : Core
83 nancyw 1.19.2.11.2.4 //********************************************************************
84 jaza 1.17
85     /**
86 nancyw 1.19.2.11.2.12 * 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 nancyw 1.19.2.11.2.14 switch ($delta) {
97 nancyw 1.19.2.11.2.12 case 0:
98     $block = _taxonomy_browser_block_view($delta);
99     break;
100     }
101     return $block;
102     }
103     }
104    
105     /**
106 nancyw 1.19.2.11.2.7 * Settings form.
107 jaza 1.17 */
108 nancyw 1.19.2.1 function taxonomy_browser_admin_settings() {
109     if (!module_exists('node_type_filter') && !drupal_set_message()) {
110 nancyw 1.19.2.11.2.1 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 jaza 1.17 }
112 nancyw 1.19.2.11.2.7
113 nancyw 1.19.2.11.2.18 drupal_add_js(drupal_get_path('module', 'taxonomy_browser') .'/taxonomy_browser.js', 'module');
114    
115 jaza 1.17 $form['taxonomy_browser_guidelines'] = array(
116     '#title' => t('Guidelines'),
117     '#type' => 'textarea',
118     '#default_value' => variable_get('taxonomy_browser_guidelines', _taxonomy_browser_guidelines_default()),
119 nancyw 1.19.2.11.2.7 '#rows' => 2,
120 jaza 1.17 '#description' => t('Instructions which should appear at top of the category browser main page'),
121 nancyw 1.19.2.11.2.1 );
122 nancyw 1.19.2.11.2.7
123 nancyw 1.19.2.5 $form['taxonomy_browser_select_type'] = array(
124     '#title' => t('Selection type'),
125     '#type' => 'radios',
126 nancyw 1.19.2.11.2.14 '#default_value' => variable_get('taxonomy_browser_select_type', 1),
127 nancyw 1.19.2.7 '#options' => array(t('Selection box'), t('Check boxes')),
128 nancyw 1.19.2.9 '#description' => t('This option determines whether the user will see a selection list or check boxes.'),
129 nancyw 1.19.2.11.2.7 '#prefix' => '<div class="taxonomy_browser_radios">',
130     '#suffix' => '</div>',
131 nancyw 1.19.2.11.2.1 );
132 nancyw 1.19.2.11.2.7
133 nancyw 1.19.2.11.2.21 $form['taxonomy_browser_collapse'] = array(
134     '#title' => t('Make vocabularies collapsible'),
135     '#type' => 'checkbox',
136     '#default_value' => variable_get('taxonomy_browser_collapse', FALSE),
137     '#description' => t('Do you want to display of the terms within a vocabulary to be collapsible? Requires "Check boxes" above.'),
138     );
139    
140 nancyw 1.19.2.11.2.8 $form['taxonomy_browser_default_op'] = array(
141     '#title' => t('"Items containing" default'),
142     '#type' => 'radios',
143     '#default_value' => variable_get('taxonomy_browser_default_op', 0),
144     '#options' => array(t('All'), t('Any')),
145     '#description' => t('This option determines which "Items containing" choice is the default.'),
146     '#prefix' => '<div class="taxonomy_browser_radios">',
147     '#suffix' => '</div>',
148     );
149    
150 nancyw 1.19.2.10 $form['taxonomy_browser_count_nodes'] = array(
151     '#title' => t('Show node count'),
152     '#type' => 'checkbox',
153 nancyw 1.19.2.11.2.21 '#default_value' => variable_get('taxonomy_browser_count_nodes', FALSE),
154 nancyw 1.19.2.10 '#description' => t('Do you want to display the count of nodes tagged with each term? This can be SQL-intensive.'),
155 nancyw 1.19.2.11.2.1 );
156 nancyw 1.19.2.11.2.7
157 nancyw 1.19.2.10 $form['taxonomy_browser_show_unused'] = array(
158     '#title' => t('Show unused terms'),
159     '#type' => 'checkbox',
160 nancyw 1.19.2.11.2.21 '#default_value' => variable_get('taxonomy_browser_show_unused', FALSE),
161 nancyw 1.19.2.11.2.14 '#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.'),
162 nancyw 1.19.2.11.2.1 );
163 nancyw 1.19.2.11.2.7
164 nancyw 1.19.2.11.2.4 $form['taxonomy_browser_allow_children'] = array(
165     '#title' => t('Allow child terms to be included'),
166     '#type' => 'checkbox',
167 nancyw 1.19.2.11.2.21 '#default_value' => variable_get('taxonomy_browser_allow_children', FALSE),
168 nancyw 1.19.2.11.2.4 '#description' => t('Do you want the user to see a check box to include child terms (sub-terms)?'),
169     );
170 nancyw 1.19.2.5
171 nancyw 1.19.2.11.2.7 $form['taxonomy_browser_need_perm'] = array(
172     '#title' => t('Requires permission'),
173     '#type' => 'checkbox',
174 nancyw 1.19.2.11.2.21 '#default_value' => variable_get('taxonomy_browser_need_perm', FALSE),
175 nancyw 1.19.2.11.2.7 '#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'))),
176     );
177    
178 nancyw 1.19.2.11.2.21 $form['taxonomy_browser_show_types'] = array(
179     '#title' => t('Show content types with vocabulary'),
180     '#type' => 'checkbox',
181     '#default_value' => variable_get('taxonomy_browser_show_types', FALSE),
182     '#description' => t('If checked, this option displays a list of the content types for which this vocabulary may be used.'),
183     );
184    
185 nancyw 1.19.2.11.2.14 $select = array();
186     $vocabularies = taxonomy_get_vocabularies();
187 jaza 1.17 foreach ($vocabularies as $vocabulary) {
188     $select[$vocabulary->vid] = $vocabulary->name;
189     }
190 nancyw 1.19.2.11.2.1
191 jaza 1.17 $form['taxonomy_browser_vocabularies'] = array(
192     '#title' => t('Included Vocabularies'),
193 nancyw 1.19.2.11.2.14 '#type' => 'checkboxes',
194 jaza 1.17 '#default_value' => variable_get('taxonomy_browser_vocabularies', array()),
195     '#options' => $select,
196 nancyw 1.19.2.8 '#description' => t('Select the vocabularies the user can select from on the category browser page.'),
197 nancyw 1.19.2.11.2.14 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
198     '#suffix' => '</div>',
199 nancyw 1.19.2.11.2.1 );
200 nancyw 1.19.2.11.2.14
201 nancyw 1.19.2.8 if (module_exists('node_type_filter')) {
202 nancyw 1.19.2.11.2.14 $filter_options = node_get_types('names');
203 nancyw 1.19.2.8 $form['taxonomy_browser_omit'] = array(
204 nancyw 1.19.2.11.2.14 '#type' => 'checkboxes',
205 nancyw 1.19.2.8 '#title' => t('Omit content types'),
206     '#options' => $filter_options,
207 nancyw 1.19.2.11.2.3 '#default_value' => variable_get('taxonomy_browser_omit', array('')),
208 nancyw 1.19.2.8 '#description' => t('If any of these types is selected, it will be omitted from the list on the "Category Browser" page.'),
209 nancyw 1.19.2.11.2.14 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
210     '#suffix' => '</div>',
211 nancyw 1.19.2.11.2.1 );
212 nancyw 1.19.2.8 }
213 jaza 1.17
214 nancyw 1.19.2.11.2.1 return system_settings_form($form);
215 jaza 1.17 }
216    
217 nancyw 1.19.2.11.2.4 function taxonomy_browser_admin_settings_validate($form, &$form_state) {
218 nancyw 1.19.2.11.2.21 if ($form_state['values']['taxonomy_browser_count_nodes'] == FALSE
219     && $form_state['values']['taxonomy_browser_show_unused'] == TRUE) {
220 nancyw 1.19.2.11.2.4 form_set_error('taxonomy_browser_show_unused', t('"Show unused" requires "count nodes."'));
221     }
222     }
223    
224     //********************************************************************
225     //* Module Functions : Public
226     //********************************************************************
227 jaza 1.17
228     /**
229     * Menu callback: the query building interface for nodes selected based on
230     * taxonomy terms.
231     */
232     function taxonomy_browser_page() {
233 nancyw 1.19.2.1 $output .= drupal_get_form('taxonomy_browser_form');
234     return $output;
235     }
236    
237     function taxonomy_browser_form() {
238 jaza 1.17 $form = array();
239 nancyw 1.19.2.11.2.7 $selection_types = array('select', 'checkboxes');
240 nancyw 1.19.2.11.2.14 $select_type = $selection_types[variable_get('taxonomy_browser_select_type', 1)];
241 nancyw 1.19.2.11.2.21 $collapsible = variable_get('taxonomy_browser_collapse', FALSE) && ($select_type == 'checkboxes');
242     $count_nodes = variable_get('taxonomy_browser_count_nodes', FALSE);
243     $show_unused = variable_get('taxonomy_browser_show_unused', FALSE);
244     $allow_children = variable_get('taxonomy_browser_allow_children', FALSE);
245 nancyw 1.19.2.11.2.7 $node_types = node_get_types('names');
246 nancyw 1.19.2.11.2.11
247 jaza 1.17 $form['scope'] = array(
248     '#type' => 'fieldset',
249     '#title' => t('Scope'),
250 nancyw 1.19.2.11.2.21 '#collapsible' => TRUE,
251     '#collapsed' => FALSE,
252 nancyw 1.19.2.11.2.7 '#attributes' => array('class' => 'taxonomy_browser_scope'),
253 nancyw 1.19.2.11.2.6 );
254 nancyw 1.19.2.11.2.4
255 nancyw 1.19.2.1 if (module_exists('node_type_filter')) {
256 nancyw 1.19.2.11 if ($count_nodes) {
257     $total_count = 0;
258 nancyw 1.19.2.11.2.11 $result = db_query('SELECT DISTINCT(type), COUNT(nid) AS count FROM {node} WHERE status=1 GROUP BY type ORDER BY type');
259 nancyw 1.19.2.11 while ($counter = db_fetch_array($result)) {
260 nancyw 1.19.2.11.2.7 // Check if we know about this type - a disabled module could have orphans.
261 nancyw 1.19.2.11.2.11 if (isset($node_types[$counter['type']])) {
262     $node_types[$counter['type']] .= ' ('. $counter['count'] .')';
263 nancyw 1.19.2.11.2.7 }
264     else {
265 nancyw 1.19.2.11.2.21 $node_types['unknown'] .= $counter['type'] .' ??? ('. $counter['count'] .') ';
266 nancyw 1.19.2.11.2.7 watchdog('Taxonomy Browser', 'Unknown content type found: @type', array('@type' => $counter['type']), WATCHDOG_WARNING);
267     }
268 nancyw 1.19.2.11 $total_count += $counter['count'];
269     }
270     }
271 nancyw 1.19.2.11.2.11
272 nancyw 1.19.2.11.2.15 $omit = array_filter(variable_get('taxonomy_browser_omit', array()));
273 nancyw 1.19.2.10 if (!empty($omit)) {
274 nancyw 1.19.2.11.2.1 foreach ($omit as $omit_type) {
275 nancyw 1.19.2.11.2.11 unset($node_types[$omit_type]);
276 nancyw 1.19.2.10 }
277     }
278 nancyw 1.19.2.11.2.13 $desc = t('Not selecting any type is the same as selecting all types.');
279     if ($count_nodes) {
280     $desc .= ' '. t('The total count of all types is !count.', array('!count' => $total_count));
281     }
282 jaza 1.17 $form['scope']['node_filter'] = array(
283 nancyw 1.19.2.11.2.7 '#type' => $select_type,
284 jaza 1.17 '#title' => t('Restrict search by content type'),
285 nancyw 1.19.2.11.2.7 '#options' => $node_types,
286 nancyw 1.19.2.11.2.21 '#multiple' => TRUE,
287 nancyw 1.19.2.11.2.7 '#prefix' => '<div class="taxonomy_browser_checkboxes">',
288     '#suffix' => '</div>',
289 nancyw 1.19.2.11.2.13 '#description' => $desc,
290 nancyw 1.19.2.11.2.7 );
291 jaza 1.17 }
292 nancyw 1.19.2.11.2.9 else {
293     $form['scope']['node_filter'] = array(
294     '#type' => value,
295     '#value' => array(),
296     );
297     }
298 jaza 1.17
299     $form['scope']['operator'] = array(
300     '#type' => 'radios',
301     '#title' => t('Items containing'),
302     '#options' => array(t('<strong>all</strong> terms'), t('<strong>any</strong> terms')),
303 nancyw 1.19.2.11.2.8 '#default_value' => variable_get('taxonomy_browser_default_op', 0),
304 nancyw 1.19.2.11.2.7 '#prefix' => '<div class="taxonomy_browser_radios">',
305     '#suffix' => '</div>',
306     );
307 jaza 1.17
308 nancyw 1.19.2.11.2.14 $vocabularies = array_filter(variable_get('taxonomy_browser_vocabularies', array()));
309 nancyw 1.19.2.11.2.10 // Has the admin selected any vocabs?
310     if (count($vocabularies) == 0) {
311 nancyw 1.19.2.11.2.14 $vocabs = taxonomy_get_vocabularies();
312     foreach ($vocabs as $vocabulary) {
313     $vocabularies[$vocabulary->vid] = 1;
314     }
315 nancyw 1.19.2.11.2.10 }
316 nancyw 1.19.2.11.2.4
317     if ($allow_children) {
318     $form['children'] = array(
319     '#type' => 'fieldset',
320     '#title' => t('Include Children'),
321 nancyw 1.19.2.11.2.21 '#collapsible' => TRUE,
322     '#collapsed' => FALSE,
323 nancyw 1.19.2.11.2.4 );
324     $form['children']['include_children'] = array(
325     '#type' => 'checkbox',
326     '#title' => t('Automatically include children (sub-terms)'),
327     '#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."'),
328     );
329     }
330     else {
331 nancyw 1.19.2.11.2.7 $form['include_children'] = array(
332 nancyw 1.19.2.11.2.4 '#type' => 'value',
333 nancyw 1.19.2.11.2.21 '#value' => FALSE,
334 nancyw 1.19.2.11.2.4 );
335     }
336 jaza 1.17
337     $form['taxonomy'] = array(
338     '#type' => 'fieldset',
339     '#title' => t('Categories'),
340 nancyw 1.19.2.11.2.21 '#collapsible' => TRUE,
341     '#collapsed' => FALSE,
342 jaza 1.17 '#tree' => TRUE,
343     );
344 weitzman 1.19
345 nancyw 1.19.2.11.2.1 $selection_types = array('select', 'checkboxes');
346 weitzman 1.19 $i = 0;
347 nancyw 1.19.2.11.2.14 foreach ($vocabularies as $v => $sel) {
348 nancyw 1.19.2.11.2.1 $voc = taxonomy_vocabulary_load($v);
349 nancyw 1.19.2.11.2.7
350     $voc_node_types = array();
351 nancyw 1.19.2.11.2.20 if (isset($voc->nodes) && !empty($voc->nodes)) {
352     foreach ($voc->nodes as $key => $type) {
353     $voc_node_types[] = $node_types[$type];
354     }
355     }
356     else {
357     drupal_set_message(t('The %name vocabulary does not appear to be associated with any content types.', array('%name' => $voc->name)), 'warning');
358 nancyw 1.19.2.11.2.7 }
359 nancyw 1.19.2.11.2.20
360 nancyw 1.19.2.11.2.7 $count_types = count($voc_node_types);
361     if (count($voc_node_types) == 1) {
362     $node_type_list = $voc_node_types[0];
363     }
364     else {
365     $node_type_list = implode(', ', $voc_node_types);
366     }
367    
368 nancyw 1.19.2.5 $tree = taxonomy_get_tree($v);
369     $term_opts = array();
370    
371     if ($tree) {
372     foreach ($tree as $term) {
373 nancyw 1.19.2.11.2.21 $opt_string = NULL;
374 nancyw 1.19.2.10 if ($count_nodes) {
375 nancyw 1.19.2.11.2.16 $count = taxonomy_term_count_nodes($term->tid);
376 nancyw 1.19.2.10 if ($count > 0 || $show_unused) {
377 nancyw 1.19.2.11.2.19 $opt_string = decode_entities(check_plain($term->name)) .' ('. $count .')';
378 nancyw 1.19.2.10 }
379     }
380     else {
381 nancyw 1.19.2.11.2.19 $opt_string = decode_entities(check_plain($term->name));
382 nancyw 1.19.2.11.2.1 }
383     if ($opt_string) {
384     $term_opts[$term->tid] = str_repeat('-', $term->depth) . $opt_string;
385 nancyw 1.19.2.10 }
386 nancyw 1.19.2.5 }
387     }
388    
389 nancyw 1.19.2.11.2.20 $vocname = check_plain($voc->name);
390 nancyw 1.19.2.11.2.21 $description = $voc->description ? decode_entities(check_markup($voc->description)) : NULL;
391     if (variable_get('taxonomy_browser_show_types', FALSE)) {
392     $description .= t('"!name" is used for: !types.', array('!name' => '<strong>'. $vocname .'</strong>', '!types' => (empty($node_type_list) ? '<em>'. t('nothing') .'</em>' : $node_type_list)));
393     }
394    
395 nancyw 1.19.2.10 if (!empty($term_opts)) {
396 nancyw 1.19.2.11.2.21 $voc_element = array(
397 nancyw 1.19.2.11.2.1 '#type' => $select_type,
398 nancyw 1.19.2.11.2.12 '#title' => $vocname,
399 nancyw 1.19.2.10 '#options' => $term_opts,
400 nancyw 1.19.2.11.2.21 '#multiple' => TRUE,
401 nancyw 1.19.2.11.2.20 '#description' => $description,
402 nancyw 1.19.2.11.2.1 '#prefix' => '<div class="taxonomy_browser_'. $select_type .'">',
403 nancyw 1.19.2.10 '#suffix' => '</div>',
404 nancyw 1.19.2.11.2.7 '#field_suffix' => $node_type_list,
405 nancyw 1.19.2.10 '#weight' => $i,
406     );
407 nancyw 1.19.2.11.2.21
408     if ($collapsible) {
409     $fld_set = 'set'. $voc->vid;
410     $form['taxonomy'][$fld_set] = array(
411     '#type' => 'fieldset',
412     '#title' => $vocname,
413     '#collapsible' => TRUE,
414     '#collapsed' => TRUE,
415     );
416     $form['taxonomy'][$fld_set][$v] = $voc_element;
417     }
418     else {
419     $form['taxonomy'][$v] = $voc_element;
420     }
421 nancyw 1.19.2.11.2.7 ++$i;
422 nancyw 1.19.2.10 }
423 jaza 1.17 }
424    
425     $form['submit'] = array(
426     '#type' => 'submit',
427     '#value' => t('Search'),
428 nancyw 1.19.2.11.2.7 // '#submit' => TRUE,
429     );
430 nancyw 1.19.2.1
431     return $form;
432 jaza 1.17 }
433    
434     /**
435 nancyw 1.19.2.11.2.3 * Implementation of hook_theme().
436     */
437     function taxonomy_browser_theme() {
438     return array(
439     'taxonomy_browser_page' => array(
440     'arguments' => array('form'),
441     ),
442     );
443     }
444    
445     /**
446 jaza 1.17 * Themable form output for the category browser page.
447     */
448     function theme_taxonomy_browser_page($form) {
449     $output = '';
450    
451     $vocabularies = variable_get('taxonomy_browser_vocabularies', array());
452     if (empty($vocabularies)) {
453 nancyw 1.19.2.2 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'))));
454 jaza 1.17 return ' ';
455     }
456    
457 nancyw 1.19.2.2 $output .= drupal_render($form);
458 jaza 1.17 return $output;
459     }
460    
461     /**
462     * Implementation of hook_form_validate().
463     */
464 nancyw 1.19.2.11.2.1 function taxonomy_browser_form_validate($form, &$form_state) {
465 nancyw 1.19.2.11.2.4
466     $include_children = $form_state['values']['include_children'];
467     $tids = _taxonomy_browser_get_tid_list($form_state['values']['taxonomy'], $include_children);
468    
469     $operator = $form_state['values']['operator'] ? 'or' : 'and';
470    
471 nancyw 1.19.2.11.2.21 if ($operator == 'and' && $include_children == TRUE) {
472 nancyw 1.19.2.11.2.4 form_set_error('operator', t('You must use "Items containing <strong>any</strong>" to include child terms.'));
473     }
474    
475 jaza 1.17 if (empty($tids)) {
476     form_set_error('taxonomy', t('You must select at least one category in your search.'));
477     }
478     else {
479 nancyw 1.19.2.11.2.7 // $node_type = (isset($form_values['node_filter']) && $form_values['node_filter'] != 'all') ? $form_values['node_filter'] : NULL;
480     $node_type = str_replace(',0', '', implode(',', $form_state['values']['node_filter']));
481    
482 jaza 1.17 if (!taxonomy_browser_count_nodes($tids, $operator, 0, $node_type)) {
483     form_set_error('taxonomy', t('No posts match your criteria.'));
484     }
485     }
486     }
487    
488     /**
489     * Implementation of hook_form_submit().
490     */
491 nancyw 1.19.2.11.2.1 function taxonomy_browser_form_submit($form, &$form_state) {
492 jaza 1.17 $tids = _taxonomy_browser_get_tid_list();
493 nancyw 1.19.2.1
494 nancyw 1.19.2.11.2.1 $operator = $form_state['values']['operator'] ? 'or' : 'and';
495 jaza 1.17 $str_tids = ($operator == 'and') ? implode(',', $tids) : implode('+', $tids);
496 nancyw 1.19.2.11.2.4
497 nancyw 1.19.2.11.2.7 $types = array_filter($form_state['values']['node_filter']);
498     $node_type = str_replace(',0', '', implode(',', $types));
499    
500     if ($types) {
501     $redir = array('taxonomy/term/'. $str_tids, (isset($node_type) ? 'type='. $node_type : ''));
502     }
503     else {
504     $redir = 'taxonomy/term/'. $str_tids;
505     }
506    
507     $form_state['redirect'] = $redir;
508 jaza 1.17 }
509    
510 nancyw 1.19.2.11.2.4 //********************************************************************
511 nancyw 1.19.2.11.2.12 //* Module Functions : Private
512 nancyw 1.19.2.11.2.4 //********************************************************************
513 jaza 1.17
514     /**
515 nancyw 1.19.2.11.2.12 * Get the output to be displayed by the block.
516     *
517     * @param
518     * $delta - integer for the block number.
519     *
520     * @return
521     * array containing the title ("subject") and content of the block.
522     */
523     function _taxonomy_browser_block_view($delta) {
524     $block = array();
525     switch ($delta) {
526     case 0:
527     $block = array(
528     'content' => drupal_get_form('taxonomy_browser_form'),
529     );
530     break;
531     }
532     return $block;
533     }
534    
535     /**
536 jaza 1.17 * Private function to count the number of nodes found by the user's query.
537     */
538     function taxonomy_browser_count_nodes($tids = array(), $operator = 'or', $depth = 0, $nodetype = NULL) {
539     if (count($tids) > 0) {
540     // For each term ID, generate an array of descendant term IDs to the right depth.
541     $descendant_tids = array();
542     if ($depth === 'all') {
543     $depth = NULL;
544     }
545     foreach ($tids as $index => $tid) {
546     $term = taxonomy_get_term($tid);
547     $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
548     $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
549     }
550    
551 nancyw 1.19.2.11.2.21 $type_where = NULL;
552 nancyw 1.19.2.3 if ($nodetype) {
553 nancyw 1.19.2.11.2.7 // $type_where = "n.type = '". db_escape_string($nodetype) ."'";
554     $type_where = "n.type IN ('". implode("', '", explode(',', db_escape_string($nodetype))) ."')";
555 nancyw 1.19.2.3 }
556 nancyw 1.19.2.11.2.7
557 jaza 1.17 if ($operator == 'or') {
558     $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
559    
560 nancyw 1.19.2.11.2.21 $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 jaza 1.17 }
562     else {
563     $joins = '';
564     $wheres = array();
565 nancyw 1.19.2.3 if ($type_where) {
566     $wheres[] = $type_where;
567     }
568 jaza 1.17 foreach ($descendant_tids as $index => $tids) {
569 nancyw 1.19.2.11.2.1 $joins .= 'INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid ';
570 jaza 1.17 $wheres[] = 'tn'. $index .'.tid IN ('. implode(',', $tids) .')';
571     }
572 nancyw 1.19.2.11.2.1 $sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE '. implode(' AND ', $wheres);
573 jaza 1.17 }
574    
575     return db_result(db_query(db_rewrite_sql($sql_count)));
576     }
577    
578     return 0;
579     }
580    
581     /**
582     * Transforms an unpredictably and irregularly nested set of tids (as returned
583     * from a taxonomy form) into a linear array of tids.
584     */
585 nancyw 1.19.2.11.2.21 function _taxonomy_browser_get_tid_list($tids = NULL, $include_children = FALSE) {
586 jaza 1.17 static $tid_list;
587 nancyw 1.19.2.11.2.4
588 jaza 1.17 if (isset($tids) && is_array($tids)) {
589     $tid_list = array();
590     foreach ($tids as $key => $tid) {
591     if (!empty($tid)) {
592     if (is_array($tid)) {
593     foreach ($tid as $key2 => $tid2) {
594     if (!empty($tid2)) {
595 nancyw 1.19.2.11.2.4 $tid_list[$tid2] = $tid2;
596 jaza 1.17 }
597     }
598     }
599     else {
600 nancyw 1.19.2.11.2.4 $tid_list[$tid] = $tid;
601 jaza 1.17 }
602 nancyw 1.19.2.11.2.4 } /* end !empty */
603     } /* end foreach */
604 jaza 1.17 }
605    
606 nancyw 1.19.2.11.2.4 if ($include_children) {
607     foreach ($tid_list as $tid) {
608     _taxonomy_browser_get_kids($tid_list, $tid);
609     }
610     }
611    
612 jaza 1.17 return $tid_list;
613     }
614    
615 nancyw 1.19.2.11.2.4 function _taxonomy_browser_get_kids(&$tid_list, $tid) {
616     $children = taxonomy_get_children($tid);
617     if ($children) {
618     foreach ($children as $child_tid => $child_term) {
619     _taxonomy_browser_get_kids($tid_list, $child_tid);
620     }
621     }
622     else {
623     $tid_list[$tid] = $tid;
624     }
625     }
626    
627 jaza 1.17 /**
628     * Provides default guideline text.
629     */
630     function _taxonomy_browser_guidelines_default() {
631     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>');
632     }

  ViewVC Help
Powered by ViewVC 1.1.2