? 377970-auto_username_to_D6.patch Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/auto_username/README.txt,v retrieving revision 1.1 diff -u -p -r1.1 README.txt --- README.txt 7 Apr 2008 21:09:17 -0000 1.1 +++ README.txt 1 Jul 2010 19:15:41 -0000 @@ -1,4 +1,4 @@ -; $Id $ +; $Id$ ABOUT This module disables the "Username" field on user registration and user edit @@ -11,7 +11,7 @@ auto_username depends on token.module. REQUIREMENTS -- Drupal 5.x +- Drupal 6.x - Token module CONFIGURATION @@ -24,5 +24,9 @@ Maintainer: Larry Garfield http://www.palantir.net/ +Porting to Drupal 6.x: +Fabio Varesano +http://www.varesano.net/ + This module was initially developed by Palantir.net and released to the Drupal community under the GNU General Public License. Index: auto_username.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/auto_username/auto_username.info,v retrieving revision 1.1 diff -u -p -r1.1 auto_username.info --- auto_username.info 7 Apr 2008 21:09:17 -0000 1.1 +++ auto_username.info 1 Jul 2010 19:15:41 -0000 @@ -1,4 +1,5 @@ ; $Id $ name = Automatic User Names description = Allows a user's username to be assigned based on profile fields. -depends = token \ No newline at end of file +core = 6.x +dependencies[] = token \ No newline at end of file Index: auto_username.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/auto_username/auto_username.module,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 auto_username.module --- auto_username.module 14 Apr 2008 15:32:59 -0000 1.1.2.1 +++ auto_username.module 1 Jul 2010 19:15:41 -0000 @@ -1,12 +1,13 @@ 'admin/user/auto-username', - 'title' => t('Username rules'), - 'description' => t('Configure the way that usernames are automatically generated.'), - 'callback' => 'drupal_get_form', - 'callback arguments' => array('auto_username_configuration'), - 'access' => user_access('configure username patterns'), - ); - } + $items['admin/user/auto-username'] = array( + 'title' => 'Username rules', + 'description' => 'Configure the way that usernames are automatically generated.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('auto_username_configuration'), + 'access arguments' => array('configure username patterns'), + ); return $items; } function auto_username_configuration() { + + // Always run before pathauto + if (module_exists('pathauto')) { + $weight = db_result(db_query("SELECT weight FROM {system} WHERE name='auto_username'")); + + // update the weight in the system table + db_query("UPDATE {system} SET weight=" . ($weight + 1) . " WHERE name='pathauto'"); + } + $form = array(); + // Other module configuration. + $form['aun_general_settings'] = array( + '#type' => 'fieldset', + '#title' => t('General settings'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + // The basic pattern. Always supports PHP, as there's little value in not doing so. - $form['aun_pattern'] = array( + $form['aun_general_settings']['aun_pattern'] = array( '#type' => 'textarea', '#title' => t('Pattern for username'), '#description' => t('Enter the pattern for usernames. You may use any of the tokens listed below.'), '#default_value' => variable_get('aun_pattern', ''), ); - // General module configuration. - $form['aun_settings'] = array( + $form['aun_general_settings']['token_help'] = array( + '#title' => t('Replacement patterns'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Note that profile fields that are not present in the user registration form will get replaced with an empty string when the account is created. That is rarely desirable. Also, values such as the user id are not available yet on user creation, so using them is not advisable.'), + ); + $form['aun_general_settings']['token_help']['help'] = array( + '#value' => theme('token_help', 'user'), + ); + + // Other module configuration. + $form['aun_other_settings'] = array( '#type' => 'fieldset', - '#title' => t('Other options'), + '#title' => t('Other settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); - $form['aun_settings']['aun_php'] = array( + $form['aun_other_settings']['aun_php'] = array( '#type' => 'checkbox', '#title' => t('Evaluate PHP in pattern.'), '#description' => t('If this box is checked, the pattern will be executed as PHP code after token substitution has taken place. You must surround the PHP code in <?php and ?> tags. Token replacement will take place before PHP code execution. That means you can place tokens into the PHP code where you want that value to appear literally.'), '#default_value' => variable_get('aun_php', 0), ); - $form['aun_settings']['aun_update_on_edit'] = array( + $form['aun_other_settings']['aun_update_on_edit'] = array( '#type' => 'checkbox', '#title' => t('Update on user edit'), '#description' => t('If this box is checked, the username will be reset any time the user\'s profile is updated. That can help to enforce a username format, but may result in a user\'s login name changing unexpectedly. It is best used in conjunction with an alternative login mechanism, such as OpenID or an e-mail address.'), '#default_value' => variable_get('aun_update_on_edit', 1), ); - $form['aun_settings']['aun_reduce_ascii'] = array( + $form['aun_other_settings']['aun_reduce_ascii'] = array( '#type' => 'checkbox', '#title' => t('Reduce strings to letters and numbers from ASCII-96'), '#default_value' => variable_get('aun_reduce_ascii', 0), '#description' => t('Filters the new username to only letters and numbers found in the ASCII-96 set.'), ); - $form['aun_settings']['aun_replace_whitespace'] = array( + $form['aun_other_settings']['aun_replace_whitespace'] = array( '#type' => 'checkbox', '#title' => t('Replace whitespace with separator.'), '#default_value' => variable_get('aun_replace_whitespace', 0), '#description' => t('Replace all whitespace in tokens with the separator character specified below. Note that this will affect the tokens themselves, not the pattern specified above. To avoid spaces entirely, ensure that the pattern above contains no spaces.'), ); - $form['aun_settings']['aun_separator'] = array( + $form['aun_other_settings']['aun_separator'] = array( '#type' => 'textfield', '#title' => t('Separator'), '#description' => t('This value will be used in place of selected punctuation characters (see below).'), @@ -102,17 +128,6 @@ function auto_username_configuration() { ); } - $form['token_help'] = array( - '#title' => t('Replacement patterns'), - '#type' => 'fieldset', - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#description' => t('Note that profile fields that are not present in the user registration form will get replaced with an empty string when the account is created. That is rarely desirable. Also, values such as the user id are not available yet on user creation, so using them is not advisable.'), - ); - $form['token_help']['help'] = array( - '#value' => theme('token_help', 'user'), - ); - return system_settings_form($form); } @@ -134,7 +149,7 @@ function auto_username_user($op, &$edit, // Add a serial to the name for uniqueness. $counter = 1; $base_name = $new_name; - while (db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $new_name)) > 0) { + while (db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $new_name)) > 0) { $new_name = $base_name . $counter++; } // Save to the session so that we can grab it in the form submit handler @@ -144,6 +159,9 @@ function auto_username_user($op, &$edit, break; case 'insert': + // change $account so that other modules (eg. pathauto) will be able to use the new name + $account->name = $new_name; + $edit['name'] = $new_name; db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array($new_name, $account->uid)); break; @@ -172,7 +190,7 @@ function _auto_username_patternprocessor // Because token.module doesn't let us escape individual tokens, we have to // bypass its normal processing routine. This pattern is borrowed almost // entirely from pathauto. - $placeholders = auto_username_get_placeholders('user', $account); + $placeholders = auto_username_get_placeholders('user', (object) $account); $pattern = variable_get('aun_pattern', ''); if (trim($pattern)) { $output = str_replace($placeholders['tokens'], $placeholders['values'], $pattern); @@ -304,7 +322,7 @@ function auto_username_cleanstring($stri /** * Implementation of hook_form_alter(). */ -function auto_username_form_alter($form_id, &$form) { +function auto_username_form_alter(&$form, &$form_state, $form_id) { if ('user_register' == $form_id) { // Fake a real user name submission, because we have to validate against // the bulit-in username validation even though we're going to change the @@ -313,10 +331,8 @@ function auto_username_form_alter($form_ '#type' => 'value', '#value' => user_password(10), ); - - // Swap in our own submit handler so that we can override the username. - unset($form['#submit']['user_register_submit']); - $form['#submit']['auto_username_user_register_submit'] = array(); + + $form['#submit'][] = 'auto_username_user_register_submit'; } if ('user_edit' == $form_id && variable_get('aun_update_on_edit', 1)) { @@ -324,7 +340,7 @@ function auto_username_form_alter($form_ if (isset($form['account']['name'])) { $form['account']['name'] = array( '#type' => 'value', - '#value' => $form['account']['name']['#default_value'] ? $form['account']['name']['#default_value'] : user_password(), + '#value' => $form['account']['name']['#default_value'] ? $form['account']['name']['#default_value'] : user_password(10), ); } } @@ -332,17 +348,17 @@ function auto_username_form_alter($form_ /** * Submit handler for the user regsitration form. - * + * * We need to override the submit handler so that we can change the username * before the welcome email gets sent out. Unfortunately the email gets sent * directly from the form submit handler. Argl. */ -function auto_username_user_register_submit($form_id, $form_values) { +function auto_username_user_register_submit($form, &$form_state) { - $form_values['name'] = $_SESSION['auto_username']['new_name']; + $form_state['values']['name'] = $_SESSION['auto_username']['new_name']; unset($_SESSION['auto_username']['new_name']); - - return user_register_submit($form_id, $form_values); + + return; } /** @@ -396,7 +412,7 @@ function auto_username_punctuation_chars if (!function_exists('profile_token_values')) { /** - * Implementation of hook_token_values() + * Implementation of hook_token_values(). * * We have to fill in support for profile fields, which by rights should be * in token itself. Hopefully this can be moved back into token module later. @@ -421,11 +437,11 @@ if (!function_exists('profile_token_valu $account = user_load(array('uid' => $user->uid)); } - $result = db_query("SELECT * FROM {profile_fields} ORDER BY category, weight"); + $result = db_query("SELECT * FROM {profile_fields} WHERE type='textfield' ORDER BY category, weight"); while ($record = db_fetch_object($result)) { $name = isset($account->{$record->name}) ? $account->{$record->name} : ''; $values[$record->name .'-raw'] = $name; - $values[$record->name .'-filtered'] = check_plain($name); + $values[$record->name .'-filtered'] = trim(check_plain($name)); } break; } @@ -433,7 +449,7 @@ if (!function_exists('profile_token_valu } /** - * Implementation of hook_token_list() + * Implementation of hook_token_list(). * * We have to fill in support for profile fields, which by rights should be * in token itself. Hopefully this can be moved back into token module later. @@ -441,7 +457,7 @@ if (!function_exists('profile_token_valu function profile_token_list($type = 'all') { if (($type == 'user' || $type == 'all')) { - $result = db_query("SELECT * FROM {profile_fields} ORDER BY category, weight"); + $result = db_query("SELECT * FROM {profile_fields} WHERE type='textfield' ORDER BY category, weight"); while ($record = db_fetch_object($result)) { $tokens['user'][$record->name .'-raw'] = $record->title; $tokens['user'][$record->name .'-filtered'] = t('@field (filtered)', array('@field' => $record->title));