forked from ding2/ding_facetbrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ding_facetbrowser.module
392 lines (361 loc) · 12.1 KB
/
ding_facetbrowser.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
<?php
/**
* Implements hook_block_info().
*/
function ding_facetbrowser_block_info() {
return array(
'facetbrowser' => array(
'info' => t('Facet browser'),
'cache' => DRUPAL_NO_CACHE,
),
);
}
/**
* Implements hook_menu().
*/
function ding_facetbrowser_menu() {
$items['admin/config/ting/facets'] = array(
'title' => 'Ting facets',
'description' => 'Configure facets.',
'weight' => -20,
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_facetbrowser_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'ding_facetbrowser.admin.inc',
);
$items['admin/config/ting/facets/%/delete'] = array(
'title' => 'Delete facets',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_facetbrowser_delete_facet', 4),
'access arguments' => array('administer ting settings'),
'file' => 'ding_facetbrowser.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
*
* @return array
*/
function ding_facetbrowser_theme() {
return array(
'facetbrowser' => array(
'variables' => array(
'facets' => NULL,
'searchkey' => NULL,
),
),
'ding_facetbrowser_settings' => array(
'render element' => 'form',
'file' => 'ding_facetbrowser.admin.inc',
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types.
*/
function ding_facetbrowser_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function ding_facetbrowser_ctools_plugin_api($module, $api) {
if ($module == 'panels_mini' && $api == 'panels_default') {
return array('version' => 1);
}
if ($module == 'page_manager' && $api == 'pages_default') {
return array('version' => 1);
}
if ($module == "strongarm" && $api == "strongarm") {
return array("version" => "1");
}
}
/**
* Create a custom form with checkbox elements for facets and ajax callback
*
* @param array $form
* @param array $form_state
* @param array $facets
* @return array $form
*/
function ding_facetbrowser_elements($facets = FALSE, $search = FALSE) {
global $base_root;
$output = '';
// Building the facets fieldsets
$i = 0;
$facet_show_count = variable_get('ding_facetbrowser_showcount');
foreach (variable_get('ding_facetbrowser_facets', array()) as $configured_facet) {
$checkboxes = array();
$default_values = array();
$selected = array();
$not_selected = array();
if (isset($facets[$configured_facet['name']])) {
$facet = $facets[$configured_facet['name']];
// Decide if the facet is to have the hidden class or not
$facet_class = ($i >= $facet_show_count) ? 'hidden' : 'visible';
if (count($facet->terms) > 0) {
if (empty($configured_facet['sorting'])) {
$configured_facet['sorting'] = NULL;
}
switch ($configured_facet['sorting']) {
case 'alphabetical':
ksort($facet->terms);
break;
case 'alphabetical_reverse':
krsort($facet->terms);
break;
case 'numeric':
ksort($facet->terms, SORT_NUMERIC);
break;
case 'numeric_reverse':
krsort($facet->terms, SORT_NUMERIC);
break;
default:
break;
}
foreach ($facet->terms as $term_name => $term_count) {
$facet_name = explode('.', $facet->name);
$element_name = end($facet_name);
$facets_string = $facet->name . ':' . $term_name;
$term_name_title = $term_name;
// Check if term name is longer than 14 chars,
// if so, short it down to fit facetbrowser
$substr_length = 19
+ substr_count(drupal_substr($term_name, 0, 20), 'l')
+ drupal_strtolower(substr_count(drupal_substr($term_name, 0, 20), 'i'))
- drupal_strlen((string)$term_count);
if (drupal_strlen($term_name) > $substr_length + 3) {
$term_name_title = drupal_substr($term_name, 0, $substr_length) . '...';
}
if (_isFacetSet($facet->name, $term_name) === TRUE) {
$default_values[] = $term_name;
$facets_string = '-' . $facets_string;
$selected[$term_name] = l($term_name_title . ' <span class="count">(' . $term_count . ')</span>', $base_root . _parseRequestUri(), array(
'html' => TRUE,
'query' => array(
'facets' => array('' => $facets_string),
),
));
}
else {
$not_selected[$term_name] = l($term_name_title . ' <span class="count">(' . $term_count . ')</span>', $base_root . _parseRequestUri(), array(
'html' => TRUE,
'query' => array(
'facets' => array('' => $facets_string),
),
));
}
}
$output .= '<fieldset id="' . drupal_html_id($facet->name);
$output .= '" data-count="' . count($facet->terms);
$output .= '" class="form-wrapper">';
$output .= ' <legend><span class="fieldset-legend">' ;
$output .= t($configured_facet['title']) . '</span></legend>';
$output .= '<div class="fieldset-wrapper"> <div id="edit-';
$output .= $facet->name . '" class="form-checkboxes">';
$title = t($configured_facet['title']);
if (count($selected) > 0) {
$output .= '<div class="selected-checkbox-group">';
foreach ($selected as $key => $value) {
$output .= '<div class="form-item form-type-checkbox form-item-';
$output .= $facet->name . '-' . $key;
$output .= ' form-disabled selected-checkbox">';
$output .= '<input disabled="disabled" checked="checked "';
$output .= 'type="checkbox" id="edit-';
$output .= $facet->name . '-' . $key . '" name="' . $facet->name;
$output .= '[' . $key . ']" value="' . $key;
$output .= '" class="form-checkbox">';
$output .= '<label class="option" for="edit-' . $facet->name . '-';
$output .= $key . '">' . $value . '</label></div>';
}
$output .= '</div>';
}
if (count($not_selected) > 0) {
$output .= '<div class="unselected-checkbox-group">';
foreach ($not_selected as $key => $value) {
$output .= '<div class="form-item form-type-checkbox form-item-';
$output .= $facet->name . '-' . $key;
$output .= ' form-disabled unselected-checkbox">';
$output .= '<input disabled="disabled" type="checkbox" id="edit-';
$output .= $facet->name . '-' . $key . '" name="' . $facet->name;
$output .= '[' . $key . ']" value="' . $key;
$output .= '" class="form-checkbox">';
$output .= '<label class="option" for="edit-' . $facet->name . '-';
$output .= $key . '">' . $value . '</label></div>';
}
$output .= '</div>';
}
$output .= '</div>';
$output .= '</div>'; // Wrapper.
$output .= '</fieldset>';
}
$i++; // Loop is done so time to up that counter
}
}
return array(
'#type' => 'markup',
'#markup' => $output,
);
}
/**
* Check if selected facet from url matches the one from params.
*
* @param String $term_cat
* @param String $term_name
* @return Boolean
*/
function _isFacetSet($term_cat, $term_name) {
$facets = &drupal_static('ding_facetbrowser_facets');
if (!isset($facets)) {
$request_uri_parts = explode('?', request_uri());
$facets = explode('&', rawurldecode(end($request_uri_parts)));
drupal_static('ding_facetbrowser_facets', $facets);
}
foreach ($facets as $facet) {
if (preg_replace('/facets\[\d*\]=/i', '', $facet) == $term_cat . ':' . $term_name) {
return TRUE;
}
}
return FALSE;
}
/**
* Check the URI and drop pager parameter, if available.
*
* @return String
*/
function _parseRequestUri() {
$new_query_part = array();
if (stristr(request_uri(), '?')) {
$request_uri_parts = explode('?', request_uri());
$query = rawurldecode(end($request_uri_parts));
$query_parts = explode('&', $query);
foreach ($query_parts as $key => $part) {
if (preg_match('/^page\=/', $part)) {
unset($query_parts[$key]);
continue;
}
$new_query_part[] = $part;
}
$query = $new_query_part;
return $request_uri_parts[0] . '?' . implode('&', $query);
}
else {
return request_uri();
}
}
/**
* Parse the selected facets from the $form_state to a readable string for the ting_search
*
* @param array $form_state
* @return string
*/
function _facetString($form_state = NULL) {
$facets = array();
if (!empty($form_state['values']) && is_array($form_state['values'])) {
foreach ($form_state['values'] as $name => $data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
if ($key == $value && $value !== 0) {
$facets[] = 'facet.' . $name . ':' . $value;
}
}
}
}
}
return implode(';', $facets);
}
/**
* Facetbrowser theming function.
*
* @param array $all_facets
* @return string
*/
function theme_facetbrowser($search_result = FALSE) {
$all_facets = array_shift($search_result);
$searchkey = array_shift($search_result);
$elements = array();
if (is_array($all_facets)) {
$facet = ding_facetbrowser_elements($all_facets, $searchkey);
$elements[] = drupal_render($facet);
}
return implode('', $elements);
}
/**
* Implements hook_block_view().
*
* @return Array
*/
function ding_facetbrowser_block_view($delta = FALSE) {
$block = array();
$block_content = FALSE;
$content_in_facet = FALSE;
$executed_search_module = FALSE;
drupal_add_js(drupal_get_path('module', 'ding_facetbrowser') . '/js/jquery.cookie.min.js');
drupal_add_js(drupal_get_path('module', 'ding_facetbrowser') . '/js/facetbrowser.js');
drupal_add_css(drupal_get_path('module', 'ding_facetbrowser') . '/css/facetbrowser.css');
// TODO: Get the facetbrowser dom id automatic
drupal_add_js(
array('dingFacetBrowser' => array(
'mainElement' => '.pane-ding-facetbrowser',
'showCount' => variable_get('ding_facetbrowser_number_of_terms', 5)),
), 'setting');
// Invoke ding_facetbrowser() from executed search module
$executed_search = menu_get_item();
if (!empty($executed_search['page_arguments'])) {
list($executed_search_module, ) = $executed_search['page_arguments'];
}
if (is_string($executed_search_module)) {
$results = module_invoke($executed_search_module, 'ding_facetbrowser');
if (is_object($results)) {
foreach ($results->facets as $result) {
if (count($result->terms)) {
$content_in_facet = TRUE;
break;
}
}
if ($content_in_facet === TRUE || $results->show_empty == TRUE) {
$block_content = theme('facetbrowser', array($results->facets, (!empty($results->searchkey) ? $results->searchkey : NULL)));
}
}
switch ($delta) {
case 'facetbrowser':
$block['subject'] = t('Facet browser');
$block['content'] = $block_content;
break;
}
}
return $block;
}
/**
* Implements hook_theme_registry_alter().
*/
function ding_facetbrowser_theme_registry_alter(&$theme_registry) {
$theme_registry['panels_pane']['preprocess functions'][] = '_ding_facetbrowser_translate_pane_title';
}
/**
* Translate pane title.
*
* @param array $vars
* Pane data.
*/
function _ding_facetbrowser_translate_pane_title(&$vars) {
// Translate only overriden title of ding_facetbrowser.
if ($vars['pane']->type == 'ding_facetbrowser' && $vars['pane']->configuration['override_title']) {
$vars['title'] = t($vars['title']);
}
}
/**
* Implements hook_ting_pre_execute().
*/
function ding_facetbrowser_ting_pre_execute($request) {
if ($request instanceof TingSearchRequest) {
$request->setTermsInFacets(variable_get('ding_facetbrowser_term_count_limit', 50));
}
}