search_api_saved_searches.search_entity.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @file
  4. * Contains the entity class for saved searches.
  5. */
  6. /**
  7. * Class representing "Saved searches" settings.
  8. */
  9. class SearchApiSavedSearch extends Entity {
  10. /**
  11. * The user that owns this saved search.
  12. *
  13. * @var stdClass
  14. */
  15. protected $user;
  16. /**
  17. * The settings this saved search uses.
  18. *
  19. * @var SearchApiSavedSearchesSettings
  20. */
  21. protected $settings;
  22. /**
  23. * The search index this saved search uses.
  24. *
  25. * @var SearchApiIndex
  26. */
  27. protected $index;
  28. // Database values that will be set when object is loaded
  29. /**
  30. * @var integer
  31. */
  32. public $id;
  33. /**
  34. * @var integer
  35. */
  36. public $uid;
  37. /**
  38. * @var string
  39. */
  40. public $settings_id;
  41. /**
  42. * @var boolean
  43. */
  44. public $enabled;
  45. /**
  46. * @var string
  47. */
  48. public $name;
  49. /**
  50. * @var string
  51. */
  52. public $mail;
  53. /**
  54. * @var integer
  55. */
  56. public $created;
  57. /**
  58. * @var integer
  59. */
  60. public $last_queued;
  61. /**
  62. * @var integer
  63. */
  64. public $last_execute;
  65. /**
  66. * @var integer
  67. */
  68. public $notify_interval;
  69. /**
  70. * Array representing the search query to execute, containing:
  71. * keys: The parsed fulltext keys.
  72. * fields: The fields that will be fulltext-searched.
  73. * filters: An array of filters, as used in SearchApiQueryFilterInterface.
  74. * options: The query options.
  75. *
  76. * @var array
  77. */
  78. public $query;
  79. /**
  80. * @var array
  81. */
  82. public $options;
  83. /**
  84. * @var string
  85. */
  86. public $results;
  87. /**
  88. * Constructor as a helper to the parent constructor.
  89. */
  90. public function __construct(array $values = array()) {
  91. parent::__construct($values, 'search_api_saved_search');
  92. }
  93. /**
  94. * Permanently saves the entity.
  95. *
  96. * @see entity_save()
  97. */
  98. public function save() {
  99. $settings = $this->settings();
  100. if ((!$this->enabled && empty($this->options['key'])) || !empty($settings->options['registered_user_delete_key'])) {
  101. $this->options['key'] = drupal_hash_base64(drupal_random_bytes(12));
  102. }
  103. $date_field = isset($settings->options['date_field']) ? $settings->options['date_field'] : NULL;
  104. if ($this->enabled && !isset($this->results) && !$date_field) {
  105. $results = array();
  106. $response = $this->query()->execute();
  107. $this->results = implode(',', array_keys($response['results']));
  108. }
  109. $ret = parent::save();
  110. if ($ret == SAVED_NEW && !$this->enabled) {
  111. $params = array(
  112. 'user' => user_load($this->uid),
  113. 'search' => $this,
  114. );
  115. drupal_mail('search_api_saved_searches', 'activate', $this->mail, user_preferred_language($params['user']), $params);
  116. }
  117. return $ret;
  118. }
  119. /**
  120. * @return
  121. * The user that owns this saved search.
  122. */
  123. public function user() {
  124. if (!isset($this->user)) {
  125. $this->user = user_load($this->uid);
  126. }
  127. return $this->user;
  128. }
  129. /**
  130. * @return SearchApiSavedSearchesSettings
  131. * The settings this saved search uses.
  132. *
  133. * @throws SearchApiException
  134. * If the settings don't exist.
  135. */
  136. public function settings() {
  137. if (!isset($this->settings)) {
  138. $this->settings = search_api_saved_searches_settings_load($this->settings_id);
  139. }
  140. if (!$this->settings) {
  141. throw new SearchApiException(t("The saved search settings with the ID %id don't exist, but are used by an existing saved search.", array('%id' => $this->settings_id)));
  142. }
  143. return $this->settings;
  144. }
  145. /**
  146. * @return SearchApiIndex
  147. * The index this saved search uses.
  148. *
  149. * @throws SearchApiException
  150. * If the index doesn't exist.
  151. */
  152. public function index() {
  153. if (!isset($this->index)) {
  154. $this->index = search_api_index_load($this->settings()->index_id);
  155. }
  156. if (!$this->index) {
  157. throw new SearchApiException(t("The index with the ID %id doesn't exist, but has saved search settings attached.", array('%id' => $this->settings()->index_id)));
  158. }
  159. return $this->index;
  160. }
  161. /**
  162. * @return SearchApiQueryInterface
  163. * A query for getting all new results for this saved search.
  164. *
  165. * @throws SearchApiException
  166. * If the saved search's index is disabled.
  167. */
  168. public function query() {
  169. $query = $this->index()->query($this->query['options']);
  170. if ($this->query['keys']){
  171. $query->keys($this->query['keys']);
  172. }
  173. if ($this->query['fields']){
  174. $query->fields($this->query['fields']);
  175. }
  176. if ($this->query['filters']){
  177. $filters = &$query->getFilter()->getFilters();
  178. $filters = $this->query['filters'];
  179. }
  180. return $query;
  181. }
  182. /**
  183. * Return the URL where this search can be viewed, if any.
  184. */
  185. public function url() {
  186. if (isset($this->options['page']['path'])) {
  187. return url($this->options['page']['path'], $this->options['page']);
  188. }
  189. }
  190. /**
  191. * Return a link to the URL where this search can be viewed, if any.
  192. */
  193. public function l($text) {
  194. if (isset($this->options['page']['path'])) {
  195. return l($text, $this->options['page']['path'], $this->options['page']);
  196. }
  197. }
  198. }