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

Contents of /contributions/modules/ubrowser/ubrowser.module

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


Revision 1.4 - (show annotations) (download) (as text)
Mon Feb 11 18:26:51 2008 UTC (21 months, 2 weeks ago) by rszrama
Branch: MAIN
CVS Tags: DRUPAL-5--1-3, HEAD
Branch point for: DRUPAL-5
Changes since 1.3: +2 -6 lines
File MIME type: text/x-php
Updates to translation strings.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Core code for uBrowser, a new way to browse and select nodes!
7 *
8 * uBrowser lets you browse taxonomy vocabularies like they were file systems.
9 * You can browse for nodes, search them for keywords, and choose to view or
10 * take an action on the node(s) you select.
11 *
12 * Development sponsored by the Ubercart project. http://www.ubercart.org
13 */
14
15
16 /*******************************************************************************
17 * Hook Functions (Drupal)
18 ******************************************************************************/
19
20 /**
21 * Implementation of hook_menu().
22 */
23 function ubrowser_menu($may_cache) {
24 if ($may_cache) {
25 // Main ubrowser URL.. returns the HTML for the uBrowser to be placed in a div.
26 $items[] = array(
27 'path' => 'ubrowser/main',
28 'title' => t('Browse taxonomy'),
29 'description' => t('Browse taxonomy like a file tree.'),
30 'callback' => 'ubrowser_display',
31 'access' => user_access('access content'),
32 'type' => MENU_CALLBACK,
33 );
34 // Search URL, displays results in the node div
35 $items[] = array(
36 'path' => 'ubrowser/nodesearch',
37 'title' => t('Search nodes'),
38 'description' => t('Display search results in the node box.'),
39 'callback' => 'ubrowser_nodesearch',
40 'access' => user_access('access content'),
41 'type' => MENU_CALLBACK,
42 );
43 // Let admin browse their content with uBrowser
44 $items[] = array(
45 'path' => 'admin/content/ubrowser',
46 'title' => t('Category browser'),
47 'description' => t('Browse nodes in your vocabularies using uBrowser.'),
48 'callback' => 'ubrowser_admin',
49 'access' => user_access('administer taxonomy'),
50 'type' => MENU_NORMAL_ITEM,
51 );
52 }
53 else {
54 drupal_add_css(drupal_get_path('module', 'ubrowser') .'/ubrowser.css');
55 if (is_numeric(arg(2))) {
56 // URL to display nodes in the node div
57 $items[] = array(
58 'path' => 'ubrowser/nodes/'. arg(2),
59 'title' => t('Display nodes'),
60 'description' => t('Display the child nodes of a taxonomy term.'),
61 'callback' => 'ubrowser_nodes',
62 'callback arguments' => array(arg(2), arg(3)),
63 'access' => user_access('access content'),
64 'type' => MENU_CALLBACK,
65 );
66 }
67 }
68
69 return $items;
70 }
71
72
73 /*******************************************************************************
74 * Callback Functions, Forms, and Tables
75 ******************************************************************************/
76
77 /**
78 * Allow admin to build a uBrowser and browse their content with it.
79 */
80 function ubrowser_admin() {
81
82 $output = '<p>'. t('uBrowser allows you to browse the content of your site by taxonomy vocabulary. Use the form to build uBrowser here. The textarea beneath the uBrowser will display the PHP to put the browser on a page in your site.') .'</p>';
83
84 $settings = array(
85 'div' => '#ubrowser',
86 'class' => '',
87 'vid' => TRUE,
88 'nids' => TRUE,
89 'search' => TRUE,
90 'view' => TRUE,
91 'window' => TRUE,
92 'close' => TRUE,
93 'multi' => TRUE,
94 'filter' => TRUE,
95 'select' => TRUE,
96 'categ' => TRUE,
97 'nodesg' => TRUE,
98 'nodepl' => TRUE,
99 'code' => '#php-results',
100 );
101
102 $output .= drupal_get_form('ubrowser_builder', $settings) .'<br />'
103 . ubrowser_div('ubrowser', t('Make your choices and click display.'));
104
105 $output .= '<div style="clear: both;"></div><br />'
106 .'<p><b>'. t('PHP to build this uBrowser on a page:') .'</b><br />'
107 .'<textarea id="php-results" rows="8" cols="60" readonly></textarea></p>';
108
109 return $output;
110 }
111
112 /**
113 * Return the HTML to display the uBrowser inside a div.
114 */
115 function ubrowser_display($settings = NULL) {
116 if ($settings === NULL && (empty($_POST) || !isset($_POST['div']))) {
117 drupal_goto('<front>');
118 }
119
120 /**
121 * List of settings array keys and values:
122 * div = CSS selector of the div in which the uBrowser should display.
123 * class = Class to add to the div when uBrowser is loaded and remove when
124 * uBrowser is closed.
125 * vid = Vocabulary ID the uBrowser should browse.
126 * nids = String true or false to display node IDs in the node select box.
127 * filter = Comma delimeted string of node types to display.
128 * search = String true or false to display a node search form.
129 * close = String true or false to display a close button that wipes the div.
130 * view = String true or false to display a view button that browses
131 * to the selected node.
132 * window = String current or new for which window to view nodes in.
133 * categ = String of text to replace 'category' in displays, or leave blank.
134 * nodesg = String of text to replace 'node' in displays, or leave blank.
135 * nodepl = String of text to replace 'nodes' in displays, or leave blank.
136 * select = String of Javascript to execute on the select button
137 * (not displayed if no Javascript is set).
138 * multi = String true or false to allow users to select multiple nodes.
139 */
140 if ($settings === NULL) {
141 $settings = array(
142 'div' => $_POST['div'],
143 'class' => $_POST['class'],
144 'vid' => intval($_POST['vid']),
145 'nids' => (($_POST['nids'] == 'true') ? '' : '/no_nids'),
146 'filter' => $_POST['filter'],
147 'search' => $_POST['search'],
148 'close' => $_POST['close'],
149 'view' => $_POST['view'],
150 'window' => (($_POST['window'] == 'new') ? 'new' : 'current'),
151 'categ' => ((strlen($_POST['categ']) > 0) ? $_POST['categ'] : 'category'),
152 'nodesg' => ((strlen($_POST['nodesg']) > 0) ? $_POST['nodesg'] : 'node'),
153 'nodepl' => ((strlen($_POST['nodepl']) > 0) ? $_POST['nodepl'] : 'nodes'),
154 'select' => $_POST['select'],
155 'multi' => (($_POST['multi'] == 'true') ? TRUE : FALSE),
156 );
157 $return_output = FALSE;
158 }
159 else {
160 if ($settings['nids'] == 'false') {
161 $settings['nids'] = '/no_nids';
162 }
163 else {
164 $settings['nids'] = '';
165 }
166 if (is_null($settings['categ']) || strlen($settings['categ']) == 0) {
167 $settings['categ'] = 'category';
168 }
169 if (is_null($settings['nodesg']) || strlen($settings['nodesg']) == 0) {
170 $settings['nodesg'] = 'node';
171 }
172 if (is_null($settings['nodepl']) || strlen($settings['nodepl']) == 0) {
173 $settings['nodepl'] = 'nodes';
174 }
175 $return_output = TRUE;
176 }
177
178 if ($settings['vid'] > 0) {
179 $tree = taxonomy_get_tree($settings['vid']);
180 $options = array();
181 if ($tree && (count($tree) > 0)) {
182 foreach ($tree as $term) {
183 $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
184 }
185 }
186 if (count($options) == 0) {
187 $options[0] = t('No terms found.');
188 }
189 }
190
191 // Get the appropriate forms into variables.
192 $term_select_form = drupal_get_form('ubrowser_tree_form', $options, $settings);
193 $node_select_form = drupal_get_form('ubrowser_nodes_form', NULL, $settings['categ'], $settings['nodesg'], $settings['multi']);
194 $search_form = '';
195 $actions_form = '';
196 if ($settings['search'] == 'true') {
197 $search_form = drupal_get_form('ubrowser_node_search_form');
198 }
199 if (strlen($settings['select']) > 0 || $settings['view'] == 'true' || $settings['close'] == 'true') {
200 $actions_form = drupal_get_form('ubrowser_node_actions_form', $settings);
201 }
202
203 // Output the table with appropriate classes and IDs for uBrowser and the CSS.
204 $output = '<table class="ubrowser-table"><tr>'
205 .'<td id="ubrowser-tree">'. $term_select_form .'</td>'
206 .'<td id="ubrowser-nodes">'. $node_select_form .'</td>'
207 .'</tr><tr>'
208 .'<td id="ubrowser-search">'. $search_form .'</td>'
209 .'<td id="ubrowser-actions">'. $actions_form .'</td>'
210 .'</tr></table>';
211
212 if ($return_output) {
213 return $output;
214 }
215
216 print $output;
217 exit();
218 }
219
220 /**
221 * Create the form for the term select box; also stores the widget settings.
222 */
223 function ubrowser_tree_form($options, $settings = NULL) {
224 if (strlen($settings['categ']) > 0) {
225 $category = $settings['categ'];
226 }
227 else {
228 $category = 'category';
229 }
230
231 $form['tid'] = array(
232 '#type' => 'select',
233 '#title' => t('Select a !category', array('!category' => $category)),
234 '#size' => 10,
235 '#options' => $options,
236 '#attributes' => array('onchange' => "load_node_select(this.value, '". base_path() ."');"),
237 );
238
239 /**
240 * Save the settings array as hidden input fields in the uBrowser form.
241 * Settings are accessed by ID for different onclick actions:
242 * $('#edit-ub-key').val(); // Get the value of settings key using jQuery.
243 */
244 if (!is_null($settings)) {
245 foreach ($settings as $key => $value) {
246 $form['settings']['ub-'. $key] = array(
247 '#type' => 'hidden',
248 '#value' => $value,
249 );
250 }
251 }
252
253 return $form;
254 }
255
256 /**
257 * Return the HTML for the node select box.
258 */
259 function ubrowser_nodes($tid, $nids) {
260 if ($_POST['multi'] == 'true' || $_POST['multi'] == '1') {
261 $multi = TRUE;
262 }
263 else {
264 $multi = FALSE;
265 }
266
267 if (isset($_POST['filter'])) {
268 $filter = $_POST['filter'];
269 }
270 else {
271 $filter = NULL;
272 }
273
274 if (strlen($_POST['categ']) > 0) {
275 $category = $_POST['categ'];
276 }
277 else {
278 $category = 'category';
279 }
280
281 if (strlen($_POST['nodesg']) > 0) {
282 $nodesg = $_POST['nodesg'];
283 }
284 else {
285 $nodesg = 'node';
286 }
287
288 if (strlen($_POST['nodepl']) > 0) {
289 $nodepl = $_POST['nodepl'];
290 }
291 else {
292 $nodepl = 'nodes';
293 }
294
295 if ($tid == 0) {
296 $options = array(0 => t('Select a !category...'), array('!category' => $category));
297 }
298 else {
299 $result = _ubrowser_select_nodes(array($tid), $filter);
300 $options = array(0 => t('!Category contains !count !nodes!punctuation',
301 array('!Category' => ucfirst($category),
302 '!count' => db_num_rows($result),
303 '!nodes' => ((db_num_rows($result) == 1) ? $nodesg : $nodepl),
304 '!punctuation' => ((db_num_rows($result) == 0) ? '.' : ':'))));
305 while($node = db_fetch_object($result)) {
306 if ($nids == 'no_nids') {
307 $options[$node->nid] = $node->title;
308 }
309 else {
310 $options[$node->nid] = '('. $node->nid .') '. $node->title;
311 }
312 }
313 }
314
315 $output = drupal_get_form('ubrowser_nodes_form', $options, $category, $nodesg, $multi);
316
317 print $output;
318 exit();
319 }
320
321 /**
322 * Create the form for the node select box.
323 */
324 function ubrowser_nodes_form($options, $categ = 'category', $nodesg = 'node', $multi = FALSE) {
325 if ($options === NULL) {
326 $options = array(0 => t('Select a !category...', array('!category' => $categ)));
327 }
328 else {
329 foreach ($options as $nid => $title) {
330 if ($nid > 0) {
331 $result = db_query("SELECT dst FROM {url_alias} WHERE src = 'node/". $nid ."'");
332 if ($alias = db_fetch_object($result)) {
333 $url = $alias->dst;
334 }
335 else {
336 $url = 'node/'. $nid;
337 }
338 $form['unid-path-'. $nid] = array(
339 '#type' => 'hidden',
340 '#value' => $url,
341 );
342 }
343 }
344 }
345
346 $form['unid'] = array(
347 '#type' => 'select',
348 '#multiple' => $multi,
349 '#title' => t('Select a !nodesg', array('!nodesg' => $nodesg)),
350 '#size' => 10,
351 '#options' => $options,
352 );
353
354 return $form;
355 }
356
357 /**
358 * Perform a search for nodes and display the results in the node box.
359 */
360 function ubrowser_nodesearch() {
361 $keys = check_plain($_POST['keys']);
362 $filter = check_plain($_POST['filter']);
363 $category = check_plain($_POST['categ']);
364 $nodesg = check_plain($_POST['nodesg']);
365 $nodepl = check_plain($_POST['nodepl']);
366
367 if (is_null($keys) || strlen($keys) < 3) {
368 print '';
369 exit();
370 }
371
372 if (strlen($filter) > 0) {
373 $filter = "'". str_replace(',', "','", $filter) ."'";
374 $results = _ubrowser_search($keys, 'node',
375 'INNER JOIN {node} n ON n.nid = i.sid',
376 "(n.type IN ($filter))");
377 }
378 else {
379 $results = _ubrowser_search($keys, 'node');
380 }
381 if (($j = count($results)) == 0) {
382 $options = array(
383 -1 => t('Search yielded no results for:'),
384 0 => $keys,
385 );
386 }
387 elseif ($j == 1) {
388 $options = array(0 => t('Search yielded 1 result:'));
389 $result = db_query("SELECT nid, title FROM {node} WHERE nid = %d", $results[0]->sid);
390 if ($node = db_fetch_object($result)) {
391 if ($_POST['nids'] == '/no_nids') {
392 $options[$node->nid] = $node->title;
393 }
394 else {
395 $options[$node->nid] = '('. $node->nid .') '. $node->title;
396 }
397 }
398 }
399 else {
400 $options = array(0 => t('Search yielded !count results:', array('!count' => $j)));
401 foreach($results as $result) {
402 $nid_array[] = $result->sid;
403 }
404 $result = db_query("SELECT nid, title FROM {node} WHERE nid IN (%s)", implode(',', $nid_array));
405 while ($node = db_fetch_object($result)) {
406 if ($_POST['nids'] == '/no_nids') {
407 $options[$node->nid] = $node->title;
408 }
409 else {
410 $options[$node->nid] = '('. $node->nid .') '. $node->title;
411 }
412 }
413 }
414
415 $output = drupal_get_form('ubrowser_nodes_form', $options, $category, $nodesg);
416 print $output;
417 exit();
418 }
419
420 /**
421 * Create the form for the node search box.
422 */
423 function ubrowser_node_search_form() {
424 $form = array(
425 '#attributes' => array('onsubmit' => 'return ubrowser_search_submit();'),
426 );
427
428 $form['usearch_keys'] = array(
429 '#type' => 'textfield',
430 '#size' => 10,
431 );
432 $form['usearch_submit'] = array(
433 '#type' => 'button',
434 '#value' => t('Search'),
435 '#attributes' => array('onclick' => "return load_node_search('". base_path() ."');"),
436 );
437
438 return $form;
439 }
440
441 /**
442 * Theme the node search box.
443 */
444 function theme_ubrowser_node_search_form($form) {
445 $output = '<table id="ubrowser-search-table"><tr>'
446 .'<td>'. drupal_render($form['usearch_keys']) .'</td>'
447 .'<td>'. drupal_render($form['usearch_submit']) .'</td>'
448 .'</tr></table>';
449
450 return $output;
451 }
452
453 /**
454 * Create the form to display the actions buttons:
455 * view = Browses to the selected node.
456 * select = Executes the select Javascript using eval().
457 * close = Empties the uBrowser div and removes its class where applicable.
458 */
459 function ubrowser_node_actions_form($settings) {
460 if ($settings['view'] == 'true') {
461 $form['uview'] = array(
462 '#type' => 'button',
463 '#value' => t('View'),
464 '#attributes' => array('onclick' => "return ubrowser_action_view('". base_path() ."');"),
465 );
466 }
467
468 if (strlen($settings['select']) > 0) {
469 $form['uselect'] = array(
470 '#type' => 'button',
471 '#value' => t('Select'),
472 '#attributes' => array('onclick' => 'return ubrowser_action_select();'),
473 );
474 }
475
476 if ($settings['close'] == 'true') {
477 $form['uclose'] = array(
478 '#type' => 'button',
479 '#value' => t('Close'),
480 '#attributes' => array('onclick' => 'return ubrowser_action_close();'),
481 );
482 }
483
484 return $form;
485 }
486
487 function theme_ubrowser_node_actions_form($form) {
488 $output = '<table class="ubrowser-table"><tr>'
489 .'<td>'. drupal_render($form) .'</td>'
490 .'</tr></table>';
491
492 return $output;
493 }
494
495 /*******************************************************************************
496 * Module and Helper Functions
497 ******************************************************************************/
498
499 /**
500 * Return the HTML for a uBrowser populated div.
501 *
502 * @param $settings
503 * Associate array with the keys that define settings for uBrowser... see
504 * ubrowser_button() for a complete list.
505 * @return
506 * A string containing the HTML of the uBrowser div.
507 */
508 function ubrowser($settings, $id = 'ubrowser') {
509 drupal_add_css(drupal_get_path('module', 'ubrowser') .'/ubrowser.css');
510 drupal_add_js(drupal_get_path('module', 'ubrowser') .'/ubrowser.js', 'module');
511
512 $settings = ubrowser_check_settings($settings);
513
514 $output = '<div id="'. $id .'" class="'. $settings['class'] .'">'
515 . ubrowser_display($settings) .'</div>';
516
517 return $output;
518 }
519
520 /**
521 * Return the HTML for an empty uBrowser div.
522 *
523 * @param $id
524 * The ID of the generated div.
525 * @param $message
526 * The message to display in the div before loading uBrowser. Defaults to
527 * uBrowser, but can be set to NULL to display nothing.
528 * @param $class
529 * An optional class you can set for the div.
530 * @return
531 * A string containing the HTML of the div.
532 */
533 function ubrowser_div($id = 'ubrowser', $message = 'uBrowser', $class = 'ubrowser-div') {
534 if ($message !== NULL) {
535 $message = '<em>'. $message .'</em>';
536 }
537 if ($class !== NULL) {
538 $class = ' class="'. $class .'"';
539 }
540
541 drupal_add_css(drupal_get_path('module', 'ubrowser') .'/ubrowser.css');
542 drupal_add_js(drupal_get_path('module', 'ubrowser') .'/ubrowser.js', 'module');
543
544 return '<div id="'. $id .'"'. $class .'>'. $message .'</div>';
545 }
546
547 /**
548 * Return the HTML for a button to display uBrowser in a div.
549 *
550 * I pondered how to make this work. In the end I decided to just put the
551 * Javascript in the button element itself. An alternative was to write a
552 * function out in the page and call it with a button click, but since this is
553 * all transparent, I'm going to uglify the HTML and leave it like it is.
554 *
555 * @param $value
556 * Text displayed on the button.
557 * @param $settings
558 * Associate array with the following keys that define settings for uBrowser:
559 * - div = CSS selector of the div in which the uBrowser should display.
560 * - class = Class to add to the div when uBrowser is loaded and remove when
561 * uBrowser is closed.
562 * - vid = Vocabulary ID the uBrowser should browse.
563 * - nids = String true or false to display node IDs in the node select box.
564 * - filter = Comma delimeted string of node types to display.
565 * - search = String true or false to display a node search form.
566 * - close = String true or false to display a close button that wipes the div.
567 * - view = String true or false to display a view button that browses
568 * to the selected node.
569 * - window = String current or new for which window to view nodes in.
570 * - categ = String of text to replace 'category' in displays, or leave blank.
571 * - nodesg = String of text to replace 'node' in displays, or leave blank.
572 * - nodepl = String of text to replace 'nodes' in displays, or leave blank.
573 * - select = String of Javascript to execute on the select button, not
574 * displayed if no value is set.
575 * - multi = String true or false to allow users to select multiple nodes.
576 * @param $name
577 * Optional name of the button.
578 * @param $id
579 * Optional id of the button.
580 * @return
581 * A string containing the HTML of the button.
582 */
583 function ubrowser_button($value, $settings, $name = NULL, $id = NULL) {
584 $value = str_replace('"', '', $value);
585 if (strlen($value) == 0) {
586 $value = t('Display uBrowser');
587 }
588 if ($name !== NULL) {
589 $name = ' name="'. $name .'"';
590 }
591 if ($id !== NULL) {
592 $id = ' id="'. $id .'"';
593 }
594
595 drupal_add_css(drupal_get_path('module', 'ubrowser') .'/ubrowser.css');
596 drupal_add_js(drupal_get_path('module', 'ubrowser') .'/ubrowser.js', 'module');
597
598 $js_settings = str_replace('"', "'", drupal_to_js((object) $settings));
599
600 $output = '<input type="button" value="'. $value .'"'. $name . $id
601 .' onclick="display_ubrowser(\''. base_path() .'\', '. $js_settings
602 .');" />';
603
604 return $output;
605 }
606
607 /**
608 * Return the settings array with any ommitted settings set to defaults.
609 *
610 * @param $settings
611 * The settings array. See ubrowser_button() for available keys.
612 * @return
613 * The settings array with all ommitted settings set to their defaults.
614 */
615 function ubrowser_check_settings($settings) {
616 $defaults = array(
617 'class' => '',
618 'nids' => '',
619 'filter' => '',
620 'search' => 'false',
621 'close' => 'false',
622 'view' => 'false',
623 'select' => '',
624 'multi' => 'false',
625 );
626
627 foreach($defaults as $key => $value) {
628 if (!array_key_exists($key, $settings)) {
629 $settings[$key] = $value;
630 }
631 }
632
633 return $settings;
634 }
635
636 /**
637 * Display a form that lets you build and display uBrowser on the fly.
638 *
639 * ubrowser_builder() lets you print a form to a page that can be used for
640 * custom building of uBrowsers. This is used from ubrowser_admin() to display
641 * the form at admin/content/browse.
642 *
643 * @param $settings
644 * A settings array that specifies what to set as default settings and what
645 * to display as options on the form. The following keys should either be
646 * set to TRUE, meaning they will be displayed on the form, or to an actual
647 * value to be used as the default value:
648 * - div = MUST be set, cannot be TRUE. Use a CSS selector for the div the
649 * custom built uBrowser will populate.
650 * - class = MUST be set, cannot be TRUE. The name of the class to add to
651 * the uBrowser div when it's loaded.
652 * - vid = TRUE to display a select box of vocabularies, otherwise specify
653 * the vocabulary ID this uBrowser must use.
654 * - nids = TRUE to display a checkbox for displaying node IDs, otherwise
655 * specify the string true or false.
656 * - search = TRUE to display a checkbox for displaying the search form,
657 * otherwise specify the string true or false.
658 * - view = TRUE to display a checkbox for displaying the view button,
659 * otherwise specify the string true or false.
660 * - window = TRUE to display a checkbox for choosing the view target window,
661 * otherwise specify the string new or current.
662 * - close = TRUE to display a checkbox for displaying the close button,
663 * otherwise specify the string true or false.
664 * - multi = TRUE to display a checkbox for displaying the multi select box,
665 * otherwise specify the string true or false.
666 * - filter = TRUE to display a set of checkboxes to filter displayed nodes by
667 * node type; only shows node types associated with current
668 * vocabularies. Alternately, specify a comma delimited string
669 * of node types to filter by default.
670 * - select = TRUE to display a textfield for entering Javascript for the
671 * select button, otherwise specify an empty string to not show
672 * a select button or a string of Javascript to execute.
673 * - nodesg = TRUE to display a textfield for entering the replacement text
674 * for the word 'node' in uBrowser displays. May be a string
675 * for the default replacement, or may be left unset for 'node'.
676 * - nodepl = TRUE to display a textfield for entering the replacement text
677 * for the word 'nodes' in uBrowser displays. May be a string
678 * for the default replacement, or may be left unset for 'nodes'.
679 * - code = MUST be either not set (to not display code) or set to the
680 * CSS selector of a textarea to populate with the PHP code to
681 * reproduce the built uBrowser. I can't imagine anyone else
682 * using this but figured it should be documented.
683 * - display = Optional, replacement text for the "Display uBrowser" button.
684 */
685 function ubrowser_builder($settings) {
686 $form['ubb-div'] = array(
687 '#type' => 'hidden',
688 '#value' => $settings['div'],
689 );
690
691 // Add the vocabulary select box
692 if ($settings['vid'] === TRUE) {
693 $vocabs = taxonomy_get_vocabularies();
694 $options = array();
695 foreach($vocabs as $vocab) {
696 $options[$vocab->vid] = $vocab->name;
697 }
698 $form['ubb-vid'] = array(
699 '#type' => 'select',
700 '#title' => t('Vocabulary'),
701 '#description' => t('Select which vocabulary to browse.'),
702 '#options' => $options,
703 );
704 }
705 else {
706 $form['ubb-vid'] = array(
707 '#type' => 'hidden',
708 '#value' => $settings['vid'],
709 );
710 }
711
712 // Add the node type selection boxes
713 $form['filter-options'] = array(
714 '#type' => 'fieldset',
715 '#title' => t('Filter by node type'),
716 '#collapsible' => TRUE,
717 '#collapsed' => TRUE,
718 );
719
720 if ($settings['filter'] === TRUE) {
721 $options = array();
722 $result = db_query("SELECT DISTINCT type FROM {vocabulary_node_types}");
723 while ($type = db_fetch_object($result)) {
724 $options[$type->type] = $type->type;
725 }
726 $form['filter-options']['ubb-filter'] = array(
727 '#type' => 'checkboxes',
728 '#title' => t('Filter results for the following node types'),
729 '#description' => t('Selecting none will display all nodes.'),
730 '#options' => $options,
731 '#prefix' => '<span id="filter-checkboxes">',
732 '#suffix' => '</span>',
733 );
734 }
735 else {
736 $form['filter-options']['ubb-filter-desc'] = array(
737 '#value' => '<div>Using default filter: '. $settings['filter'] .'</div>',
738 );
739 $form['filter-options']['ubb-filter'] = array(
740 '#type' => 'hidden',
741 '#value' => $settings['filter'],
742 );
743 }
744
745 // Add a select button textfield
746 if ($settings['select'] === TRUE) {
747 $form['ubb-select'] = array(
748 '#type' => 'textfield',
749 '#title' => t('Select button Javascript'),
750 '#description' => t('Leave blank for no select button.'),
751 '#size' => 48,
752 );
753 }
754 else {
755 $form['ubb-select'] = array(
756 '#type' => 'hidden',
757 '#value' => $settings['select'],
758 );
759 }
760
761 // Add the various settings checkboxes
762
763 $form['display-options'] = array(
764 '#type' => 'fieldset',
765 '#title' => t('Display options'),
766 '#collapsible' => TRUE,
767 '#collapsed' => TRUE,
768 );
769
770 if ($settings['nids'] === TRUE) {
771 $form['display-options']['ubb-nids'] = array(
772 '#type' => 'checkbox',
773 '#title' => t('Display node IDs'),
774 );
775 }
776 else {
777 $form['display-options']['ubb-nids'] = array(
778 '#type' => 'hidden',
779 '#value' => 'false',
780 );
781 }
782
783 if ($settings['search'] === TRUE) {
784 $form['display-options']['ubb-search'] = array(
785 '#type' => 'checkbox',
786 '#title' => t('Display the search form'),
787 );
788 }
789 else {
790 $form['display-options']['ubb-search'] = array(
791 '#type' => 'hidden',
792 '#value' => 'false',
793 );
794 }
795
796 if ($settings['view'] === TRUE) {
797 $form['display-options']['ubb-view'] = array(
798 '#type' => 'checkbox',
799 '#title' => t('Display view button'),
800 );
801 }
802 else {
803 $form['display-options']['ubb-view'] = array(
804 '#type' => 'hidden',
805 '#value' => 'false',
806 );
807 }
808
809 if ($settings['window'] === TRUE) {
810 $form['display-options']['ubb-window'] = array(
811 '#type' => 'checkbox',
812 '#title' => t('View nodes in new window'),
813 );
814 }
815 else {
816 $form['display-options']['ubb-window'] = array(
817 '#type' => 'hidden',
818 '#value' => 'current',
819 );
820 }
821
822 if ($settings['close'] === TRUE) {
823 $form['display-options']['ubb-close'] = array(
824 '#type' => 'checkbox',
825 '#title' => t('Display close button'),
826 );
827 }
828 else {
829 $form['display-options']['ubb-close'] = array(
830 '#type' => 'hidden',
831 '#value' => 'false',
832 );
833 }
834
835 if ($settings['multi'] === TRUE) {
836 $form['display-options']['ubb-multi'] = array(
837 '#type' => 'checkbox',
838 '#title' => t('Allow multiple node selection'),
839 );
840 }
841 else {
842 $form['display-options']['ubb-multi'] = array(
843 '#type' => 'hidden',
844 '#value' => 'false',
845 );
846 }
847
848 $form['display-options']['ubb-class'] = array(
849 '#type' => 'textfield',
850 '#title' => t('CSS class name'),
851 '#description' => t('Add a CSS class to the browser.'),
852 '#default_value' => $settings['class'],
853 '#size' => 24,
854 );
855
856 $form['display-names'] = array(
857 '#type' => 'fieldset',
858 '#title' => t('Alter display terms'),
859 '#collapsible' => TRUE,
860 '#collapsed' => TRUE,
861 );
862 $form['display-names']['instructions'] = array(
863 '#value' => '<div>'. t('Use the following textfields to provide alternate words for display in uBrowser.') .'</div>',
864 );
865
866 if ($settings['categ'] === TRUE) {
867 $form['display-names']['ubb-categ'] = array(
868 '#type' => 'textfield',
869 '#title' => t('category'),
870 '#size' => 16,
871 '#prefix' => '<span style="float: left; margin-right: 1em;">',
872 '#suffix' => '</span>',
873 );
874 }
875 else {
876 $form['display-names']['ubb-categ'] = array(
877 '#type' => 'hidden',
878 '#value' => $settings['categ'],
879 );
880 }
881
882 if ($settings['nodesg'] === TRUE) {
883 $form['display-names']['ubb-nodesg'] = array(
884 '#type' => 'textfield',
885 '#title' => t('node'),
886 '#size' => 16,
887 '#prefix' => '<span style="float: left; margin-right: 1em;">',
888 '#suffix' => '</span>',
889 );
890 }
891 else {
892 $form['display-names']['ubb-nodesg'] = array(
893 '#type' => 'hidden',
894 '#value' => $settings['nodesg'],
895 );
896 }
897
898 if ($settings['nodepl'] === TRUE) {
899 $form['display-names']['ubb-nodepl'] = array(
900 '#type' => 'textfield',
901 '#title' => t('nodes'),
902 '#size' => 16,
903 '#prefix' => '<span style="float: left; margin-right: 1em;">',
904 '#suffix' => '</span>',
905 );
906 }
907 else {
908 $form['display-names']['ubb-nodepl'] = array(
909 '#type' => 'hidden',
910 '#value' => $settings['nodepl'],
911 );
912 }
913
914 if (isset($settings['code'])) {
915 $code_id = $settings['code'];
916 }
917 else {
918 $code_id = 'null';
919 }
920
921 $form['ubb-build'] = array(
922 '#type' => 'submit',
923 '#value' => isset($settings['display']) ? $settings['display'] : t('Display uBrowser'),
924 '#attributes' => array('onclick' => "return build_ubrowser('". base_path()
925 ."', '$code_id');"),
926 );
927
928 return $form;
929 }
930
931 function theme_ubrowser_builder($form) {
932 $output = '<table cellpadding="5"><tr valign="top">'
933 .'<td>'. drupal_render($form['ubb-vid']) .'</td>'
934 .'<td>'. drupal_render($form['ubb-select']) .'</td>'
935 .'</tr><tr valign="top">'
936 .'<td>'. drupal_render($form['display-options']) .'</td>'
937 .'<td>'. drupal_render($form['filter-options']) .'</td>'
938 .'</tr><tr>'
939 .'<td colspan="2">'. drupal_render($form['display-names']) .'</td>'
940 .'</tr><tr>'
941 .'<td colspan="2">'. drupal_render($form) .'</td>'
942 .'</tr></table>';
943
944 return $output;
945 }
946
947 /**
948 * Rewrite of taxonomy_select_nodes() to return every matching node, not just
949 * a subset for paged display. Includes ability to filter by node type.
950 */
951 function _ubrowser_select_nodes($tids = array(), $filter = NULL, $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC') {
952 if (count($tids) > 0) {
953 if ($filter != NULL) {
954 if (strpos($filter, ',') > 0) {
955 $filter_array = explode(',', $filter);
956 $filter_where = '';
957 foreach ($filter_array as $type) {
958 $filter_where .= ($filter_where == '') ? ' AND (' : ' OR ';
959 $filter_where .= "n.type = '". $type ."'";
960 }
961 $filter_where .= ')';
962 }
963 else {
964 $filter_where = " AND n.type = '". $filter ."'";
965 }
966 }
967
968 $descendant_tids = array();
969 if ($depth === 'all') {
970 $depth = NULL;
971 }
972 foreach ($tids as $index => $tid) {
973 $term = taxonomy_get_term($tid);
974 $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
975 $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
976 }
977
978 if ($operator == 'or') {
979 $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
980 $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 '. $filter_where .' ORDER BY '. $order;
981 }
982 else {
983 $joins = '';
984 $wheres = '';
985 foreach ($descendant_tids as $index => $tids) {
986 $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
987 $wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
988 }
989 $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres . $filter_where .' ORDER BY '. $order;
990 }
991 $sql = db_rewrite_sql($sql);
992 $result = db_query($sql);
993 }
994
995 return $result;
996 }
997
998 /**
999 * Rewrite of do_search() to return every matching node, not just a subset
1000 * for paged display.
1001 */
1002 function _ubrowser_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $select2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
1003 $query = search_parse_query($keywords);
1004
1005 if ($query[2] == '') {
1006 form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
1007 }
1008 if ($query === NULL || $query[0] == '' || $query[2] == '') {
1009 return array();
1010 }
1011
1012 $conditions = $where1 .' AND ('. $query[2] .") AND i.type = '%s'";
1013 $arguments = array_merge($arguments1, $query[3], array($type, $query[4]));
1014 $result = db_query_temporary("SELECT i.type, i.sid, SUM(i.score * t.count) AS relevance, COUNT(*) AS matches FROM {search_index} i INNER JOIN {search_total} t ON i.word = t.word $join1 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d", $arguments, 'temp_search_sids');
1015
1016 $normalize = db_result(db_query('SELECT MAX(relevance) FROM temp_search_sids'));
1017 if (!$normalize) {
1018 return array();
1019 }
1020 $select2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * i.relevance)', $select2);
1021
1022 $conditions = '('. $query[0] .')';
1023 $arguments = array_merge($arguments2, $query[1]);
1024 $result = db_query_temporary("SELECT i.type, i.sid, $select2 FROM temp_search_sids i INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type $join2 WHERE $conditions $sort_parameters", $arguments, 'temp_search_results');
1025 if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_results'))) == 0) {
1026 return array();
1027 }
1028 $count_query = "SELECT $count";
1029
1030 $result = db_query("SELECT * FROM temp_search_results");
1031 $results = array();
1032 while ($item = db_fetch_object($result)) {
1033 $results[] = $item;
1034 }
1035 return $results;
1036 }

  ViewVC Help
Powered by ViewVC 1.1.2