search_api_saved_searches.search_entity.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_execute;
  61. /**
  62. * @var integer
  63. */
  64. public $notify_interval;
  65. /**
  66. * Array representing the search query to execute, containing:
  67. * keys: The parsed fulltext keys.
  68. * fields: The fields that will be fulltext-searched.
  69. * filters: An array of filters, as used in SearchApiQueryFilterInterface.
  70. * options: The query options.
  71. *
  72. * @var array
  73. */
  74. public $query;
  75. /**
  76. * @var array
  77. */
  78. public $options;
  79. /**
  80. * @var string
  81. */
  82. public $results;
  83. /**
  84. * Constructor as a helper to the parent constructor.
  85. */
  86. public function __construct(array $values = array()) {
  87. parent::__construct($values, 'search_api_saved_search');
  88. }
  89. /**
  90. * Permanently saves the entity.
  91. *
  92. * @see entity_save()
  93. */
  94. public function save() {
  95. $settings = $this->settings();
  96. if ((!$this->enabled && empty($this->options['key'])) || !empty($settings->options['registered_user_delete_key'])) {
  97. $this->options['key'] = drupal_hash_base64(drupal_random_bytes(12));
  98. }
  99. $date_field = isset($settings->options['date_field']) ? $settings->options['date_field'] : NULL;
  100. if ($this->enabled && !isset($this->results) && !$date_field) {
  101. $results = array();
  102. $response = $this->query()->execute();
  103. $this->results = implode(',', array_keys($response['results']));
  104. }
  105. $ret = parent::save();
  106. if ($ret == SAVED_NEW && !$this->enabled) {
  107. $params = array(
  108. 'user' => user_load($this->uid),
  109. 'search' => $this,
  110. );
  111. drupal_mail('search_api_saved_searches', 'activate', $this->mail, user_preferred_language($params['user']), $params);
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * @return
  117. * The user that owns this saved search.
  118. */
  119. public function user() {
  120. if (!isset($this->user)) {
  121. $this->user = user_load($this->uid);
  122. }
  123. return $this->user;
  124. }
  125. /**
  126. * @return SearchApiIndex
  127. * The settings this saved search uses.
  128. *
  129. * @throws SearchApiException
  130. * If the settings don't exist.
  131. */
  132. public function settings() {
  133. if (!isset($this->settings)) {
  134. $this->settings = search_api_saved_searches_settings_load($this->settings_id);
  135. }
  136. if (!$this->settings) {
  137. 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)));
  138. }
  139. return $this->settings;
  140. }
  141. /**
  142. * @return SearchApiIndex
  143. * The index this saved search uses.
  144. *
  145. * @throws SearchApiException
  146. * If the index doesn't exist.
  147. */
  148. public function index() {
  149. if (!isset($this->index)) {
  150. $this->index = search_api_index_load($this->settings()->index_id);
  151. }
  152. if (!$this->index) {
  153. 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)));
  154. }
  155. return $this->index;
  156. }
  157. /**
  158. * @return SearchApiQueryInterface
  159. * A query for getting all new results for this saved search.
  160. *
  161. * @throws SearchApiException
  162. * If the saved search's index is disabled.
  163. */
  164. public function query() {
  165. $query = $this->index()->query($this->query['options']);
  166. if ($this->query['keys']){
  167. $query->keys($this->query['keys']);
  168. }
  169. if ($this->query['fields']){
  170. $query->fields($this->query['fields']);
  171. }
  172. if ($this->query['filters']){
  173. $filters = &$query->getFilter()->getFilters();
  174. $filters = $this->query['filters'];
  175. }
  176. return $query;
  177. }
  178. /**
  179. * Return the URL where this search can be viewed, if any.
  180. */
  181. public function url() {
  182. if (isset($this->options['page']['path'])) {
  183. return url($this->options['page']['path'], $this->options['page']);
  184. }
  185. }
  186. /**
  187. * Return a link to the URL where this search can be viewed, if any.
  188. */
  189. public function l($text) {
  190. if (isset($this->options['page']['path'])) {
  191. return l($text, $this->options['page']['path'], $this->options['page']);
  192. }
  193. }
  194. }