You are here

fac.admin.inc in Fast Autocomplete 7

This file contains the settings forms of the Fast Autocomplete module.

File

inc/fac.admin.inc
View source
<?php

/**
 * @file
 * This file contains the settings forms of the Fast Autocomplete module.
 */

/**
 * Fast AutoComplete settings form.
 */
function fac_settings_form($form, $form_state) {
  $form = array();
  $form['backend'] = array(
    '#type' => 'fieldset',
    '#title' => t('Backend'),
  );

  // Get all available backend service options.
  $service_options = array();
  $service_infos = fac_get_service_info();
  foreach ($service_infos as $service_info) {
    $service_options[$service_info['class']] = $service_info['name'];
  }
  $form['backend']['fac_backend_service'] = array(
    '#type' => 'select',
    '#title' => t('The backend service to use'),
    '#description' => t('Select the backend service that is used by Fast Autocomplete. Note that if a service is dependent on another module (for example: the Search API backend service needs the Search API module to be enabled) it only shows up if that module is enabled.'),
    '#options' => $service_options,
    '#default_value' => variable_get('fac_backend_service', ''),
    '#required' => TRUE,
  );
  $form['behavior'] = array(
    '#type' => 'fieldset',
    '#title' => t('Behavior'),
  );
  $form['behavior']['fac_input_selectors'] = array(
    '#type' => 'textarea',
    '#title' => t('Inputs to enable Fast Autocomplete on'),
    '#description' => t('Enter the jQuery selector(s) for text input elements to enable the Fast Autocomplete functionality on those elements. You can provide multiple selectors divided by commas.'),
    '#default_value' => variable_get('fac_input_selectors', ''),
    '#attributes' => array(
      'placeholder' => t('for example: input[name="search_block_form"], input[name="some_other_form"]'),
    ),
  );
  $form['behavior']['fac_empty_result'] = array(
    '#type' => 'textarea',
    '#title' => t('Text to show when the search input gets focus and there is no search term in the input.'),
    '#description' => t('Enter the HTML to show when the search input gets focus and there is no search term in the input. Useful for "quick links" for instance.'),
    '#default_value' => variable_get('fac_empty_result', ''),
  );
  $form['behavior']['fac_all_results_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show a "view all results" link'),
    '#description' => t('Enable this option to show a "view all results" link below the suggestions,'),
    '#default_value' => variable_get('fac_all_results_link', TRUE),
  );
  $form['behavior']['fac_all_results_link_threshold'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum number of suggestions to show "view all results" link'),
    '#description' => t('Enter the minimum number of suggestions to have to show the "view all results" link. Enter "0" to always show the "view all results" link.'),
    '#default_value' => variable_get('fac_all_results_link_threshold', "0"),
    '#size' => 3,
    '#states' => array(
      'visible' => array(
        'input[name="fac_all_results_link"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="fac_all_results_link"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Get all available view modes and create the view modes option array.
  $entities_info = entity_get_info();
  $view_modes = array();
  foreach ($entities_info as $entity_info) {
    $view_modes = array_merge($view_modes, $entity_info['view modes']);
  }

  // Add two default view mode options (not returned by entity_get_info()).
  $view_mode_options = array(
    'default' => t('Default'),
    'full' => t('Full'),
  );
  foreach ($view_modes as $system_name => $view_mode) {
    $view_mode_options[$system_name] = $view_mode['label'];
  }
  $form['behavior']['fac_view_mode'] = array(
    '#type' => 'select',
    '#title' => t('Select which view mode to use for rendering the suggestions'),
    '#description' => t('The view mode that is used to render the suggestions the search terms result in. Note that when you select a view mode that is not configured for the suggestion entity, the default view mode is used.'),
    '#default_value' => variable_get('fac_view_mode', 'fac'),
    '#options' => $view_mode_options,
  );
  $form['behavior']['fac_key_min_length'] = array(
    '#type' => 'textfield',
    '#title' => t('The minimum key length to trigger Fast Autocomplete'),
    '#description' => t('Enter the minimum key length to trigger the Fast Autocomplete on an input field. The minimum value is 1.'),
    '#default_value' => variable_get('fac_key_min_length', 1),
    '#required' => TRUE,
    '#size' => 2,
  );
  $form['behavior']['fac_key_max_length'] = array(
    '#type' => 'textfield',
    '#title' => t('The maximum key length to trigger Fast Autocomplete'),
    '#description' => t('Enter the maximum key length to trigger the Fast Autocomplete on an input field. The minimum value is 1.'),
    '#default_value' => variable_get('fac_key_max_length', 5),
    '#required' => TRUE,
    '#size' => 2,
  );
  $form['behavior']['fac_breakpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Breakpoint'),
    '#description' => t('Enter a minimum width in pixels to disable the Fast Autocomplete behavior until this minimum width is reached. Insert 0 to always enable the Fast Autocomplete behavior.'),
    '#default_value' => variable_get('fac_breakpoint', 0),
    '#size' => 4,
  );
  $form['behavior']['fac_use_module_css'] = array(
    '#type' => 'checkbox',
    '#title' => t('Include the css provided by the module in the output'),
    '#description' => t("Enable this option to include the css provided by the Fast Autocomplete module in the output. Don't forget to clear the Drupal cache after changing this option!"),
    '#default_value' => variable_get('fac_use_module_css', TRUE),
  );
  $form['behavior']['fac_result_location'] = array(
    '#type' => 'textfield',
    '#title' => t('Result location'),
    '#description' => t('Enter a jQuery selector for a single element that is used to append the results to. If left empty, the results will be appended to the form the input is in.'),
    '#default_value' => variable_get('fac_result_location', ''),
  );
  $form['json_files'] = array(
    '#type' => 'fieldset',
    '#title' => t('Json files'),
  );
  $form['json_files']['fac_clean_up_files'] = array(
    '#type' => 'checkbox',
    '#title' => t('Periodically clean up json files'),
    '#description' => t('Enable cleaning up json files on cron to refresh the contents of the json files that contain the autocomplete suggestions.'),
    '#default_value' => variable_get('fac_clean_up_files', TRUE),
  );
  $form['json_files']['fac_files_expire_time'] = array(
    '#type' => 'textfield',
    '#title' => t('Expire time'),
    '#description' => t('How old do the json files have to be to be considered expired? The value for this field should contain a relative string compared to now like "-1 month" or "-1 day"'),
    '#default_value' => variable_get('fac_files_expire_time', '-1 day'),
    '#size' => 20,
    '#states' => array(
      'visible' => array(
        'input[name="fac_clean_up_files"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="fac_clean_up_files"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['json_files']['fac_bulk_generate_json_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Bulk generate json files'),
    '#description' => t('Enable daily bulk generating and refreshing json files on cron instead of only generating json files on user request.'),
    '#default_value' => variable_get('fac_bulk_generate_json_enabled', FALSE),
  );
  $form['json_files']['fac_bulk_generate_json_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Size'),
    '#description' => t('To what size of the search key should the json files be generated? CAUTION: setting a size larger than 2 rapidly increases the number of combinations to generate json files for!'),
    '#default_value' => variable_get('fac_bulk_generate_json_size', 2),
    '#size' => 3,
    '#states' => array(
      'visible' => array(
        'input[name="fac_bulk_generate_json_enabled"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="fac_bulk_generate_json_enabled"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['json_files']['fac_files_delete_all'] = array(
    '#type' => 'button',
    '#value' => t('Delete all json files'),
    '#limit_validation_errors' => array(),
    '#name' => 'delete',
  );
  return system_settings_form($form);
}

/**
 * Fast Autocomplete settings form validation callback.
 */
function fac_settings_form_validate(&$form, &$form_state) {
  if ($form_state['clicked_button']['#name'] == 'delete') {
    drupal_goto('admin/config/search/fac/delete');
  }
  if ($form_state['values']['fac_all_results_link']) {
    if (!is_numeric($form_state['values']['fac_all_results_link_threshold'])) {
      form_set_error('fac_all_results_link_threshold', t('Please enter zero or a positive number.'));
    }
    else {
      if ($form_state['values']['fac_all_results_link_threshold'] < 0) {
        form_set_error('fac_all_results_link_threshold', t('The minimum value is 0.'));
      }
    }
  }
  if (!is_numeric($form_state['values']['fac_key_min_length'])) {
    form_set_error('fac_key_min_length', t('Please enter a positive number.'));
  }
  else {
    if ($form_state['values']['fac_key_min_length'] < 1) {
      form_set_error('fac_key_min_length', t('The minimum value is 1.'));
    }
  }
  if (!is_numeric($form_state['values']['fac_key_max_length'])) {
    form_set_error('fac_key_max_length', t('Please enter a positive number.'));
  }
  else {
    if ($form_state['values']['fac_key_max_length'] < 1) {
      form_set_error('fac_key_max_length', t('The minimum value is 1'));
    }
  }
  if (!is_numeric($form_state['values']['fac_breakpoint'])) {
    form_set_error('fac_breakpoint', t('Please enter zero or a positive number.'));
  }
  else {
    if ($form_state['values']['fac_breakpoint'] < 0) {
      form_set_error('fac_breakpoint', t('The minimum value is 0.'));
    }
  }
  if ($form_state['values']['fac_clean_up_files'] == 1 && empty($form_state['values']['fac_files_expire_time'])) {
    form_set_error('fac_files_expire_time', t('The field expire time is required!'));
  }
  if ($form_state['values']['fac_bulk_generate_json_enabled'] == 1 && empty($form_state['values']['fac_bulk_generate_json_size'])) {
    form_set_error('fac_bulk_generate_json_size', t('The field size is required!'));
  }
  if (strpos($form_state['values']['fac_result_location'], ',') !== FALSE) {
    form_set_error('fac_result_location', t('Please enter one selector'));
  }
}

/**
 * Returns the backend service settings form.
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
 *
 * @return array
 *   The backend service settings form.
 */
function fac_backend_settings_form(array $form, array $form_state) {
  $backend_service = variable_get('fac_backend_service', '');
  if (empty($backend_service)) {
    drupal_set_message(t('You have not selected a backend service yet. Please select one below.'), 'warning');
    drupal_goto('admin/config/search/fac');
  }
  else {
    $service = new $backend_service();
    $form = $service
      ->configurationForm($form, $form_state);
  }
  return $form;
}

/**
 * Validates the backend service settings form submitted values.
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
 */
function fac_backend_settings_form_validate(array &$form, array &$form_state) {
  $backend_service = variable_get('fac_backend_service', '');
  $service = new $backend_service();
  $service
    ->configurationFormValidate($form, $form_state['values'], $form_state);
}

/**
 * Submits the backend service settings form submitted values.
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
 */
function fac_backend_settings_form_submit(array &$form, array &$form_state) {
  $backend_service = variable_get('fac_backend_service', '');
  $service = new $backend_service();
  $service
    ->configurationFormSubmit($form, $form_state['values'], $form_state);
  drupal_set_message(t('The configuration options have been saved.'));
  drupal_set_message(t('Note that you might need to delete the cached JSON files. You can use <a href="!url">the "delete all json files" button at the bottom of the general settings form</a>.', array(
    '!url' => url('admin/config/search/fac', array(
      'fragment' => 'edit-fac-files-delete-all',
    )),
  )));
}

/**
 * Fast AutoComplete delete json files form.
 */
function fac_delete_form(array $form, array $form_state) {
  $form = array();
  return confirm_form($form, t('Delete the Fast Autocomplete json files'), 'admin/config/search/fac', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

/**
 * Submit handler for the fac_delete_form.
 */
function fac_delete_form_submit(array $form, array &$form_state) {
  fac_delete_json_files();
  variable_set('fac_hmac_key', FALSE);
  $form_state['redirect'] = 'admin/config/search/fac';
}

Functions

Namesort descending Description
fac_backend_settings_form Returns the backend service settings form.
fac_backend_settings_form_submit Submits the backend service settings form submitted values.
fac_backend_settings_form_validate Validates the backend service settings form submitted values.
fac_delete_form Fast AutoComplete delete json files form.
fac_delete_form_submit Submit handler for the fac_delete_form.
fac_settings_form Fast AutoComplete settings form.
fac_settings_form_validate Fast Autocomplete settings form validation callback.