You are here

function duplicate_role_form_submit in Duplicate role 6.2

Same name and namespace in other branches
  1. 6 duplicate_role.module \duplicate_role_form_submit()
  2. 7 duplicate_role.module \duplicate_role_form_submit()

Form submit. Insert records into database.

File

./duplicate_role.module, line 49

Code

function duplicate_role_form_submit($form, &$form_state) {
  $rolnovo_nome = $form_state['values']['nome'];
  $rolvello_id = $form_state['values']['varid'];

  // 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.'));
}