summaryrefslogtreecommitdiffstats
path: root/search_autocomplete.install
blob: e1e69cbf0f4d8759bc63511e0a29deb9f101ed9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php

/**
 * @file
 * This file is used to install/update/delete the module tables in database
 *
 * @authors
 * Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>
 *
 * Sponsored by:
 * www.axiomcafe.fr
 */

// -----------------------------------------------------------------------------------------------
/**
 * Implementation of hook_schema().
 * Set the schema of database
 * @return the schema for of the table to create
 */
function search_autocomplete_schema() {
  // schema for search_autocomplete database
  $schema['search_autocomplete_forms'] = array(
    'description' => t('Store the forms to autocomplete using Search Autocomplete.'),
    'fields' => array(
      'fid'       => array(
        'type'        => 'serial',
        'unsigned'    => TRUE,
        'not null'    => TRUE,
      ),
      'title'     => array(
        'description' => 'Human readable name for the form',
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'selector'  => array(
        'description' => 'Reference id selector of the the form in drupal',
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'weight'    => array(
        'description' => 'Form weight in table',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'enabled'   => array(
        'description' => 'Define if autocomplete is activated or not',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'parent_fid' => array(
        'description' => 'Define if the from follows the configuration of another one',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'min_char'  => array(
        'description' => 'Minimum of character before triggering suggestions',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 3,
      ),
      'max_sug'   => array(
        'description' => 'Maximum number of suggestions',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 15,
      ),
    ),
    'primary key' => array('fid'),
  );
  
  // schema for search_autocomplete database
  $schema['search_autocomplete_suggestions'] = array(
    'description' => t('Store the suggestions for this form.'),
    'fields' => array(
      'sid'   => array(
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'sug_fid' => array(
        'description' => 'Form fid the Suggestion belongs to',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'sug_enabled' => array(
        'description' => 'Define if suggestion is activated or not',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'sug_prefix' => array(
        'description' => 'Human readable prefix in suggestion',
        'type'        => 'varchar',
        'length'      => 15,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'sug_title' => array(
        'description' => 'Human readable title for the suggestion',
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'sug_name' => array(
        'description' => 'Reference name of the the suggestion in drupal',
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'sug_dependencies' => array(
        'description' => 'Name of the module (if such) which the suggestion depends on',
        'type'        => 'varchar',
        'length'      => 255,
        'not null'    => TRUE,
        'default'     => '',
      ),
      'sug_weight' => array(
        'description' => 'Suggestion weight in table',
        'type'        => 'int',
        'not null'    => TRUE,
        'default'     => 0,
      ),
      'sug_query' => array(
        'description' => 'The database query for this suggestion',
        'type'        => 'varchar',
        'length'      => 512,
        'not null'    => TRUE,
        'default'     => '',
      ),
    ),
  );
      
      
  return $schema;
} // function search_autocomplete_schema

//-----------------------------------------------------------------------------------------------
/**
 * Implementation of hook_install().
 */
function search_autocomplete_install() {
  
  $limit      = variable_get('search_autocomplete_limit', 15);
  $trigger    = variable_get('search_autocomplete_trigger', 3);
  $enabled    = 1;

  // ----------------
  // declare insertion statement
  $insert = db_insert('search_autocomplete_forms')
         ->fields(array('title', 'selector', 'weight', 'enabled', 'min_char', 'max_sug'));     
  $insert->values(array(
    'title' => st('Search page - Node Tab') . "  (search/node/%)",
    'selector'  => '#search-form[action="/search/node"]',
    'weight'    => 0,
    'enabled'   => $enabled,
    'min_char'  => $trigger,
    'max_sug'   => $limit
  ));
  $insert->values(array(
    'title'     => st('Search page - User Tab') . "  (search/user/%)",
    'selector'  => '#search-form[action="/search/user"]',
    'weight'    => 1,
    'enabled'   => $enabled,
    'min_char'  => $trigger,
    'max_sug'   => $limit
  ));
  $insert->values(array(
    'title'     => st('Search Block'),
    'selector'  => "#search-block-form",
    'weight'    => 0,
    'enabled'   => $enabled,
    'min_char'  => $trigger,
    'max_sug'   => $limit
  ));
  $insert->execute();
  
  // declare insertion statement
  $insert = db_insert('search_autocomplete_suggestions')
             ->fields(array(
                'sid',
                'sug_fid', 
                'sug_enabled', 
                'sug_prefix', 
                'sug_title', 
                'sug_name', 
                'sug_dependencies', 
                'sug_weight', 
                'sug_query')
                );     
  // for earch possible default form
  for ($form = 0; $form <= 3; $form++) {
    // values for user title default form
    $insert->values(array(
      'sid'               => 1,
      'sug_fid'           => $form,
      'sug_enabled'       => 1,
      'sug_prefix'        => "node:",
      'sug_title'         => "Add node titles",
      'sug_name'          => "node_title",
      'sug_dependencies'  => "",
      'sug_weight'        => 1,
      'sug_query'         => "SELECT n.title FROM {node} n WHERE n.status = 1 AND LOWER(n.title) LIKE LOWER('%%%s%%')"
    )); 
    // values for username default form
    $insert->values(array(
      'sid'               => 2,
      'sug_fid'           => $form,
      'sug_enabled'       => 1,
      'sug_prefix'        => "user:",
      'sug_title'         => "Add usernames",
      'sug_name'          => "username",
      'sug_dependencies'  => "",
      'sug_weight'        => 2,
      'sug_query'         => "SELECT u.name FROM {users} u WHERE u.status = 1 AND LOWER(u.name) LIKE LOWER('%%%s%%')"
    ));
    // values for taxonomies default form
    $insert->values(array(
      'sid'               => 3,
      'sug_fid'           => $form,
      'sug_enabled'       => 1,
      'sug_prefix'        => "taxo:",
      'sug_title'         => "Add taxonomies",
      'sug_name'          => "taxo_title",
      'sug_dependencies'  => "taxonomy",
      'sug_weight'        => 3,
      'sug_query'         => "SELECT t.name FROM {taxonomy_term_data} t WHERE LOWER(t.name) LIKE LOWER('%%%s%%')"
    )); 
    // values for comment title default form
    $insert->values(array(
      'sid'               => 4,
      'sug_fid'           => $form,
      'sug_enabled'       => 1,
      'sug_prefix'        => "comment:",
      'sug_title'         => "Add comment titles",
      'sug_name'          => "comment_title",
      'sug_dependencies'  => "comment",
      'sug_weight'        => 4,
      'sug_query'         => "SELECT c.subject FROM {comment} c WHERE c.subject LIKE LOWER('%%%s%%')"
    ));
    // values for comment title default form
    $insert->values(array(
      'sid'               => 5,
      'sug_fid'           => $form,
      'sug_enabled'       => 1,
      'sug_prefix'        => "",
      'sug_title'         => "Add all possible words (any word appearing on your website)",
      'sug_name'          => "word_title",
      'sug_dependencies'  => "search",
      'sug_weight'        => 5,
      'sug_query'         => "SELECT DISTINCT s.word FROM {search_index} s, {node} n WHERE s.type = 'node' AND n.nid = s.sid AND n.status = 1 AND LOWER(s.word) LIKE LOWER('%%%s%%')"
    ));
    $insert->execute();
  }

  drupal_set_message(st('Search Autocomplete is now correctly installed!'));

} // function search_autocomplete_install

// -----------------------------------------------------------------------------------------------
/**
 * Implementation of hook_update_N().
 * Get ready from version 1.x to 2.x
 */
function search_autocomplete_update_6200() {
  // if tables does'not already exists: run install
  $ret = array();
  $ok_result = TRUE;  // so far so good
  
  $results = drupal_install_schema('search_autocomplete');        //Install the database specified in  'function search_autocomplete_schema'
  foreach ($results as $result) {                                 // Check eventual errors that could have occured
    if (!$result->success)
      drupal_set_message(st('An error has occured during table creation, please retry. If the problem persist please post an issue and report the code: #err_code:400 @query', $result['query']), 'error');
  }
  
  if (db_table_exists('search_autocomplete_forms') && db_table_exists('search_autocomplete_suggestions')) {
    search_autocomplete_install();
  }

  variable_del('search_autocomplete_forms');
  variable_del('search_autocomplete_test');
  variable_del('search_autocomplete_limit');
  variable_del('search_autocomplete_trigger');
  variable_del('search_autocomplete_method');

  // Rebuild system table contents.
  system_rebuild_module_data();
  system_rebuild_theme_data();

  return $ret;
}