-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysearch.module
182 lines (158 loc) · 4.36 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
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
<?php
/**
* @file
* Provides search and results render functionality.
*/
/**
* Implements hook_theme().
*/
function mysearch_theme() {
$templates_path = drupal_get_path('module', 'mysearch') . '/templates';
$items = array();
$items['mysearch_nothing_found'] = array(
'arguments' => array('keywords' => NULL),
'path' => $templates_path,
'template' => 'mysearch-nothing-found',
);
$items['mysearch_results_list'] = array(
'arguments' => array('keywords' => NULL, 'results' => array()),
'path' => $templates_path,
'template' => 'mysearch-results-list',
);
return $items;
}
/**
* Implements hook_permission().
*/
function mysearch_permission() {
return array(
'access mysearch' => array(
'title' => t('Access My Search'),
'description' => t('Allows a user to access search results'),
)
);
}
/**
* Implements hook_menu().
*/
function mysearch_menu() {
$items = array();
$items['mysearch/%'] = array(
'title' => 'Search',
'title callback' => 'mysearch_search_result_page_title',
'title arguments' => array(1),
'page callback' => 'mysearch_search_results_page',
'page arguments' => array(1),
'access arguments' => array('access mysearch'),
);
$items['admin/config/search/mysearch'] = array(
'title' => 'My Search',
'description' => 'Configure MySearch settings.',
'file' => 'mysearch.admin.inc',
'page callback' => 'drupal_get_form',
'page arguments' => array('mysearch_admin_settings'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Generates page title according to search string.
*
* @param $query
*
* @return string
*/
function mysearch_search_result_page_title($query) {
return t('Search: @query', array('@query' => $query));
}
/**
* 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.
*
* @param string $keywords
*
* @return string
*/
function mysearch_search_results_page($keywords) {
$keywords = trim($keywords);
// Handle search string.
if (empty($keywords)) {
return theme('mysearch_nothing_found', array('keywords' => $keywords));
}
$nodes = mysearch_execute_search($keywords);
// Nothing found by the query.
if (!$nodes) {
return theme('mysearch_nothing_found', array('keywords' => $keywords));
}
// Generate list of links.
$links = array();
foreach ($nodes as $node) {
$links[] = array(
'title' => $node->title,
'href' => 'node/' . $node->nid
);
}
$results = theme('links', array('links' => $links));
// Output simple list of links.
$output = theme('mysearch_results_list',
array(
'keywords' => $keywords,
'results' => $results
)
);
return $output;
}
/**
* Executes search by keywords into nodes.
*
* @param $keywords
*
* @return bool|array
*/
function mysearch_execute_search($keywords) {
$query = new EntityFieldQuery();
// Find all active nodes and sort them by title.
$query->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->addTag('search_conditions')
->addMetaData('keywords', $keywords)
->propertyOrderBy('title', 'ASC');
// If we selected some content types in the settings
// then search inside them.
$content_types = variable_get('mysearch_content_types', array());
if (!empty($content_types)) {
$types = array();
foreach ($content_types as $type) {
if ($type != 0) {
$types[] = $type;
}
}
if (!empty($types)) {
$query->propertyCondition('type', $content_types, 'IN');
}
}
$entities = $query->execute();
if (empty($entities)) {
return FALSE;
}
// Return array of nodes.
$nodes = node_load_multiple(array_keys($entities['node']));
return $nodes;
}
/**
* Implements hook_query_TAG_alter() for search_conditions.
*
* @param \QueryAlterableInterface $query
*/
function mysearch_query_search_conditions_alter(QueryAlterableInterface $query) {
$keywords = '%' . $query->getMetaData('keywords') . '%';
// Add condtition to search by body and title.
$query->leftJoin('field_data_body', 'b', 'node.nid = b.entity_id');
$or = db_or()
->condition('b.body_value', $keywords, 'LIKE')
->condition('node.title', $keywords, 'LIKE');
$query->condition($or);
}