search_api_saved_searches.admin.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <?php
  2. /**
  3. * @file
  4. * Admin UI functions and form callbacks for saved searches.
  5. */
  6. /**
  7. * Form builder for editing the saved search settings for an index.
  8. *
  9. * @param SearchApiIndex $index
  10. * The index for which to edit settings.
  11. *
  12. * @see search_api_saved_searches_index_edit_validate()
  13. * @see search_api_saved_searches_index_edit_submit()
  14. * @ingroup forms
  15. */
  16. function search_api_saved_searches_index_edit(array $form, array &$form_state, SearchApiIndex $index) {
  17. if (!$index->enabled) {
  18. $form = array(
  19. 'note' => array(
  20. '#markup' => t('Saved searches cannot be enabled for a disabled search index. Enable the index first.'),
  21. ),
  22. );
  23. return $form;
  24. }
  25. $settings = search_api_saved_searches_settings_load_multiple(FALSE, array('index_id' => $index->machine_name));
  26. if (!$settings) {
  27. $delta = $index->machine_name;
  28. if (strlen($delta) > 32) {
  29. $delta = substr($delta, 0, 32);
  30. }
  31. $base = $delta;
  32. for ($i = 1; search_api_saved_searches_settings_load($delta); ++$i) {
  33. $suffix = '_' . $i;
  34. $delta = substr($base, 0, 32 - strlen($suffix)) . $suffix;
  35. }
  36. $settings = entity_create('search_api_saved_searches_settings', array(
  37. 'delta' => $delta,
  38. 'index_id' => $index->machine_name,
  39. 'enabled' => FALSE,
  40. 'options' => array(),
  41. ));
  42. }
  43. else {
  44. $settings = reset($settings);
  45. }
  46. // Set default options.
  47. $settings->options += array(
  48. 'date_field' => NULL,
  49. 'description' => '',
  50. 'default_true' => TRUE,
  51. 'ids_list' => array(),
  52. 'registered_choose_mail' => FALSE,
  53. 'choose_name' => FALSE,
  54. 'registered_user_delete_key' => FALSE,
  55. 'show_empty_list' => FALSE,
  56. 'user_select_interval' => TRUE,
  57. 'interval_options' => array(
  58. 60 * 60 * 24 => t('Daily'),
  59. 60 * 60 * 24 * 7 => t('Weekly'),
  60. ),
  61. 'set_interval' => 60 * 60 * 24,
  62. 'mail' => array(),
  63. 'manual' => array(),
  64. );
  65. $settings->options['mail'] += array(
  66. 'activate' => array(),
  67. 'notify' => array(),
  68. );
  69. $settings->options['mail']['activate'] += array(
  70. 'send' => TRUE,
  71. 'title' => t('Activate your saved search at [site:name]'),
  72. 'body' => t("A saved search on [site:name] with this e-mail address was created.
  73. To activate this saved search, click the following link:
  74. [search-api-saved-search:activate-url]
  75. If you didn't create this saved search, just ignore this mail and it will be deleted.
  76. -- [site:name] team"),
  77. );
  78. $settings->options['mail']['notify'] += array(
  79. 'title' => t('New results for your saved search at [site:name]'),
  80. 'body' => t('[user:name],
  81. There are new results for your saved search on [site:name]:
  82. [search-api-saved-searches:results]
  83. You can configure your saved searches at the following address:
  84. [user:search-api-saved-searches-url]
  85. -- [site:name] team'),
  86. 'results' => t('New results for search "!name_token"', array('!name_token' => '[search-api-saved-search:name]')) .
  87. ":\n[search-api-saved-search:items]\n[search-api-saved-search:results-capped]",
  88. 'result' => '[search-api-saved-search-result:label] ([search-api-saved-search-result:url])',
  89. 'max_results' => 0,
  90. 'results_capped' => "…\n" . t('View all results') . ': [search-api-saved-search:view-url]',
  91. );
  92. $settings->options['manual'] += array(
  93. 'allow' => FALSE,
  94. 'fulltext' => FALSE,
  95. 'fields' => array(),
  96. 'page' => array(),
  97. );
  98. $settings->options['manual']['page'] += array(
  99. 'path' => '',
  100. 'fulltext' => '',
  101. 'direct_filter_params' => FALSE,
  102. );
  103. $form_state['settings'] = $settings;
  104. $options = $settings->options;
  105. $form['#tree'] = TRUE;
  106. $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  107. $form['enabled'] = array(
  108. '#type' => 'checkbox',
  109. '#title' => t('Enable'),
  110. '#description' => t('Enable saved searches for this index.'),
  111. '#default_value' => $settings->enabled,
  112. );
  113. $form['options'] = array(
  114. '#type' => 'fieldset',
  115. '#title' => t('Settings'),
  116. '#states' => array(
  117. 'invisible' => array(
  118. ':input[name="enabled"]' => array('checked' =>FALSE),
  119. ),
  120. ),
  121. );
  122. $form['options']['misc'] = array(
  123. '#type' => 'fieldset',
  124. '#title' => t('Miscellaneous'),
  125. '#collapsible' => TRUE,
  126. '#collapsed' => $settings->enabled,
  127. );
  128. $fields = array();
  129. foreach ($index->getFields() as $key => $field) {
  130. if ($field['type'] == 'date') {
  131. $fields[$key] = t('Determine by @name', array('@name' => $field['name']));
  132. }
  133. }
  134. if ($fields) {
  135. $fields = array(NULL => t('Determine by result ID')) + $fields;
  136. $form['options']['misc']['date_field'] = array(
  137. '#type' => 'select',
  138. '#title' => t('Method for determining new results'),
  139. '#description' => t('The method by which to decide which results are new. "Determine by result ID" will internally save the IDs of all results that were previously found by the user and only report results not already reported. (This might use a lot of memory for large result sets.) The other options check whether the date in the selected field is later than the date of last notification.'),
  140. '#options' => $fields,
  141. '#default_value' => $options['date_field'],
  142. '#parents' => array('options', 'date_field'),
  143. );
  144. }
  145. else {
  146. $form['options'] = array(
  147. '#type' => 'value',
  148. '#value' => NULL,
  149. );
  150. }
  151. $form['options']['misc']['description'] = array(
  152. '#type' => 'textarea',
  153. '#title' => t('Description'),
  154. '#description' => t('Enter a text that will be displayed to users when creating a saved search.'),
  155. '#default_value' => $settings->options['description'],
  156. '#parents' => array('options', 'description'),
  157. );
  158. $search_ids = variable_get('search_api_saved_searches_search_ids', array());
  159. if (empty($search_ids[$index->machine_name]) || count($search_ids[$index->machine_name]) <= 1) {
  160. $form['options']['default_true'] = array(
  161. '#type' => 'value',
  162. '#value' => $options['default_true'],
  163. );
  164. $form['options']['ids_list'] = array(
  165. '#type' => 'value',
  166. '#value' => $options['ids_list'],
  167. );
  168. }
  169. else {
  170. $form['options']['misc']['default_true'] = array(
  171. '#type' => 'select',
  172. '#title' => t('Display for searches'),
  173. '#options' => array(
  174. TRUE => t('For all except the selected'),
  175. FALSE => t('Only for the selected'),
  176. ),
  177. '#default_value' => $options['default_true'],
  178. '#parents' => array('options', 'default_true'),
  179. );
  180. $form['options']['misc']['ids_list'] = array(
  181. '#type' => 'select',
  182. '#title' => t('Search IDs'),
  183. '#options' => $search_ids[$index->machine_name],
  184. '#size' => min(4, count($search_ids[$index->machine_name])),
  185. '#multiple' => TRUE,
  186. '#default_value' => $options['ids_list'],
  187. '#parents' => array('options', 'ids_list'),
  188. );
  189. }
  190. $form['options']['misc']['registered_choose_mail'] = array(
  191. '#type' => 'checkbox',
  192. '#title' => t('Let logged-in users also enter a different mail address'),
  193. '#default_value' => $options['registered_choose_mail'],
  194. '#parents' => array('options', 'registered_choose_mail'),
  195. );
  196. $form['options']['misc']['choose_name'] = array(
  197. '#type' => 'checkbox',
  198. '#title' => t('Customizable search name'),
  199. '#description' => t("Let users modify the saved search's name even when not manually creating it."),
  200. '#default_value' => $options['choose_name'],
  201. '#parents' => array('options', 'choose_name'),
  202. );
  203. $form['options']['misc']['registered_user_delete_key'] = array(
  204. '#type' => 'checkbox',
  205. '#title' => t('Create a delete link for registered users'),
  206. '#description' => t('If enabled, registered users will also get the option to delete their searches via a special link, without logging in.'),
  207. '#default_value' => $options['registered_user_delete_key'],
  208. '#parents' => array('options', 'registered_user_delete_key'),
  209. );
  210. $form['options']['misc']['show_empty_list'] = array(
  211. '#type' => 'checkbox',
  212. '#title' => t('Show empty list of saved searches'),
  213. '#description' => t('If enabled, the "Saved searches" tab for registered users will even be available when no saved searches were created by them yet.'),
  214. '#default_value' => $options['show_empty_list'],
  215. '#parents' => array('options', 'show_empty_list'),
  216. );
  217. // @todo Access options? Maximum number of saved searches per user, maximum
  218. // number of results to create saved search, minimum number of filters?
  219. $form['options']['interval'] = array(
  220. '#type' => 'fieldset',
  221. '#title' => t('Notification interval'),
  222. '#collapsible' => TRUE,
  223. '#collapsed' => $settings->enabled,
  224. );
  225. $form['options']['interval']['user_select_interval'] = array(
  226. '#type' => 'checkbox',
  227. '#title' => t('Let users select the interval'),
  228. '#default_value' => $options['user_select_interval'],
  229. '#parents' => array('options', 'user_select_interval'),
  230. );
  231. $interval_options = array();
  232. foreach ($options['interval_options'] as $k => $v) {
  233. $interval_options[] = "$k | $v";
  234. }
  235. $interval_options = implode("\n", $interval_options);
  236. $form['options']['interval']['interval_options'] = array(
  237. '#type' => 'textarea',
  238. '#title' => t('Interval options'),
  239. '#description' => t('The possible intervals the user can choose from. Enter one value per line, in the format seconds|label. Use a negative value for disabling notifications.'),
  240. '#default_value' => $interval_options,
  241. '#parents' => array('options', 'interval_options'),
  242. '#states' => array(
  243. 'visible' => array(
  244. ':input[name="options[user_select_interval]"]' => array('checked' => TRUE),
  245. ),
  246. ),
  247. );
  248. $interval_options = array(
  249. 60 * 60 * 24 => t('Daily'),
  250. 60 * 60 * 24 * 7 => t('Weekly'),
  251. 0 => t('Custom'),
  252. );
  253. $form['options']['interval']['set_interval'] = array(
  254. '#type' => 'select',
  255. '#title' => t('Fixed notification interval'),
  256. '#options' => $interval_options,
  257. '#default_value' => isset($interval_options[$options['set_interval']]) ? $options['set_interval'] : 0,
  258. '#parents' => array('options', 'set_interval'),
  259. '#states' => array(
  260. 'visible' => array(
  261. ':input[name="options[user_select_interval]"]' => array('checked' => FALSE),
  262. ),
  263. ),
  264. );
  265. $form['options']['interval']['set_interval_custom'] = array(
  266. '#type' => 'textfield',
  267. '#description' => t('Enter the custom notification interval, in seconds. Use a negative value to disable notifications.'),
  268. '#default_value' => $options['set_interval'],
  269. '#parents' => array('options', 'set_interval_custom'),
  270. '#states' => array(
  271. 'visible' => array(
  272. ':input[name="options[user_select_interval]"]' => array('checked' => FALSE),
  273. ':input[name="options[set_interval]"]' => array('value' => 0),
  274. ),
  275. ),
  276. );
  277. $form['options']['mail'] = array(
  278. '#type' => 'fieldset',
  279. '#title' => t('E-mails'),
  280. '#collapsible' => TRUE,
  281. '#collapsed' => $settings->enabled,
  282. );
  283. $form['options']['mail']['activate_send'] = array(
  284. '#type' => 'checkbox',
  285. '#title' => t('Use activation mail for anonymous users'),
  286. '#description' => t("Will need saved searches created by anonymous users, or by normal users with an e-mail address that isn't their own, to be activated by clicking a link in an e-mail."),
  287. '#default_value' => $options['mail']['activate']['send'],
  288. '#parents' => array('options', 'mail', 'activate', 'send'),
  289. );
  290. $form['options']['mail']['activate'] = array(
  291. '#type' => 'fieldset',
  292. '#title' => t('Activation mail'),
  293. '#collapsible' => TRUE,
  294. '#collapsed' => $settings->enabled,
  295. );
  296. $form['options']['mail']['activate']['#states']['visible'][':input[name="options[mail][activate][send]"]']['checked'] = TRUE;
  297. $form['options']['mail']['activate']['title'] = array(
  298. '#type' => 'textfield',
  299. '#title' => t('Subject'),
  300. '#description' => t("Enter the mail's subject.") . ' ' . t('Available variables are: @vars.',
  301. array('@vars' => '[site:name], [user:name], [user:mail], [search-api-saved-search:activate-url], ' .
  302. '[search-api-saved-search:view-url]')),
  303. '#default_value' => $options['mail']['activate']['title'],
  304. '#required' => TRUE,
  305. );
  306. $form['options']['mail']['activate']['body'] = array(
  307. '#type' => 'textarea',
  308. '#title' => t('Body'),
  309. '#description' => t("Enter the mail's body.") . ' ' . t('Available variables are: @vars.',
  310. array('@vars' => '[site:name], [site:url], [user:name], [user:mail], [site:url-brief], ' .
  311. '[search-api-saved-searches:results], [user:search-api-saved-searches-url]')) . ' ' .
  312. t('The replacement for @var can be configured below.', array('@var' => '[search-api-saved-searches:results]')) . '<br />' .
  313. t('Please note: For anonymous users, the [user:*] variables will be empty.'),
  314. '#default_value' => $options['mail']['activate']['body'],
  315. '#rows' => 12,
  316. '#required' => TRUE,
  317. );
  318. $form['options']['mail']['notify'] = array(
  319. '#type' => 'fieldset',
  320. '#title' => t('Notification mails'),
  321. '#collapsible' => TRUE,
  322. '#collapsed' => $settings->enabled,
  323. );
  324. $form['options']['mail']['notify']['title'] = array(
  325. '#type' => 'textfield',
  326. '#title' => t('Subject'),
  327. '#description' => t("Enter the mail's subject.") . ' ' . t('Available variables are: @vars.',
  328. array('@vars' => '[site:name], [user:name], [user:mail], [search-api-saved-search:name], [search-api-saved-search:created]')) . '<br />' .
  329. t('The search-specific variables are taken from the first saved search of the mail. (Alerts from multiple searches are sent at the same time.)'),
  330. '#default_value' => $options['mail']['notify']['title'],
  331. '#required' => TRUE,
  332. );
  333. $form['options']['mail']['notify']['body'] = array(
  334. '#type' => 'textarea',
  335. '#title' => t('Body'),
  336. '#description' => t("Enter the mail's body.") . ' ' . t('Available variables are: @vars.',
  337. array('@vars' => '[site:name], [site:url], [user:name], [user:mail], [site:url-brief], ' .
  338. '[search-api-saved-searches:results], [user:search-api-saved-searches-url], ' .
  339. '[search-api-saved-search:name], [search-api-saved-search:created], ' .
  340. '[search-api-saved-search:results-capped], [search-api-saved-search:view-url], ' .
  341. '[search-api-saved-search:edit-url], [search-api-saved-search:delete-url]')) . ' ' .
  342. t('The replacement for @var can be configured below.', array('@var' => '[search-api-saved-searches:results]')) . '<br />' .
  343. t('The search-specific variables are taken from the first saved search of the mail. (Alerts from multiple searches are sent at the same time.)') . '<br />' .
  344. t('Please note: For anonymous users, the [user:*] variables will be empty.'),
  345. '#default_value' => $options['mail']['notify']['body'],
  346. '#rows' => 12,
  347. '#required' => TRUE,
  348. );
  349. $form['options']['mail']['notify']['results'] = array(
  350. '#type' => 'textarea',
  351. '#title' => t('Results replacement'),
  352. '#description' => t('Enter the text that will replace the [search-api-saved-searches:results] variable above. If there are new results for several saved searches of a single user, this text will be inserted multiple times.') . ' ' .
  353. t('Available variables are: @vars.',
  354. array('@vars' => '[site:url], [search-api-saved-search:name], [search-api-saved-search:created], ' .
  355. '[search-api-saved-search:items], [search-api-saved-search:results-capped], [search-api-saved-search:view-url], ' .
  356. '[search-api-saved-search:edit-url], [search-api-saved-search:delete-url]')) . ' ' .
  357. t('The replacement for @var can be configured below.', array('@var' => '[search-api-saved-search:items]')),
  358. '#default_value' => $options['mail']['notify']['results'],
  359. '#rows' => 5,
  360. '#required' => TRUE,
  361. );
  362. $form['options']['mail']['notify']['result'] = array(
  363. '#type' => 'textarea',
  364. '#title' => t('Result item replacement'),
  365. '#description' => t('Enter the text that will replace the [search-api-saved-search:items] variable above. This text will be inserted on a new line once for every new result.') . ' ' .
  366. t('Available variables are: @vars.',
  367. array('@vars' => '[site:url], [search-api-saved-search-result:label], [search-api-saved-search-result:url], ' .
  368. '[search-api-saved-search:view-url], [search-api-saved-search:edit-url], [search-api-saved-search:delete-url]')),
  369. '#default_value' => $options['mail']['notify']['result'],
  370. '#rows' => 2,
  371. '#required' => TRUE,
  372. );
  373. $tokens = token_info();
  374. $type = $index->item_type;
  375. if (isset($tokens['types'][$type])) {
  376. $form['options']['mail']['notify']['result']['#description'] .= ' ' . t('You can also use the @varstyle variables.',
  377. array('@varstyle' => "[$type:*]"));
  378. }
  379. $form['options']['mail']['notify']['max_results'] = array(
  380. '#type' => 'textfield',
  381. '#title' => t('Maximum number of results'),
  382. '#description' => t('Maximum number of results per search that will be included in the mail. Specify an integer greater than 0 to limit the results.'),
  383. '#default_value' => $options['mail']['notify']['max_results'],
  384. '#element_validate' => array('element_validate_integer'),
  385. );
  386. $form['options']['mail']['notify']['results_capped'] = array(
  387. '#type' => 'textarea',
  388. '#title' => t('Show text if not all results are displayed'),
  389. '#description' => t('Enter the text that will replace the [search-api-saved-search:results-capped] variable above. This text will only be inserted for searches in which the number of new results exceeds the set maximum.') . ' ' .
  390. t('Available variables are: @vars.',
  391. array('@vars' => '[site:url], [search-api-saved-search:name], [search-api-saved-search:created], ' .
  392. '[search-api-saved-search:items], [search-api-saved-search:view-url], ' .
  393. '[search-api-saved-search:edit-url], [search-api-saved-search:delete-url]')),
  394. '#default_value' => $options['mail']['notify']['results_capped'],
  395. '#rows' => 4,
  396. '#states' => array(
  397. 'invisible' => array(
  398. ':input[name="options[mail][notify][max_results]"]' => array('value' => 0),
  399. ),
  400. ),
  401. );
  402. $form['options']['manual'] = array(
  403. '#type' => 'fieldset',
  404. '#title' => t('Manual saved search creation'),
  405. '#description' => t('This provides a form to users where they can create a saved search manually, without first executing a search.'),
  406. '#collapsible' => TRUE,
  407. '#collapsed' => $settings->enabled,
  408. );
  409. $form['options']['manual']['allow'] = array(
  410. '#type' => 'checkbox',
  411. '#title' => t('Allow manual creation of saved searches'),
  412. '#default_value' => $options['manual']['allow'],
  413. );
  414. $form['options']['manual']['fulltext'] = array(
  415. '#type' => 'checkbox',
  416. '#title' => t('Provide textfield for fulltext keywords'),
  417. '#default_value' => $options['manual']['fulltext'],
  418. );
  419. if (!empty($index->options['fields'])) {
  420. $fields = array();
  421. foreach ($index->getFields() as $field => $info) {
  422. $fields[$field] = $info['name'];
  423. }
  424. }
  425. if (!empty($fields)) {
  426. $form['options']['manual']['fields'] = array(
  427. '#type' => 'checkboxes',
  428. '#title' => t('Fields for which to provide filter options'),
  429. '#options' => $fields,
  430. '#attributes' => array(
  431. 'class' => array('search-api-checkboxes-list'),
  432. ),
  433. '#default_value' => drupal_map_assoc($options['manual']['fields']),
  434. );
  435. }
  436. $form['options']['manual']['page'] = array(
  437. '#type' => 'fieldset',
  438. '#title' => t('Search page'),
  439. '#description' => t('Specify the page where search results can be viewed.'),
  440. '#collapsible' => TRUE,
  441. );
  442. $form['options']['manual']['page']['path'] = array(
  443. '#type' => 'textfield',
  444. '#title' => t('Base path'),
  445. '#description' => t('Base path for a search without any of the restrictions that can be applied. ' .
  446. 'If this is not specified, no link to a search page will be provided to users for manually created searches.'),
  447. '#default_value' => $options['manual']['page']['path'],
  448. );
  449. $form['options']['manual']['page']['fulltext'] = array(
  450. '#type' => 'textfield',
  451. '#title' => t('Fulltext URL parameter'),
  452. '#description' => t('Query parameter used for passing the fulltext keywords in the URL. ' .
  453. 'If left empty, the fulltext keywords (if present at all) will be appended to the path, instead of being passed as a parameter.'),
  454. '#default_value' => $options['manual']['page']['fulltext'],
  455. );
  456. $form['options']['manual']['page']['direct_filter_params'] = array(
  457. '#type' => 'select',
  458. '#title' => t('Method for passing filter values in the URL'),
  459. '#options' => array(
  460. FALSE => t('In filter[*], as used by facets'),
  461. TRUE => t('As direct URL parameters'),
  462. ),
  463. '#default_value' => $options['manual']['page']['direct_filter_params'],
  464. );
  465. foreach (array('fulltext', 'fields', 'page') as $v) {
  466. $form['options']['manual'][$v]['#states']['visible'][':input[name="options[manual][allow]"]']['checked'] = TRUE;
  467. }
  468. foreach (array('fulltext', 'direct_filter_params') as $v) {
  469. $form['options']['manual']['page'][$v]['#states']['visible'][':input[name="options[manual][page][path]"]']['empty'] = FALSE;
  470. }
  471. $form['submit'] = array(
  472. '#type' => 'submit',
  473. '#value' => t('Save settings'),
  474. );
  475. return $form;
  476. }
  477. /**
  478. * Form validation handler for search_api_saved_searches_index_edit().
  479. *
  480. * @see search_api_saved_searches_index_edit()
  481. * @see search_api_saved_searches_index_edit_submit()
  482. */
  483. function search_api_saved_searches_index_edit_validate(array $form, array &$form_state) {
  484. $values = &$form_state['values'];
  485. $interval_options = $values['options']['interval_options'];
  486. $values['options']['interval_options'] = array();
  487. foreach (explode("\n", $interval_options) as $line) {
  488. $parts = explode('|', $line, 2);
  489. if (count($parts) == 1) {
  490. $k = $v = trim($line);
  491. }
  492. else {
  493. list($k, $v) = array_map('trim', $parts);
  494. }
  495. $values['options']['interval_options'][$k] = $v;
  496. }
  497. if ($values['options']['user_select_interval']) {
  498. foreach ($values['options']['interval_options'] as $k => $v) {
  499. if (!preg_match('/^-?\d+$/', $k)) {
  500. form_error($form['options']['interval_options'], t('"@input" is not a valid integer. Only integers can be used as intervals.', array('@input' => $k)));
  501. }
  502. }
  503. }
  504. elseif ($values['options']['set_interval'] == 0) {
  505. $v = $values['options']['set_interval_custom'];
  506. if (!preg_match('/^-?\d+$/', $v)) {
  507. form_error($form['options']['set_interval_custom'], t('"@input" is not a valid integer. Only integers can be used as intervals.', array('@input' => $v)));
  508. }
  509. }
  510. }
  511. /**
  512. * Form submission handler for search_api_saved_searches_index_edit().
  513. *
  514. * @see search_api_saved_searches_index_edit()
  515. * @see search_api_saved_searches_index_edit_vaidate()
  516. */
  517. function search_api_saved_searches_index_edit_submit(array $form, array &$form_state) {
  518. $values = $form_state['values'];
  519. $settings = $form_state['settings'];
  520. $options = $values['options'];
  521. if ($options['set_interval'] == 0) {
  522. $options['set_interval'] = $options['set_interval_custom'];
  523. }
  524. unset($options['set_interval_custom']);
  525. $options['manual']['fields'] = array_keys(array_filter($options['manual']['fields']));
  526. $settings->enabled = $values['enabled'];
  527. $settings->options = $options;
  528. $ret = $settings->save();
  529. if ($ret === FALSE) {
  530. drupal_set_message('An error occurred while trying to save the settings. Check the logs for details.', 'error');
  531. $form_state['rebuild'] = TRUE;
  532. }
  533. else {
  534. drupal_set_message('The settings were successfully saved.');
  535. }
  536. }