Projet

Général

Profil

Paste
Télécharger (7,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / tasks / search.inc @ 1e39edcb

1
<?php
2

    
3
/**
4
 * @file
5
 * Handle the 'node view' override task.
6
 *
7
 * This plugin overrides node/%node and reroutes it to the page manager, where
8
 * a list of tasks can be used to service this request based upon criteria
9
 * supplied by access plugins.
10
 */
11

    
12
/**
13
 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
14
 * more information.
15
 */
16
function page_manager_search_page_manager_tasks() {
17
  if (!module_exists('search')) {
18
    return;
19
  }
20

    
21
  return array(
22
    // This is a 'page' task and will fall under the page admin UI
23
    'task type' => 'page',
24
    'title' => t('Search'),
25

    
26
    // There are multiple search pages, let's override each of them
27
    // separately.
28
    'subtasks' => TRUE,
29
    'subtask callback' => 'page_manager_search_subtask',
30
    'subtasks callback' => 'page_manager_search_subtasks',
31

    
32
    // Menu hooks so that we can alter the node/%node menu entry to point to us.
33
    'hook menu alter' => 'page_manager_search_menu_alter',
34

    
35
    // This is task uses 'context' handlers and must implement these to give the
36
    // handler data it needs.
37
    'handler type' => 'context',
38
    'get arguments' => 'page_manager_search_get_arguments',
39
    'get context placeholders' => 'page_manager_search_get_contexts',
40
    'access callback' => 'page_manager_search_access_check',
41

    
42
  );
43
}
44

    
45
/**
46
 * Callback defined by page_manager_search_page_manager_tasks().
47
 *
48
 * Alter the search tabs to work with page manager. The search flow is
49
 * quite odd, and tracing through the code takes hours to realize
50
 * that the tab you click on does not normally actually handle
51
 * the search. This tries to account for that.
52
 *
53
 * Note to module authors: This tends to work a lot better with modules
54
 * that override their own search pages if their _alter runs *before*
55
 * this one.
56
 */
57
function page_manager_search_menu_alter(&$items, $task) {
58
  // We are creating two sets of tabs. One set is for searching without
59
  // keywords. A second set is for searching *with* keywords. This
60
  // is necessary because search/node/% and search/node need to be
61
  // different due to the way the search menu items function.
62

    
63
  $default_info = search_get_default_module_info();
64
  if (empty($default_info)) {
65
    // Nothing to do.
66
    return;
67
  }
68

    
69
  // Go through each search module item.
70
  foreach (search_get_info() as $module => $info) {
71
    if (variable_get('page_manager_search_disabled_' . $module, TRUE)) {
72
      continue;
73
    }
74

    
75
    $path = 'search/' . $info['path'];
76
    $callback = $items["$path/%menu_tail"]['page callback'];
77

    
78
    if ($callback == 'search_view' || variable_get('page_manager_override_anyway', FALSE)) {
79
      $items["$path"]['page callback'] = 'page_manager_search_page';
80
      $items["$path"]['file path'] = $task['path'];
81
      $items["$path"]['file'] = $task['file'];
82

    
83
      $items["$path/%menu_tail"]['page callback'] = 'page_manager_search_page';
84
      $items["$path/%menu_tail"]['file path'] = $task['path'];
85
      $items["$path/%menu_tail"]['file'] = $task['file'];
86
    }
87
    else {
88
      // automatically disable this task if it cannot be enabled.
89
      variable_set('page_manager_search_disabled_' . $module, TRUE);
90
      if (!empty($GLOBALS['page_manager_enabling_search'])) {
91
        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');
92
      }
93
    }
94
  }
95
}
96

    
97
/**
98
 * Entry point for our overridden search page.
99
 *
100
 */
101
function page_manager_search_page($type) {
102
  ctools_include('menu');
103
//  menu_set_active_trail(ctools_get_menu_trail('search/' . $type));
104

    
105
  // Get the arguments and construct a keys string out of them.
106
  $args = func_get_args();
107

    
108
  // We have to remove the $type.
109
  array_shift($args);
110

    
111
  // And implode() it all back together.
112
  $keys = $args ? implode('/', $args) : '';
113

    
114
  // Allow other modules to alter the search keys
115
  drupal_alter(array('search_keys', 'search_'. $type .'_keys'), $keys);
116

    
117
  // Load my task plugin
118
  $task = page_manager_get_task('search');
119
  $subtask = page_manager_get_task_subtask($task, $type);
120

    
121
  // Load the node into a context.
122
  ctools_include('context');
123
  ctools_include('context-task-handler');
124
  $contexts = ctools_context_handler_get_task_contexts($task, $subtask, array($keys));
125

    
126
  $output = ctools_context_handler_render($task, $subtask, $contexts, array($keys));
127
  if ($output !== FALSE) {
128
    return $output;
129
  }
130

    
131
  $function = 'search_view';
132
  foreach (module_implements('page_manager_override') as $module) {
133
    $call = $module . '_page_manager_override';
134
    if (($rc = $call('search')) && function_exists($rc)) {
135
      $function = $rc;
136
      break;
137
    }
138
  }
139

    
140
  // Otherwise, fall back.
141

    
142
  // Put the $type back on the arguments.
143
  module_load_include('inc', 'search', 'search.pages');
144
  array_unshift($args, $type);
145
  return call_user_func_array($function, $args);
146
}
147

    
148
/**
149
 * Callback to get arguments provided by this task handler.
150
 *
151
 * Since this is the node view and there is no UI on the arguments, we
152
 * create dummy arguments that contain the needed data.
153
 */
154
function page_manager_search_get_arguments($task, $subtask_id) {
155
  return array(
156
    array(
157
      'keyword' => 'keywords',
158
      'identifier' => t('Keywords'),
159
      'id' => 1,
160
      'name' => 'string',
161
      'settings' => array('use_tail' => TRUE),
162
    ),
163
  );
164
}
165

    
166
/**
167
 * Callback to get context placeholders provided by this handler.
168
 */
169
function page_manager_search_get_contexts($task, $subtask_id) {
170
  return ctools_context_get_placeholders_from_argument(page_manager_search_get_arguments($task, $subtask_id));
171
}
172

    
173
/**
174
 * Callback to enable/disable the page from the UI.
175
 */
176
function page_manager_search_enable($cache, $status) {
177
  variable_set('page_manager_search_disabled_' . $cache->subtask_id, $status);
178

    
179
  // Set a global flag so that the menu routine knows it needs
180
  // to set a message if enabling cannot be done.
181
  if (!$status) {
182
    $GLOBALS['page_manager_enabling_search'] = TRUE;
183
  }
184
}
185

    
186
/**
187
 * Task callback to get all subtasks.
188
 *
189
 * Return a list of all subtasks.
190
 */
191
function page_manager_search_subtasks($task) {
192
  $return = array();
193
  foreach (search_get_info() as $module => $info) {
194
    if ($info['path']) {
195
      // We don't pass the $info because the subtask build could be called
196
      // singly without the $info when just the subtask needs to be built.
197
      $return[$module] = page_manager_search_build_subtask($task, $module);
198
    }
199
  }
200

    
201
  return $return;
202
}
203

    
204
/**
205
 * Callback to return a single subtask.
206
 */
207
function page_manager_search_subtask($task, $subtask_id) {
208
  return page_manager_search_build_subtask($task, $subtask_id);
209
}
210

    
211
/**
212
 * Build a subtask array for a given page.
213
 */
214
function page_manager_search_build_subtask($task, $module) {
215
  $search_info = search_get_info();
216
  $info = $search_info[$module];
217
  $path = 'search/' . $info['path'];
218
  $subtask = array(
219
    'name' => $module,
220
    'admin title' => $info['title'],
221
    'admin path' => "$path/!keywords",
222
    'admin description' => t('Search @type', array('@type' => $info['title'])),
223
    'admin type' => t('System'),
224
    'row class' => empty($page->disabled) ? 'page-manager-enabled' : 'page-manager-disabled',
225
    'storage' => t('In code'),
226
    'disabled' => variable_get('page_manager_search_disabled_' . $module, TRUE),
227
    // This works for both enable AND disable
228
    'enable callback' => 'page_manager_search_enable',
229
  );
230

    
231
  return $subtask;
232
}
233

    
234
/**
235
 * Callback to determine if a page is accessible.
236
 *
237
 * @param $task
238
 *   The task plugin.
239
 * @param $subtask_id
240
 *   The subtask id
241
 * @param $contexts
242
 *   The contexts loaded for the task.
243
 * @return
244
 *   TRUE if the current user can access the page.
245
 */
246
function page_manager_search_access_check($task, $subtask_id, $contexts) {
247
  $context = reset($contexts);
248
  return _search_menu_access($context->data);
249
}