search_api_page.module 13 KB

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