Code to create a per import delimiter

soyarma - May 13, 2009 - 18:38
Project:User Import
Version:6.x-1.2
Component:Code
Category:task
Priority:normal
Assigned:soyarma
Status:reviewed & tested by the community
Description

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.

#1

soyarma - May 13, 2009 - 19:46

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

beday00 - August 3, 2009 - 23:10

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

beday00 - August 5, 2009 - 02:11

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

AttachmentSize
csv_settings.patch 6.26 KB

#4

betz - August 10, 2009 - 09:54
Status:needs review» reviewed & tested by the community

Was exactly what i needed.
Worked like a charm!

 
 

Drupal is a registered trademark of Dries Buytaert.