diff --git a/search_api_saved_searches.module b/search_api_saved_searches.module index 59f2d46..1702171 100644 --- a/search_api_saved_searches.module +++ b/search_api_saved_searches.module @@ -396,6 +396,10 @@ function search_api_saved_searches_rules_action_info() { ), ), 'provides' => array( + 'search' => array( + 'type' => 'search_api_saved_search', + 'label' => t('The executed search.'), + ), 'result_count' => array( 'type' => 'integer', 'label' => t('The count of results that were found.'), @@ -478,21 +482,17 @@ function search_api_saved_searches_get_searches_to_be_executed($settings_id = NU } /** - * Worker function to check if there are any new saved search results. + * Callback: Implements the "Fetch the new results for a search" rules action. * * @param int $search_id - * The ID of the saved searcg setting entity. + * The ID of the saved search setting entity. * * @return array - * Array of the results count and the results list for the given search id. + * Array of the results count and the results list for the given search ID. */ function search_api_saved_searches_rules_get_saved_search_new_items($search_id) { $search = search_api_saved_search_load($search_id); - $search_result = search_api_saved_search_fetch_search_results($search); - return array( - 'result_count' => $search_result['result_count'], - 'results' => $search_result['results'], - ); + return search_api_saved_search_fetch_search_results($search); } /** @@ -1380,17 +1380,25 @@ function search_api_saved_searches_check_updates(array $search_ids) { $index = $settings->index(); $mail_params = array(); foreach ($searches as $search) { - $mail_params['searches'][] = search_api_saved_search_fetch_search_results($search); + $results = search_api_saved_search_fetch_search_results($search); + if (!$results['result_count']) { + continue; + } + // Load the result items. + if ($results['results']) { + $results['results'] = $index->loadItems($results['results']); + } + $mail_params['searches'][] = $results; } // If we set any searches in the mail parameters, send the mail. if ($mail_params) { $mail_params['user'] = user_load($search->uid); $mail_params['settings'] = $settings; $message = drupal_mail('search_api_saved_searches', 'notify', $search->mail, - user_preferred_language($mail_params['user']), $mail_params); + user_preferred_language($mail_params['user']), $mail_params); if ($message['result']) { watchdog('search_api_saved_searches', 'A mail with new saved search results was sent to @mail.', - array('@mail' => $search->mail), WATCHDOG_INFO); + array('@mail' => $search->mail), WATCHDOG_INFO); } } } @@ -1398,22 +1406,25 @@ function search_api_saved_searches_check_updates(array $search_ids) { /** * Fetches the results for a given search object. * - * @param object $search - * The search object to check if there are new result. + * @param SearchApiSavedSearch $search + * The saved search to check for new results. + * + * @return array + * An associative array with the following keys: + * - search: The executed search. + * - result_count: The number of new results. + * - results: The IDs of the new results. * * @throws SearchApiException - * If an error occurred in one of the searches. + * If an error occurred in the search. */ -function search_api_saved_search_fetch_search_results($search) { +function search_api_saved_search_fetch_search_results(SearchApiSavedSearch $search) { $return = array( + 'search' => $search, 'result_count' => 0, 'results' => array(), ); - if (empty($search_id)) { - return $return; - } - $settings = $search->settings(); try { // Make sure we run the query as the user who owns the saved search. @@ -1426,7 +1437,6 @@ function search_api_saved_search_fetch_search_results($search) { if (!empty($settings->options['date_field'])) { $query->condition($settings->options['date_field'], $search->last_execute, '>'); } - $response = $query->execute(); if (!empty($response['results'])) { $old = array(); @@ -1443,7 +1453,6 @@ function search_api_saved_search_fetch_search_results($search) { if (!empty($settings->options['mail']['notify']['max_results'])) { $sent_new = array_slice($new, 0, $settings->options['mail']['notify']['max_results']); } - $new_results = $sent_new + $new; // Let other modules alter these results. drupal_alter('search_api_saved_searches_new_results', $new_results, $search); @@ -1454,11 +1463,8 @@ function search_api_saved_search_fetch_search_results($search) { if (!empty($settings->options['mail']['notify']['max_results'])) { $sent_new = array_slice($new_results, 0, $settings->options['mail']['notify']['max_results']); } - $num_results = count($new_results); - $return['search'] = $search; - $return['result_count'] = $num_results; + $return['result_count'] = count($new_results); $return['results'] = $sent_new; - } } if (empty($settings->options['date_field']) && ($new || array_diff($old, $results))) { @@ -1476,5 +1482,6 @@ function search_api_saved_search_fetch_search_results($search) { $args['@id'] = $search->id; throw new SearchApiException(t('%type while trying to check for new results on saved search @id: !message in %function (line %line of %file).', $args)); } + return $return; } diff --git a/search_api_saved_searches.rules.inc b/search_api_saved_searches.rules.inc deleted file mode 100644 index 313ef20..0000000 --- a/search_api_saved_searches.rules.inc +++ /dev/null @@ -1,114 +0,0 @@ - array( - 'label' => t('Fetch the saved searches'), - 'parameter' => array( - 'index_id' => array( - 'type' => 'integer', - 'label' => t('Index for which to retrieve searches'), - 'description' => t('Select the search index for which saved searches should be retrieved.'), - 'options list' => '_search_api_saved_searches_settings_options_list', - ), - ), - 'provides' => array( - 'search_api_saved_search' => array( - 'type' => 'list', - 'label' => t('List of the IDs of the saved searches that require executing.'), - ), - ), - 'group' => t('Search API Saved Searches'), - ), - 'search_api_saved_searches_rules_get_saved_search_new_items' => array( - 'label' => t('Fetch the new results for a saved search'), - 'parameter' => array( - 'index_id' => array( - 'type' => 'integer', - 'label' => t('Saved search ID'), - 'description' => t('The ID of the saved search for which to retrieve new results.'), - ), - ), - 'provides' => array( - 'result_count' => array( - 'type' => 'integer', - 'label' => t('The count of results that were found.'), - ), - 'results' => array( - 'type' => 'list', - 'label' => t('The list of new results for the saved search since it was last executed.'), - ), - ), - 'group' => t('Search API Saved Searches'), - ), - ); -} - -/** - * Callback: Implements the "Fetch the saved searches" rules action. - * - * @param int|null $settings_id - * (optional) The ID of the saved search settings entity for which to retrieve - * searches. NULL to retrieve for all. - * - * @return array - * An associative array with key "search_api_saved_search" containing the IDs - * of all searches that should be executed. - */ -function search_api_saved_searches_rules_index_results($settings_id) { - return array( - 'search_api_saved_search' => search_api_saved_searches_get_searches_to_be_executed($settings_id), - ); -} - - - -/** - * Worker function to check if there are any new saved search results. - * - * @param int $search_id - * The ID of the saved searcg setting entity. - * - * @return array - * Array of the results count and the results list for the given search id. - */ -function search_api_saved_searches_rules_get_saved_search_new_items($search_id) { - $search = search_api_saved_search_load($search_id); - $search_result = search_api_saved_search_fetch_search_results($search); - return array( - 'result_count' => $search_result['result_count'], - 'results' => $search_result['results'], - ); -} - - -/** - * Retrieves the options list for selecting a saved search settings entity. - * - * @return string[] - * An associative array mapping saved search settings IDs to index names. - */ -function _search_api_saved_searches_settings_options_list() { - // Fetch the list of saved searches setting and make a list of values. - $entities = entity_load('search_api_saved_searches_settings'); - $ids = array(); - foreach ($entities as $entity) { - $ids[$entity->index_id][] = $entity->id; - } - - $indexes = search_api_index_load_multiple(array_keys($ids)); - $options = array(); - foreach ($indexes as $index_id => $index) { - foreach ($ids[$index_id] as $settings_id) { - $options[$settings_id] = $index->label(); - } - } - return $options; -}