array( '#title' => 'Search', 'view', 'customize', 'search content', 'search menus', 'search users' ) ); } /** * Implementation of hook_navigation_widgets(). */ function navigate_search_navigate_plugin($op, $widget = NULL) { switch ($op) { case 'output': switch ($settings['plugin']) { case 'navigate_search': if (!user_access('navigate_search search content') && !user_access('navigate_search search menu')) { return FALSE; } return navigate_search_widget($settings['wid']); break; } break; case 'settings': return array( 'widget' => array( 'navigate_search' => array( 'callback' => NULL, 'content' => 'Search', 'name' => 'Search', 'single' => FALSE, ), ) ); break; } } /** * Generate search widget */ function navigate_search_widget($wid) { $settings = navigate_widget_settings_get($wid); if (user_access('navigate_search search menu')) { $inputs['menu'] = navigate_button(array( 'name' => 'menu', 'content' => t('Menu'), 'class' => 'navigate-search-menu', 'default' => '1', 'on' => 1, 'off' => 0, 'help' => 'When this is checked, searching will search menu items.', 'wid' => $wid, )); } if (user_access('navigate_search search users')) { $inputs['users'] = navigate_button(array( 'name' => 'users', 'content' => t('Users'), 'class' => 'navigate-search-users', 'default' => '0', 'on' => 1, 'off' => 0, 'help' => 'When this is checked, searching will search users.', 'wid' => $wid, )); } if (user_access('navigate_search search content')) { $inputs['quick'] = navigate_button(array( 'name' => 'type', 'content' => t('Quick'), 'class' => 'navigate-search-quick', 'group' => 'navigate_search_type', 'default' => 'quick', 'help' => 'When selected, this will search titles for your search phrase.', 'on' => 'quick', 'wid' => $wid, )); $inputs['full'] = navigate_button(array( 'name' => 'type', 'content' => t('Full'), 'class' => 'navigate-search-full', 'group' => 'navigate_search_type', 'default' => 'quick', 'help' => 'When selected, the search will be done using the site\'s search functions. This means that you will need to run cron.php before this will return any results.', 'on' => 'full', 'wid' => $wid, )); } $inputs['search'] = navigate_input(array( 'widget_type' => 'search', 'name' => 'search_phrase', 'class' => 'navigate-search-phrase', 'select_all' => TRUE, 'callback' => 'process', 'help' => 'Type a search phrase and press Enter to submit.', 'wid' => $wid, )); $inputs['search_button'] = navigate_callback_button(array( 'plugin' => 'navigate_search', 'id' => 'search-submit', 'callback' => 'process', 'tooltip' => 'Click to run search.', 'value' => '', 'wid' => $wid, )); // Get content types $result = db_query('SELECT type FROM {node_type}'); while ($row = db_fetch_array($result)) { $content_type_inputs[$row['type']] = navigate_button(array( 'name' => 'content_type_'. $row['type'], 'content' => ucwords(str_replace('_', ' ', $row['type'])), 'class' => 'navigate-content-type-'. $row['type'], 'default' => 1, 'on' => 1, 'wid' => $wid, )); } // Get cache $cache = navigate_widget_cache_get($wid, 'content'); $output = theme('navigate_search_widget', $inputs, $content_type_inputs, $wid, $cache); return $output; } /** * Theme search widget */ function theme_navigate_search_widget($inputs, $content_type_inputs, $wid, $cache) { //navigate_button('menu', t('menu'), 'navigate-search-menu', $wid); // Hide results if there's no cache $hide = ''; if (!$cache) { $hide = ' style="display:none" '; } $search_inputs = ''; if (!user_access('navigate customize')) { $search_inputs = '
'. $search_inputs .'
'; } $menu = ''; if (user_access('navigate_search search menu')) { $menu = ' '; } $users = ''; if (user_access('navigate_search search users')) { $users = ' '; } $content['widget'] = ' '. $search_inputs .' '; $content['title'] = t('Search'); $content_types = ''; foreach ($content_type_inputs as $type) { $content_types .= ''; } $content['settings'] = ' '; return $content; } /** * Select all or none for content type select */ function navigate_search_select_all_types() { global $user; if (!$wid = db_result(db_query("SELECT wid FROM {navigate_widgets} WHERE wid = '%d' AND uid = '%d'", $_POST['wid'], $user->uid))) { return FALSE; } $before = 0; $after = 1; if (isset($_POST['none'])) { $before = 1; $after = 0; } $settings = navigate_widget_settings_get($wid); foreach ($settings as $key => $val) { if (strpos($key, 'content_type_') !== FALSE && $val == $before) { navigate_variable_set(array('name' => $key, 'value' => $after, 'wid' => $wid)); } } } function navigate_search_search() { global $_navigate_search_matches; $output = ''; $settings = navigate_widget_settings_get($_POST['wid']); foreach ($settings as $key => $val) { if (strpos($key, 'content_type_') !== FALSE && $val == 1) { $types[] = str_replace('content_type_', '', $key); } } $phrase = check_plain($_POST['phrase']); if ($phrase == '') { navigate_widget_cache_set($_POST['wid'], 'content', ''); return FALSE; } // Quick content search if (user_access('navigate_search search content')) { // Search content if ($settings['type'] == 'quick') { $search_content = TRUE; // Search by title $divider = ''; $and = ''; $search_query = ''; $search_array = explode(' ', $phrase); foreach ($search_array as $value) { $search_query .= $and .' title LIKE "%%%s%" '; $search_values[] = $value; $and = ' AND '; } // Get types $or = ''; $type_query = ''; if (isset($types)) { foreach ($types as $type) { $type_query .= $or .' type="%s" '; $search_values[] = $type; $or = ' OR '; } if ($type_query) { $type_query = ' AND ('. $type_query .') '; } } // Full query if ($search_query) { $full_query = 'SELECT title,nid FROM {node} WHERE ('. $search_query .') '. $type_query .' ORDER BY {node}.changed DESC, {node}.title ASC'; } $result = db_query_range($full_query, $search_values, 0, 50); $content_matches = ''; while ($row = db_fetch_array($result)) { $menu_item = array( 'path' => 'node/'. $row['nid'], 'title' => $row['title'], ); $menu_item['wid'] = $_POST['wid']; $content_matches .= theme('navigate_link', $menu_item); $content_results = TRUE; } // Full content search } elseif ($settings['type'] == 'full') { $content_matches = ''; if (drupal_strlen($phrase) < 3) { $output .= '
'. t('Please enter a longer phrase') .'
'; $divider = ''; } else { $search_content = TRUE; // Search using drupal search $divider = ''; // Get types $types_key = ''; if (isset($types)) { $types_key = ' type:'. implode(',', $types); } //$phrase .= ' type:page '; $results = module_invoke('node', 'search', 'search', $phrase . $types_key); foreach ($results as $result) { $menu_item = array( 'path' => 'node/'. $result['node']->nid, 'title' => $result['title'], 'description' => '
Type: '. $result['type'] .'
'. $result['snippet'], ); $menu_item['wid'] = $_POST['wid']; $content_matches .= theme('navigate_link', $menu_item); $content_results = TRUE; } } } $output = ''; if (isset($search_content)) { if (!isset($content_results)) { $output .= '
'. t('No content results') .'
'; } else { $output .= ''. $content_matches; } } } // Search menu items if (user_access('navigate_search search menu')) { // Search menu if ($settings['menu'] == 1) { $divider = ''; if (isset($divider)) { $output .= $divider; } navigate_search_menu_tree(); $_navigate_search_matches = navigate_devel_menu($_navigate_search_matches); $phrase_array = explode(' ', $phrase); $menu_matches = ''; foreach ($_navigate_search_matches as $menu_item) { $menu_item['description'] = isset($menu_item['description']) ? $menu_item['description'] : ''; $searchable = $menu_item['title'] .' '. $menu_item['path'] .' '. $menu_item['description']; $match = 1; foreach ($phrase_array as $phrase) { if (stripos($searchable, $phrase, 0) === FALSE) { $match = 0; } } if ($match != 0) { $menu_item['wid'] = $_POST['wid']; $menu_matches .= theme('navigate_link', $menu_item); $menu_results = TRUE; } } if (!isset($menu_results)) { $output .= '
'. t('No menu results') .'
'; } else { $output .= ''. $menu_matches; } } } // Search users if (user_access('navigate_search search users')) { if ($settings['users'] == 1) { if (isset($divider)) { $output .= $divider; } $matches = ''; $result = db_query_range("SELECT uid, name FROM {users} WHERE (uid = '%s' OR name LIKE '%%%s%') AND uid <> 0", $phrase, $phrase, 0, 50); while ($row = db_fetch_array($result)) { $matches .= theme('navigate_search_link', $row['uid'], $row['name']); } if ($matches == '') { $output .= '
'. t('No user results') .'
'; } else { $output .= ''. $matches; } } } navigate_widget_cache_set($_POST['wid'], 'content', $output); navigate_json(array('data' => $output)); } /** * Theme a link */ function theme_navigate_search_link($uid, $username) { $char_count = 22; if (drupal_strlen($username) > $char_count) { $title = drupal_substr($username, 0, $char_count-3) .'...'; } else { $title = $username; } return ''; } /** * Create an array of all menu items */ function navigate_search_menu_tree($data = '') { global $_navigate_search_matches; if (!$data) { $data = menu_tree_all_data(); } foreach ($data as $key => $data) { if ($data['link']["hidden"] == 0) { $_navigate_search_matches[] = array( 'title' => $data['link']["title"], 'path' => $data['link']['href'], 'description' => $data['link']['description'], ); navigate_search_sub_tree($data); } } } /** * Used with navigate_search_menu_tree to iterate through menu */ function navigate_search_sub_tree($data) { if ($data['below']) { $content = navigate_search_menu_tree($data['below']); } } /** * Implementation of hook_navigate_help_page(). */ function navigate_search_navigate_help_page() { $help['access'] = FALSE; if (user_access('navigate_search search content') || user_access('navigate_search search menu')) { $help['access'] = TRUE; } $help['content'] = t('

The Search widget allows a user to search content and / or menu items. To use, type a word or two in the search box, and then presses enter or click the search icon to conduct the search. You can use partial words in your search as well. Here\'s a quick rundown of the settings:

Press CTL+SHIFT+S to focus on the search bar, even if Navigate is hidden.

'); $help['title'] = 'Search'; return $help; } /** * Implementation of hook_theme(). */ function navigate_search_theme() { return array( 'navigate_search_widget' => array( 'arguments' => array('inputs' => NULL, 'content_type_inputs' => NULL, 'wid' => NULL), ), 'navigate_search_link' => array( 'arguments' => array('uid' => NULL, 'username' => NULL), ), ); } /** * Define stripos for php 4 */ if (!function_exists('stripos')) { function stripos($haystack, $needle) { return strpos($haystack, stristr( $haystack, $needle )); } }