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', '2.0.7');
|
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_ROOT . '/' . drupal_get_path('module', $module) . "$dir$file.inc";
|
114
|
$used[$module][$dir][$file] = TRUE;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Include .inc files in a form context.
|
120
|
*
|
121
|
* This is a variant of ctools_include that will save information in the
|
122
|
* the form_state so that cached forms will properly include things.
|
123
|
*/
|
124
|
function ctools_form_include(&$form_state, $file, $module = 'ctools', $dir = 'includes') {
|
125
|
if (!isset($form_state['build_info']['args'])) {
|
126
|
$form_state['build_info']['args'] = array();
|
127
|
}
|
128
|
|
129
|
$dir = '/' . ($dir ? $dir . '/' : '');
|
130
|
form_load_include($form_state, 'inc', $module, $dir . $file);
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Add an arbitrary path to the $form_state so it can work with form cache.
|
135
|
*
|
136
|
* module_load_include uses an unfortunately annoying syntax to work, making it
|
137
|
* difficult to translate the more simple $path + $file syntax.
|
138
|
*/
|
139
|
function ctools_form_include_file(&$form_state, $filename) {
|
140
|
if (!isset($form_state['build_info']['args'])) {
|
141
|
$form_state['build_info']['args'] = array();
|
142
|
}
|
143
|
|
144
|
// Now add this to the build info files so that AJAX requests will know to load it.
|
145
|
$form_state['build_info']['files']["$filename"] = $filename;
|
146
|
require_once DRUPAL_ROOT . '/' . $filename;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Provide the proper path to an image as necessary.
|
151
|
*
|
152
|
* This helper function is used by ctools but can also be used in other
|
153
|
* modules in the same way as explained in the comments of ctools_include.
|
154
|
*
|
155
|
* @param $image
|
156
|
* The base file name (with extension) of the image to be included.
|
157
|
* @param $module
|
158
|
* Optional module containing the include.
|
159
|
* @param $dir
|
160
|
* Optional subdirectory containing the include file.
|
161
|
*/
|
162
|
function ctools_image_path($image, $module = 'ctools', $dir = 'images') {
|
163
|
return drupal_get_path('module', $module) . "/$dir/" . $image;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Include css files as necessary.
|
168
|
*
|
169
|
* This helper function is used by ctools but can also be used in other
|
170
|
* modules in the same way as explained in the comments of ctools_include.
|
171
|
*
|
172
|
* @param $file
|
173
|
* The base file name to be included.
|
174
|
* @param $module
|
175
|
* Optional module containing the include.
|
176
|
* @param $dir
|
177
|
* Optional subdirectory containing the include file.
|
178
|
*/
|
179
|
function ctools_add_css($file, $module = 'ctools', $dir = 'css') {
|
180
|
drupal_add_css(drupal_get_path('module', $module) . "/$dir/$file.css");
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Format a css file name for use with $form['#attached']['css'].
|
185
|
*
|
186
|
* This helper function is used by ctools but can also be used in other
|
187
|
* modules in the same way as explained in the comments of ctools_include.
|
188
|
*
|
189
|
* @code
|
190
|
* $form['#attached']['css'] = array(ctools_attach_css('collapsible-div'));
|
191
|
* $form['#attached']['css'][ctools_attach_css('collapsible-div')] = array('preprocess' => FALSE);
|
192
|
* @endcode
|
193
|
*
|
194
|
* @param $file
|
195
|
* The base file name to be included.
|
196
|
* @param $module
|
197
|
* Optional module containing the include.
|
198
|
* @param $dir
|
199
|
* Optional subdirectory containing the include file.
|
200
|
*/
|
201
|
function ctools_attach_css($file, $module = 'ctools', $dir = 'css') {
|
202
|
return drupal_get_path('module', $module) . "/$dir/$file.css";
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Include js files as necessary.
|
207
|
*
|
208
|
* This helper function is used by ctools but can also be used in other
|
209
|
* modules in the same way as explained in the comments of ctools_include.
|
210
|
*
|
211
|
* @param $file
|
212
|
* The base file name to be included.
|
213
|
* @param $module
|
214
|
* Optional module containing the include.
|
215
|
* @param $dir
|
216
|
* Optional subdirectory containing the include file.
|
217
|
*/
|
218
|
function ctools_add_js($file, $module = 'ctools', $dir = 'js') {
|
219
|
drupal_add_js(drupal_get_path('module', $module) . "/$dir/$file.js");
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* Format a javascript file name for use with $form['#attached']['js'].
|
224
|
*
|
225
|
* This helper function is used by ctools but can also be used in other
|
226
|
* modules in the same way as explained in the comments of ctools_include.
|
227
|
*
|
228
|
* @code
|
229
|
* $form['#attached']['js'] = array(ctools_attach_js('auto-submit'));
|
230
|
* @endcode
|
231
|
*
|
232
|
* @param $file
|
233
|
* The base file name to be included.
|
234
|
* @param $module
|
235
|
* Optional module containing the include.
|
236
|
* @param $dir
|
237
|
* Optional subdirectory containing the include file.
|
238
|
*/
|
239
|
function ctools_attach_js($file, $module = 'ctools', $dir = 'js') {
|
240
|
return drupal_get_path('module', $module) . "/$dir/$file.js";
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Get a list of roles in the system.
|
245
|
*
|
246
|
* @return
|
247
|
* An array of role names keyed by role ID.
|
248
|
*
|
249
|
* @deprecated
|
250
|
* user_roles() should be used instead.
|
251
|
*/
|
252
|
function ctools_get_roles() {
|
253
|
return user_roles();
|
254
|
}
|
255
|
|
256
|
/*
|
257
|
* Break x,y,z and x+y+z into an array. Numeric only.
|
258
|
*
|
259
|
* @param $str
|
260
|
* The string to parse.
|
261
|
*
|
262
|
* @return $object
|
263
|
* An object containing
|
264
|
* - operator: Either 'and' or 'or'
|
265
|
* - value: An array of numeric values.
|
266
|
*/
|
267
|
function ctools_break_phrase($str) {
|
268
|
$object = new stdClass();
|
269
|
|
270
|
if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
|
271
|
// The '+' character in a query string may be parsed as ' '.
|
272
|
$object->operator = 'or';
|
273
|
$object->value = preg_split('/[+ ]/', $str);
|
274
|
}
|
275
|
else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
|
276
|
$object->operator = 'and';
|
277
|
$object->value = explode(',', $str);
|
278
|
}
|
279
|
|
280
|
// Keep an 'error' value if invalid strings were given.
|
281
|
if (!empty($str) && (empty($object->value) || !is_array($object->value))) {
|
282
|
$object->value = array(-1);
|
283
|
$object->invalid_input = TRUE;
|
284
|
return $object;
|
285
|
}
|
286
|
|
287
|
if (empty($object->value)) {
|
288
|
$object->value = array();
|
289
|
}
|
290
|
|
291
|
// Doubly ensure that all values are numeric only.
|
292
|
foreach ($object->value as $id => $value) {
|
293
|
$object->value[$id] = intval($value);
|
294
|
}
|
295
|
|
296
|
return $object;
|
297
|
}
|
298
|
|
299
|
/**
|
300
|
* Set a token/value pair to be replaced later in the request, specifically in
|
301
|
* ctools_preprocess_page().
|
302
|
*
|
303
|
* @param $token
|
304
|
* The token to be replaced later, during page rendering. This should
|
305
|
* ideally be a string inside of an HTML comment, so that if there is
|
306
|
* no replacement, the token will not render on the page.
|
307
|
* @param $type
|
308
|
* The type of the token. Can be either 'variable', which will pull data
|
309
|
* directly from the page variables
|
310
|
* @param $argument
|
311
|
* If $type == 'variable' then argument should be the key to fetch from
|
312
|
* the $variables. If $type == 'callback' then it should either be the
|
313
|
* callback, or an array that will be sent to call_user_func_array().
|
314
|
*
|
315
|
* @return
|
316
|
* A array of token/variable names to be replaced.
|
317
|
*/
|
318
|
function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) {
|
319
|
static $tokens = array();
|
320
|
|
321
|
if (isset($token)) {
|
322
|
$tokens[$token] = array($type, $argument);
|
323
|
}
|
324
|
return $tokens;
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* Easily set a token from the page variables.
|
329
|
*
|
330
|
* This function can be used like this:
|
331
|
* $token = ctools_set_variable_token('tabs');
|
332
|
*
|
333
|
* $token will then be a simple replacement for the 'tabs' about of the
|
334
|
* variables available in the page template.
|
335
|
*/
|
336
|
function ctools_set_variable_token($token) {
|
337
|
$string = '<!-- ctools-page-' . $token . ' -->';
|
338
|
ctools_set_page_token($string, 'variable', $token);
|
339
|
return $string;
|
340
|
}
|
341
|
|
342
|
/**
|
343
|
* Easily set a token from the page variables.
|
344
|
*
|
345
|
* This function can be used like this:
|
346
|
* $token = ctools_set_variable_token('id', 'mymodule_myfunction');
|
347
|
*/
|
348
|
function ctools_set_callback_token($token, $callback) {
|
349
|
// If the callback uses arguments they are considered in the token.
|
350
|
if (is_array($callback)) {
|
351
|
$token .= '-' . md5(serialize($callback));
|
352
|
}
|
353
|
$string = '<!-- ctools-page-' . $token . ' -->';
|
354
|
ctools_set_page_token($string, 'callback', $callback);
|
355
|
return $string;
|
356
|
}
|
357
|
|
358
|
/**
|
359
|
* Tell CTools that sidebar blocks should not be rendered.
|
360
|
*
|
361
|
* It is often desirable to not display sidebars when rendering a page,
|
362
|
* particularly when using Panels. This informs CTools to alter out any
|
363
|
* sidebar regions during block render.
|
364
|
*/
|
365
|
function ctools_set_no_blocks($blocks = FALSE) {
|
366
|
$status = &drupal_static(__FUNCTION__, TRUE);
|
367
|
$status = $blocks;
|
368
|
}
|
369
|
|
370
|
/**
|
371
|
* Wrapper function to create UUIDs via ctools, falls back on UUID module
|
372
|
* if it is enabled. This code is a copy of uuid.inc from the uuid module.
|
373
|
* @see http://php.net/uniqid#65879
|
374
|
*/
|
375
|
|
376
|
function ctools_uuid_generate() {
|
377
|
if (!module_exists('uuid')) {
|
378
|
ctools_include('uuid');
|
379
|
|
380
|
$callback = drupal_static(__FUNCTION__);
|
381
|
|
382
|
if (empty($callback)) {
|
383
|
if (function_exists('uuid_create') && !function_exists('uuid_make')) {
|
384
|
$callback = '_ctools_uuid_generate_pecl';
|
385
|
}
|
386
|
elseif (function_exists('com_create_guid')) {
|
387
|
$callback = '_ctools_uuid_generate_com';
|
388
|
}
|
389
|
else {
|
390
|
$callback = '_ctools_uuid_generate_php';
|
391
|
}
|
392
|
}
|
393
|
return $callback();
|
394
|
}
|
395
|
else {
|
396
|
return uuid_generate();
|
397
|
}
|
398
|
}
|
399
|
|
400
|
/**
|
401
|
* Check that a string appears to be in the format of a UUID.
|
402
|
* @see http://drupal.org/project/uuid
|
403
|
*
|
404
|
* @param $uuid
|
405
|
* The string to test.
|
406
|
*
|
407
|
* @return
|
408
|
* TRUE if the string is well formed.
|
409
|
*/
|
410
|
function ctools_uuid_is_valid($uuid = '') {
|
411
|
if (empty($uuid)) {
|
412
|
return FALSE;
|
413
|
}
|
414
|
if (function_exists('uuid_is_valid') || module_exists('uuid')) {
|
415
|
return uuid_is_valid($uuid);
|
416
|
}
|
417
|
else {
|
418
|
ctools_include('uuid');
|
419
|
return uuid_is_valid($uuid);
|
420
|
}
|
421
|
}
|
422
|
|
423
|
/**
|
424
|
* Add an array of classes to the body.
|
425
|
*
|
426
|
* @param mixed $classes
|
427
|
* A string or an array of class strings to add.
|
428
|
* @param string $hook
|
429
|
* The theme hook to add the class to. The default is 'html' which will
|
430
|
* affect the body tag.
|
431
|
*/
|
432
|
function ctools_class_add($classes, $hook = 'html') {
|
433
|
if (!is_array($classes)) {
|
434
|
$classes = array($classes);
|
435
|
}
|
436
|
|
437
|
$static = &drupal_static('ctools_process_classes', array());
|
438
|
if (!isset($static[$hook]['add'])) {
|
439
|
$static[$hook]['add'] = array();
|
440
|
}
|
441
|
foreach ($classes as $class) {
|
442
|
$static[$hook]['add'][] = $class;
|
443
|
}
|
444
|
}
|
445
|
|
446
|
/**
|
447
|
* Remove an array of classes from the body.
|
448
|
*
|
449
|
* @param mixed $classes
|
450
|
* A string or an array of class strings to remove.
|
451
|
* @param string $hook
|
452
|
* The theme hook to remove the class from. The default is 'html' which will
|
453
|
* affect the body tag.
|
454
|
*/
|
455
|
function ctools_class_remove($classes, $hook = 'html') {
|
456
|
if (!is_array($classes)) {
|
457
|
$classes = array($classes);
|
458
|
}
|
459
|
|
460
|
$static = &drupal_static('ctools_process_classes', array());
|
461
|
if (!isset($static[$hook]['remove'])) {
|
462
|
$static[$hook]['remove'] = array();
|
463
|
}
|
464
|
foreach ($classes as $class) {
|
465
|
$static[$hook]['remove'][] = $class;
|
466
|
}
|
467
|
}
|
468
|
|
469
|
// -----------------------------------------------------------------------
|
470
|
// Drupal core hooks
|
471
|
|
472
|
/**
|
473
|
* Implement hook_init to keep our global CSS at the ready.
|
474
|
*/
|
475
|
function ctools_init() {
|
476
|
ctools_add_css('ctools');
|
477
|
// If we are sure that CTools' AJAX is in use, change the error handling.
|
478
|
if (!empty($_REQUEST['ctools_ajax'])) {
|
479
|
ini_set('display_errors', 0);
|
480
|
register_shutdown_function('ctools_shutdown_handler');
|
481
|
}
|
482
|
|
483
|
// Clear plugin cache on the module page submit.
|
484
|
if ($_GET['q'] == 'admin/modules/list/confirm' && !empty($_POST)) {
|
485
|
cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
|
486
|
}
|
487
|
}
|
488
|
|
489
|
/**
|
490
|
* Shutdown handler used during ajax operations to help catch fatal errors.
|
491
|
*/
|
492
|
function ctools_shutdown_handler() {
|
493
|
if (function_exists('error_get_last') AND ($error = error_get_last())) {
|
494
|
switch ($error['type']) {
|
495
|
case E_ERROR:
|
496
|
case E_CORE_ERROR:
|
497
|
case E_COMPILE_ERROR:
|
498
|
case E_USER_ERROR:
|
499
|
// Do this manually because including files here is dangerous.
|
500
|
$commands = array(
|
501
|
array(
|
502
|
'command' => 'alert',
|
503
|
'title' => t('Error'),
|
504
|
'text' => t('Unable to complete operation. Fatal error in @file on line @line: @message', array(
|
505
|
'@file' => $error['file'],
|
506
|
'@line' => $error['line'],
|
507
|
'@message' => $error['message'],
|
508
|
)),
|
509
|
),
|
510
|
);
|
511
|
|
512
|
// Change the status code so that the client will read the AJAX returned.
|
513
|
header('HTTP/1.1 200 OK');
|
514
|
drupal_json($commands);
|
515
|
}
|
516
|
}
|
517
|
}
|
518
|
|
519
|
/**
|
520
|
* Implements hook_theme().
|
521
|
*/
|
522
|
function ctools_theme() {
|
523
|
ctools_include('utility');
|
524
|
$items = array();
|
525
|
ctools_passthrough('ctools', 'theme', $items);
|
526
|
return $items;
|
527
|
}
|
528
|
|
529
|
/**
|
530
|
* Implements hook_menu().
|
531
|
*/
|
532
|
function ctools_menu() {
|
533
|
ctools_include('utility');
|
534
|
$items = array();
|
535
|
ctools_passthrough('ctools', 'menu', $items);
|
536
|
return $items;
|
537
|
}
|
538
|
|
539
|
/**
|
540
|
* Implementation of hook_cron. Clean up old caches.
|
541
|
*/
|
542
|
function ctools_cron() {
|
543
|
ctools_include('utility');
|
544
|
$items = array();
|
545
|
ctools_passthrough('ctools', 'cron', $items);
|
546
|
}
|
547
|
|
548
|
/**
|
549
|
* Ensure the CTools CSS cache is flushed whenever hook_flush_caches is invoked.
|
550
|
*/
|
551
|
function ctools_flush_caches() {
|
552
|
// Do not actually flush caches if running on cron. Drupal uses this hook
|
553
|
// in an inconsistent fashion and it does not necessarily mean to *flush*
|
554
|
// caches when running from cron. Instead it's just getting a list of cache
|
555
|
// tables and may not do any flushing.
|
556
|
if (!empty($GLOBALS['locks']['cron'])) {
|
557
|
return;
|
558
|
}
|
559
|
|
560
|
ctools_include('css');
|
561
|
ctools_css_flush_caches();
|
562
|
}
|
563
|
|
564
|
/**
|
565
|
* Implements hook_element_info_alter().
|
566
|
*
|
567
|
*/
|
568
|
function ctools_element_info_alter(&$type) {
|
569
|
ctools_include('dependent');
|
570
|
ctools_dependent_element_info_alter($type);
|
571
|
}
|
572
|
|
573
|
/**
|
574
|
* Implementation of hook_file_download()
|
575
|
*
|
576
|
* When using the private file system, we have to let Drupal know it's ok to
|
577
|
* download CSS and image files from our temporary directory.
|
578
|
*/
|
579
|
function ctools_file_download($filepath) {
|
580
|
if (strpos($filepath, 'ctools') === 0) {
|
581
|
$mime = file_get_mimetype($filepath);
|
582
|
// For safety's sake, we allow only text and images.
|
583
|
if (strpos($mime, 'text') === 0 || strpos($mime, 'image') === 0) {
|
584
|
return array('Content-type:' . $mime);
|
585
|
}
|
586
|
}
|
587
|
}
|
588
|
|
589
|
/**
|
590
|
* Implements hook_registry_files_alter().
|
591
|
*
|
592
|
* Alter the registry of files to automagically include all classes in
|
593
|
* class-based plugins.
|
594
|
*/
|
595
|
function ctools_registry_files_alter(&$files, $indexed_modules) {
|
596
|
ctools_include('registry');
|
597
|
return _ctools_registry_files_alter($files, $indexed_modules);
|
598
|
}
|
599
|
|
600
|
// -----------------------------------------------------------------------
|
601
|
// CTools hook implementations.
|
602
|
|
603
|
/**
|
604
|
* Implementation of hook_ctools_plugin_directory() to let the system know
|
605
|
* where all our own plugins are.
|
606
|
*/
|
607
|
function ctools_ctools_plugin_directory($owner, $plugin_type) {
|
608
|
if ($owner == 'ctools') {
|
609
|
return 'plugins/' . $plugin_type;
|
610
|
}
|
611
|
}
|
612
|
|
613
|
/**
|
614
|
* Implements hook_ctools_plugin_type().
|
615
|
*/
|
616
|
function ctools_ctools_plugin_type() {
|
617
|
ctools_include('utility');
|
618
|
$items = array();
|
619
|
// Add all the plugins that have their own declaration space elsewhere.
|
620
|
ctools_passthrough('ctools', 'plugin-type', $items);
|
621
|
|
622
|
return $items;
|
623
|
}
|
624
|
|
625
|
// -----------------------------------------------------------------------
|
626
|
// Drupal theme preprocess hooks that must be in the .module file.
|
627
|
|
628
|
/**
|
629
|
* A theme preprocess function to automatically allow panels-based node
|
630
|
* templates based upon input when the panel was configured.
|
631
|
*/
|
632
|
function ctools_preprocess_node(&$vars) {
|
633
|
// The 'ctools_template_identifier' attribute of the node is added when the pane is
|
634
|
// rendered.
|
635
|
if (!empty($vars['node']->ctools_template_identifier)) {
|
636
|
$vars['ctools_template_identifier'] = check_plain($vars['node']->ctools_template_identifier);
|
637
|
$vars['theme_hook_suggestions'][] = 'node__panel__' . check_plain($vars['node']->ctools_template_identifier);
|
638
|
}
|
639
|
}
|
640
|
|
641
|
function ctools_page_alter(&$page) {
|
642
|
$page['#post_render'][] = 'ctools_page_token_processing';
|
643
|
}
|
644
|
|
645
|
/**
|
646
|
* A theme post_render callback to allow content type plugins to use page
|
647
|
* template variables which are not yet available when the content type is
|
648
|
* rendered.
|
649
|
*/
|
650
|
function ctools_page_token_processing($children, $elements) {
|
651
|
$tokens = ctools_set_page_token();
|
652
|
if (!empty($tokens)) {
|
653
|
foreach ($tokens as $token => $key) {
|
654
|
list($type, $argument) = $key;
|
655
|
switch ($type) {
|
656
|
case 'variable':
|
657
|
$tokens[$token] = isset($variables[$argument]) ? $variables[$argument] : '';
|
658
|
break;
|
659
|
case 'callback':
|
660
|
if (is_string($argument) && function_exists($argument)) {
|
661
|
$tokens[$token] = $argument($variables);
|
662
|
}
|
663
|
if (is_array($argument) && function_exists($argument[0])) {
|
664
|
$function = array_shift($argument);
|
665
|
$argument = array_merge(array(&$variables), $argument);
|
666
|
$tokens[$token] = call_user_func_array($function, $argument);
|
667
|
}
|
668
|
break;
|
669
|
}
|
670
|
}
|
671
|
$children = strtr($children, $tokens);
|
672
|
}
|
673
|
return $children;
|
674
|
}
|
675
|
|
676
|
/**
|
677
|
* Implements hook_process().
|
678
|
*
|
679
|
* Add and remove CSS classes from the variables array. We use process so that
|
680
|
* we alter anything added in the preprocess hooks.
|
681
|
*/
|
682
|
function ctools_process(&$variables, $hook) {
|
683
|
if (!isset($variables['classes'])) {
|
684
|
return;
|
685
|
}
|
686
|
|
687
|
$classes = drupal_static('ctools_process_classes', array());
|
688
|
|
689
|
// Process the classses to add.
|
690
|
if (!empty($classes[$hook]['add'])) {
|
691
|
$add_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['add']);
|
692
|
$variables['classes_array'] = array_unique(array_merge($variables['classes_array'], $add_classes));
|
693
|
}
|
694
|
|
695
|
// Process the classes to remove.
|
696
|
if (!empty($classes[$hook]['remove'])) {
|
697
|
$remove_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['remove']);
|
698
|
$variables['classes_array'] = array_diff($variables['classes_array'], $remove_classes);
|
699
|
}
|
700
|
|
701
|
// Since this runs after template_process(), we need to re-implode the
|
702
|
// classes array.
|
703
|
$variables['classes'] = implode(' ', $variables['classes_array']);
|
704
|
}
|
705
|
|
706
|
// -----------------------------------------------------------------------
|
707
|
// Menu callbacks that must be in the .module file.
|
708
|
|
709
|
/**
|
710
|
* Determine if the current user has access via a plugin.
|
711
|
*
|
712
|
* This function is meant to be embedded in the Drupal menu system, and
|
713
|
* therefore is in the .module file since sub files can't be loaded, and
|
714
|
* takes arguments a little bit more haphazardly than ctools_access().
|
715
|
*
|
716
|
* @param $access
|
717
|
* An access control array which contains the following information:
|
718
|
* - 'logic': and or or. Whether all tests must pass or one must pass.
|
719
|
* - 'plugins': An array of access plugins. Each contains:
|
720
|
* - - 'name': The name of the plugin
|
721
|
* - - 'settings': The settings from the plugin UI.
|
722
|
* - - 'context': Which context to use.
|
723
|
* @param ...
|
724
|
* zero or more context arguments generated from argument plugins. These
|
725
|
* contexts must have an 'id' attached to them so that they can be
|
726
|
* properly associated. The argument plugin system should set this, but
|
727
|
* if the context is coming from elsewhere it will need to be set manually.
|
728
|
*
|
729
|
* @return
|
730
|
* TRUE if access is granted, false if otherwise.
|
731
|
*/
|
732
|
function ctools_access_menu($access) {
|
733
|
// Short circuit everything if there are no access tests.
|
734
|
if (empty($access['plugins'])) {
|
735
|
return TRUE;
|
736
|
}
|
737
|
|
738
|
$contexts = array();
|
739
|
foreach (func_get_args() as $arg) {
|
740
|
if (is_object($arg) && get_class($arg) == 'ctools_context') {
|
741
|
$contexts[$arg->id] = $arg;
|
742
|
}
|
743
|
}
|
744
|
|
745
|
ctools_include('context');
|
746
|
return ctools_access($access, $contexts);
|
747
|
}
|
748
|
|
749
|
/**
|
750
|
* Determine if the current user has access via checks to multiple different
|
751
|
* permissions.
|
752
|
*
|
753
|
* This function is a thin wrapper around user_access that allows multiple
|
754
|
* permissions to be easily designated for use on, for example, a menu callback.
|
755
|
*
|
756
|
* @param ...
|
757
|
* An indexed array of zero or more permission strings to be checked by
|
758
|
* user_access().
|
759
|
*
|
760
|
* @return
|
761
|
* Iff all checks pass will this function return TRUE. If an invalid argument
|
762
|
* is passed (e.g., not a string), this function errs on the safe said and
|
763
|
* returns FALSE.
|
764
|
*/
|
765
|
function ctools_access_multiperm() {
|
766
|
foreach (func_get_args() as $arg) {
|
767
|
if (!is_string($arg) || !user_access($arg)) {
|
768
|
return FALSE;
|
769
|
}
|
770
|
}
|
771
|
return TRUE;
|
772
|
}
|
773
|
|
774
|
/**
|
775
|
* Check to see if the incoming menu item is js capable or not.
|
776
|
*
|
777
|
* This can be used as %ctools_js as part of a path in hook menu. CTools
|
778
|
* ajax functions will automatically change the phrase 'nojs' to 'ajax'
|
779
|
* when it attaches ajax to a link. This can be used to autodetect if
|
780
|
* that happened.
|
781
|
*/
|
782
|
function ctools_js_load($js) {
|
783
|
if ($js == 'ajax') {
|
784
|
return TRUE;
|
785
|
}
|
786
|
return 0;
|
787
|
}
|
788
|
|
789
|
/**
|
790
|
* Menu _load hook.
|
791
|
*
|
792
|
* This function will be called to load an object as a replacement for
|
793
|
* %ctools_export_ui in menu paths.
|
794
|
*/
|
795
|
function ctools_export_ui_load($item_name, $plugin_name) {
|
796
|
$return = &drupal_static(__FUNCTION__, FALSE);
|
797
|
|
798
|
if (!$return) {
|
799
|
ctools_include('export-ui');
|
800
|
$plugin = ctools_get_export_ui($plugin_name);
|
801
|
$handler = ctools_export_ui_get_handler($plugin);
|
802
|
|
803
|
if ($handler) {
|
804
|
return $handler->load_item($item_name);
|
805
|
}
|
806
|
}
|
807
|
|
808
|
return $return;
|
809
|
}
|
810
|
|
811
|
// -----------------------------------------------------------------------
|
812
|
// Caching callbacks on behalf of export-ui.
|
813
|
|
814
|
/**
|
815
|
* Menu access callback for various tasks of export-ui.
|
816
|
*/
|
817
|
function ctools_export_ui_task_access($plugin_name, $op, $item = NULL) {
|
818
|
ctools_include('export-ui');
|
819
|
$plugin = ctools_get_export_ui($plugin_name);
|
820
|
$handler = ctools_export_ui_get_handler($plugin);
|
821
|
|
822
|
if ($handler) {
|
823
|
return $handler->access($op, $item);
|
824
|
}
|
825
|
|
826
|
// Deny access if the handler cannot be found.
|
827
|
return FALSE;
|
828
|
}
|
829
|
|
830
|
/**
|
831
|
* Callback for access control ajax form on behalf of export ui.
|
832
|
*
|
833
|
* Returns the cached access config and contexts used.
|
834
|
* Note that this is assuming that access will be in $item->access -- if it
|
835
|
* is not, an export UI plugin will have to make its own callbacks.
|
836
|
*/
|
837
|
function ctools_export_ui_ctools_access_get($argument) {
|
838
|
ctools_include('export-ui');
|
839
|
list($plugin_name, $key) = explode(':', $argument, 2);
|
840
|
|
841
|
$plugin = ctools_get_export_ui($plugin_name);
|
842
|
$handler = ctools_export_ui_get_handler($plugin);
|
843
|
|
844
|
if ($handler) {
|
845
|
ctools_include('context');
|
846
|
$item = $handler->edit_cache_get($key);
|
847
|
if (!$item) {
|
848
|
$item = ctools_export_crud_load($handler->plugin['schema'], $key);
|
849
|
}
|
850
|
|
851
|
$contexts = ctools_context_load_contexts($item);
|
852
|
return array($item->access, $contexts);
|
853
|
}
|
854
|
}
|
855
|
|
856
|
/**
|
857
|
* Callback for access control ajax form on behalf of export ui
|
858
|
*
|
859
|
* Returns the cached access config and contexts used.
|
860
|
* Note that this is assuming that access will be in $item->access -- if it
|
861
|
* is not, an export UI plugin will have to make its own callbacks.
|
862
|
*/
|
863
|
function ctools_export_ui_ctools_access_set($argument, $access) {
|
864
|
ctools_include('export-ui');
|
865
|
list($plugin_name, $key) = explode(':', $argument, 2);
|
866
|
|
867
|
$plugin = ctools_get_export_ui($plugin_name);
|
868
|
$handler = ctools_export_ui_get_handler($plugin);
|
869
|
|
870
|
if ($handler) {
|
871
|
ctools_include('context');
|
872
|
$item = $handler->edit_cache_get($key);
|
873
|
if (!$item) {
|
874
|
$item = ctools_export_crud_load($handler->plugin['schema'], $key);
|
875
|
}
|
876
|
$item->access = $access;
|
877
|
return $handler->edit_cache_set_key($item, $key);
|
878
|
}
|
879
|
}
|
880
|
|
881
|
/**
|
882
|
* Implements hook_menu_local_tasks_alter().
|
883
|
*/
|
884
|
function ctools_menu_local_tasks_alter(&$data, $router_item, $root_path) {
|
885
|
ctools_include('menu');
|
886
|
_ctools_menu_add_dynamic_items($data, $router_item, $root_path);
|
887
|
}
|
888
|
|
889
|
/**
|
890
|
* Implement hook_block_list_alter() to potentially remove blocks.
|
891
|
*
|
892
|
* This exists in order to replicate Drupal 6's "no blocks" functionality.
|
893
|
*/
|
894
|
function ctools_block_list_alter(&$blocks) {
|
895
|
$check = drupal_static('ctools_set_no_blocks', TRUE);
|
896
|
if (!$check) {
|
897
|
foreach ($blocks as $block_id => $block) {
|
898
|
// @todo -- possibly we can set configuration for this so that users can
|
899
|
// specify which blocks will not get rendered.
|
900
|
if (strpos($block->region, 'sidebar') !== FALSE) {
|
901
|
unset($blocks[$block_id]);
|
902
|
}
|
903
|
}
|
904
|
}
|
905
|
}
|
906
|
|
907
|
/**
|
908
|
* Implement hook_modules_enabled to clear static caches for detecting new plugins
|
909
|
*/
|
910
|
function ctools_modules_enabled($modules) {
|
911
|
ctools_include('plugins');
|
912
|
ctools_get_plugins_reset();
|
913
|
}
|
914
|
|
915
|
/**
|
916
|
* Menu theme callback.
|
917
|
*
|
918
|
* This simply ensures that Panels ajax calls are rendered in the same
|
919
|
* theme as the original page to prevent .css file confusion.
|
920
|
*
|
921
|
* To use this, set this as the theme callback on AJAX related menu
|
922
|
* items. Since the ajax page state won't be sent during ajax requests,
|
923
|
* it should be safe to use even if ajax isn't invoked.
|
924
|
*/
|
925
|
function ctools_ajax_theme_callback() {
|
926
|
if (!empty($_POST['ajax_page_state']['theme'])) {
|
927
|
return $_POST['ajax_page_state']['theme'];
|
928
|
}
|
929
|
}
|
930
|
|
931
|
/**
|
932
|
* Implements hook_ctools_entity_context_alter().
|
933
|
*/
|
934
|
function ctools_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) {
|
935
|
ctools_include('context');
|
936
|
switch ($plugin_id) {
|
937
|
case 'entity_id:taxonomy_term':
|
938
|
$plugin['no ui'] = TRUE;
|
939
|
break;
|
940
|
case 'entity:user':
|
941
|
$plugin = ctools_get_context('user');
|
942
|
unset($plugin['no ui']);
|
943
|
unset($plugin['no required context ui']);
|
944
|
break;
|
945
|
}
|
946
|
|
947
|
// Apply restrictions on taxonomy term reverse relationships whose
|
948
|
// restrictions are in the settings on the field.
|
949
|
if (!empty($plugin['parent']) &&
|
950
|
$plugin['parent'] == 'entity_from_field' &&
|
951
|
!empty($plugin['reverse']) &&
|
952
|
$plugin['to entity'] == 'taxonomy_term') {
|
953
|
$field = field_info_field($plugin['field name']);
|
954
|
if (isset($field['settings']['allowed_values'][0]['vocabulary'])) {
|
955
|
$plugin['required context']->restrictions = array('vocabulary' => array($field['settings']['allowed_values'][0]['vocabulary']));
|
956
|
}
|
957
|
}
|
958
|
}
|