You are here

function search_api_saved_searches_save_form_submit in Search API Saved Searches 7

Form validation handler for search_api_saved_searches_save_form().

Return value

boolean TRUE iff the search was successfully saved.

See also

search_api_saved_searches_save_form()

search_api_saved_searches_save_form_validate()

File

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

Code

function search_api_saved_searches_save_form_submit(array $form, array &$form_state) {
  global $user;
  $values = $form_state['values'];
  $settings = $form_state['settings'];
  if (empty($form_state['query'])) {
    $fields = $values['query']['fields'];
    $query = array(
      'keys' => isset($fields['search_api_saved_searches_fulltext']) ? $fields['search_api_saved_searches_fulltext'] : NULL,
      'fields' => NULL,
      'filters' => array(),
      'options' => array(
        'search id' => $settings->delta . ':' . 'saved-search',
      ),
    );
    unset($fields['search_api_saved_searches_fulltext']);
    foreach ($fields as $field => $value) {
      if ($value || is_numeric($value)) {
        if (is_array($value)) {
          foreach ($value as $key => $single_value) {
            if ($single_value) {
              $query['filters'][] = array(
                $field,
                $single_value,
                '=',
              );
            }
          }
        }
        else {
          $query['filters'][] = array(
            $field,
            $value,
            '=',
          );
        }
      }
      else {
        unset($fields[$field]);
      }
    }
    if (empty($values['name'])) {
      $query['original_keys'] = $query['keys'];
      $values['name'] = _search_api_saved_searches_create_name($query);
      unset($query['original_keys']);
    }
    if (empty($form_state['page']) && !empty($settings->options['manual']['page']['path'])) {
      $page_options = $settings->options['manual']['page'];
      $form_state['page'] = array(
        'path' => $page_options['path'],
        'query' => array(),
      );
      if (isset($query['keys'])) {
        if (empty($page_options['fulltext'])) {
          $form_state['page']['path'] .= '/' . $query['keys'];
        }
        else {
          $form_state['page']['query'][$page_options['fulltext']] = $query['keys'];
        }
      }
      foreach ($fields as $field => $value) {
        if (empty($page_options['direct_filter_params'])) {
          if (is_array($value)) {
            foreach ($value as $key => $single_value) {
              if ($single_value) {
                $form_state['page']['query']['f'][] = $field . ':' . $single_value;
              }
            }
          }
          else {
            $form_state['page']['query']['f'][] = $field . ':' . $value;
          }
        }
        else {
          $form_state['page']['query'][$field] = $value;
        }
      }
    }
  }
  else {
    $query = array_intersect_key($form_state['query'], drupal_map_assoc(array(
      'keys',
      'fields',
      'filters',
      'options',
    )));
  }

  // Enable the saved search right away, if a logged-in user uses their own mail
  // address, or when they have admin privileges, or when activation mails are
  // generally deactivated, or if there are already active saved searches for
  // that user with that mail address. Otherwise, an activation mail will be
  // sent.
  $enabled = !empty($user->mail) && $user->mail == $values['mail'] || user_access('administer search_api_saved_searches') || empty($settings->options['mail']['activate']['send']) || $user->uid && search_api_saved_search_load_multiple(FALSE, array(
    'enabled' => TRUE,
    'uid' => $user->uid,
    'mail' => $values['mail'],
  ));

  // If an anonymous user uses an existing user's mail address to create a
  // saved search, file the saved search under that user right away.
  $uid = $user->uid;
  if (!$uid && ($users = user_load_multiple(FALSE, array(
    'mail' => $values['mail'],
    'status' => 1,
  )))) {
    $uid = key($users);
  }
  $search = entity_create('search_api_saved_search', array(
    'uid' => $uid,
    'settings_id' => $settings->delta,
    'enabled' => $enabled,
    'name' => $values['name'],
    'mail' => $values['mail'],
    'created' => REQUEST_TIME,
    'last_queued' => REQUEST_TIME,
    'last_execute' => REQUEST_TIME,
    'notify_interval' => $values['notify_interval'],
    'query' => $query,
    'options' => array(),
  ));

  // Choose where to redirect.
  if (!empty($form_state['page'])) {
    $search->options['page'] = $form_state['page'];
    $form_state['redirect'] = array(
      $form_state['page']['path'],
      $form_state['page'],
    );
  }
  elseif ($user->uid) {
    $form_state['redirect'] = 'user/' . $user->uid . '/saved-searches';
  }

  // Store saved search object into form_state to allow custom submit handlers
  // to use it.
  $form_state['search'] = $search;

  // Save saved search.
  $ret = $search
    ->save();

  // Display success or error message.
  if (!$ret) {
    drupal_set_message(t('An error occurred while trying to save the search. Please contact the site administrator.'), 'error');
    $form_state['rebuild'] = TRUE;
    return FALSE;
  }
  else {
    if ($enabled) {
      if ($search->notify_interval < 0) {
        drupal_set_message(t('Your saved search was successfully created.'));
      }
      else {
        drupal_set_message(t('Your saved search was successfully created. You will receive e-mail notifications for new results in the future.'));
      }
    }
    else {
      drupal_set_message(t('Your saved search was successfully created. You will soon receive an e-mail with a confirmation link to activate it.'));
    }
    return TRUE;
  }
}