| 1 |
<?php
|
| 2 |
// $Id: reptag_module.inc,v 1.1.2.10 2007/07/25 16:14:31 profix898 Exp $
|
| 3 |
|
| 4 |
define('REPTAG_PROCALL', 0);
|
| 5 |
define('REPTAG_PROCSTATIC', 1);
|
| 6 |
define('REPTAG_PROCDYNAMIC', 2);
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Function _reptag_module_list().
|
| 10 |
* (return a list of available modules)
|
| 11 |
*/
|
| 12 |
function _reptag_module_list($rebuild = FALSE, $enabled = FALSE, $proc = REPTAG_PROCALL, $modules = NULL) {
|
| 13 |
$reptag_modules = array();
|
| 14 |
// build query and fetch modules
|
| 15 |
$query = $rebuild ? 'SELECT module, enabled, weight, roles FROM {reptag_registry}' : 'SELECT * FROM {reptag_registry}';
|
| 16 |
if ($enabled) {
|
| 17 |
$query .= ' WHERE enabled = 1';
|
| 18 |
}
|
| 19 |
switch ($proc) {
|
| 20 |
case REPTAG_PROCSTATIC:
|
| 21 |
$query .= $enabled ? ' AND' : ' WHERE';
|
| 22 |
$query .= ' static = 1';
|
| 23 |
break;
|
| 24 |
case REPTAG_PROCDYNAMIC:
|
| 25 |
$query .= $enabled ? ' AND' : ' WHERE';
|
| 26 |
$query .= ' static = 0';
|
| 27 |
break;
|
| 28 |
default:
|
| 29 |
}
|
| 30 |
$query .= ' ORDER BY weight ASC, module ASC';
|
| 31 |
$result = db_query($query);
|
| 32 |
while ($module = db_fetch_array($result)) {
|
| 33 |
if (!$modules || in_array($module['module'], $modules)) {
|
| 34 |
$reptag_modules[$module['module']] = $module;
|
| 35 |
$reptag_modules[$module['module']]['roles'] = unserialize($module['roles']);
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
// rebuild module registry
|
| 40 |
if ($rebuild && !$enabled) {
|
| 41 |
$modules_rebuild = _reptag_module_rebuild();
|
| 42 |
$reptag_modules = _reptag_array_intersect_key($reptag_modules, $modules_rebuild);
|
| 43 |
$reptag_modules = _reptag_array_merge_key($modules_rebuild, $reptag_modules);
|
| 44 |
_reptag_module_register($reptag_modules, TRUE);
|
| 45 |
uasort($reptag_modules, '_reptag_module_cmp');
|
| 46 |
}
|
| 47 |
|
| 48 |
return $reptag_modules;
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Function _reptag_module_cmp().
|
| 53 |
* (sort modules by weigth and name)
|
| 54 |
*/
|
| 55 |
function _reptag_module_cmp($a, $b) {
|
| 56 |
if ($a['weight'] == $b['weight']) {
|
| 57 |
return ($a['module'] == $b['module']) ? 0 : (($a['module'] < $b['module']) ? -1 : 1);
|
| 58 |
}
|
| 59 |
return ($a['weight'] < $b['weight']) ? -1 : 1;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Function _reptag_module_rebuild().
|
| 64 |
* (rebuild the modules list)
|
| 65 |
*/
|
| 66 |
function _reptag_module_rebuild() {
|
| 67 |
$reptag_modules = array();
|
| 68 |
// module (.tags)
|
| 69 |
$files = file_scan_directory(drupal_get_path('module', 'reptag'), '\.tags.inc$');
|
| 70 |
foreach ($files as $file) {
|
| 71 |
include_once($file->filename);
|
| 72 |
$module = substr($file->name, 0, -5);
|
| 73 |
$function = '_reptag_'. $module .'_info';
|
| 74 |
if (function_exists($function)) {
|
| 75 |
$reptag_info = $function();
|
| 76 |
$reptag_modules[$file->name] = array(
|
| 77 |
'module' => $module,
|
| 78 |
'path' => $file->filename,
|
| 79 |
'static' => is_array($reptag_info) ? $reptag_info[1] : FALSE,
|
| 80 |
'description' => check_plain(is_array($reptag_info) ? $reptag_info[0] : $reptag_info),
|
| 81 |
'enabled' => FALSE,
|
| 82 |
'weight' => 0,
|
| 83 |
'roles' => array()
|
| 84 |
);
|
| 85 |
}
|
| 86 |
}
|
| 87 |
// other modules (hook_reptag)
|
| 88 |
$modules = module_implements('reptag');
|
| 89 |
foreach ($modules as $module) {
|
| 90 |
$reptag_info = module_invoke($module, 'reptag', 'info');
|
| 91 |
$reptag_modules[$module] = array(
|
| 92 |
'module' => $module,
|
| 93 |
'path' => '',
|
| 94 |
'static' => is_array($reptag_info) ? $reptag_info[1] : TRUE,
|
| 95 |
'description' => check_plain(is_array($reptag_info) ? $reptag_info[0] : $reptag_info),
|
| 96 |
'enabled' => FALSE,
|
| 97 |
'weight' => 0,
|
| 98 |
'roles' => array()
|
| 99 |
);
|
| 100 |
}
|
| 101 |
|
| 102 |
return $reptag_modules;
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Function _reptag_module_register().
|
| 107 |
* (register a module / update a module's registry)
|
| 108 |
*/
|
| 109 |
function _reptag_module_register($modules, $reset = FALSE) {
|
| 110 |
db_lock_table('reptag_registry');
|
| 111 |
if ($reset) {
|
| 112 |
db_query('DELETE FROM {reptag_registry}');
|
| 113 |
}
|
| 114 |
foreach ($modules as $module => $details) {
|
| 115 |
if ($reset) {
|
| 116 |
db_query("INSERT INTO {reptag_registry} (module, path, static, description, enabled, weight, roles)
|
| 117 |
VALUES ('%s', '%s', %d, '%s', %d, %d, '%s')", $module, $details['path'], $details['static'], $details['description'], $details['enabled'], $details['weight'], serialize($details['roles']));
|
| 118 |
}
|
| 119 |
else {
|
| 120 |
db_query("UPDATE {reptag_registry} SET enabled = %d, weight = %d, roles = '%s' WHERE module = '%s'",
|
| 121 |
$details['enabled'], $details['weight'], serialize($details['roles']), $module);
|
| 122 |
}
|
| 123 |
}
|
| 124 |
db_unlock_tables();
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Function _reptag_module_reset().
|
| 129 |
*/
|
| 130 |
function _reptag_module_reset() {
|
| 131 |
_reptag_module_register(_reptag_module_rebuild(), TRUE);
|
| 132 |
}
|
| 133 |
|
| 134 |
/**
|
| 135 |
* Function _reptag_module_invoke().
|
| 136 |
*/
|
| 137 |
function _reptag_module_invoke() {
|
| 138 |
static $modules = array();
|
| 139 |
|
| 140 |
$args = func_get_args();
|
| 141 |
$module = array_shift($args);
|
| 142 |
$hook = array_shift($args);
|
| 143 |
|
| 144 |
if (!is_array($module)) {
|
| 145 |
$module = array(
|
| 146 |
'module' => $module,
|
| 147 |
'path' => $modules[$module],
|
| 148 |
);
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
// include the .tags file once
|
| 152 |
if ($module['path'] && !isset($modules[$module['module']])) {
|
| 153 |
include_once($module['path']);
|
| 154 |
}
|
| 155 |
// cache the path
|
| 156 |
$modules[$module['module']] = $module['path'];
|
| 157 |
}
|
| 158 |
|
| 159 |
// call the module hook
|
| 160 |
if (empty($module['path'])) {
|
| 161 |
if ($hook != 'process') {
|
| 162 |
array_unshift($args, $hook);
|
| 163 |
}
|
| 164 |
$function = ($hook == 'process') ? 'reptag_process' : 'reptag';
|
| 165 |
if (module_hook($module['module'], $function)) {
|
| 166 |
return call_user_func_array($module['module'] .'_'. $function, $args);
|
| 167 |
}
|
| 168 |
}
|
| 169 |
else {
|
| 170 |
$function = '_reptag_'. substr($module['module'], 0, -5) .'_'. $hook;
|
| 171 |
if (function_exists($function)) {
|
| 172 |
return call_user_func_array($function, $args);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
return FALSE;
|
| 177 |
}
|
| 178 |
|
| 179 |
/**
|
| 180 |
* Function _reptag_module_require().
|
| 181 |
*/
|
| 182 |
function _reptag_module_require($module) {
|
| 183 |
if (!empty($module['path'])) {
|
| 184 |
include_once($module['path']);
|
| 185 |
}
|
| 186 |
|
| 187 |
$require_func = '_reptag_'. substr($module['module'], 0, -5) .'_require';
|
| 188 |
if (function_exists($require_func)) {
|
| 189 |
return $require_func();
|
| 190 |
}
|
| 191 |
|
| 192 |
return TRUE;
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
* Function _reptag_module_access().
|
| 197 |
* (filter modules by permissions/roles)
|
| 198 |
*/
|
| 199 |
function _reptag_module_access($modules, $roles = NULL) {
|
| 200 |
global $user;
|
| 201 |
|
| 202 |
if (!$roles) {
|
| 203 |
$roles = _reptag_user_get_roles($user->uid);
|
| 204 |
}
|
| 205 |
foreach ($modules as $module => $details) {
|
| 206 |
if (!(array_intersect($details['roles'], $roles) || $user->uid == 1)) {
|
| 207 |
unset($modules[$module]);
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
return $modules;
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Function _reptag_module_tags().
|
| 216 |
* (fetch all available tags)
|
| 217 |
*/
|
| 218 |
function _reptag_module_tags($proc = REPTAG_PROCALL, $roles = NULL, $content = NULL, $modules = NULL) {
|
| 219 |
$module_tags = array();
|
| 220 |
|
| 221 |
$modules = _reptag_module_list(FALSE, TRUE, $proc, $modules);
|
| 222 |
$modules = _reptag_module_access($modules, $roles);
|
| 223 |
foreach ($modules as $module => $details) {
|
| 224 |
$module_tags[$module] = _reptag_module_invoke($details, 'init', $content);
|
| 225 |
}
|
| 226 |
|
| 227 |
return array_filter($module_tags);
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Function _reptag_module_help().
|
| 232 |
*/
|
| 233 |
function _reptag_module_help($roles = NULL, $modules = NULL) {
|
| 234 |
$helptags = array();
|
| 235 |
|
| 236 |
$modules = _reptag_module_list(FALSE, TRUE, REPTAG_PROCALL, $modules);
|
| 237 |
$modules = _reptag_module_access($modules, $roles);
|
| 238 |
foreach ($modules as $module => $details) {
|
| 239 |
$helptags[$module] = _reptag_module_invoke($details, 'help');
|
| 240 |
}
|
| 241 |
|
| 242 |
return array_filter($helptags);
|
| 243 |
}
|