-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjarvis.module
executable file
·425 lines (359 loc) · 11.6 KB
/
jarvis.module
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/**
* @file
* Main Jarvis class
*/
/**
* Jarvis Class
*/
class Jarvis {
private $init = FALSE;
public $hotkey;
public $ajax_search;
/**
* Singleton instance
*/
private static $instance = FALSE;
/**
* Singleton
*/
public static function instance() {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Init Jarvis
*/
public function init() {
if ( $this->init ) return;
$this->init = TRUE;
// Hotkey is '/'
$this->hotkey = 191;
// Ajax search endpoint
$this->ajax_search = 'jarvis/autocomplete';
}
/**
* Localize Javascript
* @return string to be embedded with JS
*/
public function localize() {
$jarvis = array(
'hotkey' => $this->hotkey,
'ajax_search' => url($this->ajax_search),
'menu_items' => $this->menu_items(),
);
return 'var jarvis = ' . json_encode($jarvis) . ';'; //, JSON_PRETTY_PRINT
}
/**
* Perform a search
* Echos search results to prevent the Drupal templates from being output
*/
public function search() {
$q = isset($_REQUEST['q']) ? trim($_REQUEST['q']) : '';
$response = array(
'results' => array()
);
if ( strlen($q) ) {
// Split words on whitespace
$q_words = preg_split('/\s+/', $q, NULL, PREG_SPLIT_NO_EMPTY);
$results = array_merge($this->node_search($q, $q_words), $this->term_search($q, $q_words));
usort($results, function ($a, $b) {
if ($a['jarvis_sort'] == $b['jarvis_sort']) {
if (strlen($a['title']) == strlen($b['title'])) return 0;
return strlen($a['title']) < strlen($b['title']) ? -1 : 1;
}
return $a['jarvis_sort'] > $b['jarvis_sort'] ? -1 : 1;
});
$response['results'] = $results;
// DEBUGGING
$response['q'] = $q;
$response['q_words'] = $q_words;
}
echo json_encode($response);
}
protected function node_search($q, $q_words) {
// Sort by relevance of search phrase related to title.
$relevance = "0 \n";
foreach ($q_words as $i => $q_word) {
// Keyword matches
$relevance .= "+ if(n.title REGEXP :q{$i}_full, 1, 0) \n";
$relevance .= "+ if(n.title REGEXP :q{$i}_start, 1, 0) \n";
}
$relevance .= " as 'jarvis_sort' \n";
// Search for matching titles or nid
$where = "";
// Only need to search nid if a single word
if (count($q_words) === 1) {
$where .= "n.nid = :q OR \n";
}
$where .= "(";
foreach ($q_words as $i => $q_word) {
$where .= "n.title REGEXP :q{$i}_contain AND ";
}
$where .= "1 = 1) \n";
// Query parameters
$params = array();
if (count($q_words) === 1) {
$params[':q'] = $q;
}
foreach ($q_words as $i => $q_word) {
$q_word_escaped = preg_quote($q_word);
$params[':q' . $i . '_full'] = '[[:<:]]' . $q_word_escaped . '[[:>:]]';
$params[':q' . $i . '_start'] = '[[:<:]]' . $q_word_escaped;
$params[':q' . $i . '_contain'] = $q_word_escaped;
}
$query = "SELECT DISTINCT \n" .
" n.nid AS 'id', \n" .
" n.type AS 'type', \n" .
" n.title AS 'title', \n" .
$relevance .
"FROM \n" .
" {node} n \n" .
"WHERE \n" .
$where .
"ORDER BY \n" .
" jarvis_sort DESC, \n" . // Jarvis match best to worst
" LENGTH(LOWER(n.title)), \n" . // Title length shortest to longest
" n.changed DESC \n" . // Updated most recently to oldest
"LIMIT 20";
// @see https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_query/7
$result = db_query($query, $params);
$results = $result->fetchAll();
$response = array();
foreach ($results as $item) {
$id = $item->id;
$item = (array) $item;
$item['edit_url'] = url('node/' . $id . '/edit');
$response[] = $item;
}
return $response;
}
protected function term_search($q, $q_words) {
// Sort by relevance of search phrase related to title.
$relevance = "0 \n";
foreach ($q_words as $i => $q_word) {
// Keyword matches
$relevance .= "+ if(t.name REGEXP :q{$i}_full, 1, 0) \n";
$relevance .= "+ if(t.name REGEXP :q{$i}_start, 1, 0) \n";
}
$relevance .= " as 'jarvis_sort' \n";
// Search for matching term names
$where = "(";
foreach ($q_words as $i => $q_word) {
$where .= "t.name REGEXP :q{$i}_contain AND ";
}
$where .= "1 = 1) \n";
// Query parameters
$params = array();
foreach ($q_words as $i => $q_word) {
$q_word_escaped = preg_quote($q_word);
$params[':q' . $i . '_full'] = '[[:<:]]' . $q_word_escaped . '[[:>:]]';
$params[':q' . $i . '_start'] = '[[:<:]]' . $q_word_escaped;
$params[':q' . $i . '_contain'] = $q_word_escaped;
}
$query = "SELECT DISTINCT \n" .
" t.tid AS 'id', \n" .
" v.name AS 'type', \n" .
" t.name AS 'title', \n" .
$relevance .
"FROM \n" .
" {taxonomy_term_data} t \n" .
" LEFT JOIN {taxonomy_vocabulary} v ON t.vid = v.vid \n" .
"WHERE \n" .
$where .
"ORDER BY \n" .
" jarvis_sort DESC, \n" . // Jarvis match best to worst
" LENGTH(LOWER(t.name)) \n" . // Title length shortest to longest
"LIMIT 20";
// @see https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_query/7
$result = db_query($query, $params);
$results = $result->fetchAll();
$response = array();
foreach ($results as $item) {
$item->type .= ' Term';
$id = $item->id;
$item = (array) $item;
$item['edit_url'] = url('taxonomy/term/' . $id . '/edit');
$response[] = $item;
}
return $response;
}
/**
* Parse through menus to get menu_items
* @return array
*/
public function menu_items() {
$menu_items = array();
$menus = array('management', 'user-menu');
$data = array();
$tree = array();
// Allows modules to add additional menus
drupal_alter('jarvis_menus', $menus);
// Get all menu items
foreach ($menus as $menu) {
$data = menu_tree_all_data($menu);
foreach ($data as $tree) {
$menu_items = array_merge(
$menu_items,
$this->_menu_items(array($tree))
);
}
}
// Merge together with additional menu items
$menu_items = array_merge($menu_items, $this->_additional_menu_items());
// Allow modules to alter final menu items
drupal_alter('jarvis_menu_items', $menu_items);
return $menu_items;
}
/**
* Internal function to parse through menu trees
* @param array $tree
* @return array
*/
public function _menu_items($tree, $parent = NULL) {
static $id = 0;
// Prevent runaway menus
if ( $id++ > 1000 ) return array();
$menu_items = array();
foreach ($tree as $menu_item) {
// Skip inaccessible, invisible links, or links that point their parent.
if (!$menu_item['link']['access']
|| ($menu_item['link']['type'] & MENU_LINKS_TO_PARENT)
|| $menu_item['link']['hidden'] == 1) {
continue;
}
$menu_item_parent = $parent;
if (!empty($menu_item['link']['link_path'])) {
$menu_item_parent .= $menu_item['link']['link_title'] . ' » ';
$menu_items[] = array(
'type' => 'Menu Item',
'title' => $parent . $menu_item['link']['link_title'],
'edit_url' => url($menu_item['link']['link_path'])
);
}
if (!empty($menu_item['below'])) {
$menu_items = array_merge(
$menu_items,
$this->_menu_items($menu_item['below'], $menu_item_parent)
);
}
}
return $menu_items;
}
public function _additional_menu_items() {
$menu_items = array();
// Content Types
if (user_access('administer content types')) {
foreach (node_type_get_types() as $node_type => $node) {
$menu_items[] = array(
'type' => 'Content Type',
'title' => 'Structure » Content Types » ' . $node->name,
'edit_url' => url('admin/structure/types/manage/' . $node_type)
);
}
}
foreach (node_type_get_types() as $node_type => $node) {
if (user_access('bypass node access') || user_access('create ' . $node_type . ' content')) {
$node_type = str_replace('_', '-', $node_type);
$menu_items[] = array(
'type' => 'Content',
'title' => 'Content » Add Content » ' . $node->name,
'edit_url' => url('node/add/' . $node_type)
);
}
}
// Taxonomies
if (user_access('administer taxonomy')) {
foreach (taxonomy_vocabulary_get_names() as $name => $taxonomy) {
$menu_items[] = array(
'type' => 'Taxonomy',
'title' => 'Structure » Taxonomy » ' . $taxonomy->name,
'edit_url' => url('admin/structure/taxonomy/' . $name)
);
}
}
// Nodequeues
if (module_exists('nodequeue') && user_access('administer nodequeue')) {
foreach (nodequeue_load_subqueues(nodequeue_get_all_qids()) as $subqueue) {
$menu_items[] = array(
'type' => 'Nodequeue',
'title' => 'Structure » Nodequeues » ' . $subqueue->title,
'edit_url' => url('admin/structure/nodequeue/' . $subqueue->qid . '/view/' . $subqueue->sqid)
);
}
}
// Views
if (module_exists('views') && user_access('administer views')) {
foreach (views_get_enabled_views() as $name => $view) {
$menu_items[] = array(
'type' => 'View',
'title' => 'Structure » Views » ' . ( $view->human_name ? $view->human_name : $name ),
'edit_url' => url('admin/structure/views/view/' . $name)
);
}
}
// Context
if (module_exists('context') && user_access('administer contexts')) {
foreach (context_enabled_contexts() as $name => $context) {
$menu_items[] = array(
'type' => 'Context',
'title' => 'Structure » Context » ' . ($context->tag ? $context->tag . ' » ' : '') . $name,
'edit_url' => url('admin/structure/context/list/' . $name . '/edit')
);
}
}
return $menu_items;
}
}
/**
* Init hook
* hook_page_build()
*/
function jarvis_page_build() {
if (!user_access('use jarvis'))
return;
Jarvis::instance()->init();
// Load Jarvis javascript
drupal_add_js(drupal_get_path('module', 'jarvis') . '/js/jquery-ui-1.10.2.custom.js');
drupal_add_js(Jarvis::instance()->localize(), 'inline');
drupal_add_js(drupal_get_path('module', 'jarvis') . '/js/jarvis.js');
// Load Jarvis CSS
drupal_add_css(drupal_get_path('module', 'jarvis') . '/css/jquery-ui-1.10.2.custom.css');
drupal_add_css(drupal_get_path('module', 'jarvis') . '/css/jarvis.css');
}
/**
* Perm hook
* hook_permission()
*/
function jarvis_permission() {
$items = array();
$items['use jarvis'] = array(
'title' => 'Use Jarvis Search',
'description' => 'Enable Jarvis menu and content search with the "/" hotkey.',
'restrict access' => TRUE,
);
return $items;
}
/**
* Menu hook
* hook_menu()
*/
function jarvis_menu() {
Jarvis::instance()->init();
$items = array();
// Jarvis node search
$items[Jarvis::instance()->ajax_search] = array(
'page callback' => '_jarvis_search',
'access arguments' => array('use jarvis'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Internal Jarvis Search
*/
function _jarvis_search() {
return Jarvis::instance()->search();
}