-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysearch.module
64 lines (59 loc) · 1.63 KB
/
mysearch.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
<?php
/**
* Implementation of hook_permission().
*/
function mysearch_permission() {
return array(
'access mysearch' => array(
'title' => 'Access My Search',
'description' => 'Allows a user to access search results',
)
);
}
/**
* Implementation of hook_menu().
*/
function mysearch_menu() {
$items['mysearch'] = array(
'title' => 'Search',
'page callback' => 'mysearch_searchpage',
'access arguments' => array('access mysearch'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
/**
* Menu callback provides a simple list of nodes matching the
* search term Example: hitting the URL:
* http://domain.com/mysearch/example
* will return a list of links to nodes which have the word
* example in them.
*/
function mysearch_searchpage() {
$searchterm = arg(1);
if(empty($searchterm)){
return "Please try again using keyword.";
}
$keys = array($searchterm);
$query = db_select('search_index', 'i');
$query->join('node', 'n', 'n.nid = i.sid');
$query->fields('n',array('nid', 'title'))
->condition('n.status', 1)
->condition('i.word', $searchterm)
->groupBy('n.nid')
->orderBy('i.score', 'DESC')
->addTag('node_access');
$find = $query->execute();
$items = array();
foreach ($find as $item) {
$items[] = array(
'data' => l($item->title, 'node/'.$item->nid),
);
}
if(empty($items)){
return "No results for keyword {$searchterm}";
}
$type = 'ul';
$themed_search_results = theme_item_list(array('items' => $items, 'title' => "Search results for {$searchterm}", 'type' => $type, 'attributes' => array()));
return $themed_search_results;
}