search_api_saved_searches.pages.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @file
  4. * User UI functions and form callbacks for saved searches.
  5. */
  6. /**
  7. * Page callback for listing all saved searches of a user.
  8. *
  9. * @param $account
  10. * The user of which to list saved searches.
  11. */
  12. function search_api_saved_searches_user_listing($account) {
  13. $base_path = 'search-api/saved-search/';
  14. $header = array(
  15. t('Name'),
  16. t('Created'),
  17. t('Last executed'),
  18. t('Interval'),
  19. t('Operations'),
  20. );
  21. $searches = search_api_saved_search_load_multiple(FALSE, array('uid' => $account->uid));
  22. $rows = array();
  23. foreach ($searches as $search) {
  24. $settings = $search->settings();
  25. if (empty($search->options['page'])) {
  26. $name = check_plain($search->name);
  27. }
  28. else {
  29. $name = $search->l($search->name);
  30. }
  31. $created = format_date($search->created, 'short');
  32. $last_execute = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $search->last_execute)));
  33. // Get the translated label for the interval.
  34. $available_intervals = $settings->getTranslatedOption('interval_options');
  35. if (isset($available_intervals[$search->notify_interval])) {
  36. $interval = $available_intervals[$search->notify_interval];
  37. }
  38. elseif ($search->notify_interval < 0) {
  39. $interval = t('Never');
  40. }
  41. else {
  42. $interval = format_interval($search->notify_interval, 1);
  43. }
  44. $edit_options['attributes']['class'][] = 'saved-search-edit';
  45. $delete_options['attributes']['class'][] = 'saved-search-delete';
  46. $path = $base_path . $search->id;
  47. $rows[] = array(
  48. $name,
  49. $created,
  50. $last_execute,
  51. $interval,
  52. l(t('edit'), $path . '/edit', $edit_options) . ' | ' . l(t('delete'), $path . '/delete', $delete_options),
  53. );
  54. }
  55. $render = array(
  56. '#theme' => 'table',
  57. '#header' => $header,
  58. '#rows' => $rows,
  59. '#empty' => t('No searches were saved yet.'),
  60. '#sticky' => TRUE,
  61. );
  62. return $render;
  63. }
  64. /**
  65. * Page callback for manually creating a new saved search.
  66. *
  67. * @param SearchApiSavedSearchesSettings $settings
  68. * (optional) The settings to use. If not present, either the only available
  69. * settings will be used, or a form for selecting one will be displayed.
  70. */
  71. function search_api_saved_searches_create_manual(SearchApiSavedSearchesSettings $settings = NULL) {
  72. if (!isset($settings)) {
  73. foreach (search_api_saved_searches_settings_load_multiple(FALSE, array('enabled' => TRUE)) as $settings) {
  74. if (!empty($settings->options['manual']['allow'])) {
  75. $available_settings[$settings->delta] = $settings;
  76. }
  77. }
  78. if (empty($available_settings)) {
  79. return t('There are no searches for which saved searches can be created manually.');
  80. }
  81. if (count($available_settings) == 1) {
  82. $settings = reset($available_settings);
  83. }
  84. else {
  85. $render = array();
  86. $render['question']['#markup'] = t('For which search do you want to create a saved search?');
  87. $render['list']['#theme'] = 'list';
  88. $render['list']['#items'] = array();
  89. $base = 'search-api/saved-searches/add/';
  90. foreach ($available_settings as $id => $settings) {
  91. $name = $settings->index()->name;
  92. if (!empty($settings->options['manual']['page']['path'])) {
  93. $item = menu_get_item($settings->options['manual']['page']['path']);
  94. if (!empty($item['title'])) {
  95. $name = $item['title'];
  96. }
  97. }
  98. $render['list']['#items'][]['#markup'] = l($name, $base . $id);
  99. }
  100. return $render;
  101. }
  102. }
  103. return drupal_get_form('search_api_saved_searches_save_form', $settings);
  104. }
  105. /**
  106. * Page callback that activates a saved search.
  107. *
  108. * This is mostly necessary for anonymous users, but also when a user enters a
  109. * different mail address than the one he registered with.
  110. *
  111. * @param SearchApiSavedSearch $search
  112. * The search to activate.
  113. * @param string $key
  114. * The secret access key for this search, used to authenticate the user.
  115. */
  116. function search_api_saved_searches_activate_page(SearchApiSavedSearch $search, $key) {
  117. $ret = array(
  118. 'message' => array(
  119. '#markup' => '',
  120. ),
  121. 'link' => array(
  122. '#markup' => '<p>' . $search->l(t('View this saved search')) . '</p>',
  123. ),
  124. );
  125. if ($search->enabled) {
  126. $msg = t('This saved search has already been activated.');
  127. }
  128. else {
  129. $search->enabled = TRUE;
  130. $success = $search->save();
  131. if (!$success) {
  132. drupal_set_message(t('An error occurred while trying to activate the search. Please contact the site administrator.'), 'error');
  133. return $ret;
  134. }
  135. $msg = t('Your saved search was successfully activated.');
  136. }
  137. if ($search->notify_interval >= 0) {
  138. $msg .= ' ' . t('You will receive e-mail notifications for new results in the future.');
  139. }
  140. $ret['message']['#markup'] = '<p>' . $msg . '</p>';
  141. return $ret;
  142. }
  143. /**
  144. * Form builder for editing a saved search.
  145. *
  146. * @param SearchApiSavedSearch $search
  147. * The search to edit.
  148. *
  149. * @see search_api_saved_searches_search_edit_form_submit()
  150. * @ingroup forms
  151. */
  152. function search_api_saved_searches_search_edit_form(array $form, array &$form_state, SearchApiSavedSearch $search) {
  153. $form_state['search'] = $search;
  154. $settings = $search->settings();
  155. if ($search->uid) {
  156. $form_state['destination']['path'] = 'user/' . $search->uid . '/saved-searches';
  157. }
  158. elseif (!empty($search->options['page'])) {
  159. $form_state['destination'] = array($search->options['page']['path'], $search->options['page']);
  160. }
  161. $form['name'] = array(
  162. '#type' => 'textfield',
  163. '#title' => t('Name'),
  164. '#description' => t('The name that will be displayed for this saved search.'),
  165. '#maxlength' => 50,
  166. '#required' => TRUE,
  167. '#default_value' => $search->name,
  168. );
  169. if ($settings->options['user_select_interval'] && count($settings->options['interval_options']) > 1) {
  170. $form['notify_interval'] = array(
  171. '#type' => 'select',
  172. '#title' => t('Notification interval'),
  173. '#options' => $settings->getTranslatedOption('interval_options'),
  174. '#required' => TRUE,
  175. '#default_value' => $search->notify_interval,
  176. );
  177. }
  178. $form['actions']['#type'] = 'actions';
  179. $form['actions']['submit'] = array(
  180. '#type' => 'submit',
  181. '#value' => t('Save changes'),
  182. );
  183. if (!empty($form_state['destination'])) {
  184. $form['actions']['cancel'] = array(
  185. '#type' => 'link',
  186. '#title' => t('Cancel'),
  187. '#href' => $form_state['destination']['path'],
  188. '#options' => $form_state['destination'],
  189. );
  190. }
  191. return $form;
  192. }
  193. /**
  194. * Submission handler for search_api_saved_searches_search_edit_form().
  195. *
  196. * @see search_api_saved_searches_search_edit_form()
  197. */
  198. function search_api_saved_searches_search_edit_form_submit(array $form, array &$form_state) {
  199. $values = $form_state['values'];
  200. $search = $form_state['search'];
  201. $search->name = $values['name'];
  202. if (isset($values['notify_interval'])) {
  203. $search->notify_interval = $values['notify_interval'];
  204. }
  205. if ($search->save()) {
  206. drupal_set_message(t('Successfully saved your changes.'));
  207. if (!empty($form_state['destination'])) {
  208. $form_state['redirect'] = $form_state['destination'];
  209. }
  210. }
  211. else {
  212. drupal_set_message(t('An error occurred while trying to save the changes.'), 'error');
  213. }
  214. }
  215. /**
  216. * Form builder for confirming the deletion of a saved search.
  217. *
  218. * @param SearchApiSavedSearch $search
  219. * The search to delete.
  220. *
  221. * @see search_api_saved_searches_search_delete_form_submit()
  222. * @ingroup forms
  223. */
  224. function search_api_saved_searches_search_delete_form(array $form, array &$form_state, SearchApiSavedSearch $search) {
  225. $form_state['search'] = $search;
  226. if ($search->uid && search_api_saved_search_edit_access(user_load($search->uid))) {
  227. $url = 'user/' . $search->uid . '/saved-searches';
  228. }
  229. elseif (!empty($search->options['page'])) {
  230. $url = $search->options['page'];
  231. }
  232. else {
  233. $url = '<front>';
  234. }
  235. return confirm_form($form, t('Do you really want to delete this saved search?'), $url);
  236. }
  237. /**
  238. * Submission handler for search_api_saved_searches_search_delete_form().
  239. *
  240. * @see search_api_saved_searches_search_delete_form()
  241. */
  242. function search_api_saved_searches_search_delete_form_submit(array $form, array &$form_state) {
  243. $search = $form_state['search'];
  244. $search->delete();
  245. if ($search->uid && search_api_saved_search_edit_access(user_load($search->uid))) {
  246. $url = 'user/' . $search->uid . '/saved-searches';
  247. }
  248. elseif (!empty($search->options['page'])) {
  249. $url = $search->options['page'];
  250. }
  251. else {
  252. $url = '<front>';
  253. }
  254. drupal_set_message(t('The saved search was successfully deleted.'));
  255. $form_state['redirect'] = $url;
  256. }