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

soyarma’s picture

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',

d.clarke’s picture

StatusFileSize
new6.28 KB

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

d.clarke’s picture

StatusFileSize
new6.26 KB

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

betz’s picture

Status: Needs review » Reviewed & tested by the community

Was exactly what i needed.
Worked like a charm!

robert castelo’s picture

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.

soyarma’s picture

Version: 6.x-1.2 » 6.x-2.4
Status: Needs work » Reviewed & tested by the community
StatusFileSize
new6.1 KB
new1.15 KB

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.

robert castelo’s picture

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

soyarma’s picture

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.

Marko B’s picture

Why is this still not used in current version?

robert castelo’s picture

Status: Reviewed & tested by the community » Fixed

Feature added to 6.x.3 tag

soyarma’s picture

woot!

Status: Fixed » Closed (fixed)

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