You are here

function duplicate_role_form_submit in Duplicate role 7

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

Form submit. Insert records into database.

File

./duplicate_role.module, line 48

Code

function duplicate_role_form_submit($form, &$form_state) {
  $base_role = filter_xss($form_state['values']['base_role']);
  $new_role_name = filter_xss($form_state['values']['new_role_name']);
  if (db_table_exists('role')) {

    // Create new role
    $new_role = db_insert('role');
    $new_role
      ->fields(array(
      'name' => $new_role_name,
    ));
    $new_role
      ->execute();

    // Capture new role id
    $new_role_rid = db_select('role', 'r')
      ->fields('r', array(
      'rid',
    ))
      ->condition('name', $new_role_name, '=')
      ->execute()
      ->fetchAssoc();
  }

  //Permission for old role in table "permission"
  if (db_table_exists('role_permission')) {
    $old_perms = db_select('role_permission', 'rp')
      ->fields('rp', array(
      'permission',
      'module',
    ))
      ->condition('rid', $base_role, '=')
      ->execute();
    while ($data = $old_perms
      ->fetchAssoc()) {

      // Insert permissions on new role
      $new_role_perms = db_insert('role_permission');
      $new_role_perms
        ->fields(array(
        'rid' => $new_role_rid,
        'permission' => $data['permission'],
        'module' => $data['module'],
      ));
      $new_role_perms
        ->execute();
    }
  }
  drupal_set_message(t('New role %role_name added successfully.', array(
    '%role_name' => $new_role_name,
  )));
}