You are here

function apachesolr_search_page_settings_form in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 apachesolr_search.admin.inc \apachesolr_search_page_settings_form()
  2. 6.3 apachesolr_search.admin.inc \apachesolr_search_page_settings_form()

Menu callback/form-builder for the form to create or edit a search page.

1 string reference to 'apachesolr_search_page_settings_form'
apachesolr_search_menu in ./apachesolr_search.module
Implements hook_menu().

File

./apachesolr_search.admin.inc, line 184
Administrative settings for searching.

Code

function apachesolr_search_page_settings_form($form, &$form_state, $search_page = NULL) {
  $environments = apachesolr_load_all_environments();
  $options = array(
    '' => t('<Disabled>'),
  );
  if (variable_get('apachesolr_default_environment', false)) {
    $options[-1] = t('<Default>');
  }
  foreach ($environments as $id => $environment) {
    $options[$id] = $environment['name'];
  }

  // Validate the env_id.
  if (!empty($search_page['env_id']) && $search_page['env_id'] != -1 && !apachesolr_environment_load($search_page['env_id'])) {
    $search_page['env_id'] = '';
  }

  // Initializes form with common settings.
  $form['search_page'] = array(
    '#type' => 'value',
    '#value' => $search_page,
  );
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#required' => TRUE,
    '#size' => 30,
    '#maxlength' => 32,
    '#default_value' => !empty($search_page['label']) ? $search_page['label'] : '',
    '#description' => t('The human-readable name of the search page configuration.'),
  );
  $form['page_id'] = array(
    '#type' => 'machine_name',
    '#maxlength' => 32,
    '#required' => TRUE,
    '#machine_name' => array(
      'exists' => 'apachesolr_search_page_exists',
      'source' => array(
        'label',
      ),
    ),
    '#default_value' => !empty($search_page['page_id']) ? $search_page['page_id'] : '',
    '#disabled' => !empty($search_page),
    '#description' => t('A unique machine-readable identifier for the search page configuration. It must only contain lowercase letters, numbers, and underscores.'),
  );
  $form['description_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Description'),
    '#default_value' => !empty($search_page['description']) ? TRUE : FALSE,
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Provide description'),
    '#title_display' => 'invisible',
    '#size' => 64,
    '#default_value' => !empty($search_page['description']) ? $search_page['description'] : '',
    '#dependency' => array(
      'edit-description-enable' => array(
        1,
      ),
    ),
  );
  $is_default = FALSE;
  if (!empty($search_page)) {
    $is_default = $search_page['page_id'] == apachesolr_search_default_search_page();
  }
  $form['make_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Make this Solr Search Page the default'),
    '#description' => t('Useful for eg. making facets to link to this page when they are shown on non-search pages'),
    '#default_value' => $is_default,
    '#disabled' => $is_default,
  );
  $form['info'] = array(
    '#title' => t('Search Page Information'),
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#prefix' => '<div id="dynamic-search-page">',
    '#suffix' => '</div>',
  );
  $core_search = FALSE;
  if (!empty($search_page['page_id']) && $search_page['page_id'] == 'core_search') {
    $core_search = TRUE;
  }
  if ($core_search) {
    $description = t('This page always uses the current default search environment');
  }
  else {
    $description = t('The environment that is used by this search page. If no environment is selected, this page will be disabled.');
  }
  $form['info']['env_id'] = array(
    '#title' => t('Search environment'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => !empty($search_page['env_id']) ? $search_page['env_id'] : '',
    '#disabled' => $core_search,
    '#description' => $description,
  );
  $form['info']['page_title'] = array(
    '#title' => t('Title'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#maxlength' => 255,
    '#description' => t('You can use %value to put the value of % in the path into the title and %terms to put the search terms into the title. Please note if you have several active search modules, this refers to the title of the tab instead of that of the page.'),
    '#default_value' => !empty($search_page['page_title']) ? $search_page['page_title'] : '',
  );
  $search_types = apachesolr_search_load_all_search_types();
  $options = array();
  foreach ($search_types as $id => $search_type) {
    $options[$id] = $search_type['name'];
  }
  $form['info']['search_type'] = array(
    '#title' => t('Search Type'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => !empty($search_page['settings']['apachesolr_search_search_type']) ? $search_page['settings']['apachesolr_search_search_type'] : '',
    '#access' => !$core_search,
    '#description' => t('Use this only when filtering on a value from the search path.
      For example, select Taxonomy Term to filter on a term ID (search/taxonomy/%).'),
    '#ajax' => array(
      'callback' => 'apachesolr_search_ajax_search_page_default',
      'wrapper' => 'dynamic-search-page',
      'method' => 'replace',
    ),
  );

  // Token element validate is added to validate the specific
  // tokens that are allowed
  $form['info']['search_path'] = array(
    '#title' => t('Path'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#maxlength' => 255,
    '#description' => t('For example: search/my-search-page. Search keywords will appear at the end of the path.'),
    '#default_value' => !empty($search_page['search_path']) ? $search_page['search_path'] : '',
  );
  if (!$core_search) {
    $form['info']['search_path']['#description'] .= ' ' . t('You can use one % to make the search page dynamic.');
  }
  $form['info']['custom_filter_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Custom Filter'),
    '#default_value' => !empty($search_page['settings']['apachesolr_search_custom_enable']) ? TRUE : FALSE,
  );
  if (!$core_search) {
    $additional_help_text = t('E.g. "bundle:blog, is_uid:(1 OR 2 OR %). % will be replaced by the value of % in the path"');
  }
  else {
    $additional_help_text = t('E.g. "bundle:blog, is_uid:(1 OR 2)"');
  }
  $t_args = array(
    '!additional_help_text' => $additional_help_text,
  );
  $form['info']['filters'] = array(
    '#title' => t('Custom filters'),
    '#type' => 'textfield',
    '#required' => FALSE,
    '#maxlength' => 255,
    '#description' => t('A comma-separated list of lucene filter queries to apply by default.') . ' ' . $additional_help_text,
    '#default_value' => !empty($search_page['settings']['fq']) ? implode(', ', $search_page['settings']['fq']) : '',
    '#dependency' => array(
      'edit-custom-filter-enable' => array(
        1,
      ),
      'edit-search-type' => array(
        'custom',
      ),
    ),
  );
  $form['advanced'] = array(
    '#title' => t('Advanced Search Page Options'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );

  // Results per page per search page
  $default_value = isset($search_page['settings']['apachesolr_search_per_page']) ? $search_page['settings']['apachesolr_search_per_page'] : '10';
  $form['advanced']['apachesolr_search_per_page'] = array(
    '#type' => 'textfield',
    '#size' => 3,
    '#title' => t('Results per page'),
    '#description' => t('How many items will be displayed on one page of the search result.'),
    '#default_value' => $default_value,
  );

  // Enable/disable spellcheck on pages
  $default_value = isset($search_page['settings']['apachesolr_search_spellcheck']) ? $search_page['settings']['apachesolr_search_spellcheck'] : TRUE;
  $form['advanced']['apachesolr_search_spellcheck'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable spell check'),
    '#description' => t('Display "Did you mean … ?" above search results.'),
    '#default_value' => $default_value,
  );

  // Enable/disable search form on search page (replaced by a block perhaps)
  $default_value = isset($search_page['settings']['apachesolr_search_search_box']) ? $search_page['settings']['apachesolr_search_search_box'] : TRUE;
  $form['advanced']['apachesolr_search_search_box'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable the search box on the page'),
    '#description' => t('Display a search box on the page.'),
    '#default_value' => $default_value,
  );

  // Enable/disable search form on search page (replaced by a block perhaps)
  $default_value = isset($search_page['settings']['apachesolr_search_allow_user_input']) ? $search_page['settings']['apachesolr_search_allow_user_input'] : FALSE;
  $form['advanced']['apachesolr_search_allow_user_input'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow user input using the URL'),
    '#description' => t('Allow users to use the URL for manual facetting via fq[] params (e.g. http://example.com/search/site/test?fq[]=uid:1&fq[]=tid:99). This will only work in combination with a keyword search. The recommended value is unchecked'),
    '#default_value' => $default_value,
  );

  // Use the main search page setting as the default for new pages.
  $default_value = isset($search_page['settings']['apachesolr_search_browse']) ? $search_page['settings']['apachesolr_search_browse'] : 'browse';
  $form['advanced']['apachesolr_search_browse'] = _apachesolr_search_browse_form($default_value);

  // Button for the corresponding actions
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#redirect' => 'admin/config/search/apachesolr/search-pages',
    '#value' => t('Save'),
  );
  $form['actions']['submit_edit'] = array(
    '#type' => 'submit',
    '#value' => t('Save and edit'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => 'admin/config/search/apachesolr/search-pages',
  );
  $form['#submit'][] = 'apachesolr_search_page_settings_form_submit';
  return $form;
}