search_api_page.module 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. $ret['results']['#theme'] = 'materiobase_results';
  47. $ret['results']['#index'] = search_api_index_load($page->index_id);
  48. $ret['results']['#results'] = $results;
  49. $ret['results']['#view_mode'] = isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result';
  50. $ret['results']['#keys'] = $keys;
  51. $ret['results']['#page_machine_name'] = $page->machine_name;
  52. return $items;
  53. }
  54. /**
  55. * Implements hook_theme().
  56. */
  57. function search_api_page_theme() {
  58. $themes['search_api_page_results'] = array(
  59. 'variables' => array(
  60. 'index' => NULL,
  61. 'results' => array('result count' => 0),
  62. 'items' => array(),
  63. 'view_mode' => 'search_api_page_result',
  64. 'keys' => '',
  65. 'page_machine_name' => NULL,
  66. 'spellcheck' => NULL,
  67. 'pager' => NULL,
  68. ),
  69. 'file' => 'search_api_page.pages.inc',
  70. 'template' => 'search-api-page-results',
  71. );
  72. $themes['search_api_page_result'] = array(
  73. 'variables' => array(
  74. 'index' => NULL,
  75. 'result' => NULL,
  76. 'item' => NULL,
  77. 'keys' => '',
  78. 'list_classes' => '',
  79. ),
  80. 'file' => 'search_api_page.pages.inc',
  81. 'template' => 'search-api-page-result',
  82. );
  83. $themes['search_performance'] = array(
  84. 'render element' => 'element',
  85. );
  86. $themes['search_results_list'] = array(
  87. 'render element' => 'element',
  88. );
  89. return $themes;
  90. }
  91. /**
  92. * Implements theme for rendering search-performance
  93. */
  94. function theme_search_performance($variables) {
  95. $element = array_shift($variables);
  96. return $element['#markup'];
  97. }
  98. /**
  99. * Returns HTML for a list of search results.
  100. * Taken from theme_item_list().
  101. *
  102. * @param $variables
  103. * An associative array containing:
  104. * - items: An array of items to be displayed in the list. If an item is a
  105. * string, then it is used as is. If an item is an array, then the "data"
  106. * element of the array is used as the contents of the list item. If an item
  107. * is an array with a "children" element, those children are displayed in a
  108. * nested list. All other elements are treated as attributes of the list
  109. * item element.
  110. * - type: The type of list to return (e.g. "ul", "ol").
  111. * - attributes: The attributes applied to the list element.
  112. */
  113. function theme_search_results_list($variables) {
  114. // Pull Element array from the $variables array.
  115. $variables = $variables['element'];
  116. $items = $variables['items']; // Full data
  117. $type = $variables['type'];
  118. // CSS classes for ul
  119. $attributes = (!empty($variables['attributes'])) ? $variables['attributes'] : array();
  120. $attributes['class'] = array_merge(
  121. array('item-list', 'search-results-list'),
  122. (isset($attributes['class'])) ? $attributes['class'] : array()
  123. );
  124. // Render items within a list
  125. if (!empty($items)) {
  126. $output = "<$type" . drupal_attributes($attributes) . '>';
  127. $num_items = count($items);
  128. // Parse search results as tokens to access items with full data.
  129. $i = 0;
  130. foreach ($variables['results'] as $result) {
  131. // Set css classes.
  132. $item_attributes = array();
  133. if ($i == 0) {
  134. $item_attributes['class'][] = 'first';
  135. }
  136. if ($i == $num_items - 1) {
  137. $item_attributes['class'][] = 'last';
  138. }
  139. (($i+1)%2) ? $item_attributes['class'][] = 'odd': $item_attributes['class'][] = 'even';
  140. // Define render array.
  141. $data = theme(
  142. 'search_api_page_result', array(
  143. 'index' => $variables['index'], // Use full results index.
  144. 'result' => $result,
  145. 'item' => isset($items[$result['id']]) ?
  146. $items[$result['id']] :
  147. NULL,
  148. 'keys' => $variables['keys'],
  149. 'list_classes' => drupal_attributes($item_attributes),
  150. )
  151. );
  152. $output .= $data . "\n";
  153. $i++;
  154. }
  155. $output .= "</$type>";
  156. return $output;
  157. }
  158. }
  159. /**
  160. * Implements hook_permission().
  161. */
  162. function search_api_page_permission() {
  163. return array(
  164. 'access search_api_page' => array(
  165. 'title' => t('Access search pages'),
  166. 'description' => t('Execute searches using the Search pages module.'),
  167. ),
  168. );
  169. }
  170. /**
  171. * Implements hook_block_info().
  172. */
  173. function search_api_page_block_info() {
  174. $blocks = array();
  175. foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
  176. $blocks[$page->machine_name] = array(
  177. 'info' => t('Search block: !name', array('!name' => $page->name)),
  178. );
  179. }
  180. return $blocks;
  181. }
  182. /**
  183. * Implements hook_block_view().
  184. */
  185. function search_api_page_block_view($delta) {
  186. $page = search_api_page_load($delta);
  187. if ($page) {
  188. $block = array();
  189. $block['subject'] = t($page->name);
  190. $block['content'] = drupal_get_form('search_api_page_search_form_' . $page->machine_name, $page, NULL, TRUE);
  191. return $block;
  192. }
  193. }
  194. /**
  195. * Implements hook_forms().
  196. */
  197. function search_api_page_forms($form_id, $args) {
  198. $forms = array();
  199. foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
  200. $forms['search_api_page_search_form_' . $page->machine_name] = array(
  201. 'callback' => 'search_api_page_search_form',
  202. 'callback arguments' => array(),
  203. );
  204. }
  205. return $forms;
  206. }
  207. /**
  208. * Implements hook_entity_info().
  209. */
  210. function search_api_page_entity_info() {
  211. $info['search_api_page'] = array(
  212. 'label' => t('Search page'),
  213. 'controller class' => 'EntityAPIControllerExportable',
  214. 'metadata controller class' => FALSE,
  215. 'entity class' => 'Entity',
  216. 'base table' => 'search_api_page',
  217. 'uri callback' => 'search_api_page_url',
  218. 'module' => 'search_api_page',
  219. 'exportable' => TRUE,
  220. 'entity keys' => array(
  221. 'id' => 'id',
  222. 'label' => 'name',
  223. 'name' => 'machine_name',
  224. ),
  225. );
  226. return $info;
  227. }
  228. /**
  229. * Implements hook_entity_property_info().
  230. */
  231. function search_api_page_entity_property_info() {
  232. $info['search_api_page']['properties'] = array(
  233. 'id' => array(
  234. 'label' => t('ID'),
  235. 'type' => 'integer',
  236. 'description' => t('The primary identifier for a search page.'),
  237. 'schema field' => 'id',
  238. 'validation callback' => 'entity_metadata_validate_integer_positive',
  239. ),
  240. 'index_id' => array(
  241. 'label' => t('Index ID'),
  242. 'type' => 'token',
  243. 'description' => t('The machine name of the index this search page uses.'),
  244. 'schema field' => 'index_id',
  245. ),
  246. 'index' => array(
  247. 'label' => t('Index'),
  248. 'type' => 'search_api_index',
  249. 'description' => t('The index this search page uses.'),
  250. 'getter callback' => 'search_api_page_get_index',
  251. ),
  252. 'name' => array(
  253. 'label' => t('Name'),
  254. 'type' => 'text',
  255. 'description' => t('The displayed name for a search page.'),
  256. 'schema field' => 'name',
  257. 'required' => TRUE,
  258. ),
  259. 'machine_name' => array(
  260. 'label' => t('Machine name'),
  261. 'type' => 'token',
  262. 'description' => t('The internally used machine name for a search page.'),
  263. 'schema field' => 'machine_name',
  264. 'required' => TRUE,
  265. ),
  266. 'description' => array(
  267. 'label' => t('Description'),
  268. 'type' => 'text',
  269. 'description' => t('The displayed description for a search page.'),
  270. 'schema field' => 'description',
  271. 'sanitize' => 'filter_xss',
  272. ),
  273. 'enabled' => array(
  274. 'label' => t('Enabled'),
  275. 'type' => 'boolean',
  276. 'description' => t('A flag indicating whether the search page is enabled.'),
  277. 'schema field' => 'enabled',
  278. ),
  279. );
  280. return $info;
  281. }
  282. /**
  283. * Implements hook_search_api_index_update().
  284. */
  285. function search_api_page_search_api_index_update(SearchApiIndex $index) {
  286. if (!$index->enabled && $index->original->enabled) {
  287. foreach (search_api_page_load_multiple(FALSE, array('index_id' => $index->machine_name, 'enabled' => 1)) as $page) {
  288. search_api_page_edit($page->id, array('enabled' => 0));
  289. }
  290. }
  291. }
  292. /**
  293. * Implements hook_search_api_index_delete().
  294. */
  295. function search_api_page_search_api_index_delete(SearchApiIndex $index) {
  296. // Only react on real delete, not revert.
  297. if ($index->hasStatus(ENTITY_IN_CODE)) {
  298. return;
  299. }
  300. foreach (search_api_page_load_multiple(FALSE, array('index_id' => $index->machine_name)) as $page) {
  301. search_api_page_delete($page->id);
  302. }
  303. }
  304. /**
  305. * Implements hook_search_api_page_insert().
  306. *
  307. * Rebuilds the menu table if a search page is created.
  308. */
  309. function search_api_page_search_api_page_insert(Entity $page) {
  310. menu_rebuild();
  311. }
  312. /**
  313. * Implements hook_search_api_page_update().
  314. *
  315. * Rebuilds the menu table if a search page is edited.
  316. */
  317. function search_api_page_search_api_page_update(Entity $page) {
  318. if ($page->enabled != $page->original->enabled || $page->path != $page->original->path) {
  319. menu_rebuild();
  320. }
  321. }
  322. /**
  323. * Implements hook_search_api_page_delete().
  324. *
  325. * Rebuilds the menu table if a search page is removed.
  326. */
  327. function search_api_page_search_api_page_delete(Entity $page) {
  328. menu_rebuild();
  329. }
  330. /**
  331. * Entity URI callback.
  332. */
  333. function search_api_page_url(Entity $page) {
  334. return array('path' => $page->path);
  335. }
  336. /**
  337. * Entity property getter callback.
  338. */
  339. function search_api_page_get_index(Entity $page) {
  340. return search_api_index_load($page->index_id);
  341. }
  342. /**
  343. * Loads a search page.
  344. *
  345. * @param $id
  346. * The page's id or machine name.
  347. * @param $reset
  348. * Whether to reset the internal cache.
  349. *
  350. * @return Entity
  351. * A completely loaded page object, or NULL if no such page exists.
  352. */
  353. function search_api_page_load($id, $reset = FALSE) {
  354. $ret = entity_load_multiple_by_name('search_api_page', array($id), array(), $reset);
  355. return $ret ? reset($ret) : FALSE;
  356. }
  357. /**
  358. * Load multiple search pages at once.
  359. *
  360. * @see entity_load()
  361. *
  362. * @param $ids
  363. * An array of page IDs or machine names, or FALSE to load all pages.
  364. * @param $conditions
  365. * An array of conditions on the {search_api_page} table in the form
  366. * 'field' => $value.
  367. * @param $reset
  368. * Whether to reset the internal entity_load cache.
  369. *
  370. * @return array
  371. * An array of page objects keyed by machine name.
  372. */
  373. function search_api_page_load_multiple($ids = FALSE, array $conditions = array(), $reset = FALSE) {
  374. return entity_load_multiple_by_name('search_api_page', $ids, $conditions, $reset);
  375. }
  376. /**
  377. * Inserts a new search page into the database.
  378. *
  379. * @param array $values
  380. * An array containing the values to be inserted.
  381. *
  382. * @return
  383. * The newly inserted page's id, or FALSE on error.
  384. */
  385. function search_api_page_insert(array $values) {
  386. foreach (array('name', 'machine_name', 'index_id', 'path') as $var) {
  387. if (!isset($values[$var])) {
  388. throw new SearchApiException(t('Property @field has to be set for the new search page.', array('@field' => $var)));
  389. }
  390. }
  391. if (empty($values['description'])) {
  392. $values['description'] = NULL;
  393. }
  394. if (empty($values['options'])) {
  395. $values['options'] = array();
  396. }
  397. $fields = array(
  398. 'name' => $values['name'],
  399. 'machine_name' => $values['machine_name'],
  400. 'description' => $values['description'],
  401. 'enabled' => empty($values['enabled']) ? 0 : 1,
  402. 'index_id' => $values['index_id'],
  403. 'path' => $values['path'],
  404. 'options' => $values['options'],
  405. );
  406. if (isset($values['id'])) {
  407. $fields['id'] = $values['id'];
  408. }
  409. $page = entity_create('search_api_page', $fields);
  410. $page->save();
  411. return $page->id;
  412. }
  413. /**
  414. * Changes a page's settings.
  415. *
  416. * @param $id
  417. * The edited page's ID.
  418. * @param array $fields
  419. * The new field values to set.
  420. *
  421. * @return
  422. * 1 if fields were changed, 0 if the fields already had the desired values.
  423. */
  424. function search_api_page_edit($id, array $fields) {
  425. $page = search_api_page_load($id, TRUE);
  426. $changeable = array('name' => 1, 'description' => 1, 'path' => 1, 'options' => 1, 'enabled' => 1, 'result_page_search_form'=>1);
  427. foreach ($fields as $field => $value) {
  428. if (isset($changeable[$field]) || $value === $page->$field) {
  429. $page->$field = $value;
  430. $new_values = TRUE;
  431. }
  432. }
  433. // If there are no new values, just return 0.
  434. if (empty($new_values)) {
  435. return 0;
  436. }
  437. $page->save();
  438. return 1;
  439. }
  440. /**
  441. * Deletes a search page.
  442. *
  443. * @param $id
  444. * The ID of the search page to delete.
  445. *
  446. * @return
  447. * TRUE on success, FALSE on failure.
  448. */
  449. function search_api_page_delete($id) {
  450. $page = search_api_page_load($id, TRUE);
  451. if (!$page) {
  452. return FALSE;
  453. }
  454. $page->delete();
  455. menu_rebuild();
  456. return TRUE;
  457. }
  458. /**
  459. * Display a search form.
  460. *
  461. * @param Entity $page
  462. * The search page for which this form is displayed.
  463. * @param $keys
  464. * The search keys.
  465. * @param $compact
  466. * Whether to display a compact form (e.g. for blocks) instead of a normal one.
  467. */
  468. function search_api_page_search_form(array $form, array &$form_state, Entity $page, $keys = NULL, $compact = FALSE) {
  469. $form['keys_' . $page->id] = array(
  470. '#type' => 'textfield',
  471. '#title' => t('Enter your keywords'),
  472. '#title_display' => $compact ? 'invisible' : 'before',
  473. '#default_value' => $keys,
  474. '#size' => $compact ? 15 : 30,
  475. );
  476. $form['base_' . $page->id] = array(
  477. '#type' => 'value',
  478. '#value' => $page->path,
  479. );
  480. $form['id'] = array(
  481. '#type' => 'hidden',
  482. '#value' => $page->id,
  483. );
  484. $form['submit_' . $page->id] = array(
  485. '#type' => 'submit',
  486. '#value' => t('Search'),
  487. );
  488. if (!$compact) {
  489. $form = array(
  490. '#type' => 'fieldset',
  491. '#title' => $page->name,
  492. 'form' => $form,
  493. );
  494. if ($page->description) {
  495. $form['text']['#markup'] = '<p>' . nl2br(check_plain($page->description)) . '</p>';
  496. $form['text']['#weight'] = -5;
  497. }
  498. }
  499. return $form;
  500. }
  501. /**
  502. * Validation callback for search_api_page_search_form().
  503. */
  504. function search_api_page_search_form_validate(array $form, array &$form_state) {
  505. if (!trim($form_state['values']['keys_' . $form_state['values']['id']])) {
  506. form_set_error('keys_' . $form_state['values']['id'], t('Please enter at least one keyword.'));
  507. }
  508. }
  509. /**
  510. * Submit callback for search_api_page_search_form().
  511. */
  512. function search_api_page_search_form_submit(array $form, array &$form_state) {
  513. $keys = trim($form_state['values']['keys_' . $form_state['values']['id']]);
  514. // @todo Take care of "/"s in the keys
  515. $form_state['redirect'] = $form_state['values']['base_' . $form_state['values']['id']] . '/' . $keys;
  516. }