You are here

function role_expire_user in Role Expire 6

Implementation of hook_user().

File

./role_expire.module, line 239
Role Expire module

Code

function role_expire_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'form':
      if ($category == 'account' && (user_access('administer role expire') || user_access('administer users'))) {
        return role_expire_add_expiration_input($account);
      }
      break;
    case 'validate':

      //todo  Add ifs for if (user_access('administer role expire') || user_access('administer users')
      if ($category == 'account' && (user_access('administer role expire') || user_access('administer users'))) {
        if (isset($edit['roles'])) {
          $time = time();
          foreach ($edit['roles'] as $rid => $role) {
            if (is_array($edit) && array_key_exists('role_expire_' . $role, $edit) && $edit['role_expire_' . $role] != '') {
              $expiry_time = strtotime($edit['role_expire_' . $role]);
              if (!$expiry_time) {
                form_set_error('role_expire_' . $role, t("Role expiry time is not in correct format."));
              }
              if ($expiry_time <= $time) {
                form_set_error('role_expire_' . $role, t("Role expiry time must be in the future."));
              }
            }
          }
        }
      }
      break;
    case 'after_update':
      if ($category == 'account' && (user_access('administer role expire') || user_access('administer users'))) {

        // This adds any new roles that have been given to the user.
        $new_roles = array_diff($account->roles, _role_static_user_roles($account->uid));

        // We have the new roles, loop over them and see whether we need to assign expiry to them.
        foreach ($new_roles as $role_id => $role) {
          role_expire_process_default_role_duration_for_user($role_id, $account->uid);
        }

        // This removes any new roles that have been given to the user.
        $del_roles = array_diff(_role_static_user_roles($account->uid), $account->roles);

        // We have the deleted roles, loop over them and see whether we need to assign expiry to them.
        foreach ($del_roles as $role_id => $role) {
          role_expire_delete_record($account->uid, $role_id);
        }
      }
      break;
    case 'update':
    case 'insert':
      if ($category == 'account' && (user_access('administer role expire') || user_access('administer users'))) {

        // Add roles expiry information for the user role.
        // We go over all existing roles, because user might have disabled a role.
        foreach (_role_expire_get_role() as $rid => $role) {
          $role = '';
          if (array_key_exists('role_expire_' . $rid, $edit)) {
            $role = $edit['role_expire_' . $rid];
          }
          if ($role != '') {
            $expiry_timestamp = strtotime($role);
            role_expire_write_record($account->uid, $rid, $expiry_timestamp);
          }
          else {
            if ($category == NULL || $category == 'account') {

              // Handle inserts correctly
              if ($op == 'insert') {

                // Check for Role
                if (array_key_exists($rid, $edit['roles']) && $edit['roles'][$rid] == $rid) {
                  role_expire_process_default_role_duration_for_user($rid, $account->uid);
                }
              }
              else {

                // User input is blank, so delete record.
                role_expire_delete_record($account->uid, $rid);
              }
            }
          }
          $edit['role_expire_' . $rid] = NULL;
        }
      }
      break;
    case 'delete':

      // Delete user records.
      role_expire_delete_user_records($account->uid);
      break;
    case 'load':

      // We don't load the information to the user object. Other modules can use
      // our API to query the information.

      /**
       * Load the starter roles into a static cache so it is easy to
       * see what has changed later on.
       */
      _role_static_user_roles($account->uid, $account->roles);
      break;
    case 'view':
      global $user;
      if (user_access('administer role expire') || user_access('administer users') || $user->uid == $account->uid) {
        $roles = array();
        $expiry_roles = role_expire_get_all_user_records($account->uid);
        foreach ($account->roles as $key => $val) {
          if (array_key_exists($key, $expiry_roles)) {
            $roles[$key] = t("%role role - expires on %timedate", array(
              '%role' => ucfirst($val),
              '%timedate' => format_date($expiry_roles[$key]),
            ));
          }
        }
        if ($roles) {
          $account->content['summary']['role_expire'] = array(
            '#type' => 'user_profile_item',
            '#title' => t('Role expiration'),
            '#value' => theme('item_list', $roles),
            '#attributes' => array(
              'class' => 'role-expiry-roles',
            ),
          );
        }
      }
      break;
  }
}