Download & Extend

Code to create a per import delimiter

Project:User Import
Version:6.x-2.4
Component:Code
Category:task
Priority:normal
Assigned:soyarma
Status:closed (fixed)

Issue Summary

I have a need for this as I have different files from different sources with different delims. I figured it would be useful as a feature in the module for folks who do not get comma separated CSV files, such as the excel ones which are tab by default (odd, I know).

Because I'm making some other alterations to the module to allow multilingual integration, I don't have the current 1.2 line positions to make a patch, but I do have the steps.

Step 1: Create a new column in the user_import table.
- Name it delimiter, place it after filepath (though it doesn't really matter) and make it varchar 1 with a default of ,

Step 2: modify user_import_form_field_match function
- add 3rd param to _user_import_file_row() function call.
- param: $import['delimeter']
- result: function _user_import_file_row($filename, $handle, $delim) {

Step 3: modify _user_import_file_row function
- add $delim = ',' as 3rd function param
- like so: function _user_import_file_row($filename, $handle, $delim = ',') {
- add new line to top of function:
- $delim = strtolower($delim) == 't' ? "\t" : $delim;
- change: $data_row = @fgetcsv ($handle, 1000000, ","); to
- $data_row = @fgetcsv ($handle, 1000000, $delim);

Step 4: modify _user_import_proces function
- first line of function add: $delim = strtolower($settings['delimeter']) == 't' ? "\t" : $settings['delimeter'];
- then change: while ($data = fgetcsv($handle, $line_max, ',')) {
- to: while ($data = fgetcsv($handle, $line_max, $delim)) {

Step 5: user_import_add_file_form function
- add before the return line:

$form['file_settings'] = array(
'#type' => 'fieldset',
'#title' => t('File Settings'),
'#collapsible' => TRUE,
'#description' => t("File column delimeter"),
);

$form['file_settings']['delimeter'] = array(
'#type' => 'textfield',
'#title' => t('Delimeter'),
'#size' => 4,
'#default_value' => ',',
'#description' => t('The column delimeter for the file. Use t for tab. '),
);

Step 6: Modify _user_import_settings_save function

- alter update query to have: delimeter: = '%s',
- after: filepath = '%s',
- alter update query to have: $settings['delimeter'],
- after: $settings['filepath'],
- alter insert query to have: delimeter,
- after: started,
- alter insert query to have: '%s',
- after the 5th item in the values set which is the first: %d,
- alter insert query to have: $settings['delimeter'],
- after: time(),

Once this is done, each import file can have its own delimiter.

Comments

#1

Missed one step.

In the user_import_edit_file_fields function add before the return:

$form['delimiter'] = array(
'#type' => 'value',
'#value' => $import['delimiter'],
);

There's also a typo in the last step above.
delimeter: = '%s',
should be
delimeter = '%s',

#2

I also ran into the same issue as well as needing to specify a string escape character and an unlimited line length.

The attached patch creates a 'CSV Settings' fieldset on the configure tab that allows users to specify a delimiter and a string escape character. It also allows users to specify a line length of 0 which allows php to determine the line length. If a user would like to use a tab delimiter they can enter \t.

Can someone please review the proposed solution?

Thanks,
Dale

AttachmentSize
csv_settings.patch 6.28 KB

#3

Fix for my patch so that it is easier to apply.

AttachmentSize
csv_settings.patch 6.26 KB

#4

Status:needs review» reviewed & tested by the community

Was exactly what i needed.
Worked like a charm!

#5

Status:reviewed & tested by the community» needs work

I couldn't get the patch in #3 to work with the sample.txt file.

Sorry, not going to review #1 as there's no patch file.

#6

Version:6.x-1.2» 6.x-2.4
Status:needs work» reviewed & tested by the community

Adding patch files for 2.4

A new column is added to the user_import table, so you'll need to run update.php after applying the patches.

AttachmentSize
user_import.install.patch 1.15 KB
user_import.module.patch 6.1 KB

#7

Have you tried this out with the sample.txt file that's included with User Import module?

#8

I tested it with the sample.txt file and I also created a copy of sample.txt with pipe delimiters and it worked in that case too.

#9

Why is this still not used in current version?

#10

Status:reviewed & tested by the community» fixed

Feature added to 6.x.3 tag

#11

woot!

#12

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nobody click here