search.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @file
  4. * Handle the 'node view' override task.
  5. *
  6. * This plugin overrides node/%node and reroutes it to the page manager, where
  7. * a list of tasks can be used to service this request based upon criteria
  8. * supplied by access plugins.
  9. */
  10. /**
  11. * Specialized implementation of hook_page_manager_task_tasks().
  12. *
  13. * See api-task.html for more information.
  14. */
  15. function page_manager_search_page_manager_tasks() {
  16. if (!module_exists('search')) {
  17. return;
  18. }
  19. return array(
  20. // This is a 'page' task and will fall under the page admin UI.
  21. 'task type' => 'page',
  22. 'title' => t('Search'),
  23. // There are multiple search pages, let's override each of them
  24. // separately.
  25. 'subtasks' => TRUE,
  26. 'subtask callback' => 'page_manager_search_subtask',
  27. 'subtasks callback' => 'page_manager_search_subtasks',
  28. // Menu hooks so that we can alter the node/%node menu entry to point to us.
  29. 'hook menu alter' => 'page_manager_search_menu_alter',
  30. // This is task uses 'context' handlers and must implement these to give the
  31. // handler data it needs.
  32. 'handler type' => 'context',
  33. 'get arguments' => 'page_manager_search_get_arguments',
  34. 'get context placeholders' => 'page_manager_search_get_contexts',
  35. 'access callback' => 'page_manager_search_access_check',
  36. );
  37. }
  38. /**
  39. * Callback defined by page_manager_search_page_manager_tasks().
  40. *
  41. * Alter the search tabs to work with page manager. The search flow is
  42. * quite odd, and tracing through the code takes hours to realize
  43. * that the tab you click on does not normally actually handle
  44. * the search. This tries to account for that.
  45. *
  46. * Note to module authors: This tends to work a lot better with modules
  47. * that override their own search pages if their _alter runs *before*
  48. * this one.
  49. */
  50. function page_manager_search_menu_alter(&$items, $task) {
  51. // We are creating two sets of tabs. One set is for searching without
  52. // keywords. A second set is for searching *with* keywords. This
  53. // is necessary because search/node/% and search/node need to be
  54. // different due to the way the search menu items function.
  55. $default_info = search_get_default_module_info();
  56. if (empty($default_info)) {
  57. // Nothing to do.
  58. return;
  59. }
  60. // Go through each search module item.
  61. foreach (search_get_info() as $module => $info) {
  62. if (variable_get('page_manager_search_disabled_' . $module, TRUE)) {
  63. continue;
  64. }
  65. $path = 'search/' . $info['path'];
  66. $callback = $items["$path/%menu_tail"]['page callback'];
  67. if ($callback == 'search_view' || variable_get('page_manager_override_anyway', FALSE)) {
  68. $items["$path"]['page callback'] = 'page_manager_search_page';
  69. $items["$path"]['file path'] = $task['path'];
  70. $items["$path"]['file'] = $task['file'];
  71. $items["$path/%menu_tail"]['page callback'] = 'page_manager_search_page';
  72. $items["$path/%menu_tail"]['file path'] = $task['path'];
  73. $items["$path/%menu_tail"]['file'] = $task['file'];
  74. }
  75. else {
  76. // Automatically disable this task if it cannot be enabled.
  77. variable_set('page_manager_search_disabled_' . $module, TRUE);
  78. if (!empty($GLOBALS['page_manager_enabling_search'])) {
  79. drupal_set_message(t('Page manager module is unable to enable @path because some other module already has overridden with %callback.', array('%callback' => $callback, '@path' => $path)), 'error');
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * Entry point for our overridden search page.
  86. */
  87. function page_manager_search_page($type) {
  88. ctools_include('menu');
  89. // Get the arguments and construct a keys string out of them.
  90. $args = func_get_args();
  91. // We have to remove the $type.
  92. array_shift($args);
  93. // And implode() it all back together.
  94. $keys = $args ? implode('/', $args) : '';
  95. // Allow other modules to alter the search keys.
  96. drupal_alter(array('search_keys', 'search_' . $type . '_keys'), $keys);
  97. // Load my task plugin.
  98. $task = page_manager_get_task('search');
  99. $subtask = page_manager_get_task_subtask($task, $type);
  100. // Load the node into a context.
  101. ctools_include('context');
  102. ctools_include('context-task-handler');
  103. $contexts = ctools_context_handler_get_task_contexts($task, $subtask, array($keys));
  104. $output = ctools_context_handler_render($task, $subtask, $contexts, array($keys));
  105. if ($output !== FALSE) {
  106. return $output;
  107. }
  108. $function = 'search_view';
  109. foreach (module_implements('page_manager_override') as $module) {
  110. $call = $module . '_page_manager_override';
  111. if (($rc = $call('search')) && function_exists($rc)) {
  112. $function = $rc;
  113. break;
  114. }
  115. }
  116. // Otherwise, fall back.
  117. // Put the $type back on the arguments.
  118. module_load_include('inc', 'search', 'search.pages');
  119. array_unshift($args, $type);
  120. return call_user_func_array($function, $args);
  121. }
  122. /**
  123. * Callback to get arguments provided by this task handler.
  124. *
  125. * Since this is the node view and there is no UI on the arguments, we
  126. * create dummy arguments that contain the needed data.
  127. */
  128. function page_manager_search_get_arguments($task, $subtask_id) {
  129. return array(
  130. array(
  131. 'keyword' => 'keywords',
  132. 'identifier' => t('Keywords'),
  133. 'id' => 1,
  134. 'name' => 'string',
  135. 'settings' => array('use_tail' => TRUE),
  136. ),
  137. );
  138. }
  139. /**
  140. * Callback to get context placeholders provided by this handler.
  141. */
  142. function page_manager_search_get_contexts($task, $subtask_id) {
  143. return ctools_context_get_placeholders_from_argument(page_manager_search_get_arguments($task, $subtask_id));
  144. }
  145. /**
  146. * Callback to enable/disable the page from the UI.
  147. */
  148. function page_manager_search_enable($cache, $status) {
  149. variable_set('page_manager_search_disabled_' . $cache->subtask_id, $status);
  150. // Set a global flag so that the menu routine knows it needs
  151. // to set a message if enabling cannot be done.
  152. if (!$status) {
  153. $GLOBALS['page_manager_enabling_search'] = TRUE;
  154. }
  155. }
  156. /**
  157. * Task callback to get all subtasks.
  158. *
  159. * Return a list of all subtasks.
  160. */
  161. function page_manager_search_subtasks($task) {
  162. $return = array();
  163. foreach (search_get_info() as $module => $info) {
  164. if ($info['path']) {
  165. // We don't pass the $info because the subtask build could be called
  166. // singly without the $info when just the subtask needs to be built.
  167. $return[$module] = page_manager_search_build_subtask($task, $module);
  168. }
  169. }
  170. return $return;
  171. }
  172. /**
  173. * Callback to return a single subtask.
  174. */
  175. function page_manager_search_subtask($task, $subtask_id) {
  176. return page_manager_search_build_subtask($task, $subtask_id);
  177. }
  178. /**
  179. * Build a subtask array for a given page.
  180. */
  181. function page_manager_search_build_subtask($task, $module) {
  182. $search_info = search_get_info();
  183. $info = $search_info[$module];
  184. $path = 'search/' . $info['path'];
  185. $subtask = array(
  186. 'name' => $module,
  187. 'admin title' => $info['title'],
  188. 'admin path' => "$path/!keywords",
  189. 'admin description' => t('Search @type', array('@type' => $info['title'])),
  190. 'admin type' => t('System'),
  191. 'row class' => empty($page->disabled) ? 'page-manager-enabled' : 'page-manager-disabled',
  192. 'storage' => t('In code'),
  193. 'disabled' => variable_get('page_manager_search_disabled_' . $module, TRUE),
  194. // This works for both enable AND disable.
  195. 'enable callback' => 'page_manager_search_enable',
  196. );
  197. return $subtask;
  198. }
  199. /**
  200. * Callback to determine if a page is accessible.
  201. *
  202. * @param $task
  203. * The task plugin.
  204. * @param $subtask_id
  205. * The subtask id
  206. * @param $contexts
  207. * The contexts loaded for the task.
  208. *
  209. * @return
  210. * TRUE if the current user can access the page.
  211. */
  212. function page_manager_search_access_check($task, $subtask_id, $contexts) {
  213. $context = reset($contexts);
  214. return _search_menu_access($context->data);
  215. }