Commit f6d9b0ad authored by Nesta Campbell's avatar Nesta Campbell
Browse files

Syncing HEAD with the latest 5 realease (I think).

parent 0eb7ceb1
......@@ -9,3 +9,36 @@ Installation
2. Enable the search_config module on the admin modules page
3. Go to administer » settings » search, you should now see a
"Advanced search form configuration" collapsed fieldset.
General Information
-------------------
This module started out as a simple means of configuring the *display* of the
advance search form. A few features have been added since thanks to the
contributions of others.
In simple use cases the search_config module allows site admins to decide which
fields to display on the advanced search form when enabled. This does not stop
astute users from entering the search criteria directly into the search text
fields. There is also the option of selecting which node types not to index.
Once selected they are also automatically removed from the advance search
form. This gives more control over the content that can be searched to those
who need it.
Search config now has the option of choosing which search implementation
should be the default, for example, "content", "user", "apachesolr", etc. The
regular "content" search provided by the node module is selected by default.
Three new permissions are also provided, these simple control the display of
fields per role and is useful if fields should not be removed globally from
the form.
The following fields or selected members of their groups can removed from the
form globally or on role by role basis:
- keywords
- categories
- node types
Some of what this module does can also be achieved by theming the form.
; $Id$
name = Search config
description = Configure the advanced search form
description = Configure display of the advanced search form
dependencies = search
......@@ -3,24 +3,9 @@
/**
* @file
* Allows admins to configure the advanced search form.
* Used to configure the advanced search form and other search behaviour.
*
* In general the search_config module allows users to decide which fields
* to show on the advanced search form. This does not stop astute users from
* placing the search criteria directly in the search text fields. The module
* also provides four (3) new permissions but for the time being won't do much
* good.
*
* Admins will be able to decide if the following fields or selected members
* of their groups are removed from the search form.
* - keywords
* - categories
* - node types
*
* What this module achieves at the moment can also be achieved by theming the
* search form. Also, it does not control what can be searched.
*
* @author Nesta Campbell
* @author Nesta Campbell <nesta.campbell@gmail.com>
* @todo Look at multiple hierarchy taxonomies
*
*/
......@@ -28,9 +13,7 @@
/**
* Implementation of hook_perm()
*
* At the moment this module does not control access to what can be searched,
* only what fields are displayed to the user. Therefore, these permissions
* don't really do much but allow the displaying of form fields per role.
* Allow the displaying of form fields per role.
*/
function search_config_perm() {
return array('search by node type', 'search by category', 'use keyword search');
......@@ -40,78 +23,109 @@ function search_config_perm() {
* Implementation of hook_form_alter()
*/
function search_config_form_alter($form_id, &$form) {
switch ($form_id) {
case 'search_form':
if (arg(1) == 'node') {
// Present the user with the appropriate search results tab as defined by
// variable "search_config_default_search" (labeled "Default Search")
// in the "Advanced search settings" fieldset in admin/settings/search
if ($form_id == 'search_theme_form') {
$form['#submit'] = array();
$form['#submit']['search_form_submit'] = array();
$form['module']['#value'] = variable_get('search_config_default_search', 'node');
$form['module']['#type'] = 'hidden';
$form['processed_keys'] = $form['search_theme_form_keys'];
$form['processed_keys']['#weight'] = -1;
unset($form['search_theme_form_keys']);
}
if ($form_id == 'search_form') {
if (arg(1) == 'node') {
// Keywords
if (user_access('use keyword search')) {
if (variable_get('search_config_disable_or', 0)) {
unset($form['advanced']['keywords']['or']);
}
if (variable_get('search_config_disable_phrase', 0)) {
unset($form['advanced']['keywords']['phrase']);
}
if (variable_get('search_config_disable_negative', 0)) {
unset($form['advanced']['keywords']['negative']);
}
}
// Remove all the keyword search fields
else {
unset($form['advanced']['keywords']);
}
// Node types
// We will need to rebuild the checkboxes for the node types
$remove = variable_get('search_config_disable_type', array());
if ($remove['all'] || !user_access('search by node type')) {
unset($form['advanced']['type']);
}
else {
$types = node_get_types('names');
// Keywords
if (user_access('use keyword search')) {
if (variable_get('search_config_disable_or', 0)) {
unset($form['advanced']['keywords']['or']);
}
if (variable_get('search_config_disable_phrase', 0)) {
unset($form['advanced']['keywords']['phrase']);
}
if (variable_get('search_config_disable_negative', 0)) {
unset($form['advanced']['keywords']['negative']);
foreach ($types as $module => $type) {
if ($remove[$module]) {
unset($types[$module]);
}
}
// Remove all the keyword search fields
else {
unset($form['advanced']['keywords']);
// Rebuild form item -- copied from node.module
if (count($types) == 1) {
$type_keys = array_keys($types);
$form['advanced']['type'] = array(
'#type' => 'hidden',
'#default_value' => $type_keys[0],
);
}
else {
$form['advanced']['type'] = array(
'#type' => 'checkboxes',
'#title' => t('Only of the type(s)'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $types,
);
}
}
// Node types
// We will need to rebuild the checkboxes for the node types
$remove = variable_get('search_config_disable_type', array());
if ($remove['all'] || !user_access('search by node type')) {
unset($form['advanced']['type']);
if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
// Taxonomy
if (variable_get('search_config_disable_category_all', 0) || !user_access('search by category')) {
unset($form['advanced']['category']);
}
else {
$types = node_get_types('names');
foreach ($types as $module => $type) {
if ($remove[$module]) {
unset($types[$module]);
}
$terms = variable_get('search_config_disable_category', array());
if (module_exists('og_vocab')) {
$groupnode = og_get_group_context();
$sql = "SELECT t.tid FROM {vocabulary} v, {term_data} t LEFT JOIN {og_vocab} ov ON v.vid = ov.vid WHERE (v.module = 'og_vocab' AND ov.nid != %d) AND t.vid=v.vid";
$result = db_query($sql, $groupnode->nid);
while ($row = db_fetch_object($result)) {
$terms[$row->tid] = $row->tid;
}
}
// Rebuild form item -- copied from node.module
if (count($types) == 1) {
$type_keys = array_keys($types);
$form['advanced']['type'] = array(
'#type' => 'hidden',
'#default_value' => $type_keys[0],
);
}
else {
$form['advanced']['type'] = array(
'#type' => 'checkboxes',
'#title' => t('Only of the type(s)'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $types,
);
// FIXME: What about multiple hierarchy categories?
foreach ($taxonomy as $vocab => $term) {
foreach ($term as $k => $v) {
if(in_array($k, $terms)) {
unset($taxonomy[$vocab][$k]);
}
}
// Remove empty vocabs
if (count($taxonomy[$vocab]) == 0) {
unset($taxonomy[$vocab]);
}
}
}
if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
// Taxonomy
if (variable_get('search_config_disable_category_all', 0) || !user_access('search by category')) {
if (count($taxonomy) == 0) {
unset($form['advanced']['category']);
}
else {
$terms = variable_get('search_config_disable_category', array());
// FIXME: What about multiple hierarchy categories?
foreach ($taxonomy as $vocab => $term) {
foreach ($term as $k => $v) {
if(in_array($k, $terms)) {
unset($taxonomy[$vocab][$k]);
}
}
}
// Taxonomy box:
$form['advanced']['category'] = array(
'#type' => 'select',
......@@ -122,11 +136,10 @@ function search_config_form_alter($form_id, &$form) {
'#options' => $taxonomy,
'#multiple' => TRUE,
);
}
}
}
break;
}
}
}
......@@ -138,18 +151,33 @@ function search_config_search($op) {
case 'admin':
$form['search_config'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced search form configuration'),
'#title' => t('Advanced search configuration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// Search results default tab
$description = t('Which tab on the search results will be presented by default. The standard Drupal installation defaults to node_search ("Content" tab). This option allows you to default to the "User" tab or any other tab from contributed search modules, e.g. Apachesolr which implements a tab called "Search" to display its results');
$searches = drupal_map_assoc(module_implements('search', false, false));
unset ($searches['search_config']);
$form['search_config']['search_config_default_search'] = array(
'#type' => 'radios',
'#title' => t('Default Search'),
'#options' => $searches,
'#multiple' => FALSE,
'#default_value' => variable_get('search_config_default_search', 'node'),
'#description' => t($description)
);
// Keyword boxes:
$form['search_config']['keywords'] = array(
'#type' => 'fieldset',
'#title' => t('Keywords'),
'#collapsible' => TRUE,
'#collapsed' => TRUE ,
'#description' => t('Configuration for which keyword search fields should be displayed.')
'#description' => t('Configuration for which keyword search fields should not be displayed.')
);
$form['search_config']['keywords']['search_config_disable_or'] = array(
......@@ -180,7 +208,6 @@ function search_config_search($op) {
'#description' => t('Categories to display')
);
$form['search_config']['category']['search_config_disable_category_all'] = array(
'#type' => 'checkbox',
'#title' => t('Disable category search'),
......@@ -207,16 +234,97 @@ function search_config_search($op) {
'#title' => t('Node types'),
'#collapsible' => TRUE,
'#collapsed' => TRUE ,
'#description' => t('Node types that users shouldn\'t be allowed to search by.')
'#validate' => array('search_config_validate' => array())
);
$form['search_config']['type']['form'] = array(
'#type' => 'fieldset',
'#title' => t('Search Form'),
'#collapsible' => FALSE,
'#description' => t('Node types that users shouldn\'t be allowed to search by using the the advanced search form.')
);
$form['search_config']['type']['search_config_disable_type'] = array(
'#type' => 'checkboxes',
'#options' => $types,
'#default_value' => variable_get('search_config_disable_type', array())
);
$form['search_config']['type']['index'] = array(
'#type' => 'fieldset',
'#title' => t('Search Index'),
'#collapsible' => FALSE,
'#description' => t('Node types that should not be index by the search module. Any node type set to not be indexed will also be removed from the search form. If you select all available node types to not be index then the search module will be rendered unusable as no nodes will be indexed.')
);
// If all node types are disabled then the search module is useless.
unset($types['all']);
reset($types);
$form['search_config']['type']['index']['search_config_disable_index_type'] = array(
'#type' => 'checkboxes',
'#options' => $types,
'#default_value' => variable_get('search_config_disable_index_type', array())
);
return $form;
}
}
?>
function search_config_validate($form_values) {
if ($form_values['#post']['form_id'] == 'search_admin_settings' && $form_values['#post']['op'] == 'Save configuration') {
$post_values = $form_values['#post'];
$node_types = node_get_types('names');
if (!isset($post_values['search_config_disable_index_type'])) {
return;
}
$index_types = $post_values['search_config_disable_index_type'];
if (!isset($post_values['search_config_disable_type'])) {
$post_values['search_config_disable_type'] = array();
}
$form_types = $post_values['search_config_disable_type'];
if (count($index_types) != 0 && count($index_types) == count($node_types)) {
form_set_error('search_config_disable_index_type', t('You can not set all node types to be not indexed by the search module. Disable the search module if that is what you want.'));
}
if (isset($post_values['search_config_disable_type']['all'])) {
return;
}
$type_diff = array_diff($index_types, $form_types);
if (count($type_diff)) {
$node_errors = array();
foreach ($type_diff as $type) {
$node_errors[] = $node_types[$type];
}
form_set_error('search_config_disable_type', t('Search index node types do not match form node types. Please check the setting for %nodes.', array('%nodes' => implode(', ', $node_errors))));
}
}
}
/**
* Implementation of hook_update_index()
*/
function search_config_update_index() {
// This hook is only called when the search module is enabled
// The function_exists check can be removed, but there is no harm in leaving it in
if (function_exists('search_wipe')) {
$types = node_get_types('names');
$remove = variable_get('search_config_disable_index_type', array());
foreach ($remove as $type => $value) {
if (isset($types[$type]) && $value === $type) {
$result = db_query("SELECT n.nid FROM {node} n INNER JOIN {search_dataset} s ON n.nid=s.sid WHERE n.type = '%s'", $type);
while ($data = db_fetch_object($result)) {
search_wipe($data->nid, 'node');
}
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment