Extra flight fields, improved data import function

This commit is contained in:
2014-07-27 15:13:28 +10:00
parent 215a261ce2
commit 5984437bff
5 changed files with 147 additions and 40 deletions

View File

@@ -66,6 +66,8 @@ function booking_import_data_admin_submit($form, &$form_state)
$update_counter = 0;
$delimiter = ",";
$result_array = array();
$datetime_fields = array('booking_outflight_origin_ts', 'booking_outflight_destination_ts', 'booking_rtrnflight_origin_ts',
'booking_rtrnflight_destination_ts');
$builtin_fields_to_import = array('nid', 'booking_status');
$custom_fields_to_import = explode(";", variable_get('booking_import_include_fields', ''));
@@ -79,8 +81,11 @@ function booking_import_data_admin_submit($form, &$form_state)
drupal_set_message(t('Input file uploaded successfully, filename: "@filename"', array('@filename' => $file->filename)));
$filename = drupal_realpath($file->uri);
//as per http://stackoverflow.com/questions/4541749/fgetcsv-fails-to-read-line-ending-in-mac-formatted-csv-file-any-better-solution
ini_set("auto_detect_line_endings", "1");
//convert csv to associative array
//based on http://stackoverflow.com/questions/4801895/csv-to-associative-array
//based on http://stackoverflow.com/questions/4801895/csv-to-associative-array
if( ($handle = fopen( $filename, "r")) !== FALSE) {
$rowCounter = 0;
while (($rowData = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
@@ -132,6 +137,12 @@ function booking_import_data_admin_submit($form, &$form_state)
$update_text .= " set booking status to '" . $record[$field] . "'; ";
$update_array[$field] = _booking_status_lookup($record[$field]);
}
//check for fields that need to be converted to a timestamp from text
elseif (in_array($field, $datetime_fields))
{
$update_array[$field] = _datetime_to_ts_nonstrict($record[$field]);
$update_text .= " set '" . $field . "' to '" . $update_array[$field] . "'; ";
}
elseif ( (! isset($record[$field])) || $record[$field] == '' )
{
//drupal_set_message("Error: Unable to locate expected field '$field' in input file for record number $update_counter.", 'error', FALSE);