1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * CTools primary module file. |
---|
6 | * |
---|
7 | * Most of the CTools tools are in their own .inc files. This contains |
---|
8 | * nothing more than a few convenience functions and some hooks that |
---|
9 | * must be implemented in the module file. |
---|
10 | */ |
---|
11 | |
---|
12 | define('CTOOLS_API_VERSION', '1.9'); |
---|
13 | |
---|
14 | /** |
---|
15 | * Test the CTools API version. |
---|
16 | * |
---|
17 | * This function can always be used to safely test if CTools has the minimum |
---|
18 | * API version that your module can use. It can also try to protect you from |
---|
19 | * running if the CTools API version is too new, but if you do that you need |
---|
20 | * to be very quick about watching CTools API releases and release new versions |
---|
21 | * of your software as soon as the new release is made, or people might end up |
---|
22 | * updating CTools and having your module shut down without any recourse. |
---|
23 | * |
---|
24 | * It is recommended that every hook of your module that might use CTools or |
---|
25 | * might lead to a use of CTools be guarded like this: |
---|
26 | * |
---|
27 | * @code |
---|
28 | * if (!module_invoke('ctools', 'api_version', '1.0')) { |
---|
29 | * return; |
---|
30 | * } |
---|
31 | * @endcode |
---|
32 | * |
---|
33 | * Note that some hooks such as _menu() or _theme() must return an array(). |
---|
34 | * |
---|
35 | * You can use it in your hook_requirements to report this error condition |
---|
36 | * like this: |
---|
37 | * |
---|
38 | * @code |
---|
39 | * define('MODULENAME_MINIMUM_CTOOLS_API_VERSION', '1.0'); |
---|
40 | * define('MODULENAME_MAXIMUM_CTOOLS_API_VERSION', '1.1'); |
---|
41 | * |
---|
42 | * function MODULENAME_requirements($phase) { |
---|
43 | * $requirements = array(); |
---|
44 | * if (!module_invoke('ctools', 'api_version', MODULENAME_MINIMUM_CTOOLS_API_VERSION, MODULENAME_MAXIMUM_CTOOLS_API_VERSION)) { |
---|
45 | * $requirements['MODULENAME_ctools'] = array( |
---|
46 | * 'title' => $t('MODULENAME required Chaos Tool Suite (CTools) API Version'), |
---|
47 | * 'value' => t('Between @a and @b', array('@a' => MODULENAME_MINIMUM_CTOOLS_API_VERSION, '@b' => MODULENAME_MAXIMUM_CTOOLS_API_VERSION)), |
---|
48 | * 'severity' => REQUIREMENT_ERROR, |
---|
49 | * ); |
---|
50 | * } |
---|
51 | * return $requirements; |
---|
52 | * } |
---|
53 | * @endcode |
---|
54 | * |
---|
55 | * Please note that the version is a string, not an floating point number. |
---|
56 | * This will matter once CTools reaches version 1.10. |
---|
57 | * |
---|
58 | * A CTools API changes history will be kept in API.txt. Not every new |
---|
59 | * version of CTools will necessarily update the API version. |
---|
60 | * @param $minimum |
---|
61 | * The minimum version of CTools necessary for your software to run with it. |
---|
62 | * @param $maximum |
---|
63 | * The maximum version of CTools allowed for your software to run with it. |
---|
64 | */ |
---|
65 | function ctools_api_version($minimum, $maximum = NULL) { |
---|
66 | if (version_compare(CTOOLS_API_VERSION, $minimum, '<')) { |
---|
67 | return FALSE; |
---|
68 | } |
---|
69 | |
---|
70 | if (isset($maximum) && version_compare(CTOOLS_API_VERSION, $maximum, '>')) { |
---|
71 | return FALSE; |
---|
72 | } |
---|
73 | |
---|
74 | return TRUE; |
---|
75 | } |
---|
76 | |
---|
77 | // ----------------------------------------------------------------------- |
---|
78 | // General utility functions |
---|
79 | |
---|
80 | /** |
---|
81 | * Include .inc files as necessary. |
---|
82 | * |
---|
83 | * This fuction is helpful for including .inc files for your module. The |
---|
84 | * general case is including ctools funcitonality like this: |
---|
85 | * |
---|
86 | * @code |
---|
87 | * ctools_include('plugins'); |
---|
88 | * @endcode |
---|
89 | * |
---|
90 | * Similar funcitonality can be used for other modules by providing the $module |
---|
91 | * and $dir arguments like this: |
---|
92 | * |
---|
93 | * @code |
---|
94 | * // include mymodule/includes/import.inc |
---|
95 | * ctools_include('import', 'mymodule'); |
---|
96 | * // include mymodule/plugins/foobar.inc |
---|
97 | * ctools_include('foobar', 'mymodule', 'plugins'); |
---|
98 | * @endcode |
---|
99 | * |
---|
100 | * @param $file |
---|
101 | * The base file name to be included. |
---|
102 | * @param $module |
---|
103 | * Optional module containing the include. |
---|
104 | * @param $dir |
---|
105 | * Optional subdirectory containing the include file. |
---|
106 | */ |
---|
107 | function ctools_include($file, $module = 'ctools', $dir = 'includes') { |
---|
108 | static $used = array(); |
---|
109 | |
---|
110 | $dir = '/' . ($dir ? $dir . '/' : ''); |
---|
111 | |
---|
112 | if (!isset($used[$module][$dir][$file])) { |
---|
113 | require_once './' . drupal_get_path('module', $module) . "$dir$file.inc"; |
---|
114 | $used[$module][$dir][$file] = true; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Provide the proper path to an image as necessary. |
---|
120 | * |
---|
121 | * This helper function is used by ctools but can also be used in other |
---|
122 | * modules in the same way as explained in the comments of ctools_include. |
---|
123 | * |
---|
124 | * @param $image |
---|
125 | * The base file name (with extension) of the image to be included. |
---|
126 | * @param $module |
---|
127 | * Optional module containing the include. |
---|
128 | * @param $dir |
---|
129 | * Optional subdirectory containing the include file. |
---|
130 | */ |
---|
131 | function ctools_image_path($image, $module = 'ctools', $dir = 'images') { |
---|
132 | return drupal_get_path('module', $module) . "/$dir/" . $image; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Include css files as necessary. |
---|
137 | * |
---|
138 | * This helper function is used by ctools but can also be used in other |
---|
139 | * modules in the same way as explained in the comments of ctools_include. |
---|
140 | * |
---|
141 | * @param $file |
---|
142 | * The base file name to be included. |
---|
143 | * @param $module |
---|
144 | * Optional module containing the include. |
---|
145 | * @param $dir |
---|
146 | * Optional subdirectory containing the include file. |
---|
147 | */ |
---|
148 | function ctools_add_css($file, $module = 'ctools', $dir = 'css') { |
---|
149 | drupal_add_css(drupal_get_path('module', $module) . "/$dir/$file.css"); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Include js files as necessary. |
---|
154 | * |
---|
155 | * This helper function is used by ctools but can also be used in other |
---|
156 | * modules in the same way as explained in the comments of ctools_include. |
---|
157 | * |
---|
158 | * @param $file |
---|
159 | * The base file name to be included. |
---|
160 | * @param $module |
---|
161 | * Optional module containing the include. |
---|
162 | * @param $dir |
---|
163 | * Optional subdirectory containing the include file. |
---|
164 | */ |
---|
165 | function ctools_add_js($file, $module = 'ctools', $dir = 'js') { |
---|
166 | drupal_add_js(drupal_get_path('module', $module) . "/$dir/$file.js"); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Central static variable storage. Modeled after Drupal 7's drupal_static(). |
---|
171 | * |
---|
172 | * @param $name |
---|
173 | * Globally unique name for the variable. For a function with only one static, |
---|
174 | * variable, the function name (e.g. via the PHP magic __FUNCTION__ constant) |
---|
175 | * is recommended. For a function with multiple static variables add a |
---|
176 | * distinguishing suffix to the function name for each one. |
---|
177 | * @param $default_value |
---|
178 | * Optional default value. |
---|
179 | * @param $reset |
---|
180 | * TRUE to reset a specific named variable, or all variables if $name is NULL. |
---|
181 | * Resetting every variable should only be used, for example, for running unit |
---|
182 | * tests with a clean environment. Should be used only though via function |
---|
183 | * ctools_static_reset(). |
---|
184 | */ |
---|
185 | function &ctools_static($name, $default_value = NULL, $reset = FALSE) { |
---|
186 | static $data = array(); |
---|
187 | |
---|
188 | // Reset a single value, or all values. |
---|
189 | if ($reset) { |
---|
190 | if (isset($name)) { |
---|
191 | unset($data[$name]); |
---|
192 | } |
---|
193 | else { |
---|
194 | $data = array(); |
---|
195 | } |
---|
196 | // We must return a reference to a variable. |
---|
197 | $dummy = NULL; |
---|
198 | return $dummy; |
---|
199 | } |
---|
200 | |
---|
201 | if (!isset($data[$name])) { |
---|
202 | $data[$name] = $default_value; |
---|
203 | } |
---|
204 | |
---|
205 | return $data[$name]; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * Reset one or all centrally stored static variable(s). |
---|
210 | * Modeled after Drupal 7's drupal_static_reset(). |
---|
211 | * |
---|
212 | * @param $name |
---|
213 | * Name of the static variable to reset. Omit to reset all variables. |
---|
214 | */ |
---|
215 | function ctools_static_reset($name) { |
---|
216 | ctools_static($name, NULL, TRUE); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Get a list of roles in the system. |
---|
221 | * |
---|
222 | * @return |
---|
223 | * An array of role names keyed by role ID. |
---|
224 | */ |
---|
225 | function ctools_get_roles() { |
---|
226 | static $roles = NULL; |
---|
227 | if (!isset($roles)) { |
---|
228 | $roles = array(); |
---|
229 | $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name"); |
---|
230 | while ($obj = db_fetch_object($result)) { |
---|
231 | $roles[$obj->rid] = $obj->name; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | return $roles; |
---|
236 | } |
---|
237 | |
---|
238 | /* |
---|
239 | * Break x,y,z and x+y+z into an array. Numeric only. |
---|
240 | * |
---|
241 | * @param $str |
---|
242 | * The string to parse. |
---|
243 | * |
---|
244 | * @return $object |
---|
245 | * An object containing |
---|
246 | * - operator: Either 'and' or 'or' |
---|
247 | * - value: An array of numeric values. |
---|
248 | */ |
---|
249 | function ctools_break_phrase($str) { |
---|
250 | $object = new stdClass(); |
---|
251 | |
---|
252 | if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) { |
---|
253 | // The '+' character in a query string may be parsed as ' '. |
---|
254 | $object->operator = 'or'; |
---|
255 | $object->value = preg_split('/[+ ]/', $str); |
---|
256 | } |
---|
257 | else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) { |
---|
258 | $object->operator = 'and'; |
---|
259 | $object->value = explode(',', $str); |
---|
260 | } |
---|
261 | |
---|
262 | // Keep an 'error' value if invalid strings were given. |
---|
263 | if (!empty($str) && (empty($object->value) || !is_array($object->value))) { |
---|
264 | $object->value = array(-1); |
---|
265 | $object->invalid_input = TRUE; |
---|
266 | return $object; |
---|
267 | } |
---|
268 | |
---|
269 | if (empty($object->value)) { |
---|
270 | $object->value = array(); |
---|
271 | } |
---|
272 | |
---|
273 | // Doubly ensure that all values are numeric only. |
---|
274 | foreach ($object->value as $id => $value) { |
---|
275 | $object->value[$id] = intval($value); |
---|
276 | } |
---|
277 | |
---|
278 | return $object; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Set a token/value pair to be replaced later in the request, specifically in |
---|
283 | * ctools_preprocess_page(). |
---|
284 | * |
---|
285 | * @param $token |
---|
286 | * The token to be replaced later, during page rendering. This should |
---|
287 | * ideally be a string inside of an HTML comment, so that if there is |
---|
288 | * no replacement, the token will not render on the page. |
---|
289 | * @param $type |
---|
290 | * The type of the token. Can be either 'variable', which will pull data |
---|
291 | * directly from the page variables |
---|
292 | * @param $argument |
---|
293 | * If $type == 'variable' then argument should be the key to fetch from |
---|
294 | * the $variables. If $type == 'callback' then it should either be the |
---|
295 | * callback, or an array that will be sent to call_user_func_array(). |
---|
296 | * |
---|
297 | * @return |
---|
298 | * A array of token/variable names to be replaced. |
---|
299 | */ |
---|
300 | function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) { |
---|
301 | static $tokens = array(); |
---|
302 | |
---|
303 | if (isset($token)) { |
---|
304 | $tokens[$token] = array($type, $argument); |
---|
305 | } |
---|
306 | |
---|
307 | return $tokens; |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * Easily set a token from the page variables. |
---|
312 | * |
---|
313 | * This function can be used like this: |
---|
314 | * $token = ctools_set_variable_token('tabs'); |
---|
315 | * |
---|
316 | * $token will then be a simple replacement for the 'tabs' about of the |
---|
317 | * variables available in the page template. |
---|
318 | */ |
---|
319 | function ctools_set_variable_token($token) { |
---|
320 | $string = '<!-- ctools-page-' . $token . ' -->'; |
---|
321 | ctools_set_page_token($string, 'variable', $token); |
---|
322 | return $string; |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * Easily set a token from the page variables. |
---|
327 | * |
---|
328 | * This function can be used like this: |
---|
329 | * $token = ctools_set_variable_token('id', 'mymodule_myfunction'); |
---|
330 | */ |
---|
331 | function ctools_set_callback_token($token, $callback) { |
---|
332 | // If the callback uses arguments they are considered in the token. |
---|
333 | if (is_array($callback)) { |
---|
334 | $token .= '-' . md5(serialize($callback)); |
---|
335 | } |
---|
336 | $string = '<!-- ctools-page-' . $token . ' -->'; |
---|
337 | ctools_set_page_token($string, 'callback', $callback); |
---|
338 | return $string; |
---|
339 | } |
---|
340 | |
---|
341 | // ----------------------------------------------------------------------- |
---|
342 | // Drupal core hooks |
---|
343 | |
---|
344 | /** |
---|
345 | * Implement hook_init to keep our global CSS at the ready. |
---|
346 | */ |
---|
347 | function ctools_init() { |
---|
348 | ctools_add_css('ctools'); |
---|
349 | // If we are sure that CTools' AJAX is in use, change the error handling. |
---|
350 | if (!empty($_REQUEST['ctools_ajax'])) { |
---|
351 | ini_set('display_errors', 0); |
---|
352 | register_shutdown_function('ctools_shutdown_handler'); |
---|
353 | } |
---|
354 | |
---|
355 | // Clear plugin cache on the module page submit. |
---|
356 | if ($_GET['q'] == 'admin/build/modules/list/confirm' && !empty($_POST)) { |
---|
357 | cache_clear_all('ctools_plugin_files:', 'cache', TRUE); |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | * Shutdown handler used during ajax operations to help catch fatal errors. |
---|
363 | */ |
---|
364 | function ctools_shutdown_handler() { |
---|
365 | if (function_exists('error_get_last') AND ($error = error_get_last())){ |
---|
366 | switch($error['type']){ |
---|
367 | case E_ERROR: |
---|
368 | case E_CORE_ERROR: |
---|
369 | case E_COMPILE_ERROR: |
---|
370 | case E_USER_ERROR: |
---|
371 | // Do this manually because including files here is dangerous. |
---|
372 | $commands = array( |
---|
373 | array( |
---|
374 | 'command' => 'alert', |
---|
375 | 'title' => t('Error'), |
---|
376 | 'text' => t('Unable to complete operation. Fatal error in @file on line @line: @message', array( |
---|
377 | '@file' => $error['file'], |
---|
378 | '@line' => $error['line'], |
---|
379 | '@message' => $error['message'], |
---|
380 | )), |
---|
381 | ), |
---|
382 | ); |
---|
383 | |
---|
384 | // Change the status code so that the client will read the AJAX returned. |
---|
385 | header('HTTP/1.1 200 OK'); |
---|
386 | drupal_json($commands); |
---|
387 | } |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | /** |
---|
392 | * Implementation of hook_theme(). |
---|
393 | */ |
---|
394 | function ctools_theme() { |
---|
395 | ctools_include('utility'); |
---|
396 | $items = array(); |
---|
397 | ctools_passthrough('ctools', 'theme', $items); |
---|
398 | return $items; |
---|
399 | } |
---|
400 | |
---|
401 | /** |
---|
402 | * Implementation of hook_menu(). |
---|
403 | */ |
---|
404 | function ctools_menu() { |
---|
405 | ctools_include('utility'); |
---|
406 | $items = array(); |
---|
407 | ctools_passthrough('ctools', 'menu', $items); |
---|
408 | return $items; |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * Implementation of hook_cron. Clean up old caches. |
---|
413 | */ |
---|
414 | function ctools_cron() { |
---|
415 | ctools_include('utility'); |
---|
416 | $items = array(); |
---|
417 | ctools_passthrough('ctools', 'cron', $items); |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * Ensure the CTools CSS cache is flushed whenever hook_flush_caches is invoked. |
---|
422 | */ |
---|
423 | function ctools_flush_caches() { |
---|
424 | // Do not actually flush caches if running on cron. Drupal uses this hook |
---|
425 | // in an inconsistent fashion and it does not necessarily mean to *flush* |
---|
426 | // caches when running from cron. Instead it's just getting a list of cache |
---|
427 | // tables and may not do any flushing. |
---|
428 | if (variable_get('cron_semaphore', FALSE)) { |
---|
429 | return; |
---|
430 | } |
---|
431 | |
---|
432 | ctools_include('css'); |
---|
433 | ctools_css_flush_caches(); |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * Provide a search form with a different id so that form_alters will miss it |
---|
438 | * and thus not get advanced search settings. |
---|
439 | */ |
---|
440 | function ctools_forms() { |
---|
441 | $forms['ctools_search_form']= array( |
---|
442 | 'callback' => 'search_form', |
---|
443 | ); |
---|
444 | |
---|
445 | return $forms; |
---|
446 | } |
---|
447 | |
---|
448 | /** |
---|
449 | * Implements hook_form_alter(). |
---|
450 | */ |
---|
451 | function ctools_form_alter(&$form, $form_state, $form_id) { |
---|
452 | $form['#after_build'][] = 'ctools_ajax_form_after_build'; |
---|
453 | } |
---|
454 | |
---|
455 | /** |
---|
456 | * #after_build callback: Mark the $form['#action'] as a trusted URL for Ajax. |
---|
457 | */ |
---|
458 | function ctools_ajax_form_after_build($form, $form_state) { |
---|
459 | $settings = array( |
---|
460 | 'CToolsUrlIsAjaxTrusted' => array( |
---|
461 | $form['#action'] => TRUE, |
---|
462 | ), |
---|
463 | ); |
---|
464 | drupal_add_js($settings, 'setting'); |
---|
465 | return $form; |
---|
466 | } |
---|
467 | |
---|
468 | /** |
---|
469 | * Implementation of hook_file_download() |
---|
470 | * |
---|
471 | * When using the private file system, we have to let Drupal know it's ok to |
---|
472 | * download CSS and image files from our temporary directory. |
---|
473 | */ |
---|
474 | function ctools_file_download($filepath) { |
---|
475 | if (strpos($filepath, 'ctools') === 0) { |
---|
476 | $mime = file_get_mimetype($filepath); |
---|
477 | // For safety's sake, we allow only text and images. |
---|
478 | if (strpos($mime, 'text') === 0 || strpos($mime, 'image') === 0) { |
---|
479 | return array('Content-type:' . $mime); |
---|
480 | } |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | // ----------------------------------------------------------------------- |
---|
485 | // CTools hook implementations. |
---|
486 | |
---|
487 | /** |
---|
488 | * Implementation of hook_ctools_plugin_directory() to let the system know |
---|
489 | * where all our own plugins are. |
---|
490 | */ |
---|
491 | function ctools_ctools_plugin_directory($owner, $plugin_type) { |
---|
492 | if ($owner == 'ctools') { |
---|
493 | return 'plugins/' . $plugin_type; |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | /** |
---|
498 | * Implementation of hook_js_replacements(). |
---|
499 | * This is a hook that is not a standard yet. We hope jquery_update and others |
---|
500 | * will expose this hook to inform modules which scripts they are modifying |
---|
501 | * in the theme layer. |
---|
502 | * The return format is $scripts[$type][$old_path] = $new_path. |
---|
503 | */ |
---|
504 | function ctools_js_replacements() { |
---|
505 | $replacements = array(); |
---|
506 | // Until jquery_update is released with its own replacement hook, we will |
---|
507 | // take those replacements into account here. |
---|
508 | if (module_exists('jquery_update')) { |
---|
509 | $replacements = array_merge_recursive($replacements, jquery_update_get_replacements()); |
---|
510 | foreach ($replacements as $type => $type_replacements) { |
---|
511 | foreach ($type_replacements as $old_path => $new_filename) { |
---|
512 | $replacements[$type][$old_path] = drupal_get_path('module', 'jquery_update') . "/replace/$new_filename"; |
---|
513 | } |
---|
514 | } |
---|
515 | $replacements['core']['misc/jquery.js'] = jquery_update_jquery_path(); |
---|
516 | } |
---|
517 | return $replacements; |
---|
518 | } |
---|
519 | |
---|
520 | /** |
---|
521 | * Inform CTools that the layout plugin can be loaded from themes. |
---|
522 | */ |
---|
523 | function ctools_ctools_plugin_access() { |
---|
524 | return array( |
---|
525 | 'child plugins' => TRUE, |
---|
526 | ); |
---|
527 | } |
---|
528 | |
---|
529 | // ----------------------------------------------------------------------- |
---|
530 | // Drupal theme preprocess hooks that must be in the .module file. |
---|
531 | |
---|
532 | /** |
---|
533 | * Override or insert PHPTemplate variables into the templates. |
---|
534 | * |
---|
535 | * This needs to be in the .module file to ensure availability; we can't change the |
---|
536 | * paths or it won't be able to find templates. |
---|
537 | */ |
---|
538 | function ctools_garland_preprocess_page(&$vars) { |
---|
539 | $vars['tabs2'] = ctools_menu_secondary_local_tasks(); |
---|
540 | |
---|
541 | // Hook into color.module |
---|
542 | if (module_exists('color')) { |
---|
543 | _color_page_alter($vars); |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | /** |
---|
548 | * A theme preprocess function to automatically allow panels-based node |
---|
549 | * templates based upon input when the panel was configured. |
---|
550 | */ |
---|
551 | function ctools_preprocess_node(&$vars) { |
---|
552 | // The 'panel_identifier' attribute of the node is added when the pane is |
---|
553 | // rendered. |
---|
554 | if (!empty($vars['node']->panel_identifier)) { |
---|
555 | $vars['panel_identifier'] = check_plain($vars['node']->panel_identifier); |
---|
556 | $vars['template_files'][] = 'node-panel-' . check_plain($vars['node']->panel_identifier); |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | /** |
---|
561 | * A theme preprocess function to allow content type plugins to use page |
---|
562 | * template variables which are not yet available when the content type is |
---|
563 | * rendered. |
---|
564 | */ |
---|
565 | function ctools_preprocess_page(&$variables) { |
---|
566 | $tokens = ctools_set_page_token(); |
---|
567 | if (!empty($tokens)) { |
---|
568 | $temp_tokens = array(); |
---|
569 | foreach ($tokens as $token => $key) { |
---|
570 | list($type, $argument) = $key; |
---|
571 | switch ($type) { |
---|
572 | case 'variable': |
---|
573 | $temp_tokens[$token] = isset($variables[$argument]) ? $variables[$argument] : ''; |
---|
574 | break; |
---|
575 | case 'callback': |
---|
576 | if (is_string($argument) && function_exists($argument)) { |
---|
577 | $temp_tokens[$token] = $argument($variables); |
---|
578 | } |
---|
579 | if (is_array($argument) && function_exists($argument[0])) { |
---|
580 | $function = array_shift($argument); |
---|
581 | $argument = array_merge(array(&$variables), $argument); |
---|
582 | $temp_tokens[$token] = call_user_func_array($function, $argument); |
---|
583 | } |
---|
584 | break; |
---|
585 | } |
---|
586 | } |
---|
587 | $tokens = $temp_tokens; |
---|
588 | unset($temp_tokens); |
---|
589 | $variables['content'] = strtr($variables['content'], $tokens); |
---|
590 | } |
---|
591 | |
---|
592 | if (defined('CTOOLS_AJAX_INCLUDED')) { |
---|
593 | ctools_ajax_page_preprocess($variables); |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | // ----------------------------------------------------------------------- |
---|
598 | // Menu callbacks that must be in the .module file. |
---|
599 | |
---|
600 | /** |
---|
601 | * Determine if the current user has access via a plugin. |
---|
602 | * |
---|
603 | * This function is meant to be embedded in the Drupal menu system, and |
---|
604 | * therefore is in the .module file since sub files can't be loaded, and |
---|
605 | * takes arguments a little bit more haphazardly than ctools_access(). |
---|
606 | * |
---|
607 | * @param $access |
---|
608 | * An access control array which contains the following information: |
---|
609 | * - 'logic': and or or. Whether all tests must pass or one must pass. |
---|
610 | * - 'plugins': An array of access plugins. Each contains: |
---|
611 | * - - 'name': The name of the plugin |
---|
612 | * - - 'settings': The settings from the plugin UI. |
---|
613 | * - - 'context': Which context to use. |
---|
614 | * @param ... |
---|
615 | * zero or more context arguments generated from argument plugins. These |
---|
616 | * contexts must have an 'id' attached to them so that they can be |
---|
617 | * properly associated. The argument plugin system should set this, but |
---|
618 | * if the context is coming from elsewhere it will need to be set manually. |
---|
619 | * |
---|
620 | * @return |
---|
621 | * TRUE if access is granted, false if otherwise. |
---|
622 | */ |
---|
623 | function ctools_access_menu($access) { |
---|
624 | // Short circuit everything if there are no access tests. |
---|
625 | if (empty($access['plugins'])) { |
---|
626 | return TRUE; |
---|
627 | } |
---|
628 | |
---|
629 | $contexts = array(); |
---|
630 | foreach (func_get_args() as $arg) { |
---|
631 | if (is_object($arg) && get_class($arg) == 'ctools_context') { |
---|
632 | $contexts[$arg->id] = $arg; |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | ctools_include('context'); |
---|
637 | return ctools_access($access, $contexts); |
---|
638 | } |
---|
639 | |
---|
640 | /** |
---|
641 | * Determine if the current user has access via checks to multiple different |
---|
642 | * permissions. |
---|
643 | * |
---|
644 | * This function is a thin wrapper around user_access that allows multiple |
---|
645 | * permissions to be easily designated for use on, for example, a menu callback. |
---|
646 | * |
---|
647 | * @param ... |
---|
648 | * An indexed array of zero or more permission strings to be checked by |
---|
649 | * user_access(). |
---|
650 | * |
---|
651 | * @return |
---|
652 | * Iff all checks pass will this function return TRUE. If an invalid argument |
---|
653 | * is passed (e.g., not a string), this function errs on the safe said and |
---|
654 | * returns FALSE. |
---|
655 | */ |
---|
656 | function ctools_access_multiperm() { |
---|
657 | foreach (func_get_args() as $arg) { |
---|
658 | if (!is_string($arg) || !user_access($arg)) { |
---|
659 | return FALSE; |
---|
660 | } |
---|
661 | } |
---|
662 | return TRUE; |
---|
663 | } |
---|
664 | |
---|
665 | /** |
---|
666 | * Check to see if the incoming menu item is js capable or not. |
---|
667 | * |
---|
668 | * This can be used as %ctools_js as part of a path in hook menu. CTools |
---|
669 | * ajax functions will automatically change the phrase 'nojs' to 'ajax' |
---|
670 | * when it attaches ajax to a link. This can be used to autodetect if |
---|
671 | * that happened. |
---|
672 | */ |
---|
673 | function ctools_js_load($js) { |
---|
674 | if ($js == 'ajax') { |
---|
675 | return TRUE; |
---|
676 | } |
---|
677 | return 0; |
---|
678 | } |
---|
679 | |
---|
680 | /** |
---|
681 | * Menu _load hook. |
---|
682 | * |
---|
683 | * This function will be called to load an object as a replacement for |
---|
684 | * %ctools_export_ui in menu paths. |
---|
685 | */ |
---|
686 | function ctools_export_ui_load($item_name, $plugin_name) { |
---|
687 | $return = &ctools_static(__FUNCTION__, FALSE); |
---|
688 | |
---|
689 | if (!$return) { |
---|
690 | ctools_include('export-ui'); |
---|
691 | $plugin = ctools_get_export_ui($plugin_name); |
---|
692 | |
---|
693 | if ($plugin) { |
---|
694 | // Get the load callback. |
---|
695 | $item = ctools_export_crud_load($plugin['schema'], $item_name); |
---|
696 | return empty($item) ? FALSE : $item; |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | return $return; |
---|
701 | } |
---|
702 | |
---|
703 | // ----------------------------------------------------------------------- |
---|
704 | // Caching callbacks on behalf of export-ui. |
---|
705 | |
---|
706 | /** |
---|
707 | * Menu access callback for various tasks of export-ui. |
---|
708 | */ |
---|
709 | function ctools_export_ui_task_access($plugin_name, $op, $item = NULL) { |
---|
710 | ctools_include('export-ui'); |
---|
711 | $plugin = ctools_get_export_ui($plugin_name); |
---|
712 | $handler = ctools_export_ui_get_handler($plugin); |
---|
713 | |
---|
714 | if ($handler) { |
---|
715 | return $handler->access($op, $item); |
---|
716 | } |
---|
717 | |
---|
718 | // Deny access if the handler cannot be found. |
---|
719 | return FALSE; |
---|
720 | } |
---|
721 | |
---|
722 | /** |
---|
723 | * Cache callback on behalf of ctools_export_ui. |
---|
724 | */ |
---|
725 | function ctools_export_ui_context_cache_get($plugin_name, $key) { |
---|
726 | ctools_include('export-ui'); |
---|
727 | $plugin = ctools_get_export_ui($plugin_name); |
---|
728 | $handler = ctools_export_ui_get_handler($plugin); |
---|
729 | if ($handler) { |
---|
730 | $item = $handler->edit_cache_get($key); |
---|
731 | if (!$item) { |
---|
732 | $item = ctools_export_crud_load($handler->plugin['schema'], $key); |
---|
733 | } |
---|
734 | return $item; |
---|
735 | } |
---|
736 | } |
---|
737 | |
---|
738 | /** |
---|
739 | * Cache callback on behalf of ctools_export_ui. |
---|
740 | */ |
---|
741 | function ctools_export_ui_context_cache_set($plugin_name, $key, $item) { |
---|
742 | ctools_include('export-ui'); |
---|
743 | $plugin = ctools_get_export_ui($plugin_name); |
---|
744 | $handler = ctools_export_ui_get_handler($plugin); |
---|
745 | if ($handler) { |
---|
746 | return $handler->edit_cache_set_key($item, $key); |
---|
747 | } |
---|
748 | } |
---|
749 | |
---|
750 | /** |
---|
751 | * Callback for access control ajax form on behalf of export ui. |
---|
752 | * |
---|
753 | * Returns the cached access config and contexts used. |
---|
754 | * Note that this is assuming that access will be in $item->access -- if it |
---|
755 | * is not, an export UI plugin will have to make its own callbacks. |
---|
756 | */ |
---|
757 | function ctools_export_ui_ctools_access_get($argument) { |
---|
758 | ctools_include('export-ui'); |
---|
759 | list($plugin_name, $key) = explode(':', $argument); |
---|
760 | |
---|
761 | $plugin = ctools_get_export_ui($plugin_name); |
---|
762 | $handler = ctools_export_ui_get_handler($plugin); |
---|
763 | |
---|
764 | if ($handler) { |
---|
765 | ctools_include('context'); |
---|
766 | $item = $handler->edit_cache_get($key); |
---|
767 | if (!$item) { |
---|
768 | $item = ctools_export_crud_load($handler->plugin['schema'], $key); |
---|
769 | } |
---|
770 | |
---|
771 | $contexts = ctools_context_load_contexts($item); |
---|
772 | return array($item->access, $contexts); |
---|
773 | } |
---|
774 | } |
---|
775 | |
---|
776 | /** |
---|
777 | * Callback for access control ajax form on behalf of export ui |
---|
778 | * |
---|
779 | * Returns the cached access config and contexts used. |
---|
780 | * Note that this is assuming that access will be in $item->access -- if it |
---|
781 | * is not, an export UI plugin will have to make its own callbacks. |
---|
782 | */ |
---|
783 | function ctools_export_ui_ctools_access_set($argument, $access) { |
---|
784 | ctools_include('export-ui'); |
---|
785 | list($plugin_name, $key) = explode(':', $argument); |
---|
786 | |
---|
787 | $plugin = ctools_get_export_ui($plugin_name); |
---|
788 | $handler = ctools_export_ui_get_handler($plugin); |
---|
789 | |
---|
790 | if ($handler) { |
---|
791 | ctools_include('context'); |
---|
792 | $item = $handler->edit_cache_get($key); |
---|
793 | if (!$item) { |
---|
794 | $item = ctools_export_crud_load($handler->plugin['schema'], $key); |
---|
795 | } |
---|
796 | $item->access = $access; |
---|
797 | return $handler->edit_cache_set_key($item, $key); |
---|
798 | } |
---|
799 | } |
---|