/[drupal]/contributions/modules/faceted_search/faceted_search.admin.inc
ViewVC logotype

Contents of /contributions/modules/faceted_search/faceted_search.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Mon May 4 23:08:03 2009 UTC (6 months, 3 weeks ago) by davidlesieur
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +198 -3 lines
File MIME type: text/x-php
#436586 by David Lesieur and EmTeedee: Allow environment to be saved when no facet can be enabled. Also moved some functions to .admin.inc file.
1 <?php
2 // $Id: faceted_search.admin.inc,v 1.5 2009/03/11 21:31:19 davidlesieur Exp $
3
4 /**
5 * @file
6 * Administrative page callbacks for the Faceted Search module.
7 */
8
9 /**
10 * List all environments and provide links to operations on those.
11 */
12 function faceted_search_list_page() {
13 $items = array();
14 foreach (faceted_search_get_env_ids() as $env_id) {
15 $env = faceted_search_env_load($env_id);
16 $links = array(
17 array('title' => t('Edit'), 'href' => 'admin/settings/faceted_search/'. $env_id),
18 array('title' => t('Delete'), 'href' => 'admin/settings/faceted_search/delete/'. $env_id),
19 );
20 // Check that the base_path is a valid setting, because it is a
21 // faceted_search_ui thing that faceted_search, as the parent class,
22 // should normally not know about.
23 $display = isset($env->settings['base_path']) ? l($env->name, $env->settings['base_path']) : $name;
24 $items[$env->name] = array($display, filter_xss_admin($env->description), theme('links', $links));
25 }
26 if (count($items)) {
27 ksort($items); // Sort items by name.
28 $output = '<p>'. t('This page shows all of the faceted search environments that are currently defined.') .'</p>';
29 $output .= theme('table', array(t('Environment'), t('Description'), t('Operations')), $items);
30 }
31 else {
32 $output = '<p>'. t('No faceted search environments have currently been defined.') .'</p>';
33 }
34 return $output;
35 }
36
37 /**
38 * Delete an environment.
39 */
40 function faceted_search_delete_form($form_state, $env) {
41 $form['env'] = array(
42 '#type' => 'value',
43 '#value' => $env,
44 );
45 $form = confirm_form(
46 $form,
47 t('Are you sure you want to delete %env?', array('%env' => $env->name)),
48 'admin/settings/faceted_search',
49 t('This action cannot be undone.'),
50 t('Delete'),
51 t('Cancel')
52 );
53 return $form;
54 }
55
56 /**
57 * Handle the submit button to delete an environment.
58 */
59 function faceted_search_delete_form_submit($form, &$form_state) {
60 $name = $form_state['values']['env']->name;
61 $env_id = $form_state['values']['env']->env_id;
62
63 db_query('DELETE FROM {faceted_search_env} WHERE env_id = %d', $env_id);
64 db_query('DELETE FROM {faceted_search_filters} WHERE env_id = %d', $env_id);
65
66 _block_rehash();
67 menu_rebuild();
68
69 // Delete the localized strings of this environment.
70 faceted_search_env_locale_delete($env_id);
71
72 drupal_set_message(t('Faceted search environment %name deleted.', array('%name' => $name)));
73
74 $form_state['redirect'] = 'admin/settings/faceted_search';
75 }
76
77 /**
78 * Form for editing an environment.
79 *
80 * @param $env
81 * The environment to edit, or NULL if adding a new one.
82 */
83 function faceted_search_edit_form($form_state, $env = NULL) {
84 if (isset($env)) {
85 drupal_set_title(t('Faceted search environment: @name', array('@name' => $env->name)));
86 }
87 else {
88 drupal_set_title(t('Add a faceted search environment'));
89 $env = new faceted_search();
90 }
91 $form['env'] = array(
92 '#type' => 'value',
93 '#value' => $env,
94 );
95
96 // Basic information section.
97 $form['info'] = array(
98 '#type' => 'fieldset',
99 '#title' => t('Basic information'),
100 '#collapsible' => TRUE,
101 '#collapsed' => FALSE,
102 );
103 $form['info']['name'] = array(
104 '#type' => 'textfield',
105 '#title' => t('Name'),
106 '#maxlength' => 32,
107 '#size' => 32,
108 '#required' => TRUE,
109 '#default_value' => $env->name,
110 '#description' => t('A unique name to help site administrators identify the environment. Only alphanumeric and the underscore character are allowed here.'),
111 );
112 $form['info']['title'] = array(
113 '#type' => 'textfield',
114 '#title' => t('Title'),
115 '#maxlength' => 128,
116 '#size' => 32,
117 '#required' => TRUE,
118 '#default_value' => $env->settings['title'],
119 '#description' => t("The title shown to users on the environment's pages."),
120 );
121 $form['info']['description'] = array(
122 '#type' => 'textfield',
123 '#title' => t('Description'),
124 '#maxlength' => 255,
125 '#size' => 60,
126 '#default_value' => $env->description,
127 '#description' => t('The description of the environment for site administrators.'),
128 );
129 $types = array();
130 foreach (array_keys(node_get_types('names')) as $type) {
131 $types[$type] = $type;
132 }
133 $names = node_get_types('names');
134 faceted_search_localize_types($names);
135 $form['info']['types'] = array(
136 '#title' => t('Content types'),
137 '#type' => 'checkboxes',
138 '#options' => $names,
139 '#description' => t('Only the checked types will appear in this search environment. If none is checked, all types will be allowed.'),
140 '#default_value' => array_intersect($types, array_filter($env->settings['types'])),
141 );
142 $form['info']['ignore_status'] = array(
143 '#type' => 'checkbox',
144 '#title' => t('Let unpublished nodes appear in search results'),
145 '#default_value' => $env->settings['ignore_status'],
146 '#description' => t("When this is enabled, unpublished nodes are allowed to appear in search results to users with the 'administer nodes' permission. Note that unpublished nodes are never indexed by Drupal core, so they will never appear in keyword search results although they <em>will</em> appear in guided search results."),
147 '#weight' => 1,
148 );
149
150 // Facets section.
151 $form['facets'] = array(
152 '#type' => 'fieldset',
153 '#title' => t('Facets'),
154 '#collapsible' => TRUE,
155 '#collapsed' => FALSE,
156 '#description' => t('<p>Facets provide categories for users to browse and refine their search.</p><p>Check the facets you wish to expose to users. In listings, the heavier facets will sink and the lighter facets will be positioned nearer the top.</p><p>The Sort criteria applies to categories listings only, and the Categories limit applies to category listings in the Guided search only. For performance reasons, it is advisable to select some limit (users will still be able to get the full listing of categories by following a <em>more</em> link).</p>'),
157 );
158 $form['facets']['facets'] = array(
159 '#theme' => 'faceted_search_facets_settings',
160 '#tree' => TRUE,
161 );
162
163 // Gather every possible facet.
164 $all_filter_settings = faceted_search_load_filter_settings($env, TRUE);
165 $facets = array();
166 foreach (module_implements('faceted_search_collect') as $module) {
167 $hook = $module .'_faceted_search_collect';
168 $hook($facets, 'facets', $env, NULL);
169 }
170
171 // Prepare facets for use, assigning them their settings and sorting them.
172 faceted_search_prepare_filters($facets, $all_filter_settings);
173
174 // Add the facets section's content to the form.
175 _faceted_search_facet_settings_form($form['facets']['facets'], $facets);
176
177 // Keyword search section.
178 $form['keyword'] = array(
179 '#type' => 'fieldset',
180 '#title' => t('Keyword search'),
181 '#collapsible' => TRUE,
182 '#collapsed' => TRUE,
183 );
184 $form['keyword']['fields'] = array(
185 '#type' => 'fieldset',
186 '#title' => t('Fields'),
187 '#collapsible' => TRUE,
188 '#collapsed' => FALSE,
189 '#description' => t('<p>Users may restrict keyword search by field. <em>Anywhere</em> lets them search for keywords in the full nodes.</p><p>Check the fields you wish to expose to users for keyword search. In listings, the heavier fields will sink and the lighter fields will be positioned nearer the top.</p>'),
190 );
191 $form['keyword']['fields']['keyword_filters'] = array(
192 '#theme' => 'faceted_search_keyword_filters_settings',
193 '#tree' => TRUE,
194 );
195
196 // Gather keyword filters.
197 $keyword_filters = array();
198 foreach (module_implements('faceted_search_collect') as $module) {
199 $hook = $module .'_faceted_search_collect';
200 $hook($keyword_filters, 'keyword filters', $env, NULL);
201 }
202 // Gather the node keyword filter. This is the default, always-enabled keyword
203 // filter that allows searching in the full node index.
204 faceted_search_collect_node_keyword_filters($keyword_filters, 'keyword filters', $env);
205
206 // Prepare facets for use, assigning them their settings and sorting them.
207 faceted_search_prepare_filters($keyword_filters, $all_filter_settings);
208
209 // Add the facets section's content to the form.
210 _faceted_search_filter_settings_form($form['keyword']['fields']['keyword_filters'], $keyword_filters);
211
212 // Buttons.
213 $form['buttons']['#weight'] = 1000; // Ensure buttons remain at the bottom when form is altered.
214 $form['buttons']['save'] = array(
215 '#type' => 'submit',
216 '#value' => t('Save'),
217 );
218 $form['buttons']['save_and_edit'] = array(
219 '#type' => 'submit',
220 '#value' => t('Save and edit'),
221 );
222
223 return $form;
224 }
225
226 /**
227 * Validation callback for the environment edit form.
228 */
229 function faceted_search_edit_form_validate($form, &$form_state) {
230 // Name must be alphanumeric or underscores, no other punctuation.
231 if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
232 form_set_error('name', t('Name must be alphanumeric or underscores only.'));
233 }
234
235 // Name must be unique.
236 if (db_result(db_query("SELECT COUNT(*) FROM {faceted_search_env} WHERE name = '%s' AND env_id <> %d", $form_state['values']['name'], isset($form_state['values']['env']->env_id) ? $form_state['values']['env']->env_id : 0)) > 0) {
237 form_set_error('name', t('Name %name already in use.', array('%name' => $form_state['values']['name'])));
238 }
239
240 // Weights must be numeric.
241 if ($form_state['values']['facets']) {
242 foreach ($form_state['values']['facets'] as $key => $settings) {
243 if (!is_numeric($settings['weight'])) {
244 form_set_error("facets][$key][weight", t('Weight must be a numeric value.'));
245 }
246 }
247 }
248 foreach ($form_state['values']['keyword_filters'] as $key => $settings) {
249 if (!is_numeric($settings['weight'])) {
250 form_set_error("keyword_filters][$key][weight", t('Weight must be a numeric value.'));
251 }
252 }
253 }
254
255 /**
256 * Form callback for saving environment data.
257 */
258 function faceted_search_edit_form_submit($form, &$form_state) {
259 $env = $form_state['values']['env'];
260
261 // Save search environment.
262 $update = $env->env_id ? 'env_id' : NULL;
263 $env->name = $form_state['values']['name'];
264 $env->description = $form_state['values']['description'];
265
266 // Settings are only saved if they have a value in the original environment
267 // (we can rely on this since any setting always has at least a default
268 // value).
269 foreach ($form_state['values'] as $key => $value) {
270 if (isset($env->settings[$key])) {
271 $env->settings[$key] = $value;
272 }
273 }
274
275 // Save to database, obtaining a new env_id if not updating an existing
276 // environment.
277 drupal_write_record('faceted_search_env', $env, $update);
278
279 // Save filter settings.
280 if ($form_state['values']['facets'] || $form_state['values']['keyword_filters']) {
281 faceted_search_save_filter_settings(
282 $env->env_id,
283 array_merge(
284 is_null($form_state['values']['facets']) ? array() : $form_state['values']['facets'],
285 is_null($form_state['values']['keyword_filters']) ? array() : $form_state['values']['keyword_filters']));
286 }
287
288 // Rebuild the menus, if only for updating titles.
289 menu_rebuild();
290
291 // Refresh localizable strings.
292 faceted_search_env_locale_refresh($env);
293
294 drupal_set_message(t('The faceted search environment %name has been saved.', array('%name' => $form_state['values']['name'])));
295
296 if ($form_state['values']['op'] == t('Save')) {
297 $form_state['redirect'] = 'admin/settings/faceted_search';
298 }
299 else {
300 $form_state['redirect'] = 'admin/settings/faceted_search/'. $env->env_id;
301 }
302 }
303
304 function theme_faceted_search_facets_settings($form) {
305 uasort($form, '_faceted_search_element_sort');
306
307 $output = '';
308 $header = array('', t('Facet'), t('Type'), t('Weight'), t('Sort criteria'), t('Categories limit'));
309 $rows_enabled = array(array('', '<em>'. t('Enabled facets') .'</em>', '', '', '', ''));
310 $rows_disabled = array(array('', '<em>'. t('Disabled facets') .'</em>', '', '', '', ''));
311 foreach (element_children($form) as $key) {
312 unset($form[$key]['status']['#title']);
313 unset($form[$key]['weight']['#title']);
314 unset($form[$key]['max_categories']['#title']);
315 unset($form[$key]['sort']['#title']);
316 $row = array(
317 drupal_render($form[$key]['status']),
318 $form[$key]['#title'] . ($form[$key]['help']['#value'] ? ' ('. check_plain($form[$key]['help']['#value']) .')' : ''),
319 drupal_render($form[$key]['type']),
320 drupal_render($form[$key]['weight']),
321 drupal_render($form[$key]['sort']),
322 drupal_render($form[$key]['max_categories']),
323 );
324 if ($form[$key]['status']['#value']) {
325 $rows_enabled[] = $row;
326 }
327 else {
328 $rows_disabled[] = $row;
329 }
330 }
331
332 if (count($rows_enabled) > 1 && count($rows_disabled) > 1) {
333 $rows = array_merge($rows_enabled, $rows_disabled);
334 }
335 elseif (count($rows_enabled) > 1) {
336 $rows = $rows_enabled;
337 }
338 elseif (count($rows_disabled) > 1) {
339 $rows = $rows_disabled;
340 }
341 else {
342 $rows = array(array('', '<em>'. t('No facets available.') .'</em>', '', '', '', ''));
343 }
344 $output .= theme('table', $header, $rows);
345 $output .= drupal_render($form);
346 return $output;
347 }
348
349 function theme_faceted_search_keyword_filters_settings($form) {
350 uasort($form, '_faceted_search_element_sort');
351
352 $output = '';
353 $header = array('', t('Field'), t('Weight'));
354 $rows_enabled = array(array('', '<em>'. t('Enabled fields') .'</em>', ''));
355 $rows_disabled = array(array('', '<em>'. t('Disabled fields') .'</em>', ''));
356 foreach (element_children($form) as $key) {
357 unset($form[$key]['status']['#title']);
358 unset($form[$key]['weight']['#title']);
359 unset($form[$key]['type']);
360 $row = array(
361 drupal_render($form[$key]['status']),
362 $form[$key]['#title'] . ($form[$key]['help']['#value'] ? ' ('. check_plain($form[$key]['help']['#value']) .')' : ''),
363 drupal_render($form[$key]['weight']),
364 );
365 if ($form[$key]['status']['#value']) {
366 $rows_enabled[] = $row;
367 }
368 else {
369 $rows_disabled[] = $row;
370 }
371 }
372
373 if (count($rows_enabled) > 1 && count($rows_disabled) > 1) {
374 $rows = array_merge($rows_enabled, $rows_disabled);
375 }
376 elseif (count($rows_enabled) > 1) {
377 $rows = $rows_enabled;
378 }
379 elseif (count($rows_disabled) > 1) {
380 $rows = $rows_disabled;
381 }
382 else {
383 $rows = array(array('', '<em>'. t('No keyword filters available.') .'</em>', '', '', '', ''));
384 }
385 $output .= theme('table', $header, $rows);
386 $output .= drupal_render($form);
387 return $output;
388 }
389
390 /**
391 * Build a form for a filter's settings.
392 *
393 * @param $form
394 * The form to modify.
395 * @param $filters
396 * The filters whose settings are to be added.
397 */
398 function _faceted_search_filter_settings_form(&$form, $filters) {
399 foreach ($filters as $filter) {
400 $key = $filter->get_key() .'_'. $filter->get_id();
401 $form[$key] = array(
402 '#title' => $filter->get_label(),
403 '#weight' => $filter->get_weight(),
404 );
405 $form[$key]['filter_key'] = array(
406 '#type' => 'value',
407 '#value' => $filter->get_key(),
408 );
409 $form[$key]['filter_id'] = array(
410 '#type' => 'value',
411 '#value' => $filter->get_id(),
412 );
413 $form[$key]['help'] = array(
414 '#type' => 'value',
415 '#value' => $filter->get_help(),
416 );
417 $form[$key]['status'] = array(
418 '#title' => t('Enabled'),
419 '#type' => 'checkbox',
420 '#default_value' => $filter->get_status(),
421 );
422 if ($filter->get_key() == 'node') {
423 $form[$key]['status']['#value'] = TRUE;
424 $form[$key]['status']['#disabled'] = TRUE;
425 }
426 $form[$key]['type'] = array(
427 '#type' => 'markup',
428 '#value' => check_plain($filter->get_key()),
429 );
430 $form[$key]['weight'] = array(
431 '#title' => t('Weight'),
432 '#type' => 'textfield',
433 '#default_value' => $filter->get_weight(),
434 '#maxlength' => 4,
435 '#size' => 4,
436 '#required' => TRUE,
437 );
438 }
439 return $form;
440 }
441
442 /**
443 * Build a form for a facet's settings.
444 *
445 * @param $form
446 * The form to modify.
447 * @param $filters
448 * The filters whose settings are to be added.
449 */
450 function _faceted_search_facet_settings_form(&$form, $filters) {
451 $form = _faceted_search_filter_settings_form($form, $filters);
452 foreach ($filters as $filter) {
453 $key = $filter->get_key() .'_'. $filter->get_id();
454
455 // Sort criteria.
456 $sort_options = $filter->get_sort_options();
457 if (count($sort_options)) {
458 $form[$key]['sort'] = array(
459 '#title' => t('Sort criteria for categories'),
460 '#type' => 'select',
461 '#default_value' => $filter->get_sort(),
462 '#options' => $sort_options,
463 );
464 }
465 else {
466 $form[$key]['sort'] = array(
467 '#type' => 'markup',
468 '#value' => t('n/a'),
469 );
470 }
471
472 // Number of categories to show.
473 $form[$key]['max_categories'] = array(
474 '#title' => t('Number of categories to show in guided search'),
475 '#type' => 'select',
476 '#options' => array(
477 0 => t('All categories'),
478 5 => t('Up to 5 categories'),
479 10 => t('Up to 10 categories'),
480 15 => t('Up to 15 categories'),
481 20 => t('Up to 20 categories'),
482 25 => t('Up to 25 categories'),
483 30 => t('Up to 30 categories'),
484 40 => t('Up to 40 categories'),
485 50 => t('Up to 50 categories'),
486 100 => t('Up to 100 categories'),
487 ),
488 '#default_value' => $filter->get_max_categories(),
489 );
490 }
491
492 return $form;
493 }
494

  ViewVC Help
Powered by ViewVC 1.1.2