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

Diff of /contributions/modules/livesearch/livesearch.module

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

revision 1.17, Wed Aug 8 03:08:14 2007 UTC revision 1.18, Sun Jan 20 19:49:54 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: livesearch.module,v 1.16 2007/05/08 06:19:14 kourge Exp $  // $Id: livesearch.module,v 1.17 2007/08/08 03:08:14 kourge Exp $
3    
4  /**  /**
5   * Implementation of hook_menu().   * Implementation of hook_menu().
# Line 8  function livesearch_menu($may_cache) { Line 8  function livesearch_menu($may_cache) {
8    static $settings_injected;    static $settings_injected;
9    $items = array();    $items = array();
10    $path = drupal_get_path('module', 'livesearch');    $path = drupal_get_path('module', 'livesearch');
11    
12    if (user_access('search content')) {    if (user_access('search content')) {
13      drupal_add_js($path . '/livesearch.js');      drupal_add_js($path .'/livesearch.js');
14      drupal_add_css($path . '/livesearch.css');      drupal_add_css($path .'/livesearch.css');
15      drupal_add_css(drupal_get_path('module', 'search') . '/search.css');      drupal_add_css(drupal_get_path('module', 'search') .'/search.css');
16    
17      if (!$settings_injected) {      if (!$settings_injected) {
18        $search_results_location = (string)variable_get('search_results_location', 'after_search_box');        $search_results_location = variable_get('livesearch_search_results_location', 'after_search_box');
19        $settings = array('liveSearch' => array(        $settings = array('liveSearch' => array(
20          'targetSearchBox' => (string)variable_get('target_search_box', 'theme'),          'targetSearchBox' => variable_get('livesearch_target_search_box', 'theme'),
21          'compactSearchBox' => (bool)variable_get('compact_search_box', TRUE),          'compactSearchBox' => variable_get('livesearch_compact_search_box', TRUE),
22          'delayDuration' => (int)variable_get('delay_duration', 1250),          'delayDuration' => variable_get('livesearch_delay_duration', 1250),
23          'queryURL' => url('livesearch/node'),          'queryURL' => url('livesearch/node'),
24          'minimumWordSize' => (int)variable_get('minimum_word_size', 3),          'minimumWordSize' => variable_get('minimum_word_size', 3),
25          'useCustomElement' => ($search_results_location == 'specified_custom_element'),          'useCustomElement' => ($search_results_location == 'specified_custom_element'),
26          'customElementId' => (string)variable_get('custom_search_result_element_id', ''),          'customElementId' => variable_get('livesearch_custom_search_result_element_id', ''),
27          'hideSnippets' => (bool)variable_get('hide_snippets', FALSE),          'hideSnippets' => variable_get('livesearch_hide_snippets', FALSE),
28          'showItemInfo' => (bool)variable_get('show_item_info', TRUE),          'showItemInfo' => variable_get('livesearch_show_item_info', TRUE),
29          'scrollToResults' => (bool)variable_get('scroll_to_search_result', TRUE),          'scrollToResults' => variable_get('livesearch_scroll_to_search_result', TRUE),
30          'searchAllowed' => TRUE          'searchAllowed' => TRUE
31        ));        ));
32    
33        drupal_add_js($settings, 'setting');        drupal_add_js($settings, 'setting');
34        $settings_injected = TRUE;        $settings_injected = TRUE;
35      }      }
# Line 40  function livesearch_menu($may_cache) { Line 40  function livesearch_menu($may_cache) {
40        $settings_injected = TRUE;        $settings_injected = TRUE;
41      }      }
42    }    }
43    
44    if (!$may_cache) {    if (!$may_cache) {
45      $items[] = array('path' => 'livesearch', 'title' => t('search'),      $items[] = array('path' => 'livesearch', 'title' => t('search'),
46        'callback' => 'livesearch_view',        'callback' => 'livesearch_view',
# Line 64  function livesearch_menu($may_cache) { Line 64  function livesearch_menu($may_cache) {
64  function livesearch_view() {  function livesearch_view() {
65    $keys = search_get_keys();    $keys = search_get_keys();
66    $type = arg(1);    $type = arg(1);
67    
68    // Search only if there's actually something.    // Search only if there's actually something.
69    if (trim($keys)){    if (trim($keys)){
70      // Log the search keys:      // Log the search keys:
71      watchdog('search', t('%keys (@type).', array(      watchdog('search', t('%keys (@type).', array(
72        '%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')        '%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')
73      )), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));      )), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
74    
75      // Collect the search results:      // Collect the search results:
76      $results = search_data($keys);      $results = search_data($keys);
77    
# Line 90  function livesearch_view() { Line 90  function livesearch_view() {
90    
91      // Output the bare results.      // Output the bare results.
92      print drupal_to_js($results);      print drupal_to_js($results);
93        exit;
94    }    }
95    return;    return;
96  }  }
# Line 98  function livesearch_view() { Line 99  function livesearch_view() {
99   * Menu callback; displays the livesearch module settings page.   * Menu callback; displays the livesearch module settings page.
100   */   */
101  function livesearch_admin_settings() {  function livesearch_admin_settings() {
102    $form = array(    $form = array();
103      'appearance' => array(  
104      $form['appearance'] = array(
105        '#type' => 'fieldset',        '#type' => 'fieldset',
106        '#title' => t('Appearance'),        '#title' => t('Appearance'),
107      );
108        'target_search_box' => array(    $form['appearance']['livesearch_target_search_box'] = array(
109          '#type' => 'radios',      '#type' => 'radios',
110          '#title' => t('Target search box'),      '#title' => t('Target search box'),
111          '#default_value' => (string)variable_get('target_search_box', 'theme'),      '#default_value' => variable_get('livesearch_target_search_box', 'auto'),
112          '#options' => array('theme' => t('Theme'), 'block' => t('Block')),      '#options' => array(
113          '#description' => t('The targeted search box will provide live search functionality.')        'auto' => t('Automatically detect'),
114        ),        'block' => t('Block'),
115        'compact_search_box' => array(        'theme' => t('Theme')
         '#type' => 'checkbox',  
         '#title' => t('Compact search box'),  
         '#default_value' => (bool)variable_get('compact_search_box', TRUE),  
         '#description' => t('This will place the word Search in the search box. The word Search will disappear once the box gains focus and will appear again if the box is empty when it loses focus, like Apple-style search boxes. This will also add a "compact" class to the search form. The default Live Search stylesheet hides the Search button and lengthens the search box when this option is enabled. How the form is ultimately styled in this condition is completely up to the theme applied. It is recommended that the Search block title to be hidden as well to eliminate redundancy, although the default Live Search stylesheet does not do this.')  
       ),  
   
       'search_results' => array(  
         '#type' => 'fieldset',  
         '#title' => t('Search results'),  
   
         'search_results_location' => array(  
           '#type' => 'radios',  
           '#title' => t('Location'),  
           '#default_value' => (string)variable_get('search_results_location', 'after_search_box'),  
           '#options' => array(  
             'after_search_box' => t('After search box'),  
             'specified_custom_element' => t('Use specified custom element')  
           ),  
           '#description' => t('Specify where the search results will be injected into. By default, it will be injected in a div element with the id "live-search-results" right after the search box. You can also manually specify which element to inject the search results in by supplying its id. This will allow more theming flexibility.')  
         ),  
         'custom_search_result_element_id' => array(  
           '#type' => 'textfield',  
           '#title' => t('Custom element id'),  
           '#default_value' => (string)variable_get('custom_search_result_element_id', ''),  
           '#description' => t('Specify the id of the custom element that you want the search results injected into.')  
         ),  
   
         'hide_snippets' => array(  
           '#type' => 'checkbox',  
           '#title' => t('Hide snippets'),  
           '#default_value' => (bool)variable_get('hide_snippets', FALSE),  
           '#description' => t('A snippet is an excerpt of content indicating where the keyword occurs in an item.')  
         ),  
         'show_item_info' => array(  
           '#type' => 'checkbox',  
           '#title' => t('Show item info'),  
           '#default_value' => (bool)variable_get('show_item_info', TRUE),  
           '#description' => t('An item info is the string of node metadata indicating, for example, node type, author, posted date, number of comments, etc.')  
         ),  
         'scroll_to_search_result' => array(  
           '#type' => 'checkbox',  
           '#title' => t('Scroll to the top of search results'),  
           '#default_value' => (bool)variable_get('scroll_to_search_result', TRUE),  
           '#description' => t('When a search is completed, scroll to the top of the search result. This is particularly useful when results are paged, since the user will not have to scroll back up to the top of the results.')  
         )  
       )  
116      ),      ),
117        '#description' => t('The targeted search box will provide live search functionality. With "Automatically detect," Live Search would first try the block search box, then the theme search box, after that, other common names, and finally fail silently if none is found.')
118      'performance' => array(    );
119        '#type' => 'fieldset',    $form['appearance']['livesearch_compact_search_box'] = array(
120        '#title' => t('Performance'),      '#type' => 'checkbox',
121        '#collapsible'=> TRUE,      '#title' => t('Compact search box'),
122        '#default_value' => variable_get('livesearch_compact_search_box', TRUE),
123        'delay_duration' => array(      '#description' => t('This will place the word Search in the search box. The word Search will disappear once the box gains focus and will appear again if the box is empty when it loses focus, like Apple-style search boxes. This will also add a "compact" class to the search form. The default Live Search stylesheet hides the Search button and lengthens the search box when this option is enabled. How the form is ultimately styled in this condition is completely up to the theme applied. It is recommended that the Search block title to be hidden as well to eliminate redundancy, although the default Live Search stylesheet does not do this.')
124          '#type' => 'textfield',    );
125          '#title' => t('Ajax request firing delay'),  
126          '#default_value' => (int)variable_get('delay_duration', 1250),    $form['appearance']['search_results'] = array(
127          '#size' => 5,      '#type' => 'fieldset',
128          '#maxlength' => 5,      '#title' => t('Search results')
         '#description' => t('An Ajax request is fired after this amount of period when the user stops typing. This duration is measured in milliseconds. This value should be low enough so that the user does not notice a significant delay, yet high enough so that a mere pause in typing or slower typing will not fire a horde of requests. The recommended value is higher than 1000 milliseconds (1 second) but lower than 2000 milliseconds (2 seconds).')  
       )  
     )  
129    );    );
130      $form['appearance']['search_results']['livesearch_search_results_location'] = array(
131        '#type' => 'radios',
132        '#title' => t('Location'),
133        '#default_value' => variable_get('livesearch_search_results_location', 'after_search_box'),
134        '#options' => array(
135          'after_search_box' => t('After search box'),
136          'specified_custom_element' => t('Use specified custom element')
137        ),
138        '#description' => t('Specify where the search results will be injected into. By default, it will be injected in a div element with the id "live-search-results" right after the search box. You can also manually specify which element to inject the search results in by supplying its id. This will allow more theming flexibility.')
139      );
140    
141      $form['appearance']['search_results']['livesearch_custom_search_result_element_id'] = array(
142        '#type' => 'textfield',
143        '#title' => t('Custom element id'),
144        '#default_value' => variable_get('livesearch_custom_search_result_element_id', ''),
145        '#description' => t('Specify the id of the custom element that you want the search results injected into.')
146      );
147      $form['appearance']['search_results']['livesearch_hide_snippets'] = array(
148        '#type' => 'checkbox',
149        '#title' => t('Hide snippets'),
150        '#default_value' => variable_get('livesearch_hide_snippets', FALSE),
151        '#description' => t('A snippet is an excerpt of content indicating where the keyword occurs in an item.')
152      );
153      $form['appearance']['search_results']['livesearch_show_item_info'] = array(
154        '#type' => 'checkbox',
155        '#title' => t('Show item info'),
156        '#default_value' => variable_get('livesearch_show_item_info', TRUE),
157        '#description' => t('An item info is the string of node metadata indicating, for example, node type, author, posted date, number of comments, etc.')
158      );
159      $form['appearance']['search_results']['livesearch_scroll_to_search_result'] = array(
160        '#type' => 'checkbox',
161        '#title' => t('Scroll to the top of search results'),
162        '#default_value' => variable_get('livesearch_scroll_to_search_result', TRUE),
163        '#description' => t('When a search is completed, scroll to the top of the search result. This is particularly useful when results are paged, since the user will not have to scroll back up to the top of the results.')
164      );
165    
166      $form['performance'] = array(
167        '#type' => 'fieldset',
168        '#title' => t('Performance'),
169        '#collapsible'=> TRUE
170      );
171      $form['performance']['livesearch_delay_duration'] = array(
172        '#type' => 'textfield',
173        '#title' => t('Ajax request firing delay'),
174        '#default_value' => variable_get('livesearch_delay_duration', 1250),
175        '#size' => 5,
176        '#maxlength' => 5,
177        '#description' => t('An Ajax request is fired after this amount of period when the user stops typing. This duration is measured in milliseconds. This value should be low enough so that the user does not notice a significant delay, yet high enough so that a mere pause in typing or slower typing will not fire a horde of requests. The recommended value is higher than 1000 milliseconds (1 second) but lower than 2000 milliseconds (2 seconds).')
178      );
179    
180    return system_settings_form($form);    return system_settings_form($form);
181  }  }
182    
183  /**  /**
184   * Validate callback for settings page.   * Validation callback for settings page.
185   */   */
186  function livesearch_admin_settings_validate($form_id, $form_values) {  function livesearch_admin_settings_validate($form_id, $form_values) {
187    if (!ctype_digit($form_values['delay_duration'])) {    if (!ctype_digit($form_values['livesearch_delay_duration'])) {
188      form_set_error('delay_duration', t('The firing delay duration must be a number that is an integer.'));      form_set_error('delay_duration', t('The firing delay duration must be a number that is an integer.'));
189    }    }
190  }  }

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.18

  ViewVC Help
Powered by ViewVC 1.1.2