You are here

function autoassignrole_user in Auto Assign Role 6

Same name and namespace in other branches
  1. 5.2 autoassignrole.module \autoassignrole_user()
  2. 5 autoassignrole.module \autoassignrole_user()
  3. 6.2 autoassignrole.module \autoassignrole_user()

Implementation of hook_user().

File

./autoassignrole.module, line 152
The main autoassignrole.module file

Code

function autoassignrole_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'insert':

      // If this is an administrator creating the account only use auto_assign if
      // allowed by auto_admin_active
      if (arg(0) == 'admin' && _autoassignrole_get_settings('auto_admin_active') == 0) {
        return;
      }
      $rolenames = user_roles();
      if (_autoassignrole_get_settings('user_active') == 1) {
        $user_roles = _autoassignrole_get_settings('user_roles');
        if (is_array($edit['user_roles'])) {
          foreach ($edit['user_roles'] as $k => $v) {
            if ($v != 0 && in_array($k, $user_roles, TRUE)) {
              $edit['roles'][$k] = $v;
            }
          }
        }
        elseif ($edit['user_roles'] != '') {
          $edit['roles'][$edit['user_roles']] = $edit['user_roles'];
        }
      }
      if ($rid = autoassignrole_get_active_path_rid()) {
        $edit['roles'][$rid] = $rolenames[$rid];
      }
      if (_autoassignrole_get_settings('auto_active') == 1) {
        $auto_roles = _autoassignrole_get_settings('auto_roles');
        foreach ($auto_roles as $k => $v) {
          $edit['roles'][$k] = $rolenames[$k];
        }
      }
      break;
    case 'register':
      if (_autoassignrole_get_settings('user_active') == 1) {
        $roles = user_roles(TRUE);
        $user_roles = _autoassignrole_get_settings('user_roles');
        $path_roles = db_query("SELECT rid from {autoassignrole_page}");
        while ($path_role = db_fetch_object($path_roles)) {
          unset($roles[$path_role->rid]);
        }
        foreach ($roles as $k => $r) {
          if (!in_array($k, $user_roles, TRUE)) {
            unset($roles[$k]);
          }
        }
        if (count($roles)) {
          $form['autoassignrole_user'] = array(
            '#type' => 'fieldset',
            '#title' => _autoassignrole_get_settings('user_fieldset_title'),
            '#collapsible' => FALSE,
            '#collapsed' => FALSE,
          );
          if (_autoassignrole_get_settings('user_sort') == "SORT_ASC") {
            uasort($roles, '_autoassignrole_array_asc');
          }
          else {
            uasort($roles, '_autoassignrole_array_desc');
          }
          if (!$edit || !array_key_exists('user_roles', $edit)) {
            $edit['user_roles'] = array();
          }
          if (_autoassignrole_get_settings('user_selection') == 0) {
            $form['autoassignrole_user']['user_roles'] = array(
              '#type' => 'radios',
              '#title' => t(_autoassignrole_get_settings('user_title')),
              '#options' => $roles,
              '#description' => t(_autoassignrole_get_settings('user_description')),
            );
          }
          if (_autoassignrole_get_settings('user_selection') == 1) {
            $form['autoassignrole_user']['user_roles'] = array(
              '#type' => 'select',
              '#title' => t(_autoassignrole_get_settings('user_title')),
              '#default_value' => '',
              '#description' => t(_autoassignrole_get_settings('user_description')),
            );
            if (_autoassignrole_get_settings('user_multiple')) {
              $form['autoassignrole_user']['user_roles']['#multiple'] = TRUE;
              unset($form['autoassignrole_user']['user_roles']['#default_value']);
            }
            else {
              $roles[''] = '';
            }
            $form['autoassignrole_user']['user_roles']['#options'] = $roles;
          }
          if (_autoassignrole_get_settings('user_selection') == 2) {
            $form['autoassignrole_user']['user_roles'] = array(
              '#type' => 'checkboxes',
              '#title' => t(_autoassignrole_get_settings('user_title')),
              '#default_value' => $edit['user_roles'],
              '#options' => $roles,
              '#description' => _autoassignrole_get_settings('user_description'),
            );
          }
          if (_autoassignrole_get_settings('user_required')) {
            $form['autoassignrole_user']['user_roles']['#required'] = TRUE;
          }
          return $form;
        }
      }
      break;
  }
}