You are here

function uc_roles_user_submit in Ubercart 6.2

Implements hook_user_submit().

1 call to uc_roles_user_submit()
uc_roles_user in uc_roles/uc_roles.module
Implements hook_user().

File

uc_roles/uc_roles.module, line 346

Code

function uc_roles_user_submit(&$edit, &$account, $category) {
  if (!user_access('administer users') || $category != 'account') {
    return;
  }

  // If user doesn't have "administer permissions" permission, we need to populate the roles,
  // otherwise user_save() will delete them.
  if (!isset($edit['roles'])) {
    $edit['roles'] = $account->roles;
  }

  // If a new temporary role is added.
  // Grant a new role.
  if (!empty($edit['new_role']) && $category == 'account') {

    // Save our role info, but don't save the user; user.module will do it.
    uc_roles_grant($account, $edit['new_role_add'], _uc_roles_get_expiration($edit['new_role_add_qty'], $edit['new_role_add_granularity']), FALSE);

    // Push in values so user.module will save in the roles.
    $edit['roles'][$edit['new_role_add']] = _uc_roles_get_name($edit['new_role_add']);
  }

  // Reset the new role form.
  $edit['new_role'] = $edit['new_role_add'] = $edit['new_role_add_qty'] = $edit['new_role_add_granularity'] = NULL;

  // Check if any temporary role actions were taken.
  if (isset($edit['table'])) {
    foreach ((array) $edit['table'] as $rid => $value) {

      // Remove this expiration.
      if ($value['remove']) {
        uc_roles_delete($account, $rid);
      }
      else {
        if ($value['qty'] && $edit['roles'][$rid]) {
          $qty = $value['qty'];
          $qty *= $value['polarity'] == 'add' ? 1 : -1;
          uc_roles_renew($account, $rid, _uc_roles_get_expiration($qty, $value['granularity'], $value['expiration']));
        }
      }
    }
  }

  // Reset the expiration table.
  $edit['table'] = NULL;

  // If a user's role is removed using Drupal, then so is any expiration data.
  if (isset($edit['roles']) && is_array($edit['roles'])) {
    foreach ($account->roles as $rid => $role) {
      if (!in_array($rid, array_keys($edit['roles'])) && $rid != DRUPAL_AUTHENTICATED_RID) {
        uc_roles_delete($account, $rid);
      }
    }
  }
}