search_config.module 7.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
//$Id$

/**
 * @file
 * Allows admins to configure the advanced search form.
 *
 * In general the search_config module allows users to decide which fields
 * to show on the advanced search form. This does not stop astute users from
 * placing the search criteria directly in the search text fields. The module
Nesta Campbell's avatar
Nesta Campbell committed
11
 * also provides four (4) new permissions but for the time being won't do
12
13
14
15
16
17
18
19
20
21
22
23
 * much good.
 *
 * Admins will be able to decide if the following fields or selected members 
 * of their groups are removed from the search form.
 *  - keywords
 *  - categories
 *  - node types
 *
 * What this module achieved at the moment can also be achieved by theming the
 * search form. Also, it does not control what can be searched. 
 *
 * @author Nesta Campbell
Nesta Campbell's avatar
Nesta Campbell committed
24
 * @todo Look at multiple hierarchy taxonomies
25
26
27
28
29
30
31
32
33
34
35
 * 
 */

/**
 * Implementation of hook_perm()
 *
 * At the moment this module does not control access to what can be searched
 * for only what fields are displayed to the user. Therefore, these permissions
 * don't really do much but allow the displaying of form fields per role.
 */
function search_config_perm() {
36
  return array('search by node type', 'search by category', 'use keyword search');
37
38
39
40
41
42
43
44
45
}

/**
 * Implementation of hook_form_alter()
 */
function search_config_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'search_form':
      if (arg(1) == 'node') {
46
        
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        // Keywords
        if (user_access('use keyword search')) {
          if (variable_get('search_config_disable_or', 0)) {
            unset($form['advanced']['keywords']['or']);
          }
          if (variable_get('search_config_disable_phrase', 0)) {
            unset($form['advanced']['keywords']['phrase']);
          }
          if (variable_get('search_config_disable_negative', 0)) {
            unset($form['advanced']['keywords']['negative']);
          }
        }
        // Remove all the keyword search fields
        else { 
          unset($form['advanced']['keywords']);
        }

        // Node types
        // We will need to rebuild the checkboxes for the node type
        $remove = variable_get('search_config_disable_type', array());
        if ($remove['all'] || !user_access('search by node type')) {
          unset($form['advanced']['type']);
        }
        else {
Nesta Campbell's avatar
Nesta Campbell committed
71
          $types = node_get_types('names');
72
73
74
75
76
77
78
79
          
          foreach ($types as $module => $type) {
            if ($remove[$module]) {
              unset($types[$module]);
            }
          }
        
          // Rebuild form item -- copied from node.module
Nesta Campbell's avatar
Nesta Campbell committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
          if (count($types) == 1) {
            $type_keys = array_keys($types);
            $form['advanced']['type'] = array(
              '#type' => 'hidden',
              '#default_value' => $type_keys[0],
            );
          } 
          else {
            $form['advanced']['type'] = array(
              '#type' => 'checkboxes',
              '#title' => t('Only of the type(s)'),
              '#prefix' => '<div class="criterion">',
              '#suffix' => '</div>',
              '#options' => $types,
            );
          }
96
97
        }
  
98
99
100
101
102
103
104
        if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
          // Taxonomy
          if (variable_get('search_config_disable_category_all', 0) || !user_access('search by category')) {
            unset($form['advanced']['category']);
          }
          else {
            $terms = variable_get('search_config_disable_category', array());
105

106
107
108
109
110
111
            // FIXME: What about multiple hierarchy categories?
            foreach ($taxonomy as $vocab => $term) {
              foreach ($term as $k => $v) {
                if(in_array($k, $terms)) {
                  unset($taxonomy[$vocab][$k]);
                }
112
113
114
              }
            }

115
116
117
118
119
120
121
122
123
124
            // Taxonomy box:
            $form['advanced']['category'] = array(
              '#type' => 'select',
              '#title' => t('Only in the category(s)'),
              '#prefix' => '<div class="criterion">',
              '#size' => 10,
              '#suffix' => '</div>',
              '#options' => $taxonomy,
              '#multiple' => TRUE,
            );
125

126
          }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
        }
      }
      break;
  }
}

/**
 * Implementation of hook_search()
 */
function search_config_search($op) {
  switch ($op) {
    case 'admin':
      $form['search_config'] = array(        
        '#type' => 'fieldset',
        '#title' => t('Advanced search form configuration'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );

      // Keyword boxes:
      $form['search_config']['keywords'] = array(
        '#type' => 'fieldset',
        '#title' => t('Keywords'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE ,
        '#description' => t('Configuration for which keyword search fields should be displayed.')
      );
      
      $form['search_config']['keywords']['search_config_disable_or'] = array( 
        '#type' => 'checkbox',
        '#title' => t('Containing any of the words'),
        '#default_value' => variable_get('search_config_disable_or', 0)
      );
      
      $form['search_config']['keywords']['search_config_disable_phrase'] = array(
        '#type' => 'checkbox',
        '#title' => t('Containing the phrase'),
        '#default_value' => variable_get('search_config_disable_phrase', 0)
      );
      
      $form['search_config']['keywords']['search_config_disable_negative'] = array(
        '#type' => 'checkbox',
        '#title' => t('Containing none of the words'),
        '#default_value' => variable_get('search_config_disable_negative', 0)
      );
      
      // Taxonomy box
      if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
        $form['search_config']['category'] = array(
          '#type' => 'fieldset',
          '#title' => t('Categories'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE ,
          '#description' => t('Categories to display')
        );


        $form['search_config']['category']['search_config_disable_category_all'] = array(
          '#type' => 'checkbox',
          '#title' => t('Disable category search'),
          '#default_value' => variable_get('search_config_disable_category_all', 0)
        );

        $form['search_config']['category']['search_config_disable_category'] = array(
          '#type' => 'select',
          '#title' => t('Categories'),
          '#options' => $taxonomy,
          '#size' => 10,
          '#multiple' => TRUE,
          '#default_value' => variable_get('search_config_disable_category', array()),
          '#description' => t('Disable searching by the selected categories')
        );
      }

      // Node types 
Nesta Campbell's avatar
Nesta Campbell committed
202
      $types = node_get_types('names');
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
      $types = array_merge(array('all' => 'Disable all'), $types);

      $form['search_config']['type'] = array(
        '#type' => 'fieldset',
        '#title' => t('Node types'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE ,
        '#description' => t('Node types that users shouldn\'t be allowed to search by.')
      );
      
      $form['search_config']['type']['search_config_disable_type'] = array(
        '#type' => 'checkboxes',
        '#options' => $types,
        '#default_value' => variable_get('search_config_disable_type', array())
      );
      
      return $form;
  }
}
?>