cms/drupal/sites/all/modules/vppr/vppr.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 /**
       
     3  * @file
       
     4  * Vocabulary Permissions Per Role
       
     5  *
       
     6  * Allows adding to/editing terms of/removing terms from vocabularies per role.
       
     7  */
       
     8 
       
     9 /**
       
    10  * Implements hook_menu_alter().
       
    11  */
       
    12 function vppr_menu_alter(&$items) {
       
    13   // Taxonomy overview page.
       
    14   $items['admin/structure/taxonomy']['access callback'] = 'vppr_access_taxonomy';
       
    15   $items['admin/structure/taxonomy']['page callback'] = 'vppr_taxonomy_overview_vocabularies';
       
    16   $items['admin/structure/taxonomy']['file'] = 'vppr.admin.inc';
       
    17   $items['admin/structure/taxonomy']['file path'] = drupal_get_path('module', 'vppr');
       
    18   unset($items['admin/structure/taxonomy']['page arguments']);
       
    19   // Terms list.
       
    20   $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name']['access callback'] = 'vppr_access_vocabulary_terms';
       
    21   $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name']['access arguments'] = array(3);
       
    22   // Add terms.
       
    23   $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/add']['access callback'] = 'vppr_access_vocabulary_terms';
       
    24   $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/add']['access arguments'] = array(3);
       
    25   // Term edit.
       
    26   $items['taxonomy/term/%taxonomy_term/edit']['access callback'] = 'vppr_access_term_edit';
       
    27   $items['taxonomy/term/%taxonomy_term/edit']['access arguments'] = array(2);
       
    28 }
       
    29 
       
    30 /**
       
    31  * VPPR's access callback for taxonomy overview page (main entry point).
       
    32  *
       
    33  * Grants access to taxonomy overview page even if $user has access to any of
       
    34  * the vocabularies.
       
    35  */
       
    36 function vppr_access_taxonomy() {
       
    37   if (user_access('administer taxonomy')) {
       
    38     return TRUE;
       
    39   }
       
    40   $perms = array_keys(vppr_permission());
       
    41   foreach ($perms as $perm) {
       
    42     if (user_access($perm)) {
       
    43       return TRUE;
       
    44     }
       
    45   }
       
    46   return FALSE;
       
    47 }
       
    48 
       
    49 /**
       
    50  * VPPR's access callback for terms list.
       
    51  */
       
    52 function vppr_access_vocabulary_terms($vocabulary) {
       
    53   if (user_access('administer taxonomy') || user_access('administer ' . $vocabulary->machine_name . ' vocabulary terms')) {
       
    54     return TRUE;
       
    55   }
       
    56   return FALSE;
       
    57 }
       
    58 
       
    59 /**
       
    60  * Return edit access for a given term.
       
    61  */
       
    62 function vppr_access_term_edit($term) {
       
    63   if (taxonomy_term_edit_access($term) || user_access('administer ' . $term->vocabulary_machine_name . ' vocabulary terms')) {
       
    64     return TRUE;
       
    65   }
       
    66   return FALSE;
       
    67 }
       
    68 
       
    69 /**
       
    70  * Return delete access for a given term.
       
    71  */
       
    72 function vppr_access_term_delete($term) {
       
    73   if (user_access('administer taxonomy') || user_access("delete terms in $term->vid") || user_access('administer ' . $term->vocabulary_machine_name . ' vocabulary terms')) {
       
    74     return TRUE;
       
    75   }
       
    76   return FALSE;
       
    77 }
       
    78 
       
    79 /**
       
    80  * Implements hook_form_FORM_ID_alter().
       
    81  */
       
    82 function vppr_form_taxonomy_form_term_alter(&$form, &$form_state) {
       
    83   if (isset($form['tid']['#value']) && isset($form['actions']['delete']['#access']) && isset($form['#term'])) {
       
    84     $form['actions']['delete']['#access'] = vppr_access_term_delete((object) $form['#term']);
       
    85   }
       
    86 }
       
    87 
       
    88 /**
       
    89  * Implements hook_permission().
       
    90  */
       
    91 function vppr_permission() {
       
    92   $perms = array();
       
    93   $vocabularies = taxonomy_get_vocabularies();
       
    94   foreach ($vocabularies as $vocabulary) {
       
    95     $perms['administer ' . $vocabulary->machine_name . ' vocabulary terms'] = array(
       
    96       'title' => t('Administer %name vocabulary terms', array('%name' => $vocabulary->name)),
       
    97     );
       
    98   }
       
    99   return $perms;
       
   100 }
       
   101 
       
   102 /**
       
   103  * Implements hook_taxonomy_vocabulary_update().
       
   104  */
       
   105 function vppr_taxonomy_vocabulary_update($vocabulary) {
       
   106   if ($vocabulary->old_machine_name != $vocabulary->machine_name) {
       
   107     db_update('role_permission')
       
   108       ->fields(array(
       
   109         'permission' => 'administer ' . $vocabulary->machine_name . ' vocabulary terms',
       
   110       ))
       
   111       ->condition('permission', 'administer ' . $vocabulary->old_machine_name . ' vocabulary terms')
       
   112       ->execute();
       
   113 
       
   114     // Clear the user access cache.
       
   115     drupal_static_reset('user_access');
       
   116     drupal_static_reset('user_role_permissions');
       
   117   }
       
   118 }
       
   119 
       
   120 /**
       
   121  * Implements hook_taxonomy_vocabulary_delete().
       
   122  */
       
   123 function vppr_taxonomy_vocabulary_delete($vocabulary) {
       
   124   db_delete('role_permission')
       
   125     ->condition('permission', 'administer ' . $vocabulary->machine_name . ' vocabulary terms')
       
   126     ->execute();
       
   127 
       
   128   // Clear the user access cache.
       
   129   drupal_static_reset('user_access');
       
   130   drupal_static_reset('user_role_permissions');
       
   131 }