Newer
Older
Earl Miles
committed
<?php
// $Id$
/**
* Callback function to supply a list of content types.
*/
function ctools_search_result_ctools_content_types() {
if (!module_exists('search')) {
return;
}
Earl Miles
committed
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
return array(
'single' => TRUE,
'title' => t('Search results'),
'icon' => 'icon_search.png',
'description' => t('The results of a search using keywords.'),
'required context' => new ctools_context_required(t('Keywords'), 'string'),
'category' => t('Widgets'),
'defaults' => array(
'type' => 'node',
'log' => TRUE,
'override_empty' => FALSE,
'empty_title' => '',
'empty' => '',
'empty_format' => FILTER_FORMAT_DEFAULT,
'override_no_key' => FALSE,
'no_key_title' => '',
'no_key' => '',
'no_key_format' => FILTER_FORMAT_DEFAULT,
),
);
}
/**
* Render the custom content type.
*/
function ctools_search_result_content_type_render($subtype, $conf, $panel_args, $context) {
// Display nothing at all if no keywords were entered.
if (empty($context) || empty($context->data)) {
if (!empty($conf['override_no_key'])) {
$block->title = $conf['no_key_title'];
$block->content = check_markup($conf['no_key'], $conf['no_key_format'], FALSE);
return $block;
}
return;
}
$keys = $context->data;
// Build the content type block.
$block = new stdClass();
$block->module = 'search';
$block->delta = 'result';
$results = '';
// Need settings for:
// $no result override
if (!empty($conf['log'])) {
// Log the search keys:
watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($conf['type'], 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), $_GET['q']));
}
// Collect the search results:
$results = search_data($keys, $conf['type']);
if ($results) {
$block->title = t('Search results');
$block->content = $results;
}
else {
if (empty($conf['override_empty'])) {
$block->title = t('Your search yielded no results');
$block->content = search_help('search#noresults', drupal_help_arg());
}
else {
$block->title = $conf['empty_title'];
$block->content = check_markup($conf['empty'], $conf['empty_format'], FALSE);
}
}
return $block;
}
/**
* Returns an edit form for custom type settings.
*/
function ctools_search_result_content_type_edit_form(&$form, &$form_state) {
$conf = $form_state['conf'];
$types = array();
foreach (module_implements('search') as $name) {
$types[$name] = module_invoke($name, 'search', 'name', TRUE);
}
$form['type'] = array(
'#type' => 'select',
'#title' => t('Search type'),
'#options' => $types,
'#default_value' => $conf['type'],
);
$form['log'] = array(
'#type' => 'checkbox',
'#default_value' => $conf['log'],
'#title' => t('Record a watchdog log entry when searches are made'),
);
$form['override_empty'] = array(
'#type' => 'checkbox',
'#default_value' => $conf['override_empty'],
'#title' => t('Override "no result" text'),
);
$form['empty_field']['empty_title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#default_value' => $conf['empty_title'],
'#process' => array('ctools_dependent_process'),
'#dependency' => array('edit-override-empty' => array(1)),
);
$form['empty_field']['empty'] = array(
'#title' => t('No result text'),
'#type' => 'textarea',
'#default_value' => $conf['empty'],
'#process' => array('ctools_dependent_process'),
'#dependency' => array('edit-override-empty' => array(1)),
);
$form['empty_field']['format_prefix'] = array(
'#type' => 'hidden',
'#id' => 'edit-empty-format',
'#prefix' => '<div><fieldset id="edit-empty-format" class="collapsed collapsible"><legend>' . t('Input format') . '</legend>',
'#process' => array('ctools_dependent_process'),
'#dependency' => array('edit-override-empty' => array(1)),
);
// Yes, lots of gymnastics to make this fieldset work with dependencies.
$form['empty_field']['empty_format'] = filter_form($conf['empty_format'], NULL, array('empty_format'));
unset($form['empty_field']['empty_format']['#type']);
$form['empty_field']['format_suffix'] = array(
'#value' => '</fieldset></div>',
);
$form['override_no_key'] = array(
'#type' => 'checkbox',
'#default_value' => $conf['override_no_key'],
'#title' => t('Display text if no search keywords were submitted'),
);
$form['no_key_field']['no_key_title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#default_value' => $conf['no_key_title'],
'#process' => array('ctools_dependent_process'),
'#dependency' => array('edit-override-no-key' => array(1)),
);
$form['no_key_field']['no_key'] = array(
'#title' => t('No keywords text'),
'#type' => 'textarea',
'#default_value' => $conf['no_key'],
'#process' => array('ctools_dependent_process'),
'#dependency' => array('edit-override-no-key' => array(1)),
);
$form['no_key_field']['format_prefix'] = array(
'#type' => 'hidden',
'#id' => 'edit-no-key-format',
'#prefix' => '<div><fieldset id="edit-no-key-format" class="collapsed collapsible"><legend>' . t('Input format') . '</legend>',
'#process' => array('ctools_dependent_process'),
'#dependency' => array('edit-override-no-key' => array(1)),
);
// Yes, lots of gymnastics to make this fieldset work with dependencies.
$form['no_key_field']['no_key_format'] = filter_form($conf['no_key_format'], NULL, array('no_key_format'));
unset($form['no_key_field']['no_key_format']['#type']);
$form['no_key_field']['format_suffix'] = array(
'#value' => '</fieldset></div>',
);
}
/**
* Submit handler for search form.
*/
function ctools_search_result_content_type_edit_form_submit(&$form, &$form_state) {
// Copy everything from our defaults.
foreach (array_keys($form_state['plugin']['defaults']) as $key) {
$form_state['conf'][$key] = $form_state['values'][$key];
}
}
/**
* Returns the administrative title for a type.
*/
function ctools_search_result_content_type_admin_title($subtype, $conf, $context) {
$type = module_invoke($conf['type'], 'search', 'name', TRUE);
return t('@type search result', array('@type' => $type));
}