You are here

function search_api_saved_searches_cron in Search API Saved Searches 7

Same name and namespace in other branches
  1. 8 search_api_saved_searches.module \search_api_saved_searches_cron()

Implements hook_cron().

Queue the saved searches that should be checked for new items.

File

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

Code

function search_api_saved_searches_cron() {
  $ids = search_api_saved_searches_get_searches_to_be_executed();
  if (!$ids) {
    return;
  }

  // Get the queue and load the queries.
  $queue = DrupalQueue::get('search_api_saved_searches_check_updates');
  $searches = search_api_saved_search_load_multiple($ids);

  // Group the search according to mail and settings. Grouping by mail prevents
  // a user from getting several mails at once, for different searches. Grouping
  // by settings is necessary since the mails can differ between settings.
  $user_searches = array();
  foreach ($searches as $search) {

    // Check whether notifications are enabled for this search.
    $settings = search_api_saved_searches_settings_load($search->settings_id);
    $options = $settings->options;
    if (!isset($options['mail']['notify']['send']) || $options['mail']['notify']['send']) {
      $user_searches[$search->mail . ' ' . $search->settings_id][] = $search->id;

      // Set the last execution timestamp now, so the interval doesn't move and we
      // don't get problems if the next cron run occurs before the queue is
      // completely executed.
      $search->last_queued = REQUEST_TIME;
      $search
        ->save();
    }
  }
  foreach ($user_searches as $searches) {
    $queue
      ->createItem($searches);
  }
}