search_api_page.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * @file
  4. * Generate search pages using Search API indexes.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function search_api_page_menu() {
  10. $pre = 'admin/config/search/search_api/page';
  11. $items[$pre] = array(
  12. 'title' => 'Search pages',
  13. 'description' => 'Create and configure search pages.',
  14. 'page callback' => 'search_api_page_admin_overview',
  15. 'access arguments' => array('administer search_api'),
  16. 'file' => 'search_api_page.admin.inc',
  17. 'type' => MENU_LOCAL_TASK,
  18. );
  19. $items[$pre . '/add'] = array(
  20. 'title' => 'Add search page',
  21. 'description' => 'Add a new search page.',
  22. 'page callback' => 'drupal_get_form',
  23. 'page arguments' => array('search_api_page_admin_add'),
  24. 'access arguments' => array('administer search_api'),
  25. 'file' => 'search_api_page.admin.inc',
  26. 'type' => MENU_LOCAL_ACTION,
  27. );
  28. $items[$pre . '/%search_api_page'] = array(
  29. 'title' => 'Edit search page',
  30. 'description' => 'Configure or delete a search page.',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('search_api_page_admin_edit', 5),
  33. 'access arguments' => array('administer search_api'),
  34. 'file' => 'search_api_page.admin.inc',
  35. );
  36. // During uninstallation, this would lead to a fatal error otherwise.
  37. if (module_exists('search_api_page')) {
  38. foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
  39. $items[$page->path] = array(
  40. 'title' => $page->name,
  41. 'description' => $page->description ? $page->description : '',
  42. 'page callback' => 'search_api_page_view',
  43. 'page arguments' => array((string) $page->machine_name),
  44. 'access arguments' => array('access search_api_page'),
  45. 'file' => 'search_api_page.pages.inc',
  46. 'type' => MENU_SUGGESTED_ITEM,
  47. );
  48. }
  49. }
  50. return $items;
  51. }
  52. /**
  53. * Implements hook_theme().
  54. */
  55. function search_api_page_theme() {
  56. $themes['search_api_page_results'] = array(
  57. 'variables' => array(
  58. 'index' => NULL,
  59. 'results' => array('result count' => 0),
  60. 'items' => array(),
  61. 'view_mode' => 'search_api_page_result',
  62. 'keys' => '',
  63. ),
  64. 'file' => 'search_api_page.pages.inc',
  65. );
  66. $themes['search_api_page_result'] = array(
  67. 'variables' => array(
  68. 'index' => NULL,
  69. 'result' => NULL,
  70. 'item' => NULL,
  71. 'keys' => '',
  72. ),
  73. 'file' => 'search_api_page.pages.inc',
  74. );
  75. return $themes;
  76. }
  77. /**
  78. * Implements hook_permission().
  79. */
  80. function search_api_page_permission() {
  81. return array(
  82. 'access search_api_page' => array(
  83. 'title' => t('Access search pages'),
  84. 'description' => t('Execute searches using the Search pages module.'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Implements hook_block_info().
  90. */
  91. function search_api_page_block_info() {
  92. $blocks = array();
  93. foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
  94. $blocks[$page->machine_name] = array(
  95. 'info' => t('Search block: !name', array('!name' => $page->name)),
  96. 'cache' => DRUPAL_CACHE_PER_USER,
  97. );
  98. }
  99. return $blocks;
  100. }
  101. /**
  102. * Implements hook_block_view().
  103. */
  104. function search_api_page_block_view($delta) {
  105. $page = search_api_page_load($delta);
  106. if ($page) {
  107. $block = array();
  108. $block['subject'] = t($page->name);
  109. $block['content'] = drupal_get_form('search_api_page_search_form_' . $page->machine_name, $page, NULL, TRUE);
  110. return $block;
  111. }
  112. }
  113. /**
  114. * Implements hook_forms().
  115. */
  116. function search_api_page_forms($form_id, $args) {
  117. $forms = array();
  118. foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
  119. $forms['search_api_page_search_form_' . $page->machine_name] = array(
  120. 'callback' => 'search_api_page_search_form',
  121. 'callback arguments' => array(),
  122. );
  123. }
  124. return $forms;
  125. }
  126. /**
  127. * Implements hook_entity_info().
  128. */
  129. function search_api_page_entity_info() {
  130. $info['search_api_page'] = array(
  131. 'label' => t('Search page'),
  132. 'controller class' => 'EntityAPIControllerExportable',
  133. 'metadata controller class' => FALSE,
  134. 'entity class' => 'Entity',
  135. 'base table' => 'search_api_page',
  136. 'uri callback' => 'search_api_page_url',
  137. 'module' => 'search_api_page',
  138. 'exportable' => TRUE,
  139. 'entity keys' => array(
  140. 'id' => 'id',
  141. 'label' => 'name',
  142. 'name' => 'machine_name',
  143. ),
  144. );
  145. return $info;
  146. }
  147. /**
  148. * Implements hook_entity_property_info().
  149. */
  150. function search_api_page_entity_property_info() {
  151. $info['search_api_page']['properties'] = array(
  152. 'id' => array(
  153. 'label' => t('ID'),
  154. 'type' => 'integer',
  155. 'description' => t('The primary identifier for a search page.'),
  156. 'schema field' => 'id',
  157. 'validation callback' => 'entity_metadata_validate_integer_positive',
  158. ),
  159. 'index_id' => array(
  160. 'label' => t('Index ID'),
  161. 'type' => 'token',
  162. 'description' => t('The machine name of the index this search page uses.'),
  163. 'schema field' => 'index_id',
  164. ),
  165. 'index' => array(
  166. 'label' => t('Index'),
  167. 'type' => 'search_api_index',
  168. 'description' => t('The index this search page uses.'),
  169. 'getter callback' => 'search_api_page_get_index',
  170. ),
  171. 'name' => array(
  172. 'label' => t('Name'),
  173. 'type' => 'text',
  174. 'description' => t('The displayed name for a search page.'),
  175. 'schema field' => 'name',
  176. 'required' => TRUE,
  177. ),
  178. 'machine_name' => array(
  179. 'label' => t('Machine name'),
  180. 'type' => 'token',
  181. 'description' => t('The internally used machine name for a search page.'),
  182. 'schema field' => 'machine_name',
  183. 'required' => TRUE,
  184. ),
  185. 'description' => array(
  186. 'label' => t('Description'),
  187. 'type' => 'text',
  188. 'description' => t('The displayed description for a search page.'),
  189. 'schema field' => 'description',
  190. 'sanitize' => 'filter_xss',
  191. ),
  192. 'enabled' => array(
  193. 'label' => t('Enabled'),
  194. 'type' => 'boolean',
  195. 'description' => t('A flag indicating whether the search page is enabled.'),
  196. 'schema field' => 'enabled',
  197. ),
  198. );
  199. return $info;
  200. }
  201. /**
  202. * Implements hook_search_api_index_update().
  203. */
  204. function search_api_page_search_api_index_update(SearchApiIndex $index) {
  205. if (!$index->enabled && $index->original->enabled) {
  206. foreach (search_api_page_load_multiple(FALSE, array('index_id' => $index->machine_name, 'enabled' => 1)) as $page) {
  207. search_api_page_edit($page->id, array('enabled' => 0));
  208. }
  209. }
  210. }
  211. /**
  212. * Implements hook_search_api_index_delete().
  213. */
  214. function search_api_page_search_api_index_delete(SearchApiIndex $index) {
  215. // Only react on real delete, not revert.
  216. if ($index->hasStatus(ENTITY_IN_CODE)) {
  217. return;
  218. }
  219. foreach (search_api_page_load_multiple(FALSE, array('index_id' => $index->machine_name)) as $page) {
  220. search_api_page_delete($page->id);
  221. }
  222. }
  223. /**
  224. * Implements hook_search_api_page_insert().
  225. *
  226. * Rebuilds the menu table if a search page is created.
  227. */
  228. function search_api_page_search_api_page_insert(Entity $page) {
  229. menu_rebuild();
  230. }
  231. /**
  232. * Implements hook_search_api_page_update().
  233. *
  234. * Rebuilds the menu table if a search page is edited.
  235. */
  236. function search_api_page_search_api_page_update(Entity $page) {
  237. if ($page->enabled != $page->original->enabled || $page->path != $page->original->path) {
  238. menu_rebuild();
  239. }
  240. }
  241. /**
  242. * Implements hook_search_api_page_delete().
  243. *
  244. * Rebuilds the menu table if a search page is removed.
  245. */
  246. function search_api_page_search_api_page_delete(Entity $page) {
  247. menu_rebuild();
  248. }
  249. /**
  250. * Entity URI callback.
  251. */
  252. function search_api_page_url(Entity $page) {
  253. return array('path' => $page->path);
  254. }
  255. /**
  256. * Entity property getter callback.
  257. */
  258. function search_api_page_get_index(Entity $page) {
  259. return search_api_index_load($page->index_id);
  260. }
  261. /**
  262. * Loads a search page.
  263. *
  264. * @param $id
  265. * The page's id or machine name.
  266. * @param $reset
  267. * Whether to reset the internal cache.
  268. *
  269. * @return Entity
  270. * A completely loaded page object, or NULL if no such page exists.
  271. */
  272. function search_api_page_load($id, $reset = FALSE) {
  273. $ret = entity_load_multiple_by_name('search_api_page', array($id), array(), $reset);
  274. return $ret ? reset($ret) : FALSE;
  275. }
  276. /**
  277. * Load multiple search pages at once.
  278. *
  279. * @see entity_load()
  280. *
  281. * @param $ids
  282. * An array of page IDs or machine names, or FALSE to load all pages.
  283. * @param $conditions
  284. * An array of conditions on the {search_api_page} table in the form
  285. * 'field' => $value.
  286. * @param $reset
  287. * Whether to reset the internal entity_load cache.
  288. *
  289. * @return array
  290. * An array of page objects keyed by machine name.
  291. */
  292. function search_api_page_load_multiple($ids = FALSE, array $conditions = array(), $reset = FALSE) {
  293. return entity_load_multiple_by_name('search_api_page', $ids, $conditions, $reset);
  294. }
  295. /**
  296. * Inserts a new search page into the database.
  297. *
  298. * @param array $values
  299. * An array containing the values to be inserted.
  300. *
  301. * @return
  302. * The newly inserted page's id, or FALSE on error.
  303. */
  304. function search_api_page_insert(array $values) {
  305. foreach (array('name', 'machine_name', 'index_id', 'path') as $var) {
  306. if (!isset($values[$var])) {
  307. throw new SearchApiException(t('Property @field has to be set for the new search page.', array('@field' => $var)));
  308. }
  309. }
  310. if (empty($values['description'])) {
  311. $values['description'] = NULL;
  312. }
  313. if (empty($values['options'])) {
  314. $values['options'] = array();
  315. }
  316. $fields = array(
  317. 'name' => $values['name'],
  318. 'machine_name' => $values['machine_name'],
  319. 'description' => $values['description'],
  320. 'enabled' => empty($values['enabled']) ? 0 : 1,
  321. 'index_id' => $values['index_id'],
  322. 'path' => $values['path'],
  323. 'options' => $values['options'],
  324. );
  325. if (isset($values['id'])) {
  326. $fields['id'] = $values['id'];
  327. }
  328. $page = entity_create('search_api_page', $fields);
  329. $page->save();
  330. return $page->id;
  331. }
  332. /**
  333. * Changes a page's settings.
  334. *
  335. * @param $id
  336. * The edited page's ID.
  337. * @param array $fields
  338. * The new field values to set.
  339. *
  340. * @return
  341. * 1 if fields were changed, 0 if the fields already had the desired values.
  342. */
  343. function search_api_page_edit($id, array $fields) {
  344. $page = search_api_page_load($id, TRUE);
  345. $changeable = array('name' => 1, 'description' => 1, 'path' => 1, 'options' => 1, 'enabled' => 1);
  346. foreach ($fields as $field => $value) {
  347. if (isset($changeable[$field]) || $value === $page->$field) {
  348. $page->$field = $value;
  349. $new_values = TRUE;
  350. }
  351. }
  352. // If there are no new values, just return 0.
  353. if (empty($new_values)) {
  354. return 0;
  355. }
  356. $page->save();
  357. return 1;
  358. }
  359. /**
  360. * Deletes a search page.
  361. *
  362. * @param $id
  363. * The ID of the search page to delete.
  364. *
  365. * @return
  366. * TRUE on success, FALSE on failure.
  367. */
  368. function search_api_page_delete($id) {
  369. $page = search_api_page_load($id, TRUE);
  370. if (!$page) {
  371. return FALSE;
  372. }
  373. $page->delete();
  374. menu_rebuild();
  375. return TRUE;
  376. }
  377. /**
  378. * Display a search form.
  379. *
  380. * @param Entity $page
  381. * The search page for which this form is displayed.
  382. * @param $keys
  383. * The search keys.
  384. * @param $compact
  385. * Whether to display a compact form (e.g. for blocks) instead of a normal one.
  386. */
  387. function search_api_page_search_form(array $form, array &$form_state, Entity $page, $keys = NULL, $compact = FALSE) {
  388. $form['keys_' . $page->id] = array(
  389. '#type' => 'textfield',
  390. '#title' => t('Enter your keywords'),
  391. '#title_display' => $compact ? 'invisible' : 'before',
  392. '#default_value' => $keys,
  393. '#size' => $compact ? 15 : 30,
  394. );
  395. $form['base_' . $page->id] = array(
  396. '#type' => 'value',
  397. '#value' => $page->path,
  398. );
  399. $form['id'] = array(
  400. '#type' => 'hidden',
  401. '#value' => $page->id,
  402. );
  403. $form['submit_' . $page->id] = array(
  404. '#type' => 'submit',
  405. '#value' => t('Search'),
  406. );
  407. if (!$compact) {
  408. $form = array(
  409. '#type' => 'fieldset',
  410. '#title' => check_plain($page->name),
  411. 'form' => $form,
  412. );
  413. if ($page->description) {
  414. $form['text']['#markup'] = '<p>' . nl2br(check_plain($page->description)) . '</p>';
  415. $form['text']['#weight'] = -5;
  416. }
  417. }
  418. return $form;
  419. }
  420. /**
  421. * Validation callback for search_api_page_search_form().
  422. */
  423. function search_api_page_search_form_validate(array $form, array &$form_state) {
  424. if (!trim($form_state['values']['keys_' . $form_state['values']['id']])) {
  425. form_set_error('keys_' . $form_state['values']['id'], t('Please enter at least one keyword.'));
  426. }
  427. }
  428. /**
  429. * Submit callback for search_api_page_search_form().
  430. */
  431. function search_api_page_search_form_submit(array $form, array &$form_state) {
  432. $keys = trim($form_state['values']['keys_' . $form_state['values']['id']]);
  433. // @todo Take care of "/"s in the keys
  434. $form_state['redirect'] = $form_state['values']['base_' . $form_state['values']['id']] . '/' . $keys;
  435. }