You are here

function regcode_voucher_submit in Registration codes 6.2

Same name and namespace in other branches
  1. 6 regcode_voucher/regcode_voucher.module \regcode_voucher_submit()

Submit handler

File

regcode_voucher/regcode_voucher.module, line 173

Code

function regcode_voucher_submit($form, $form_state) {
  $edit = $form_state['values'];
  $account = $GLOBALS['user'];

  // We cannot simply call user_save here because any module that expects
  // the $edit variable to contain their form data will be disappointed.
  // This includes the simplenews, profile, and especially the role_expire
  // module which cause a notice error, a blank profile, and wiped out
  // user roles respectively.
  $code = regcode_use_helper($edit, $account);
  if (is_object($code)) {
    drupal_set_message(check_plain(variable_get('regcode_voucher_message', t('Voucher code used successfully.'))));
  }

  // Make use of $edit which has been changed by the hooks to see what roles need to be saved
  // Again, we can't call user_save($account, array('roles' => $edit['roles'])) because the
  // role_expire module deletes all of the roles because it's stupid.
  if (!empty($edit['roles'])) {
    foreach ($edit['roles'] as $rid => $role_name) {
      if ($rid > DRUPAL_AUTHENTICATED_RID) {
        $res = db_query('SELECT uid FROM {users_roles} WHERE uid=%d AND rid=%d', $account->uid, $rid);
        if (!($row = db_fetch_object($res))) {
          db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
        }
      }
    }
  }
  drupal_goto('user');
}