? user_import.5x_upgrade.patch
Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_import/README.txt,v
retrieving revision 1.10
diff -u -p -r1.10 README.txt
--- README.txt	7 Jan 2007 03:23:36 -0000	1.10
+++ README.txt	10 Feb 2007 00:03:10 -0000
@@ -3,7 +3,7 @@
 ********************************************************************
 Name: user import module
 Author: Robert Castelo <www.cortextcommunications.com>
-Drupal: 4.7.x
+Drupal: 5.x
 ********************************************************************
 DESCRIPTION:
 
@@ -50,13 +50,13 @@ INSTALLATION:
 Note: It is assumed that you have Drupal up and running.  Be sure to
 check the Drupal web site if you need assistance.
 
-1. Place the entire user_import directory into your Drupal modules/
+1. Place the entire user_import directory into your Drupal sites/all/modules/
    directory.
    
 
 2. Enable the user_import modules by navigating to:
 
-     administer > modules
+     administer > build > modules
      
   Click the 'Save configuration' button at the bottom to commit your
   changes.
@@ -74,40 +74,40 @@ USAGE
 2. To import users, navigate to:
 
 'administer'
-    -- 'settings'
-        -- 'user imports'  (admin/settings/user_import)
+    -- 'user management'
+      -- 'users'
+        -- 'user imports'  (admin/user/user/import)
         
-3. Select 'Import' tab.
 
-4. Press the 'browse' button to select a file to import,
+3. Press the 'browse' button to select a file to import,
     or select a file already added through FTP.
 
-5. Click on Next.
+4. Click on Next.
 
-6. Under CSV file you should see the name of the file you just uploaded.
+5. Under CSV file you should see the name of the file you just uploaded.
 
-7. Under Options you should see Ignore First Line ( use if the first row are labels ), 
+6. Under Options you should see Ignore First Line ( use if the first row are labels ), 
     
     Contact, and Send Email.  Select whichever is appropiate.
 
-8. Under Field Match you should see the various columns from your profile page.
+7. Under Field Match you should see the various columns from your profile page.
 
-9. For each csv column select a Drupal field to map. 
+8. For each csv column select a Drupal field to map. 
 
-10. Under username select 'No', if the field is not to be used to generate the username, or select '1' - '4' 
+9. Under username select 'No', if the field is not to be used to generate the username, or select '1' - '4' 
     for the order to use the field in generating username.
 
     Example: 'LastName' and 'FirstName' are fields to be used as username.  So under the username
     selection chose '1' for 'FirstName' and '2' for 'Lastname', and the username generated will be in 
     the form 'FirstNameLastName'.
 
-11. Under Role Assign select the roles the imported users will be assigned.
+10. Under Role Assign select the roles the imported users will be assigned.
 
-12. Under Save Settings, you can save your settings for use on future imports.
+11. Under Save Settings, you can save your settings for use on future imports.
 
-13. Click "Test" to do an import without committing changes to the database.  Fix any errors that are generated.
+12. Click "Test" to do an import without committing changes to the database.  Fix any errors that are generated.
 
-14. Click "Import" to complete the import.
+13. Click "Import" to complete the import.
 
 
 ********************************************************************
Index: user_import.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_import/user_import.module,v
retrieving revision 1.13
diff -u -p -r1.13 user_import.module
--- user_import.module	7 Jan 2007 18:05:30 -0000	1.13
+++ user_import.module	10 Feb 2007 00:03:10 -0000
@@ -7,19 +7,6 @@
  */
 
 /**
- * Implementation of hook_help().
- */
-function user_import_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('Import users from a comma separated file (csv).');
-    case 'admin/settings/user_import':
-      return t("Import users from a comma separated file (csv). Click 'Import' to start a new import.");
-  }
-}
-
-
-/**
  * Implementation of hook_perm().
  */
 function user_import_perm() {
@@ -36,34 +23,32 @@ function user_import_menu($may_cache) {
   $items = array();
   
      if ($may_cache) {     
-     
-        $items[] = array(
-            'path' => 'admin/settings/user_import', 
-            'title' => t('user imports'), 
-            'callback' => 'user_import_list',
-            'access' => user_access('import users'),
-            );
-        $items[] = array(
-            'path' => 'admin/settings/user_import/list', 
-            'title' => t('list imports'), 
-            'access' => user_access('import users'),
-            'weight' => -10,
-            'type' => MENU_DEFAULT_LOCAL_TASK
-            );
+        $access = user_access('import users');
+        
         $items[] = array(
-          'path' => 'admin/settings/user_import/add', 
-          'title' => t('import'),
-          'callback' => 'user_import_preferences',
-          'access' => user_access('import users'),
-          'weight' => -5,
+          'path' => 'admin/user/user/import', 
+          'title' => t('Import users'),
+          'callback' => 'user_import_add',
+          'access' => $access,
           'type' => MENU_LOCAL_TASK
         );
+
         $items[] = array(
-          'path' => 'admin/settings/user_import/configure', 
-          'title' => t('configure'),
-          'callback' => 'user_import_configure',
-          'access' => user_access('import users'),
-          'type' => MENU_LOCAL_TASK
+            'path' => 'admin/user/user/list-imports', 
+            'title' => t('List user imports'), 
+            'access' => $access,
+            'type' => MENU_LOCAL_TASK,
+            'callback' => 'user_import_list',
+        );
+        
+        $items[] = array(
+          'path' => 'admin/settings/user_imports', 
+          'title' => t('User imports'),
+          'callback' => 'drupal_get_form',
+          'callback arguments' => array('user_import_configure'),
+          'access' => $access,
+          'type' => MENU_NORMAL_ITEM,
+          'description' => t('Define settings for bulk user imports.'),
         );
     }
 
@@ -71,13 +56,13 @@ function user_import_menu($may_cache) {
 }
 
 
-function user_import_list($action = NULL, $import_id = NULL) {
-  
+function user_import_list($import_id = NULL, $action = NULL) {
+
     // clear incomplete imports
     _user_import_incomplete_deletion();
 
     if ($import_id && is_numeric($import_id)) {
-        
+      
         $import = _user_import_settings_select($import_id);
         
         switch ($action) {
@@ -102,7 +87,7 @@ function user_import_list($action = NULL
 
     if (empty($output)) {
         $output = theme_user_import_list();
-        $output .= user_import_templates_form_deletion();
+        $output .= drupal_get_form('user_import_templates_deletion_form');
     }
     
     return $output;    
@@ -123,7 +108,7 @@ function theme_user_import_list() {
                     
         // info row
         $errors = db_result(db_query("SELECT COUNT(import_id) FROM {user_import_errors} WHERE import_id = %d", $import['import_id']));         
-        $errors_link = ($errors == 0) ? '0': l($errors, 'admin/settings/user_import/errors/' . $import['import_id']);           
+        $errors_link = ($errors == 0) ? '0': l($errors, 'admin/user/user/list-imports/' . $import['import_id'] . '/errors');           
         
         $rows[0] = array(
             check_plain($import['oldfilename']),
@@ -137,10 +122,10 @@ function theme_user_import_list() {
         $output .= theme('table', $header, $rows);
         
         // action buttons
-        $settings_link = l(t('Settings'), 'admin/settings/user_import/add/' . $import['import_id']);
-        $delete_link = l(t('Delete'), 'admin/settings/user_import/delete/' . $import['import_id']);
-        $continue_link = l(t('Continue Processing'), 'admin/settings/user_import/continue/' . $import['import_id']);
-        $import_link = l(t('Import'), 'admin/settings/user_import/import/' . $import['import_id']);
+        $settings_link = l(t('Settings'), 'admin/user/user/import/' . $import['import_id']);
+        $delete_link = l(t('Delete'), 'admin/user/user/list-imports/' . $import['import_id'] . '/delete');
+        $continue_link = l(t('Continue Processing'), 'admin/user/user/list-imports/' . $import['import_id'] . '/continue');
+        $import_link = l(t('Import'), 'admin/user/user/list-imports/' . $import['import_id'] . '/import');
         
         $output .= $settings_link  . ' | ';   
         $output .= $delete_link;   
@@ -152,11 +137,12 @@ function theme_user_import_list() {
 }
 
 
-function user_import_templates_form_deletion() {
-
+function user_import_templates_deletion_form() {
+    $form = array();
+    
     $imports = _user_import_settings_select(NULL, TRUE);
     
-    if (!$imports) return ' ';
+    if (!$imports) return $form;
 
     foreach ($imports as $import) {
 
@@ -181,13 +167,11 @@ function user_import_templates_form_dele
         '#value' => t('Delete'),
     );
     
-    $output = drupal_get_form('user_import_templates_form_deletion', $form);  
-
-    return $output; 
+    return $form;
 }
 
 
-function user_import_templates_form_deletion_submit($form_id, $form_values) {
+function user_import_templates_deletion_form_submit($form_id, $form_values) {
 
     foreach ($form_values['templates'] as $import_id) {
         
@@ -207,16 +191,26 @@ function user_import_templates_form_dele
     return;
 }
 
-
+/*
 function user_import_preferences($import_id = NULL, $template_id = NULL) {
 
     $output = (empty($import_id)) ? user_import_add() : user_import_edit($import_id, $template_id);
     return $output;
 }
+*/
 
 
-function user_import_add($import_id = NULL) {
+function user_import_add($import_id = NULL, $template = NULL) {
+
+    if ($import_id) {
+      return drupal_get_form('user_import_edit_import_form', $import_id, $template);
+    }
+    
+    return drupal_get_form('user_import_add_import_form');  
+    
+}
 
+function user_import_add_import_form() {
     $form = array();
 
     $ftp_files = _user_import_ftp_files();
@@ -288,12 +282,10 @@ function user_import_add($import_id = NU
     // Set form parameters so we can accept file uploads.
     $form['#attributes'] = array('enctype' => 'multipart/form-data');
     
-    $output = drupal_get_form('user_import_add', $form);  
-
-    return $output;    
+    return $form;
 }
 
-function user_import_add_validate($form_id, $form_values) {
+function user_import_add_import_form_validate($form_id, $form_values) {
 
     $file = _user_import_file(NULL, $form_values['file_ftp']);
 
@@ -306,7 +298,7 @@ function user_import_add_validate($form_
 }
 
 
-function user_import_add_submit($form_id, $form_values) {
+function user_import_add_import_form_submit($form_id, $form_values) {
 
     $file = _user_import_file(NULL, $form_values['file_ftp']);
     $form_values['filename'] = $file->filename;
@@ -318,12 +310,11 @@ function user_import_add_submit($form_id
     $import = _user_import_settings_save($form_values, $messages = NULL);
     if (!empty($form_values['import_template_select'])) $settings_template = '/' . check_plain($form_values['import_template_select']);
     
-    drupal_goto('admin/settings/user_import/add/' . $import['import_id'] . $settings_template);
-    
-    return;
+    return 'admin/user/user/import/' . $import['import_id'] . $settings_template;
+
 }
 
-function user_import_edit($import_id, $template_id = NULL) {
+function user_import_edit_import_form($import_id, $template_id = NULL) {
 
     $form = array();
     $import  = _user_import_settings_select($import_id);
@@ -366,9 +357,7 @@ function user_import_edit($import_id, $t
         '#value' => t('Cancel'),
     );
     
-    $output = drupal_get_form('user_import_edit', $form);  
-
-    return $output;  
+    return $form;
 }
 
 function user_import_edit_file_fields(&$form, $import) {
@@ -492,7 +481,7 @@ function user_import_edit_settings_field
         '#default_value' => $roles_selected,
     );
     
-    if (module_exist('og')) {
+    if (module_exists('og')) {
     
         $groups = user_import_get_groups();
         
@@ -534,14 +523,14 @@ function user_import_edit_settings_field
       '#type' => 'textfield',
       '#title' => t('Message'),
       '#default_value' => $import['options']['subject'],
-      '#description' => t('Customize the subject of the welcome e-mail, which is sent to imported members.') .' '. t('Available variables are:') .' %username, %site, %password, %uri, %uri_brief, %mailto, %date, %login_uri, %edit_uri, %login_url' . $profile_string . '.',
+      '#description' => t('Customize the subject of the welcome e-mail, which is sent to imported members.') .' '. t('Available variables are:') .' !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !login_url' . $profile_string . '.',
     ); 
     
     $form['email_message']['message'] = array(
       '#type' => 'textarea',
       '#title' => t('Message'),
       '#default_value' => $import['options']['message'],
-      '#description' => t('Customize the body of the welcome e-mail, which is sent to imported members.') .' '. t('Available variables are:') .' %username, %site, %password, %uri, %uri_brief, %mailto, %login_uri, %edit_uri, %login_url' . $profile_string . '.',
+      '#description' => t('Customize the body of the welcome e-mail, which is sent to imported members.') .' '. t('Available variables are:') .' !username, !site, !password, !uri, !uri_brief, !mailto, !login_uri, !edit_uri, !login_url' . $profile_string . '.',
     ); 
     
     return;
@@ -694,7 +683,7 @@ function user_import_edit_match_fields(&
 }
 
 
-function user_import_edit_validate($form_id, $form_values) {
+function user_import_edit_import_form_validate($form_id, $form_values) {
 
     switch ($_POST['op']) {
         
@@ -702,7 +691,7 @@ function user_import_edit_validate($form
 
             _user_import_settings_deletion($form_values['import_id']);
             _user_import_file_deletion($form_values['filepath'], $form_values['filename'], $form_values['oldfilename']);
-            drupal_goto ('admin/settings/user_import/add');
+            drupal_goto ('admin/user/user/import');
             
             break;
             
@@ -713,7 +702,7 @@ function user_import_edit_validate($form
                 _user_import_settings_deletion($form_values['import_id']);
                 _user_import_file_deletion($form_values['filepath'], $form_values['filename'], $form_values['oldfilename']);
             }
-            drupal_goto ('admin/settings/user_import');
+            drupal_goto ('admin/user/user/list-imports');
             break;
             
         // save settings
@@ -773,31 +762,31 @@ function user_import_edit_validate($form
     return;
 }
 
-function theme_user_import_edit($form) {
+function theme_user_import_edit_import_form($form) {
     
     $header = array(t('csv column'), t('Drupal fields'), t('username'), t('abbreviate'));
     
     foreach (element_children($form['field_match']) as $key) {
 
         $rows[] = array(
-            form_render($form['field_match'][$key]['csv']),
-            form_render($form['field_match'][$key]['field_match']),
-            form_render($form['field_match'][$key]['username']),
-            form_render($form['field_match'][$key]['abbreviate']),
+            drupal_render($form['field_match'][$key]['csv']),
+            drupal_render($form['field_match'][$key]['field_match']),
+            drupal_render($form['field_match'][$key]['username']),
+            drupal_render($form['field_match'][$key]['abbreviate']),
         );
     }
     
     $form['field_match']['#value'] = theme('table', $header, $rows);
 
-    $output .= form_render($form['remove']);
-    $output .= form_render($form['options']);
-    $output .= form_render($form['field_match']);
-    $output .= form_render($form);
+    $output .= drupal_render($form['remove']);
+    $output .= drupal_render($form['options']);
+    $output .= drupal_render($form['field_match']);
+    $output .= drupal_render($form);
     return $output;
 }
 
 
-function user_import_edit_submit($form_id, $form_values) {
+function user_import_edit_import_form_submit($form_id, $form_values) {
 
     // if import is being added - move file out of tmp directory
     if ($form_values['setting'] == 'file set') {
@@ -817,7 +806,7 @@ function user_import_edit_submit($form_i
             _user_import_settings_save($form_values);
             
             // save settings for template
-            $import_return = 'admin/settings/user_import/add/' . $form_values['import_id'];
+            $import_return = 'admin/user/user/import/' . $form_values['import_id'];
             $form_values['setting'] = 'template';
             unset($form_values['import_id']);
             _user_import_initialise_import($form_values);
@@ -836,7 +825,7 @@ function user_import_edit_submit($form_i
             _user_import_settings_save($form_values);
             
             // save settings for template
-            $import_return = 'admin/settings/user_import/add/' . $form_values['import_id'];
+            $import_return = 'admin/user/user/import/' . $form_values['import_id'];
             $form_values['setting'] = 'template';
             $form_values['import_id'] = $template_id;
             _user_import_initialise_import($form_values);
@@ -863,9 +852,8 @@ function user_import_edit_submit($form_i
     $form_values['import_template_id'] = NULL;
     $form_values['save']['name'] = NULL;
     _user_import_process($form_values);
-    drupal_goto ('admin/settings/user_import');
+    return 'admin/user/user/list-imports';
 
-    return;
 }
 
 
@@ -954,9 +942,40 @@ function user_import_configure() { 
         '#value' => t('Save'),
         );
     
-    return drupal_get_form('user_import_configure', $form);
+    return $form;
+}
+
+function user_import_configure_validate($form_id, &$form) {
+
+    if (is_numeric($form['user_import_max'])) {
+        if ($form['user_import_max'] < 10) form_set_error('user_import_max', t("Value should be at least 10."));       
+    } 
+    else {
+        form_set_error('user_import_max', t('Value must be a number.'));
+    }
+    
+    if (is_numeric($form['user_import_line_max'])) {
+        if ($form['user_import_line_max'] < 1000) form_set_error('user_import_line_max', t("Value must be higher than 1000."));
+        if ($form['user_import_line_max'] > 1000000) form_set_error('user_import_line_max', t("Value must be lower than 1,000,000."));
+    } 
+    else {
+        form_set_error('user_import_line_max', t('Value must be a number.'));
+    }
+    
+    return;
 }
 
+function user_import_configure_submit($form_id, $form_values) { 
+
+    if (form_get_errors()) return;
+    settype($form_values['user_import_max'], 'integer');
+    settype($form_values['user_import_line_max'], 'integer');
+    variable_set('user_import_max', $form_values['user_import_max']);
+    variable_set('user_import_line_max', $form_values['user_import_line_max']);
+    variable_set('user_import_settings', $form_values['user_import_settings']);
+    drupal_set_message(t('Configuration settings have been saved.'));
+    return 'admin/settings/user_import';
+}
 
 // Create Username    
 function _user_import_create_username($order, $data, $abbreviate, $username_space) {
@@ -1026,7 +1045,7 @@ function _user_import_send_email($accoun
     $profile_name = _user_import_profile('fid', 'name');
     $from = variable_get('site_mail', ini_get('sendmail_from'));
     
-    $variables = array('%username' => $account->name, '%site' => variable_get('site_name', 'drupal'), '%login_url' => user_pass_reset_url($account), '%password' => $password, '%uri' => $base_url, '%uri_brief' => drupal_substr($base_url, drupal_strlen('http://')), '%mailto' => $account->mail, '%date' => format_date(time()), '%login_uri' => url('user', NULL, NULL, TRUE), '%edit_uri' => url('user/'. $account->uid .'/edit', NULL, NULL, TRUE));
+    $variables = array('!username' => $account->name, '!site' => variable_get('site_name', 'drupal'), '!login_url' => user_pass_reset_url($account), '!password' => $password, '!uri' => $base_url, '!uri_brief' => drupal_substr($base_url, drupal_strlen('http://')), '!mailto' => $account->mail, '!date' => format_date(time()), '!login_uri' => url('user', NULL, NULL, TRUE), '!edit_uri' => url('user/'. $account->uid .'/edit', NULL, NULL, TRUE));
 
     // import info to profile
     if (is_array($profile)) {
@@ -1037,13 +1056,16 @@ function _user_import_send_email($accoun
 
     $subject = (empty($subject)) ? _user_mail_text('welcome_subject', $variables) : strtr($subject, $variables);
     $body = (empty($body)) ? _user_mail_text('welcome_body', $variables) : strtr($body, $variables);
-    user_mail($account->mail, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
+
+    drupal_mail('user_import_welcome_message', 
+      $account->mail, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from",
+      array('From' => $from));
     return;
 }
 
 function _user_import_profile($key = 'fid', $return_value = NULL) {
     
-    if (!module_exist('profile')) return; 
+    if (!module_exists('profile')) return; 
     
     static $fields_static;
     
@@ -1145,6 +1167,11 @@ function _user_import_settings_select($i
             $row['roles'] = unserialize($row['roles']);
             $row['options'] = unserialize($row['options']);
             $import[] = $row;
+            
+            // delete any remaining files for completely processed imports
+            if ($row['setting'] == 'imported') {
+              _user_import_file_deletion($row['filepath'], $row['filename'], $row['oldfilename'], false);
+            }
         }
     }
       
@@ -1200,39 +1227,6 @@ function _user_import_random_username() 
     return $username;
 }
 
-
-function user_import_configure_validate($form_id, &$form) {
-
-    if (is_numeric($form['user_import_max'])) {
-        if ($form['user_import_max'] < 10) form_set_error('user_import_max', t("Value should be at least 10."));       
-    } 
-    else {
-        form_set_error('user_import_max', t('Value must be a number.'));
-    }
-    
-    if (is_numeric($form['user_import_line_max'])) {
-        if ($form['user_import_line_max'] < 1000) form_set_error('user_import_line_max', t("Value must be higher than 1000."));
-        if ($form['user_import_line_max'] > 1000000) form_set_error('user_import_line_max', t("Value must be lower than 1,000,000."));
-    } 
-    else {
-        form_set_error('user_import_line_max', t('Value must be a number.'));
-    }
-    
-    return;
-}
-
-function user_import_configure_submit($form_id, $form_values) { 
-
-    if (form_get_errors()) return;
-    settype($form_values['user_import_max'], 'integer');
-    settype($form_values['user_import_line_max'], 'integer');
-    variable_set('user_import_max', $form_values['user_import_max']);
-    variable_set('user_import_line_max', $form_values['user_import_line_max']);
-    variable_set('user_import_settings', $form_values['user_import_settings']);
-    drupal_set_message(t('Configuration settings have been saved.'));
-    drupal_goto('admin/settings/user_import');
-}
-
 // Clean file - in case of incompatable line endings
 function _user_import_file_cleaner($filepath) {
 
@@ -1257,7 +1251,7 @@ function _user_import_process($settings)
         if (!empty($role_set)) $roles[$rid] = $rid;
     }
 
-    $handle = @fopen($settings['filepath'], "r");
+    $handle = fopen($settings['filepath'], "r");
     
     // move pointer to where test/import last finished
     if ($settings['pointer'] != 0) fseek ($handle, $settings['pointer']);
@@ -1377,12 +1371,12 @@ function _user_import_process($settings)
 }
 
 function _user_import_file_deletion($filepath, $filename, $old_filename, $message = TRUE) {
-
+    
     $removed = file_delete($filepath);
     
     if (!$message) return;
     
-    if (empty($removed)) {
+    if (empty($removed) && file_exists($filepath)) {
         drupal_set_message(t("File error: file '%old_filename' (%filename) could not be deleted.", array('%old_filename' => $oldfilename, '%filename' => $filename)), 'error');
     } 
     else {
@@ -1562,7 +1556,7 @@ function user_import_get_groups() {
 // add user to groups
 function _user_import_organic_groups($groups_selected, $uid) {
 
-    if (!module_exist('og')) return;
+    if (!module_exists('og')) return;
      
     $organic_groups = user_import_get_groups();
     $og_args = array('is_active' => 1, 'is_admin' => 0, 'mail_type' => 0, 'created' => time());
