You are here

duplicate_role.module in Duplicate role 6

File

duplicate_role.module
View source
<?php

// $Id: duplicate_role.module,v 1.x 2008/03/10  14:30:01



/**
 * Implementation of hook_help().
 */
function duplicate_role_help($section = '') {
  $output = '';
  switch ($section) {
    case "admin/user/duplirole":
      $output = '<p>' . t("Duplicate an existing role with the same permissions for modules (and taxonomies, if exist taxonomy access) as the original ones") . '</p>';
      break;
  }
  return $output;
}

// function duplicate_role_help

/**
 * Implementation of hook_perm().
 */
function duplicate_role_perm() {
  return array(
    'administer duplicate role',
  );
}

// function duplicate_role_perm

/**
 * Implementation of hook_menu().
 */
function duplicate_role_menu() {
  $admin_access = user_access('administer duplicate role');
  $items = array();
  $items[] = array(
    'path' => 'admin/user/duplirole',
    'title' => t('Duplicate Role'),
    'description' => t('Duplicate an existing role'),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'duplicate_role_form',
    'access' => $admin_access,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Form submit. Insert records into database.
 */
function duplicate_role_form_submit($form_id, $form_values) {
  $rolvello_id = $form_values['varid'];
  $rolnovo_nome = $form_values['nome'];

  // Create new role
  if (db_table_exists('role')) {
    $sql = "INSERT INTO {role} (name) VALUES ('%s')";
    $result = db_query($sql, $rolnovo_nome);

    // capture new role id
    $sql = "SELECT r.rid FROM {role} r WHERE name='%s'";
    $result = db_query($sql, $rolnovo_nome);
    while ($row = db_fetch_object($result)) {
      $rolnovo_id = $row->rid;
    }
  }

  //Permission for old role in table "permission"
  if (db_table_exists('permission')) {
    $sql = "SELECT p.perm, p.tid FROM {permission} p WHERE  rid='%d'";
    $result = db_query($sql, $rolvello_id);
    while ($row = db_fetch_object($result)) {
      $rolvello_perm = $row->perm;
      $rolvello_tid = $row->tid;
    }

    //Duplicate module permissions for new rol
    $sql = "INSERT INTO {permission} (rid,perm,tid) VALUES ('%d','%s','%d')";
    $result = db_query($sql, $rolnovo_id, $rolvello_perm, $rolvello_tid);
  }

  //Duplicate taxonomies access permissions for new rol
  if (db_table_exists('term_access')) {
    $sql = "INSERT INTO {term_access} \n\t\t(tid,rid,grant_view,grant_update,grant_delete,grant_create,grant_list) \n\t\tSELECT tid, '%d',grant_view,grant_update,grant_delete,grant_create,grant_list\n\t\tFROM term_access where rid='%d'";
    $result = db_query($sql, $rolnovo_id, $rolvello_id);
  }

  //Duplicate taxonomies access default permissions for new rol
  if (db_table_exists('term_access_defaults')) {
    $sql = "INSERT INTO {term_access_defaults} \n\t\t(vid,rid,grant_view,grant_update,grant_delete,grant_create,grant_list) \n\t\tSELECT vid, '%d',grant_view,grant_update,grant_delete,grant_create,grant_list\n\t\tFROM {term_access_defaults} where rid='%d'";
    $result = db_query($sql, $rolnovo_id, $rolvello_id);
  }
  drupal_set_message(t('New role added successfully.'));
}

/**
 * Module selection interface.
 */
function duplicate_role_form() {
  $form = array();
  $u_roles = user_roles();
  asort($u_roles);
  $options = array();
  $options[] = t('-- Please Select One --');
  foreach ($u_roles as $key => $value) {
    $options[$key] = $value;
  }
  $form['copiarol']['varid'] = array(
    '#type' => 'select',
    '#title' => t('Choose role to duplicate'),
    '#default_value' => variable_get('copiarol_varid', 0),
    '#description' => t("Select role to duplicate"),
    '#options' => $options,
  );
  $form['copiarol']['nome'] = array(
    '#type' => 'textfield',
    '#title' => t('New role'),
    '#default_value' => variable_get('copiarol_nome', ''),
    '#required' => true,
    '#size' => 40,
    '#maxlength' => 40,
    '#description' => t("Name for the new role"),
  );
  $form['copiarol']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create new role'),
  );
  return $form;
}

Functions

Namesort descending Description
duplicate_role_form Module selection interface.
duplicate_role_form_submit Form submit. Insert records into database.
duplicate_role_help Implementation of hook_help().
duplicate_role_menu Implementation of hook_menu().
duplicate_role_perm Implementation of hook_perm().