1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
*/
|
6
|
|
7
|
if (module_exists('search')) {
|
8
|
/**
|
9
|
* Plugins are described by creating a $plugin array which will be used
|
10
|
* by the system that includes this file.
|
11
|
*/
|
12
|
$plugin = array(
|
13
|
'single' => TRUE,
|
14
|
'title' => t('Search results'),
|
15
|
'icon' => 'icon_search.png',
|
16
|
'description' => t('The results of a search using keywords.'),
|
17
|
'required context' => new ctools_context_required(t('Keywords'), 'string'),
|
18
|
'category' => t('Widgets'),
|
19
|
'defaults' => array(
|
20
|
'type' => 'node',
|
21
|
'log' => TRUE,
|
22
|
'override_empty' => FALSE,
|
23
|
'empty_title' => '',
|
24
|
'empty' => '',
|
25
|
'empty_format' => filter_fallback_format(),
|
26
|
'override_no_key' => FALSE,
|
27
|
'no_key_title' => '',
|
28
|
'no_key' => '',
|
29
|
'no_key_format' => filter_fallback_format(),
|
30
|
),
|
31
|
);
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
* Render the custom content type.
|
36
|
*/
|
37
|
function ctools_search_result_content_type_render($subtype, $conf, $panel_args, $context) {
|
38
|
$search_info = search_get_info();
|
39
|
if (empty($search_info[$conf['type']])) {
|
40
|
return;
|
41
|
}
|
42
|
$info = $search_info[$conf['type']];
|
43
|
|
44
|
$keys = NULL;
|
45
|
if (!empty($context) && isset($context->data)) {
|
46
|
$keys = $context->data;
|
47
|
}
|
48
|
|
49
|
$conditions = NULL;
|
50
|
if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
|
51
|
// Build an optional array of more search conditions.
|
52
|
$conditions = $info['conditions_callback']($keys);
|
53
|
}
|
54
|
|
55
|
// Display nothing at all if no keywords were entered.
|
56
|
if (empty($keys) && empty($conditions)) {
|
57
|
if (!empty($conf['override_no_key'])) {
|
58
|
$block->title = $conf['no_key_title'];
|
59
|
$block->content = check_markup($conf['no_key'], $conf['no_key_format'], FALSE);
|
60
|
return $block;
|
61
|
}
|
62
|
return;
|
63
|
}
|
64
|
|
65
|
// Build the content type block.
|
66
|
$block = new stdClass();
|
67
|
$block->module = 'search';
|
68
|
$block->delta = 'result';
|
69
|
|
70
|
$results = '';
|
71
|
|
72
|
// Only search if there are keywords or non-empty conditions.
|
73
|
if ($keys || !empty($conditions)) {
|
74
|
|
75
|
// Collect the search results.
|
76
|
$results = search_data($keys, $info['module'], $conditions);
|
77
|
|
78
|
// A workaround for ApacheSolr.
|
79
|
// @todo see http://drupal.org/node/1343142#comment-5495248
|
80
|
// This workaround is to be removed when a better one can be written.
|
81
|
if (!empty($results['search_results']['#results'])) {
|
82
|
$results['#results'] = $results['search_results']['#results'];
|
83
|
}
|
84
|
}
|
85
|
|
86
|
if (!empty($conf['log'])) {
|
87
|
// Log the search keys:
|
88
|
watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), $_GET['q']));
|
89
|
}
|
90
|
|
91
|
if (!empty($results['#results'])) {
|
92
|
$output = "<ol class=\"search-results $conf[type]-results\">\n";
|
93
|
foreach ($results['#results'] as $result) {
|
94
|
$output .= theme('search_result', array('result' => $result, 'module' => $conf['type']));
|
95
|
}
|
96
|
$output .= "</ol>\n";
|
97
|
$output .= theme('pager', array('tags' => NULL));
|
98
|
|
99
|
$block->title = t('Search results');
|
100
|
$block->content = $output;
|
101
|
}
|
102
|
else {
|
103
|
if (empty($conf['override_empty'])) {
|
104
|
$block->title = t('Your search yielded no results');
|
105
|
$block->content = search_help('search#noresults', drupal_help_arg());
|
106
|
}
|
107
|
else {
|
108
|
$block->title = $conf['empty_title'];
|
109
|
$block->content = check_markup($conf['empty'], $conf['empty_format'], FALSE);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
return $block;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Returns an edit form for custom type settings.
|
118
|
*/
|
119
|
function ctools_search_result_content_type_edit_form($form, &$form_state) {
|
120
|
$conf = $form_state['conf'];
|
121
|
|
122
|
$types = array();
|
123
|
foreach (search_get_info() as $module => $info) {
|
124
|
$types[$module] = $info['title'];
|
125
|
}
|
126
|
|
127
|
$form['type'] = array(
|
128
|
'#type' => 'select',
|
129
|
'#title' => t('Search type'),
|
130
|
'#options' => $types,
|
131
|
'#default_value' => $conf['type'],
|
132
|
);
|
133
|
|
134
|
$form['log'] = array(
|
135
|
'#type' => 'checkbox',
|
136
|
'#default_value' => $conf['log'],
|
137
|
'#title' => t('Record a watchdog log entry when searches are made'),
|
138
|
);
|
139
|
|
140
|
$form['override_empty'] = array(
|
141
|
'#type' => 'checkbox',
|
142
|
'#default_value' => $conf['override_empty'],
|
143
|
'#title' => t('Override "no result" text'),
|
144
|
);
|
145
|
|
146
|
$form['empty_title'] = array(
|
147
|
'#title' => t('Title'),
|
148
|
'#type' => 'textfield',
|
149
|
'#default_value' => $conf['empty_title'],
|
150
|
'#dependency' => array('edit-override-empty' => array(1)),
|
151
|
);
|
152
|
|
153
|
$form['empty_field'] = array(
|
154
|
'#type' => 'text_format',
|
155
|
'#title' => t('No result text'),
|
156
|
'#default_value' => $conf['empty'],
|
157
|
'#format' => $conf['empty_format'],
|
158
|
'#dependency' => array('edit-override-empty' => array(1)),
|
159
|
);
|
160
|
|
161
|
$form['override_no_key'] = array(
|
162
|
'#type' => 'checkbox',
|
163
|
'#default_value' => $conf['override_no_key'],
|
164
|
'#title' => t('Display text if no search keywords were submitted'),
|
165
|
);
|
166
|
|
167
|
$form['no_key_title'] = array(
|
168
|
'#title' => t('Title'),
|
169
|
'#type' => 'textfield',
|
170
|
'#default_value' => $conf['no_key_title'],
|
171
|
'#dependency' => array('edit-override-no-key' => array(1)),
|
172
|
);
|
173
|
|
174
|
$form['no_key_field'] = array(
|
175
|
'#type' => 'text_format',
|
176
|
'#title' => t('No result text'),
|
177
|
'#default_value' => $conf['no_key'],
|
178
|
'#format' => $conf['no_key_format'],
|
179
|
'#dependency' => array('edit-override-no-key' => array(1)),
|
180
|
);
|
181
|
|
182
|
return $form;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Submit handler for search form.
|
187
|
*/
|
188
|
function ctools_search_result_content_type_edit_form_submit($form, &$form_state) {
|
189
|
// Copy the text_format values over to where we normally store them.
|
190
|
$form_state['values']['empty'] = $form_state['values']['empty_field']['value'];
|
191
|
$form_state['values']['empty_format'] = $form_state['values']['empty_field']['format'];
|
192
|
$form_state['values']['no_key'] = $form_state['values']['no_key_field']['value'];
|
193
|
$form_state['values']['no_key_format'] = $form_state['values']['no_key_field']['format'];
|
194
|
// Copy everything from our defaults.
|
195
|
foreach (array_keys($form_state['plugin']['defaults']) as $key) {
|
196
|
$form_state['conf'][$key] = $form_state['values'][$key];
|
197
|
}
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Returns the administrative title for a type.
|
202
|
*/
|
203
|
function ctools_search_result_content_type_admin_title($subtype, $conf, $context) {
|
204
|
$info = search_get_info();
|
205
|
$type = isset($info[$conf['type']]['title']) ? $info[$conf['type']]['title'] : t('Missing/broken type');
|
206
|
return t('@type search result', array('@type' => $type));
|
207
|
}
|