diff --git a/search_api_saved_searches.module b/search_api_saved_searches.module index 56bbfe6..efd7966 100644 --- a/search_api_saved_searches.module +++ b/search_api_saved_searches.module @@ -369,30 +369,30 @@ function search_api_saved_searches_user_delete($account) { function search_api_saved_searches_rules_action_info() { return array( 'search_api_saved_searches_rules_index_results' => array( - 'label' => t('Fetch the saved searches'), + 'label' => t('Fetch the saved searches'), 'parameter' => array( 'index_id' => array( 'type' => 'integer', - 'label' => t('Index to check results'), - 'description' => t('Select the search api saved search settings index to check for new values.'), - 'options list' => '_search_api_saved_searches_fetch_index_id', + '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 ID of the saved searches that require executing.'), + 'label' => t('List of the IDs of the saved searches that require executing.'), ), ), - 'group' => t('Search api saved searches'), + 'group' => t('Search API Saved Searches'), ), 'search_api_saved_searches_rules_get_saved_search_new_items' => array( - 'label' => t('Fetch the list new items for saved searches'), + 'label' => t('Fetch the new results for a saved search'), 'parameter' => array( 'index_id' => array( 'type' => 'integer', - 'label' => t('Saved search entity'), - 'description' => t('Using the saved search the interger of saved search'), + 'label' => t('Saved search ID'), + 'description' => t('The ID of the saved search for which to retrieve new results.'), ), ), 'provides' => array( @@ -402,67 +402,103 @@ function search_api_saved_searches_rules_action_info() { ), 'results' => array( 'type' => 'list', - 'label' => t('The list of new items in the since the last search.'), + 'label' => t('The list of new results for the saved search since it was last executed.'), ), ), - 'group' => t('Search api saved searches'), + 'group' => t('Search API Saved Searches'), ), ); } /** - * Function to fetch the list of settings values(). + * 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_fetch_index_id() { - $return = array(); - +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) { - $return[$entity->id] = $entity->index_id; + $ids[$entity->index_id][] = $entity->id; } - - return $return; + + $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; } /** - * Evaluation function for the rules index results. - * - * This function checks if there are any saved searches that need to be run. + * 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($index_id) { - $ids = db_select('search_api_saved_search', 's') - ->fields('s', array('id')) +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), + ); +} + +/** + * Retrieves the saved searches that need to be executed. + * + * @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 int[] + * The IDs of all searches that need to be executed. + */ +function search_api_saved_searches_get_searches_to_be_executed($settings_id = NULL) { + // Get all searches whose last execution lies more than the notify_interval + // in the past. Add a small amount to the current time, so small differences + // in execution time don't result in a delay until the next cron run. + $select = db_select('search_api_saved_search', 's'); + $select->fields('s', array('id')) ->condition('enabled', 1) - ->condition('notify_interval', -1, '=') + ->condition('notify_interval', 0, '>=') ->where('last_execute >= last_queued') - ->where('last_queued + notify_interval < :time', array(':time' => REQUEST_TIME + 15)) - ->execute() - ->fetchCol(); - - return array('search_api_saved_search' => $ids); + ->where('last_queued + notify_interval < :time', array(':time' => REQUEST_TIME + 15)); + if ($settings_id !== NULL) { + $select->condition('settings_id', $settings_id); + } + return $select->execute()->fetchCol(); } /** * Worker function to check if there are any new saved search results. */ function search_api_saved_searches_rules_get_saved_search_new_items($search_id) { - $search_result = array(); - $return = array('result_count' => 0, 'results' => array()); - + $return = array( + 'result_count' => 0, + 'results' => array(), + ); + if (empty($search_id)) { return $return; } + $search = search_api_saved_search_load($search_id); $settings = $search->settings(); - $index = $settings->index(); try { // Make sure we run the query as the user who owns the saved search. // Otherwise node access will not work properly. $search->query['options']['search_api_access_account'] = $search->uid; // Get actual results for the query. $query = $search->query(); - + // If a date field is set, use that to filter results. if (!empty($settings->options['date_field'])) { $query->condition($settings->options['date_field'], $search->last_execute, '>'); @@ -498,7 +534,7 @@ function search_api_saved_searches_rules_get_saved_search_new_items($search_id) $num_results = count($new_results); $return['result_count'] = $num_results; $return['results'] = $sent_new; - + } } if (empty($settings->options['date_field']) && ($new || array_diff($old, $results))) { @@ -516,7 +552,7 @@ function search_api_saved_searches_rules_get_saved_search_new_items($search_id) $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; } @@ -1328,17 +1364,7 @@ function search_api_saved_searches_mail($key, array &$message, array $params) { * Queue the saved searches that should be checked for new items. */ function search_api_saved_searches_cron() { - // Get all searches whose last execution lies more than the notify_interval - // in the past. Add a small amount to the current time, so small differences - // in execution time don't result in a delay until the next cron run. - $ids = db_select('search_api_saved_search', 's') - ->fields('s', array('id')) - ->condition('enabled', 1) - ->condition('notify_interval', 0, '>=') - ->where('last_execute >= last_queued') - ->where('last_queued + notify_interval < :time', array(':time' => REQUEST_TIME + 15)) - ->execute() - ->fetchCol(); + $ids = search_api_saved_searches_get_searches_to_be_executed(); if (!$ids) { return; }