You are here

function search_api_autocomplete_admin_overview in Search API Autocomplete 7

Form displaying an overview over all searches available for autocompletion.

Parameters

SearchApiIndex $index: The index for which autocompletion searches should be configured.

See also

search_api_autocomplete_admin_overview_submit()

search_api_autocomplete_admin_overview_submit_delete()

1 string reference to 'search_api_autocomplete_admin_overview'
search_api_autocomplete_menu in ./search_api_autocomplete.module
Implements hook_menu().

File

./search_api_autocomplete.admin.inc, line 18
Contains page callbacks and related functions for the admin UI.

Code

function search_api_autocomplete_admin_overview(array $form, array &$form_state, SearchApiIndex $index) {
  $form = array();
  $form_state['index'] = $index;
  $index_id = $index->machine_name;
  $available_suggesters = search_api_autocomplete_suggesters_for_index($index);
  if (!$available_suggesters) {
    $args = array(
      '@feature' => 'search_api_autocomplete',
      '@service_classes_url' => url('https://www.drupal.org/node/1254698#service-classes'),
    );
    drupal_set_message(t('There are currently no suggester plugins installed that support this index. To solve this problem, you can either:<ul><li>move the index to a server which supports the "@feature" feature (see the <a href="@service_classes_url">available service class</a>)</li><li>or install a module providing a new suggester plugin that supports this index</li></ul>', $args), 'error');
    if (search_api_autocomplete_search_load_multiple(FALSE, array(
      'index_id' => $index_id,
    ))) {
      $form['description'] = array(
        '#type' => 'item',
        '#title' => t('Delete autocompletion settings'),
        '#description' => t("If you won't use autocompletion with this index anymore, you can delete all autocompletion settings associated with it. " . "This will delete all autocompletion settings on this index. Settings on other indexes won't be influenced."),
      );
      $form['button'] = array(
        '#type' => 'submit',
        '#value' => t('Delete autocompletion settings'),
        '#submit' => array(
          'search_api_autocomplete_admin_overview_submit_delete',
        ),
      );
    }
    return $form;
  }
  $form['#tree'] = TRUE;
  $types = search_api_autocomplete_get_types();
  $searches = search_api_autocomplete_search_load_multiple(FALSE, array(
    'index_id' => $index_id,
  ));
  $show_status = FALSE;
  foreach ($types as $type => $info) {
    if (empty($info['list searches'])) {
      continue;
    }
    $t_searches = $info['list searches']($index);
    if (empty($t_searches)) {
      $t_searches = array();
    }
    foreach ($t_searches as $id => $search) {
      if (isset($searches[$id])) {
        $types[$type]['searches'][$id] = $searches[$id];
        $show_status |= $searches[$id]
          ->hasStatus(ENTITY_IN_CODE);
        unset($searches[$id]);
      }
      else {
        reset($available_suggesters);
        $search += array(
          'machine_name' => $id,
          'index_id' => $index_id,
          'suggester_id' => key($available_suggesters),
          'type' => $type,
          'enabled' => 0,
          'options' => array(),
        );
        $search['options'] += array(
          'results' => TRUE,
          'fields' => array(),
        );
        $types[$type]['searches'][$id] = entity_create('search_api_autocomplete_search', $search);
      }
    }
  }
  foreach ($searches as $id => $search) {
    $type = isset($types[$search->type]) ? $search->type : '';
    $types[$type]['searches'][$id] = $search;
    $types[$type]['unavailable'][$id] = TRUE;
    $show_status |= $search
      ->hasStatus(ENTITY_IN_CODE);
  }
  $base_path = 'admin/config/search/search_api/index/' . $index_id . '/autocomplete/';
  foreach ($types as $type => $info) {
    if (empty($info['searches'])) {
      continue;
    }
    if (!$type) {
      $info += array(
        'name' => t('Unavailable search types'),
        'description' => t("The modules providing these searches were disabled or uninstalled. If you won't use them anymore, you can delete their settings."),
      );
    }
    elseif (!empty($info['unavailable'])) {
      $info['description'] .= '</p><p>' . t("The searches marked with an asterisk (*) are currently not available, possibly because they were deleted. If you won't use them anymore, you can delete their settings.");
    }
    $form[$type] = array(
      '#type' => 'fieldset',
      '#title' => $info['name'],
    );
    if (!empty($info['description'])) {
      $form[$type]['#description'] = '<p>' . $info['description'] . '</p>';
    }
    $form[$type]['searches']['#theme'] = 'tableselect';
    $form[$type]['searches']['#header'] = array();
    if ($show_status) {
      $form[$type]['searches']['#header']['status'] = t('Status');
    }
    $form[$type]['searches']['#header'] += array(
      'name' => t('Name'),
      'operations' => t('Operations'),
    );
    $form[$type]['searches']['#empty'] = '';
    $form[$type]['searches']['#js_select'] = TRUE;
    foreach ($info['searches'] as $id => $search) {
      $form[$type]['searches'][$id] = array(
        '#type' => 'checkbox',
        '#default_value' => $search->enabled,
        '#parents' => array(
          'searches',
          $id,
        ),
      );
      $unavailable = !empty($info['unavailable'][$id]);
      if ($unavailable) {
        $form[$type]['searches'][$id]['#default_value'] = FALSE;
        $form[$type]['searches'][$id]['#disabled'] = TRUE;
      }
      $form_state['searches'][$id] = $search;
      $options =& $form[$type]['searches']['#options'][$id];
      if ($show_status) {
        $options['status'] = isset($search->status) ? theme('entity_status', array(
          'status' => $search->status,
        )) : '';
      }
      $options['name'] = $search->name;
      if ($unavailable) {
        $options['name'] = '* ' . $options['name'];
      }
      $items = array();
      if (!$unavailable && !empty($search->id)) {
        $items[] = l(t('edit'), $base_path . $id . '/edit');
      }
      if (!empty($search->status) && $search
        ->hasStatus(ENTITY_CUSTOM)) {
        $title = $search
          ->hasStatus(ENTITY_IN_CODE) ? t('revert') : t('delete');
        $items[] = l($title, $base_path . $id . '/delete');
      }
      if ($items) {
        $variables = array(
          'items' => $items,
          'attributes' => array(
            'class' => array(
              'inline',
            ),
          ),
        );
        $options['operations'] = theme('item_list', $variables);
      }
      else {
        $options['operations'] = '';
      }
      unset($options);
    }
  }
  if (!element_children($form)) {
    $form['message']['#markup'] = '<p>' . t('There are currently no searches known for this index.') . '</p>';
  }
  else {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  return $form;
}