View source
<?php
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_perm() {
return array(
'administer duplicate role',
);
}
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;
}
function duplicate_role_form_submit($form_id, $form_values) {
$rolvello_id = $form_values['varid'];
$rolnovo_nome = $form_values['nome'];
if (db_table_exists('role')) {
$sql = "INSERT INTO {role} (name) VALUES ('%s')";
$result = db_query($sql, $rolnovo_nome);
$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;
}
}
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;
}
$sql = "INSERT INTO {permission} (rid,perm,tid) VALUES ('%d','%s','%d')";
$result = db_query($sql, $rolnovo_id, $rolvello_perm, $rolvello_tid);
}
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);
}
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.'));
}
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;
}