Newer
Older
Earl Miles
committed
<?php
Earl Miles
committed
/**
* @file
* CTools primary module file.
*
* Most of the CTools tools are in their own .inc files. This contains
* nothing more than a few convenience functions and some hooks that
* must be implemented in the module file.
*/
Earl Miles
committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
define('CTOOLS_API_VERSION', '1.0');
/**
* Test the CTools API version.
*
* This function can always be used to safely test if CTools has the minimum
* API version that your module can use. It can also try to protect you from
* running if the CTools API version is too new, but if you do that you need
* to be very quick about watching CTools API releases and release new versions
* of your software as soon as the new release is made, or people might end up
* updating CTools and having your module shut down without any recourse.
*
* It is recommended that every hook of your module that might use CTools or
* might lead to a use of CTools be guarded like this:
*
* @code
* if (!module_invoke('ctools', 'api_version', '1.0')) {
* return;
* }
* @endcode
*
* Note that some hooks such as _menu() or _theme() must return an array().
*
* You can use it in your hook_requirements to report this error condition
* like this:
*
* @code
* define('MODULENAME_MINIMUM_CTOOLS_API_VERSION', '1.0');
* define('MODULENAME_MAXIMUM_CTOOLS_API_VERSION', '1.1');
*
* function MODULENAME_requirements($phase) {
* $requirements = array();
* if (!module_invoke('ctools', 'api_version', MODULENAME_MINIMUM_CTOOLS_API_VERSION, MODULENAME_MAXIMUM_CTOOLS_API_VERSION)) {
* $requirements['MODULENAME_ctools'] = array(
* 'title' => $t('MODULENAME required Chaos Tool Suite (CTools) API Version'),
* 'value' => t('Between @a and @b', array('@a' => MODULENAME_MINIMUM_CTOOLS_API_VERSION, '@b' => MODULENAME_MAXIMUM_CTOOLS_API_VERSION)),
* 'severity' => REQUIREMENT_ERROR,
* );
* }
* return $requirements;
* }
* @endcode
*
* Please note that the version is a string, not an floating point number.
* This will matter once CTools reaches version 1.10.
*
* A CTools API changes history will be kept in API.txt. Not every new
* version of CTools will necessarily update the API version.
* @param $minimum
* The minimum version of CTools necessary for your software to run with it.
* @param $maximum
* The maximum version of CTools allowed for your software to run with it.
*/
function ctools_api_version($minimum, $maximum = NULL) {
if (version_compare(CTOOLS_API_VERSION, $minimum, '<')) {
return FALSE;
}
if (isset($maximum) && version_compare(CTOOLS_API_VERSION, $maximum, '>')) {
return FALSE;
}
return TRUE;
}
Earl Miles
committed
/**
* Include ctools .inc files as necessary.
*/
function ctools_include($file) {
static $used = array();
if (!isset($used[$file])) {
require_once './' . drupal_get_path('module', 'ctools') . "/includes/$file.inc";
}
$used[$file] = TRUE;
}
* Provide the proper path to a CTools image
function ctools_image_path($image) {
return drupal_get_path('module', 'ctools') . '/images/' . $image;
Earl Miles
committed
/**
* Include views .css files.
Earl Miles
committed
*/
function ctools_add_css($file) {
drupal_add_css(drupal_get_path('module', 'ctools') . "/css/$file.css");
Earl Miles
committed
}
* Include views .js files.
*/
function ctools_add_js($file) {
drupal_add_js(drupal_get_path('module', 'ctools') . "/js/$file.js");
}
/**
* Implement hook_init to keep our global CSS at the ready.
*/
function ctools_init() {
ctools_add_css('ctools');
}
/**
* Provide a hook passthrough to included files.
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
* To organize things neatly, each CTools tool gets its own toolname.$type.inc
* file. If it exists, it's loaded and ctools_$tool_$type() is executed.
* To save time we pass the $items array in so we don't need to do array
* addition. It modifies the array by reference and doesn't need to return it.
*/
function _ctools_passthrough(&$items, $type = 'theme') {
$files = drupal_system_listing('.' . $type . '.inc$', drupal_get_path('module', 'ctools') . '/includes', 'name', 0);
foreach ($files as $file) {
require_once './' . $file->filename;
list($tool) = explode('.', $file->name, 2);
$function = 'ctools_' . $tool . '_' . $type;
if (function_exists($function)) {
$function($items);
}
}
}
/**
* Implementation of hook_theme().
*/
function ctools_theme() {
$items = array();
_ctools_passthrough($items, 'theme');
return $items;
}
/**
* Implementation of hook_menu().
function ctools_menu() {
$items = array();
_ctools_passthrough($items, 'menu');
return $items;
}
/**
* Implementation of hook_ctools_plugin_dierctory() to let the system know
* we implement task and task_handler plugins.
*/
function ctools_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
}
Earl Miles
committed
/**
* Get a list of roles in the system.
*/
function ctools_get_roles() {
static $roles = NULL;
if (!isset($roles)) {
$roles = array();
$result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
while ($obj = db_fetch_object($result)) {
$roles[$obj->rid] = $obj->name;
}
}
return $roles;
}
/**
* Determine if the current user has access via a plugin.
*
* This function is meant to be embedded in the Drupal menu system, and
* therefore is in the .module file since sub files can't be loaded, and
* takes arguments a little bit more haphazardly than ctools_access().
*
Earl Miles
committed
* @param $access
* An access control array which contains the following information:
* - 'logic': and or or. Whether all tests must pass or one must pass.
* - 'plugins': An array of access plugins. Each contains:
* - - 'name': The name of the plugin
* - - 'settings': The settings from the plugin UI.
* - - 'context': Which context to use.
* @param ...
* zero or more context arguments generated from argument plugins. These
* contexts must have an 'id' attached to them so that they can be
* properly associated. The argument plugin system should set this, but
* if the context is coming from elsewhere it will need to be set manually.
*
* @return
* TRUE if access is granted, false if otherwise.
*/
Earl Miles
committed
function ctools_access_menu($access) {
// Short circuit everything if there are no access tests.
if (empty($access['plugins'])) {
return TRUE;
}
$contexts = array();
foreach (func_get_args() as $arg) {
if (is_object($arg) && get_class($arg) == 'ctools_context') {
$contexts[$arg->id] = $arg;
}
}
ctools_include('context');
Earl Miles
committed
return ctools_access($access, $contexts);
/**
* Implementation of hook_cron. Clean up old caches.
*/
function ctools_cron() {
if (variable_get('ctools_last_cron', 0) < time() - 86400) {
variable_set('ctools_last_cron', time());
ctools_include('object-cache');
Earl Miles
committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Break x,y,z and x+y+z into an array. Numeric only.
*
* @param $str
* The string to parse.
*
* @return $object
* An object containing
* - operator: Either 'and' or 'or'
* - value: An array of numeric values.
*/
function ctools_break_phrase($str) {
$object = new stdClass();
if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
// The '+' character in a query string may be parsed as ' '.
$object->operator = 'or';
$object->value = preg_split('/[+ ]/', $str);
}
else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
$object->operator = 'and';
$object->value = explode(',', $str);
}
// Keep an 'error' value if invalid strings were given.
if (!empty($str) && (empty($object->value) || !is_array($object->value))) {
$object->value = array(-1);
$object->invalid_input = TRUE;
return $object;
}
// Doubly ensure that all values are numeric only.
foreach ($object->value as $id => $value) {
$object->value[$id] = intval($value);
}
return $object;
}
/**
* A theme preprocess function to automatically allow panels-based node
* templates based upon input when the panel was configured.
*/
function ctools_preprocess_node(&$vars) {
// The 'panel_identifier' attribute of the node is added when the pane is
// rendered.
if (!empty($vars['node']->panel_identifier)) {
$vars['panel_identifier'] = check_plain($vars['node']->panel_identifier);
$vars['template_files'][] = 'node-panel-' . check_plain($vars['node']->panel_identifier);
}
}
/**
* Ensure the CTools CSS cache is flushed whenever hook_flush_caches is invoked.
*/
function ctools_flush_caches() {
ctools_include('css');
ctools_css_flush_caches();
}