| 1 |
<?php
|
| 2 |
// $Id: taxonomy_delegate.module,v 1.1 2008/02/23 05:00:02 nancyw Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows delegating taxonomy maintenance.
|
| 7 |
*/
|
| 8 |
|
| 9 |
// ******************************
|
| 10 |
// ***** "Overhead" Section *****
|
| 11 |
// ******************************
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_help().
|
| 15 |
* The first case adds a little help text.
|
| 16 |
* The second case adds some submission guidelines on the create content page.
|
| 17 |
*/
|
| 18 |
function taxonomy_delegate_help($path, $args = null) {
|
| 19 |
switch ($path) {
|
| 20 |
|
| 21 |
case 'admin/help#taxonomy_delegate':
|
| 22 |
$output = '<p>'. t('The taxonomy_delegate module allows the administrator to delegate the maintenance of selected vocabularies without the need to grant "access taxonomy" permission. Delegation is done on the <a href="!link">Category Delegate</a> tab of the Categories administration page.', array('!link' => 'admin/content/taxonomy/delegate')) .'</p>';
|
| 23 |
if (module_exists('forum')) {
|
| 24 |
$output .= '<p>'. t('Note that forum containers and topics are a vocabulary, so you may delegate their maintenance to a specific role without giving full forums administration permission.') .'</p>';
|
| 25 |
}
|
| 26 |
break;
|
| 27 |
|
| 28 |
case 'admin/content/taxonomy/delegate':
|
| 29 |
$output = '<p>'. t('This is an overview of the current delegation of authority to administer categories. Setting of the delegation is done on the vocabulary edit page.') .'</p>';
|
| 30 |
|
| 31 |
return $output;
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implementation of hook_menu().
|
| 37 |
* The only menu items for this module is to list the emails that were intercepted.
|
| 38 |
* It will be under the admin >> logs section of the Administer menu.
|
| 39 |
*/
|
| 40 |
function taxonomy_delegate_menu() {
|
| 41 |
global $user;
|
| 42 |
$items = array();
|
| 43 |
|
| 44 |
// Admin Settings - becomes a tab on the admin >> categories page
|
| 45 |
$items['admin/content/taxonomy/delegate'] = array(
|
| 46 |
'title' => 'Delegation',
|
| 47 |
'page callback' => 'taxonomy_delegate_admin',
|
| 48 |
'access arguments' => array('administer taxonomy'),
|
| 49 |
'description' => 'Allow delegation of taxonomy admin to specific roles.',
|
| 50 |
'type' => MENU_LOCAL_TASK,
|
| 51 |
);
|
| 52 |
// User Admin callback.
|
| 53 |
$any = count(_taxonomy_delegate_my_vocabularies());
|
| 54 |
if ($any) {
|
| 55 |
$items['category_admin'] = array(
|
| 56 |
'title' => 'Administer My Categories',
|
| 57 |
'page callback' => 'taxonomy_delegate_mycategories',
|
| 58 |
'access arguments' => array('access content'),
|
| 59 |
);
|
| 60 |
}
|
| 61 |
|
| 62 |
return $items;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Implementation of hook_enable.
|
| 67 |
* This function logs a message saying the module is enabled and gives the user id.
|
| 68 |
*/
|
| 69 |
function taxonomy_delegate_enable() {
|
| 70 |
global $user;
|
| 71 |
watchdog('taxonomy_delegate', 'taxonomy_delegate module enabled by !name', array('!name' => theme('username', $user)));
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Implementation of hook_disable.
|
| 76 |
* This function logs a message saying the module is disabled and gives the user id.
|
| 77 |
*/
|
| 78 |
function taxonomy_delegate_disable() {
|
| 79 |
global $user;
|
| 80 |
watchdog('taxonomy_delegate', 'taxonomy_delegate module disabled by !name', array('!name' => theme('username', $user)));
|
| 81 |
}
|
| 82 |
|
| 83 |
// *****************************
|
| 84 |
// ***** "Working" Section *****
|
| 85 |
// *****************************
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Implementation of hook_form_alter.
|
| 89 |
* Alters the taxonomy vocabulary for, to allow delegation.
|
| 90 |
*/
|
| 91 |
function taxonomy_delegate_form_alter(&$form, &$form_state, $form_id) {
|
| 92 |
switch ($form_id) {
|
| 93 |
case 'taxonomy_form_vocabulary':
|
| 94 |
$vid = $form['vid']['#value'];
|
| 95 |
$roles = _taxonomy_delegate_get_roles(true);
|
| 96 |
unset($roles[DRUPAL_ANONYMOUS_RID]); // Remove anonymous role.
|
| 97 |
$defaults = _taxonomy_delegate_get_delegates($vid);
|
| 98 |
$vocab = taxonomy_vocabulary_load($vid);
|
| 99 |
|
| 100 |
$form['delete_current'] = array(
|
| 101 |
'#type' => 'value',
|
| 102 |
'#value' => count($defaults),
|
| 103 |
);
|
| 104 |
|
| 105 |
$form['taxonomy_delegate'] = array(
|
| 106 |
'#type' => 'fieldset',
|
| 107 |
'#title' => t('Delegation'),
|
| 108 |
'#description' => t('Use this section to delegate authority to maintain this vocabulary to a specific role.'),
|
| 109 |
'#collapsible' => true,
|
| 110 |
'#collapsed' => false,
|
| 111 |
'#weight' => 1,
|
| 112 |
);
|
| 113 |
|
| 114 |
$form['taxonomy_delegate']['roles'] = array(
|
| 115 |
'#type' => 'select',
|
| 116 |
'#title' => t('Choose the role(s) that may administer this vocabulary.'),
|
| 117 |
'#options' => $roles,
|
| 118 |
'#multiple' => true,
|
| 119 |
'#default_value' => $defaults,
|
| 120 |
'#description' => t('You may choose more than one by using the CTRL key.'),
|
| 121 |
);
|
| 122 |
|
| 123 |
$form['#submit'][] = 'taxonomy_delegate_vocabulary_submit';
|
| 124 |
// Make some room for me.
|
| 125 |
$form[submit]['#weight'] = 5;
|
| 126 |
$form[delete]['#weight'] = 5;
|
| 127 |
break;
|
| 128 |
} // End switch.
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
*
|
| 133 |
*/
|
| 134 |
function taxonomy_delegate_vocabulary_submit($form, &$form_state) {
|
| 135 |
// If there are some already, delete all roles currently associated with this vocabulary.
|
| 136 |
if ($form_state['values']['delete_current']) {
|
| 137 |
db_query('DELETE FROM {taxonomy_delegate} WHERE vid=%d', $form_state['values']['vid']);
|
| 138 |
if ($form_state['values']['op'] == 'Delete') {
|
| 139 |
return 'admin/content/taxonomy/delegate';
|
| 140 |
}
|
| 141 |
}
|
| 142 |
// Add the new ones into the table. Key=0 means 'none'.
|
| 143 |
if (!isset($form_state['values']['roles'][0])) {
|
| 144 |
foreach ($form_state['values']['roles'] as $key => $value) {
|
| 145 |
$insert = db_query('INSERT INTO {taxonomy_delegate} (vid, rid) VALUES(%d, %d)', $form_state['values']['vid'], $key);
|
| 146 |
if (!$insert) {
|
| 147 |
drupal_set_message("Inserted failed for role: $key", 'warning');
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
*
|
| 155 |
*/
|
| 156 |
function taxonomy_delegate_admin($op=null, $vid=null) {
|
| 157 |
$destination = drupal_get_destination();
|
| 158 |
$output = '';
|
| 159 |
$roles = _taxonomy_delegate_get_roles();
|
| 160 |
|
| 161 |
// Is there an op parameter (call back)?
|
| 162 |
if (is_null($op)) {
|
| 163 |
// Not a call back, so build the list.
|
| 164 |
$vocabularies = taxonomy_get_vocabularies();
|
| 165 |
$rows = array();
|
| 166 |
|
| 167 |
foreach ($vocabularies as $vocabulary) {
|
| 168 |
$role_list = array();
|
| 169 |
foreach (_taxonomy_delegate_get_delegates($vocabulary->vid) as $key => $rid) {
|
| 170 |
$role_list[] = $roles[$rid];
|
| 171 |
}
|
| 172 |
if (count($role_list)) {
|
| 173 |
$role_names = implode('<br/>', $role_list);
|
| 174 |
}
|
| 175 |
else {
|
| 176 |
$role_names = 'None';
|
| 177 |
}
|
| 178 |
$rows[] = array(
|
| 179 |
array('data' => $vocabulary->name, 'header' => true),
|
| 180 |
$role_names,
|
| 181 |
l(t('edit'), 'admin/content/taxonomy/edit/vocabulary/'. $vocabulary->vid,
|
| 182 |
array('query' => $destination)),
|
| 183 |
);
|
| 184 |
}
|
| 185 |
|
| 186 |
if (!empty($rows)) {
|
| 187 |
$header[] = t('Vocabulary');
|
| 188 |
$header[] = t('Roles');
|
| 189 |
$header[] = t('Op');
|
| 190 |
$output .= theme('table', $header, $rows, array('cellpadding' => '5', 'cellspacing' => '5'));
|
| 191 |
}
|
| 192 |
else { $output .= 'No vocabularies were found.'; }
|
| 193 |
return $output;
|
| 194 |
}
|
| 195 |
else {
|
| 196 |
// This is a call back to manage the list.
|
| 197 |
return drupal_get_form('taxonomy_delegate_edit_form', $vid);
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
*
|
| 203 |
*/
|
| 204 |
function taxonomy_delegate_edit_form($vid) {
|
| 205 |
$form = array();
|
| 206 |
$roles = _taxonomy_delegate_get_roles();
|
| 207 |
unset($roles[1]); // Remove anonymous role.
|
| 208 |
$defaults = _taxonomy_delegate_get_delegates($vid);
|
| 209 |
$vocab = taxonomy_vocabulary_load($vid);
|
| 210 |
|
| 211 |
$form['vid'] = array(
|
| 212 |
'#type' => 'value',
|
| 213 |
'#value' => $vid,
|
| 214 |
);
|
| 215 |
|
| 216 |
$form['delete_current'] = array(
|
| 217 |
'#type' => 'value',
|
| 218 |
'#value' => count($defaults),
|
| 219 |
);
|
| 220 |
|
| 221 |
$form['vocab'] = array(
|
| 222 |
'#type' => 'fieldset',
|
| 223 |
'#title' => '<big>'. $vocab->name ." (vid $vid)</big>",
|
| 224 |
'#description' => '<h2>'. $vocab->description .'</h2>',
|
| 225 |
'#collapsible' => true,
|
| 226 |
'#collapsed' => false,
|
| 227 |
'#prefix' => '<br/>',
|
| 228 |
);
|
| 229 |
|
| 230 |
$form['vocab']['roles'] = array(
|
| 231 |
'#type' => 'select',
|
| 232 |
'#title' => t('Choose the role(s) that may administer this vocabulary.'),
|
| 233 |
'#options' => $roles,
|
| 234 |
'#multiple' => true,
|
| 235 |
'#default_value' => $defaults,
|
| 236 |
'#description' => t('You may choose more than one by using the CTRL key.'),
|
| 237 |
);
|
| 238 |
|
| 239 |
// Add the buttons.
|
| 240 |
$form['update']['attach'] = array(
|
| 241 |
'#type' => 'submit',
|
| 242 |
'#value' => t('Update'),
|
| 243 |
'#weight' => 3
|
| 244 |
);
|
| 245 |
|
| 246 |
// This is only needed if there is a previous selection.
|
| 247 |
if (!empty($defaults)) {
|
| 248 |
$form['delete']['attach'] = array(
|
| 249 |
'#type' => 'submit',
|
| 250 |
'#value' => t('Delete'),
|
| 251 |
'#weight' => 4
|
| 252 |
);
|
| 253 |
}
|
| 254 |
$form['#redirect'] = 'admin/content/taxonomy/delegate';
|
| 255 |
|
| 256 |
return $form;
|
| 257 |
}
|
| 258 |
|
| 259 |
/**
|
| 260 |
*
|
| 261 |
*/
|
| 262 |
function taxonomy_delegate_edit_form_submit($form, &$form_state) {
|
| 263 |
// If there are some already, delete all roles currently associated with this vocabulary.
|
| 264 |
if ($form_state['values']['delete_current']) {
|
| 265 |
db_query('DELETE FROM {taxonomy_delegate} WHERE vid=%d', $form_state['values']['vid']);
|
| 266 |
if ($form_state['values']['op'] == 'Delete') {
|
| 267 |
return 'admin/content/taxonomy/delegate';
|
| 268 |
}
|
| 269 |
}
|
| 270 |
// Add the new ones into the table.
|
| 271 |
if (count($form_state['values']['roles'])) {
|
| 272 |
foreach ($form_state['values']['roles'] as $key => $value) {
|
| 273 |
$insert = db_query('INSERT INTO {taxonomy_delegate} (vid, rid) VALUES(%d, %d)', $form_state['values']['vid'], $key);
|
| 274 |
if (!$insert) {
|
| 275 |
drupal_set_message("Inserted failed for role: $key", 'warning');
|
| 276 |
}
|
| 277 |
}
|
| 278 |
}
|
| 279 |
}
|
| 280 |
|
| 281 |
/**
|
| 282 |
*
|
| 283 |
*/
|
| 284 |
function taxonomy_delegate_mycategories($op=null, $vid=null) {
|
| 285 |
global $user;
|
| 286 |
$output = '<br/>';
|
| 287 |
|
| 288 |
// In the op checking sections below, we have to make sure we should be here to help prevent
|
| 289 |
// cheaters who might be typing in the paths to avoid the legitimate security.
|
| 290 |
if ($op == 'list') { // Do list stuff.
|
| 291 |
$vocabs = _taxonomy_delegate_my_vocabularies();
|
| 292 |
if (isset($vocabs[$vid])) {
|
| 293 |
return taxonomy_delegate_overview_terms($vid);
|
| 294 |
}
|
| 295 |
else {
|
| 296 |
drupal_access_denied();
|
| 297 |
return;
|
| 298 |
}
|
| 299 |
}
|
| 300 |
|
| 301 |
if ($op == 'add') { // Do add stuff.
|
| 302 |
$vocabs = _taxonomy_delegate_my_vocabularies();
|
| 303 |
if (isset($vocabs[$vid])) {
|
| 304 |
return drupal_get_form('taxonomy_form_term', $vid);
|
| 305 |
}
|
| 306 |
else {
|
| 307 |
drupal_access_denied();
|
| 308 |
return;
|
| 309 |
}
|
| 310 |
}
|
| 311 |
|
| 312 |
if ($op == 'edit') { // Do edit stuff on a term (the $vid paramter is actually the tid).
|
| 313 |
$tid = $vid;
|
| 314 |
$term = taxonomy_get_term($tid);
|
| 315 |
$vid = $term->vid;
|
| 316 |
$vocabs = _taxonomy_delegate_my_vocabularies();
|
| 317 |
if (isset($vocabs[$vid])) {
|
| 318 |
return taxonomy_admin_term_edit($tid);
|
| 319 |
}
|
| 320 |
else {
|
| 321 |
drupal_access_denied();
|
| 322 |
return;
|
| 323 |
}
|
| 324 |
}
|
| 325 |
|
| 326 |
$vocabs = _taxonomy_delegate_my_vocabularies();
|
| 327 |
|
| 328 |
foreach ($vocabs as $vid => $name) {
|
| 329 |
$rows[] = array('<big>'. $name .'</big>',
|
| 330 |
' '. l(t('List terms'), 'category_admin/list/'. $vid),
|
| 331 |
' '. l(t('Add terms'), 'category_admin/add/'. $vid),
|
| 332 |
);
|
| 333 |
}
|
| 334 |
$header = array(
|
| 335 |
t('Category'),
|
| 336 |
array('data' => t('Operations'), 'colspan' => '2'),
|
| 337 |
);
|
| 338 |
$attributes = array(
|
| 339 |
'cellpadding' => '10',
|
| 340 |
'cellspacing' => '10',
|
| 341 |
);
|
| 342 |
$output .= theme('table', $header, $rows, $attributes);
|
| 343 |
return $output;
|
| 344 |
}
|
| 345 |
|
| 346 |
/**
|
| 347 |
* Display a tree of all the terms in a vocabulary, with options to edit each one.
|
| 348 |
* Lifted from Taxonomy module.
|
| 349 |
*/
|
| 350 |
function taxonomy_delegate_overview_terms($vid) {
|
| 351 |
$destination = drupal_get_destination();
|
| 352 |
$use_image = module_exists('taxonomy_image');
|
| 353 |
|
| 354 |
$vocabulary = taxonomy_vocabulary_load($vid);
|
| 355 |
if (!$vocabulary) {
|
| 356 |
return drupal_not_found();
|
| 357 |
}
|
| 358 |
|
| 359 |
drupal_set_title(t('Terms for ') . check_plain($vocabulary->name));
|
| 360 |
$output = '<br/>';
|
| 361 |
|
| 362 |
if ($vocabulary->tags) {
|
| 363 |
$output .= '<p>'. t('This is a freetagging vocabulary.') .'</p>';
|
| 364 |
// We are not calling taxonomy_get_tree because that might fail with a lot of tags in a freetagging vocabulary.
|
| 365 |
$results = db_rewrite_sql('SELECT t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid', $vid);
|
| 366 |
|
| 367 |
while ($term = db_fetch_object($results)) {
|
| 368 |
$rows[] = array(
|
| 369 |
($use_image ? taxonomy_image_display($term->tid) : null),
|
| 370 |
l($term->name, 'taxonomy/term/'. $term->tid),
|
| 371 |
$term->description,
|
| 372 |
l(t('edit'), 'category_admin/edit/'. $term->tid, array(), $destination),
|
| 373 |
);
|
| 374 |
}
|
| 375 |
}
|
| 376 |
else {
|
| 377 |
$tree = taxonomy_get_tree($vocabulary->vid);
|
| 378 |
foreach ($tree as $term) {
|
| 379 |
if ($use_image) {
|
| 380 |
$taxo_image = taxonomy_image_display($term->tid);
|
| 381 |
}
|
| 382 |
else {
|
| 383 |
$taxo_image = null;
|
| 384 |
}
|
| 385 |
$rows[] = array(
|
| 386 |
$taxo_image,
|
| 387 |
str_repeat('--', $term->depth) .' '. l($term->name, "taxonomy/term/$term->tid"),
|
| 388 |
$term->description,
|
| 389 |
l(t('edit'), 'category_admin/edit/'. $term->tid, array('query' => $destination)),
|
| 390 |
);
|
| 391 |
}
|
| 392 |
}
|
| 393 |
|
| 394 |
if (!$rows) {
|
| 395 |
$rows[] = array(array('data' => t('No terms available.'), 'colspan' => '5'));
|
| 396 |
}
|
| 397 |
$header = array(
|
| 398 |
($use_image ? t('Image') : null),
|
| 399 |
t('Name'), t('Description'), t('Operations'),
|
| 400 |
);
|
| 401 |
|
| 402 |
$output .= theme('table', $header, $rows, array('id' => 'taxonomy'));
|
| 403 |
|
| 404 |
return $output;
|
| 405 |
}
|
| 406 |
|
| 407 |
// ******************************
|
| 408 |
// ***** Helper Functions *****
|
| 409 |
// ******************************
|
| 410 |
|
| 411 |
/**
|
| 412 |
*
|
| 413 |
*/
|
| 414 |
function _taxonomy_delegate_my_vocabularies() {
|
| 415 |
global $user;
|
| 416 |
$list = array();
|
| 417 |
foreach ($user->roles as $rid => $name) {
|
| 418 |
$list[] = $rid;
|
| 419 |
}
|
| 420 |
$result = db_query('SELECT DISTINCT(vid) FROM {taxonomy_delegate} WHERE rid IN (%s)', implode(', ', $list));
|
| 421 |
$vocabs = array();
|
| 422 |
|
| 423 |
while ($vocab = db_fetch_array($result)) {
|
| 424 |
$vid = $vocab['vid'];
|
| 425 |
$voc = taxonomy_vocabulary_load($vid);
|
| 426 |
$vocabs[$vid] = $voc->name;
|
| 427 |
}
|
| 428 |
|
| 429 |
return $vocabs;
|
| 430 |
}
|
| 431 |
|
| 432 |
/**
|
| 433 |
* This helper function gets a keyed array of the roles and their names.
|
| 434 |
*/
|
| 435 |
function _taxonomy_delegate_get_roles($add_none = false) {
|
| 436 |
$result = db_query('SELECT rid, name FROM {role}');
|
| 437 |
$roles = array();
|
| 438 |
if ($add_none) {
|
| 439 |
$roles[0] = '<none>';
|
| 440 |
}
|
| 441 |
|
| 442 |
while ($row = db_fetch_array($result)) {
|
| 443 |
$roles[$row['rid']] = $row['name'];
|
| 444 |
}
|
| 445 |
|
| 446 |
if (empty($roles)) {
|
| 447 |
drupal_set_message('No roles found.', 'warning');
|
| 448 |
}
|
| 449 |
|
| 450 |
return $roles;
|
| 451 |
}
|
| 452 |
|
| 453 |
/**
|
| 454 |
* This helper function gets an array of the currently allowed delegate roles.
|
| 455 |
*/
|
| 456 |
function _taxonomy_delegate_get_delegates($vid) {
|
| 457 |
$result = db_query('SELECT * FROM {taxonomy_delegate} WHERE vid=%d', $vid);
|
| 458 |
$roles = array();
|
| 459 |
|
| 460 |
while ($row = db_fetch_array($result)) {
|
| 461 |
$roles[] = $row['rid'];
|
| 462 |
}
|
| 463 |
|
| 464 |
return $roles;
|
| 465 |
}
|