t('Auto assign role'), 'description' => t('Designate a role to assign all new users to.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('autoassignrole_admin_form'), 'access arguments' => array('administer autoassignrole'), 'type' => MENU_NORMAL_ITEM, 'file' => 'autoassignrole-admin.inc', ); return $items; } /** * Implementation of hook_perm(). * @return array */ function autoassignrole_perm() { return array('administer autoassignrole'); } /** * Implementation of hook_user(). */ function autoassignrole_user($op, &$edit, &$account, $category = NULL) { switch ($op) { case 'insert': if (_autoassignrole_settings('auto_active') == 1) { $edit['roles'] = _autoassignrole_settings('auto_roles'); } if (_autoassignrole_settings('user_active') == 1) { $roles = array(); $user_roles = _autoassignrole_settings('user_roles'); foreach ($edit['user_roles'] as $k => $v) { if (in_array($k, $user_roles)) { $roles[$k] = $v; } } $edit['roles'] = $roles; } break; case 'register': if (_autoassignrole_settings('user_active') == 1) { $form['autoassignrole_user'] = array( '#type' => 'fieldset', '#title' => _autoassignrole_settings('user_fieldset_title'), '#collapsible' => FALSE, '#collapsed' => FALSE, ); $roles = user_roles(TRUE); $user_roles = _autoassignrole_settings('user_roles'); foreach ($roles as $k => $r) { if (!in_array($k, $user_roles)) { unset($roles[$k]); } } if (_autoassignrole_settings('user_sort') == "SORT_ASC") { array_multisort($roles, SORT_ASC); } else { array_multisort($roles, SORT_DESC); } if (!$edit || !array_key_exists('user_roles', $edit)) { $edit['user_roles'] = array(); } $form['autoassignrole_user']['user_roles'] = array( '#type' => 'checkboxes', '#title' => t('Roles'), '#default_value' => $edit['user_roles'], '#options' => $roles, '#description' => _autoassignrole_settings('user_description'), ); return $form; } break; } } function _autoassignrole_settings($value) { static $settings = array(); if (count($settings) == 0) { $result = db_query("SELECT arid, value from {autoassignrole}"); while ($s = db_fetch_object($result)) { $settings[$s->arid] = $s->value; } return _autoassignrole_settings($value); } else { switch ($value) { case 'auto_roles': case 'user_roles': $roles = $settings[$value]; $roles = unserialize($roles); foreach ($roles as $k => $r) { if ($r == 0) { unset($roles[$k]); } } $return = $roles; break; default: $return = $settings[$value]; break; } return $return; } } function _autoassignrole_user_input($args) { switch ($args) { case 'type': if (_autoassignrole_settings('user_multiple') == 0) { $type = 'radios'; } else { $type = 'checkboxes'; } return $type; break; case 'required': if (_autoassignrole_settings('user_required') == 0) { $required = FALSE; } else { $required = TRUE; } return $required; break; } } function _autoassignrole_intersect() { $result = _autoassignrole_array_intersect_key( user_roles(TRUE), _autoassignrole_settings('user_roles') ); drupal_set_message(print_r($result,true)); if (_autoassignrole_settings('user_sort') == "SORT_ASC") { array_multisort($result, SORT_ASC); } else { array_multisort($result, SORT_DESC); } return $result; } function _autoassignrole_array_intersect_key($isec, $keys) { $argc = func_num_args(); if ($argc > 2) { for ($i = 1; !empty($isec) && $i < $argc; $i++) { $arr = func_get_arg($i); foreach (array_keys($isec) as $key) { if (!isset($arr[$key])) { unset($isec[$key]); } } } return $isec; } else { $res = array(); foreach (array_keys($isec) as $key) { if (isset($keys[$key])) { $res[$key] = $isec[$key]; } } return $res; } }