search_api_saved_searches.rules.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for the Search API Saved Searches module.
  5. */
  6. /**
  7. * Implements hook_rules_action_info().
  8. */
  9. function search_api_saved_searches_rules_action_info() {
  10. return array(
  11. 'search_api_saved_searches_rules_index_results' => array(
  12. 'label' => t('Fetch the saved searches'),
  13. 'parameter' => array(
  14. 'index_id' => array(
  15. 'type' => 'integer',
  16. 'label' => t('Index for which to retrieve searches'),
  17. 'description' => t('Select the search index for which saved searches should be retrieved.'),
  18. 'options list' => '_search_api_saved_searches_settings_options_list',
  19. ),
  20. ),
  21. 'provides' => array(
  22. 'search_api_saved_search' => array(
  23. 'type' => 'list<integer>',
  24. 'label' => t('List of the IDs of the saved searches that require executing.'),
  25. ),
  26. ),
  27. 'group' => t('Search API Saved Searches'),
  28. ),
  29. 'search_api_saved_searches_rules_get_saved_search_new_items' => array(
  30. 'label' => t('Fetch the new results for a saved search'),
  31. 'parameter' => array(
  32. 'index_id' => array(
  33. 'type' => 'integer',
  34. 'label' => t('Saved search ID'),
  35. 'description' => t('The ID of the saved search for which to retrieve new results.'),
  36. ),
  37. ),
  38. 'provides' => array(
  39. 'search' => array(
  40. 'type' => 'search_api_saved_search',
  41. 'label' => t('The executed search.'),
  42. ),
  43. 'result_count' => array(
  44. 'type' => 'integer',
  45. 'label' => t('The count of results that were found.'),
  46. ),
  47. 'results' => array(
  48. 'type' => 'list<integer>',
  49. 'label' => t('The list of new results for the saved search since it was last executed.'),
  50. ),
  51. ),
  52. 'group' => t('Search API Saved Searches'),
  53. ),
  54. );
  55. }
  56. /**
  57. * Retrieves the options list for selecting a saved search settings entity.
  58. *
  59. * @return string[]
  60. * An associative array mapping saved search settings IDs to index names.
  61. */
  62. function _search_api_saved_searches_settings_options_list() {
  63. // Fetch the list of saved searches setting and make a list of values.
  64. $entities = entity_load('search_api_saved_searches_settings');
  65. $ids = array();
  66. foreach ($entities as $entity) {
  67. $ids[$entity->index_id][] = $entity->id;
  68. }
  69. $indexes = search_api_index_load_multiple(array_keys($ids));
  70. $options = array();
  71. foreach ($indexes as $index_id => $index) {
  72. foreach ($ids[$index_id] as $settings_id) {
  73. $options[$settings_id] = $index->label();
  74. }
  75. }
  76. return $options;
  77. }
  78. /**
  79. * Callback: Implements the "Fetch the saved searches" rules action.
  80. *
  81. * @param int|null $settings_id
  82. * (optional) The ID of the saved search settings entity for which to retrieve
  83. * searches. NULL to retrieve for all.
  84. *
  85. * @return array
  86. * An associative array with key "search_api_saved_search" containing the IDs
  87. * of all searches that should be executed.
  88. */
  89. function search_api_saved_searches_rules_index_results($settings_id) {
  90. return array(
  91. 'search_api_saved_search' => search_api_saved_searches_get_searches_to_be_executed($settings_id),
  92. );
  93. }
  94. /**
  95. * Callback: Implements the "Fetch the new results for a search" rules action.
  96. *
  97. * @param int $search_id
  98. * The ID of the saved search setting entity.
  99. *
  100. * @return array
  101. * Array of the results count and the results list for the given search ID.
  102. */
  103. function search_api_saved_searches_rules_get_saved_search_new_items($search_id) {
  104. $search = search_api_saved_search_load($search_id);
  105. return search_api_saved_search_fetch_search_results($search);
  106. }