You are here

function uc_roles_user_presave in Ubercart 7.3

Implements hook_user_presave().

File

uc_roles/uc_roles.module, line 298
Grants roles upon accepted payment of products.

Code

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

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

    // Save our role info, but don't save the user; user.module will do that.
    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']));
        }
      }
    }
  }

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