You are here

function search_api_saved_searches_save_form in Search API Saved Searches 7

Form builder for creating a new saved search.

Parameters

SearchApiSavedSearchesSettings $settings: The saved search settings with which to create a new saved search.

SearchApiQueryInterface $query: (optional) If creating a saved search for an already executed query, the query.

See also

search_api_saved_searches_save_form_validate()

search_api_saved_searches_save_form_submit()

2 string references to 'search_api_saved_searches_save_form'
search_api_saved_searches_block_view in ./search_api_saved_searches.module
Implements hook_block_view().
search_api_saved_searches_create_manual in ./search_api_saved_searches.pages.inc
Page callback for manually creating a new saved search.

File

./search_api_saved_searches.module, line 778
Offers the ability to save searches and be notified of new results.

Code

function search_api_saved_searches_save_form(array $form, array &$form_state, SearchApiSavedSearchesSettings $settings, SearchApiQueryInterface $query = NULL) {
  global $user;
  if (!isset($form_state['query']) && isset($query)) {
    $options = $query
      ->getOptions();

    // When checking for new results, we need all results.
    // @todo Make this configurable?
    unset($options['offset'], $options['limit']);
    $options['search id'] = $settings->delta . ':' . 'saved-search';
    $form_state['query'] = array(
      'index_id' => $query
        ->getIndex()->machine_name,
      'keys' => $query
        ->getKeys(),
      'original_keys' => $query
        ->getOriginalKeys(),
      'fields' => $query
        ->getFields(),
      'filters' => $query
        ->getFilter()
        ->getFilters(),
      'options' => $options,
    );
  }
  $form_state['settings'] = $settings;
  $description = $settings
    ->getTranslatedOption('description');
  if (!empty($description)) {
    $form['description'] = array(
      '#type' => 'item',
      '#description' => _filter_autop(check_plain($description)),
    );
  }
  if (empty($form_state['query'])) {
    $form['query'] = _search_api_saved_searches_create_search_form($settings);
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#description' => t('Enter the name that will be displayed for this saved search.'),
      '#maxlength' => 255,
    );
  }
  else {
    $form['#prefix'] = '<div id="search-api-saved-searches-save-form-wrapper">';
    $form['#suffix'] = '</div>';
    if (empty($settings->options['choose_name'])) {
      $form['name'] = array(
        '#type' => 'value',
        '#value' => _search_api_saved_searches_create_name($form_state['query']),
      );
    }
    else {
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#maxlength' => 255,
        '#size' => 16,
        '#required' => TRUE,
        '#default_value' => _search_api_saved_searches_create_name($form_state['query']),
      );
    }
  }
  if (empty($user->mail) || $settings->options['registered_choose_mail']) {
    $form['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail address'),
      '#maxlength' => 100,
      '#size' => 16,
      '#default_value' => isset($user->mail) ? $user->mail : '',
      '#required' => TRUE,
    );
  }
  else {
    $form['mail'] = array(
      '#type' => 'value',
      '#value' => $user->mail,
    );
  }
  if ($settings->options['user_select_interval'] && count($settings->options['interval_options']) > 1) {
    $form['notify_interval'] = array(
      '#type' => 'select',
      '#title' => t('Notification interval'),
      '#options' => $settings
        ->getTranslatedOption('interval_options'),
      '#required' => TRUE,
    );
  }
  else {
    $form['notify_interval'] = array(
      '#type' => 'value',
      '#value' => $settings->options['user_select_interval'] ? reset($settings->options['interval_options']) : $settings->options['set_interval'],
    );
  }
  if (!empty($form_state['query'])) {
    $form_state['page'] = array(
      'path' => $_GET['q'],
      'query' => drupal_get_query_parameters(),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save search'),
    '#ajax' => array(
      'callback' => 'search_api_saved_searches_save_form_ajax',
      'wrapper' => 'search-api-saved-searches-save-form-wrapper',
      'effect' => 'fade',
      'method' => 'replace',
    ),
    '#executes_submit_callback' => TRUE,
  );

  // For manual search creation we don't need AJAX functionality.
  if (empty($form_state['query'])) {
    unset($form['submit']['#ajax']);
  }
  return $form;
}