| 1 |
<?php
|
| 2 |
// $Id: reptag.module,v 1.35.2.18 2007/07/25 16:14:31 profix898 Exp $
|
| 3 |
|
| 4 |
define('REPTAG_LOCALE_STRICT', 0);
|
| 5 |
define('REPTAG_LOCALE_FALLBACK', 1);
|
| 6 |
|
| 7 |
require_once(drupal_get_path('module', 'reptag') .'/reptag_process.inc');
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_theme()
|
| 11 |
*/
|
| 12 |
function reptag_theme() {
|
| 13 |
return array(
|
| 14 |
'reptag_table' => array(
|
| 15 |
'arguments' => array('form' => NULL),
|
| 16 |
),
|
| 17 |
'reptag_table_multi' => array(
|
| 18 |
'arguments' => array('form' => NULL),
|
| 19 |
),
|
| 20 |
'reptag_dialog' => array(
|
| 21 |
'template' => 'reptag-dialog',
|
| 22 |
'file' => 'reptag_admin_table.inc',
|
| 23 |
'arguments' => array('content' => NULL, 'title' => t('Rep[lacement]Tags Dialog')),
|
| 24 |
)
|
| 25 |
);
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_perm().
|
| 30 |
*/
|
| 31 |
function reptag_perm() {
|
| 32 |
return array('administer reptag', 'manage sitewide reptags', 'manage user reptags');
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implementation of hook_menu().
|
| 37 |
*/
|
| 38 |
function reptag_menu() {
|
| 39 |
$items = array();
|
| 40 |
//
|
| 41 |
$items['admin/settings/reptag'] = array(
|
| 42 |
'title' => 'Rep[lacement]Tags',
|
| 43 |
'description' => 'Replace special tags with user-defined content.',
|
| 44 |
'access callback' => 'reptag_admin_access',
|
| 45 |
'access arguments' => array(array('administer reptag', 'manage sitewide reptags', 'manage user reptags')),
|
| 46 |
'file' => 'reptag_admin.inc',
|
| 47 |
'page callback' => 'reptag_admin'
|
| 48 |
);
|
| 49 |
$items['admin/settings/reptag/admin'] = array(
|
| 50 |
'title' => 'Settings',
|
| 51 |
'access callback' => 'reptag_admin_access',
|
| 52 |
'access arguments' => array(array('administer reptag')),
|
| 53 |
'file' => 'reptag_admin.inc',
|
| 54 |
'page callback' => 'drupal_get_form',
|
| 55 |
'page arguments' => array('_reptag_admin_general_form'),
|
| 56 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 57 |
'weight' => 0
|
| 58 |
);
|
| 59 |
// Second level tabs
|
| 60 |
$items['admin/settings/reptag/admin/general'] = array(
|
| 61 |
'title' => 'General',
|
| 62 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 63 |
'weight' => 0
|
| 64 |
);
|
| 65 |
$items['admin/settings/reptag/admin/modules'] = array(
|
| 66 |
'title' => 'Modules',
|
| 67 |
'access callback' => 'reptag_admin_access',
|
| 68 |
'access arguments' => array(array('administer reptag')),
|
| 69 |
'file' => 'reptag_admin.inc',
|
| 70 |
'page callback' => 'drupal_get_form',
|
| 71 |
'page arguments' => array('_reptag_admin_modules_form'),
|
| 72 |
'type' => MENU_LOCAL_TASK,
|
| 73 |
'weight' => 1
|
| 74 |
);
|
| 75 |
$items['admin/settings/reptag/admin/permission'] = array(
|
| 76 |
'title' => 'Roles / Permissions',
|
| 77 |
'access callback' => 'reptag_admin_access',
|
| 78 |
'access arguments' => array(array('administer reptag')),
|
| 79 |
'file' => 'reptag_admin.inc',
|
| 80 |
'page callback' => 'drupal_get_form',
|
| 81 |
'page arguments' => array('_reptag_admin_roles_form'),
|
| 82 |
'type' => MENU_LOCAL_TASK,
|
| 83 |
'weight' => 2
|
| 84 |
);
|
| 85 |
$items['admin/settings/reptag/admin/content'] = array(
|
| 86 |
'title' => 'Content Types',
|
| 87 |
'access callback' => 'reptag_admin_access',
|
| 88 |
'access arguments' => array(array('administer reptag')),
|
| 89 |
'file' => 'reptag_admin.inc',
|
| 90 |
'page callback' => 'drupal_get_form',
|
| 91 |
'page arguments' => array('_reptag_admin_content_form'),
|
| 92 |
'type' => MENU_LOCAL_TASK,
|
| 93 |
'weight' => 3
|
| 94 |
);
|
| 95 |
//
|
| 96 |
$items['admin/settings/reptag/site'] = array(
|
| 97 |
'title' => 'SiteWide Tags',
|
| 98 |
'access callback' => 'reptag_admin_access',
|
| 99 |
'access arguments' => array(array('manage sitewide reptags')),
|
| 100 |
'file' => 'reptag_admin.inc',
|
| 101 |
'page callback' => 'drupal_get_form',
|
| 102 |
'page arguments' => array('_reptag_admin_site_form'),
|
| 103 |
'type' => MENU_LOCAL_TASK,
|
| 104 |
'weight' => 1
|
| 105 |
);
|
| 106 |
$items['admin/settings/reptag/user'] = array(
|
| 107 |
'title' => 'User Tags',
|
| 108 |
'access callback' => 'reptag_admin_access',
|
| 109 |
'access arguments' => array(array('manage user reptags')),
|
| 110 |
'file' => 'reptag_admin.inc',
|
| 111 |
'page callback' => 'drupal_get_form',
|
| 112 |
'page arguments' => array('_reptag_admin_user_form'),
|
| 113 |
'type' => MENU_LOCAL_TASK,
|
| 114 |
'weight' => 2
|
| 115 |
);
|
| 116 |
//
|
| 117 |
$items['admin/settings/reptag/ajax/%/%'] = array(
|
| 118 |
'access callback' => 'reptag_admin_access',
|
| 119 |
'access arguments' => array(array('administer reptag', 'manage sitewide reptags', 'manage user reptags')),
|
| 120 |
'file' => 'reptag_callback.inc',
|
| 121 |
'page callback' => '_reptag_callback',
|
| 122 |
'page arguments' => array(4, 5),
|
| 123 |
'type' => MENU_CALLBACK
|
| 124 |
);
|
| 125 |
$items['admin/settings/reptag/nojs/%/%'] = $items['admin/settings/reptag/ajax/%/%'];
|
| 126 |
$items['admin/settings/reptag/nojs/%/%']['page arguments'][] = TRUE;
|
| 127 |
//
|
| 128 |
if (module_exists('workspace')) {
|
| 129 |
$items['workspace/reptag'] = array(
|
| 130 |
'title' => 'RepTags',
|
| 131 |
'access callback' => 'reptag_admin_access',
|
| 132 |
'access arguments' => array(array('manage user reptags')),
|
| 133 |
'file' => 'reptag_admin.inc',
|
| 134 |
'page callback' => 'drupal_get_form',
|
| 135 |
'page arguments' => array('_reptag_admin_user_form'),
|
| 136 |
'type' => MENU_LOCAL_TASK
|
| 137 |
);
|
| 138 |
}
|
| 139 |
//
|
| 140 |
$items['reptag/autocomplete/%'] = array(
|
| 141 |
'access callback' => 'reptag_admin_access',
|
| 142 |
'access arguments' => array(array('administer reptag', 'manage sitewide reptags', 'manage user reptags')),
|
| 143 |
'page callback' => '_reptag_autocomplete',
|
| 144 |
'page arguments' => array(2, FALSE),
|
| 145 |
'type' => MENU_CALLBACK
|
| 146 |
);
|
| 147 |
$items['reptag/autocomplete_all/%'] = array(
|
| 148 |
'access callback' => 'reptag_admin_access',
|
| 149 |
'access arguments' => array(array('administer reptag', 'manage sitewide reptags', 'manage user reptags')),
|
| 150 |
'page callback' => '_reptag_autocomplete',
|
| 151 |
'page arguments' => array(2, TRUE),
|
| 152 |
'type' => MENU_CALLBACK
|
| 153 |
);
|
| 154 |
|
| 155 |
return $items;
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Function reptag_admin_access().
|
| 160 |
*/
|
| 161 |
function reptag_admin_access($perms) {
|
| 162 |
foreach ($perms as $perm) {
|
| 163 |
if (!user_access($perm)) {
|
| 164 |
return FALSE;
|
| 165 |
}
|
| 166 |
}
|
| 167 |
|
| 168 |
return count($perms);
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Function reptag_admin().
|
| 173 |
*/
|
| 174 |
function reptag_admin() {
|
| 175 |
if (user_access('administer reptag')) {
|
| 176 |
$form = '_reptag_admin_general_form';
|
| 177 |
}
|
| 178 |
elseif (user_access('manage sitewide reptags')) {
|
| 179 |
$form = '_reptag_admin_site_form';
|
| 180 |
}
|
| 181 |
elseif (user_access('manage user reptags')) {
|
| 182 |
$form = '_reptag_admin_user_form';
|
| 183 |
}
|
| 184 |
|
| 185 |
return drupal_get_form($form);
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Function _reptag_autocomplete().
|
| 190 |
*/
|
| 191 |
function _reptag_autocomplete($string, $all) {
|
| 192 |
global $user;
|
| 193 |
$string = strtoupper($string);
|
| 194 |
// Table tags
|
| 195 |
$iid = array(0, $user->uid);
|
| 196 |
$table_tags = array_unique(array_keys(_reptag_table_load($iid, REPTAG_LOADTAG_FORMAT)));
|
| 197 |
$matches = _reptag_match_tags($string, $table_tags);
|
| 198 |
|
| 199 |
if ($all) {
|
| 200 |
// Module tags
|
| 201 |
$module_tags = _reptag_module_tags();
|
| 202 |
foreach ($module_tags as $tags) {
|
| 203 |
$matches = array_merge($matches, _reptag_match_tags($string, array_keys($tags)));
|
| 204 |
}
|
| 205 |
}
|
| 206 |
|
| 207 |
// Display 10 items only
|
| 208 |
if (count($matches) > 10) {
|
| 209 |
$matches = array_slice($matches, 0, 10, TRUE);
|
| 210 |
}
|
| 211 |
|
| 212 |
print drupal_to_js($matches);
|
| 213 |
exit();
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Implementation of hook_nodeapi().
|
| 218 |
* (instead of hook_filter since we need access to $node context)
|
| 219 |
*/
|
| 220 |
function reptag_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 221 |
$disabled_nids = unserialize(variable_get('reptag_disabled_nids', serialize(array())));
|
| 222 |
if (!isset($node->nid) || !in_array($node->nid, $disabled_nids)) {
|
| 223 |
$fields = (array)unserialize(variable_get('reptag_type_'. $node->type .'_fields', serialize(array('content/body/#value'))));
|
| 224 |
// Add 'body' to the list of fields if 'content/body/#value' is selected
|
| 225 |
// (rss feeds and manual layouts sometimes print out the body field directly)
|
| 226 |
if (in_array('content/body/#value', $fields)) {
|
| 227 |
$fields[] = 'body';
|
| 228 |
}
|
| 229 |
switch ($op) {
|
| 230 |
case 'update':
|
| 231 |
case 'delete':
|
| 232 |
_reptag_cache_del($node, $fields);
|
| 233 |
break;
|
| 234 |
case 'view':
|
| 235 |
case 'print':
|
| 236 |
if (variable_get('reptag_cache', 0)) {
|
| 237 |
if (!variable_get('reptag_staticonly', 0)) {
|
| 238 |
if (isset($node->in_preview)) {
|
| 239 |
if (variable_get('reptag_preview', 1)) {
|
| 240 |
reptag_process($node, $fields, REPTAG_PROCALL);
|
| 241 |
}
|
| 242 |
}
|
| 243 |
else {
|
| 244 |
if (!_reptag_cache_get($node, $fields, $teaser)) {
|
| 245 |
reptag_process($node, $fields, REPTAG_PROCSTATIC);
|
| 246 |
_reptag_cache_set($node, $fields, $teaser);
|
| 247 |
}
|
| 248 |
reptag_process($node, $fields, REPTAG_PROCDYNAMIC);
|
| 249 |
}
|
| 250 |
}
|
| 251 |
}
|
| 252 |
else {
|
| 253 |
if (isset($node->in_preview)) {
|
| 254 |
if (variable_get('reptag_preview', 1)) {
|
| 255 |
reptag_process($node, $fields, REPTAG_PROCALL);
|
| 256 |
}
|
| 257 |
}
|
| 258 |
else {
|
| 259 |
reptag_process($node, $fields, REPTAG_PROCALL);
|
| 260 |
}
|
| 261 |
}
|
| 262 |
break;
|
| 263 |
}
|
| 264 |
}
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Implementation of hook_comment().
|
| 269 |
* (run reptag_process on comments (static tags only))
|
| 270 |
*/
|
| 271 |
function reptag_comment(&$comment, $op) {
|
| 272 |
if (variable_get('reptag_enable_comments', 0)) {
|
| 273 |
switch ($op) {
|
| 274 |
case 'update':
|
| 275 |
case 'delete':
|
| 276 |
if (variable_get('reptag_cache', 0)) {
|
| 277 |
_reptag_cache_del($comment, array('comment'));
|
| 278 |
}
|
| 279 |
break;
|
| 280 |
case 'view':
|
| 281 |
if (variable_get('reptag_cache', 0)) {
|
| 282 |
if ($comment->op != t('Preview comment')) {
|
| 283 |
if (!_reptag_cache_get($comment, array('comment'))) {
|
| 284 |
reptag_process($comment, array('comment'), REPTAG_PROCSTATIC);
|
| 285 |
_reptag_cache_set($comment, array('comment'));
|
| 286 |
}
|
| 287 |
}
|
| 288 |
else {
|
| 289 |
reptag_process($comment, array('comment'), REPTAG_PROCSTATIC);
|
| 290 |
}
|
| 291 |
}
|
| 292 |
else {
|
| 293 |
reptag_process($comment, array('comment'), REPTAG_PROCSTATIC);
|
| 294 |
}
|
| 295 |
break;
|
| 296 |
default:
|
| 297 |
}
|
| 298 |
}
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Implementation of hook_node_type().
|
| 303 |
*/
|
| 304 |
function reptag_node_type($op, $info) {
|
| 305 |
switch ($op) {
|
| 306 |
case 'update':
|
| 307 |
if (isset($info->old_type)) {
|
| 308 |
$fields = variable_get('reptag_type_'. $info->old_type .'_fields', serialize(array('content/body/#value')));
|
| 309 |
variable_set('reptag_type_'. $info->type .'_fields', $fields);
|
| 310 |
variable_del('reptag_type_'. $info->old_type .'_fields');
|
| 311 |
}
|
| 312 |
break;
|
| 313 |
case 'delete':
|
| 314 |
variable_del('reptag_type_'. $info->type .'_fields');
|
| 315 |
break;
|
| 316 |
}
|
| 317 |
}
|
| 318 |
|
| 319 |
/**
|
| 320 |
* Implementation of hook_form_alter().
|
| 321 |
*/
|
| 322 |
function reptag_form_alter(&$form, $form_state, $form_id) {
|
| 323 |
if (variable_get('reptag_showhelp', 1)) {
|
| 324 |
// Add reptag help to node form ...
|
| 325 |
if ((isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id
|
| 326 |
&& !variable_get('reptag_type_disable_'. $form['type']['#value'], 0))
|
| 327 |
// ... and comment form
|
| 328 |
|| ($form_id == 'comment_form' && variable_get('reptag_enable_comments', 0))) {
|
| 329 |
$help = _reptag_online_help();
|
| 330 |
if (count($help)) {
|
| 331 |
$form['reptag_help'] = array(
|
| 332 |
'#type' => 'fieldset',
|
| 333 |
'#title' => t('Rep[lacement]Tags - Help'),
|
| 334 |
'#collapsible' => TRUE,
|
| 335 |
'#collapsed' => TRUE,
|
| 336 |
'#weight' => 1
|
| 337 |
);
|
| 338 |
$form['reptag_help'][] = $help;
|
| 339 |
}
|
| 340 |
}
|
| 341 |
}
|
| 342 |
}
|
| 343 |
|
| 344 |
/**
|
| 345 |
* Implementation of hook_cron().
|
| 346 |
*/
|
| 347 |
function reptag_cron() {
|
| 348 |
cache_clear_all(NULL, 'cache_reptag');
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Implementation of hook_flush_caches().
|
| 353 |
*/
|
| 354 |
function reptag_flush_caches() {
|
| 355 |
return array('cache_reptag');
|
| 356 |
}
|
| 357 |
|
| 358 |
/**
|
| 359 |
* Function _reptag_online_help().
|
| 360 |
* (print overview of available tags)
|
| 361 |
*/
|
| 362 |
function _reptag_online_help() {
|
| 363 |
$form = array();
|
| 364 |
// Module tags
|
| 365 |
$modules = _reptag_module_help();
|
| 366 |
ksort($modules);
|
| 367 |
foreach ($modules as $module => $helptags) {
|
| 368 |
if (!empty($helptags)) {
|
| 369 |
$rows = array();
|
| 370 |
if (is_array($helptags)) {
|
| 371 |
foreach ($helptags as $tag => $repl) {
|
| 372 |
$rows[] = array($tag, ' ', $repl);
|
| 373 |
}
|
| 374 |
}
|
| 375 |
else {
|
| 376 |
$rows[] = array(array('data' => $helptags, 'colspan' => '3'));
|
| 377 |
}
|
| 378 |
$form[$module] = array(
|
| 379 |
'#type' => 'fieldset',
|
| 380 |
'#title' => ucfirst($module),
|
| 381 |
'#collapsible' => TRUE,
|
| 382 |
'#collapsed' => TRUE,
|
| 383 |
);
|
| 384 |
$form[$module]['table'] = array(
|
| 385 |
'#value' => theme('table', array(), $rows),
|
| 386 |
);
|
| 387 |
}
|
| 388 |
}
|
| 389 |
|
| 390 |
// Table tags
|
| 391 |
global $user;
|
| 392 |
$iid = array(0, $user->uid);
|
| 393 |
$reptag_table = _reptag_table_load($iid, REPTAG_LOADTAG_FORMAT);
|
| 394 |
ksort($reptag_table);
|
| 395 |
$rows = array();
|
| 396 |
foreach ($reptag_table as $tag => $repl) {
|
| 397 |
$rows[] = array(check_plain($tag), ' ', htmlentities($repl, ENT_COMPAT, 'UTF-8'));
|
| 398 |
}
|
| 399 |
if (!empty($rows)) {
|
| 400 |
$form['variables'] = array(
|
| 401 |
'#type' => 'fieldset',
|
| 402 |
'#title' => t('Additional Rep[lacement]Tags'),
|
| 403 |
'#collapsible' => TRUE,
|
| 404 |
'#collapsed' => TRUE,
|
| 405 |
);
|
| 406 |
$form['variables']['table'] = array(
|
| 407 |
'#value' => theme('table', array(), $rows),
|
| 408 |
);
|
| 409 |
}
|
| 410 |
|
| 411 |
return $form;
|
| 412 |
}
|