From 00fdb80b15a09a08a6c0e54802886d45bc530c95 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Sat, 30 Jun 2018 14:40:09 +1000 Subject: [PATCH] support data import via booking_tempid as well as nid --- booking.import_data.inc | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/booking.import_data.inc b/booking.import_data.inc index 6590e70..1fcb373 100644 --- a/booking.import_data.inc +++ b/booking.import_data.inc @@ -85,6 +85,11 @@ function booking_import_data_admin_submit($form, &$form_state) $datetime_fields = array('booking_outflight_origin_ts', 'booking_outflight_destination_ts', 'booking_rtrnflight_origin_ts', 'booking_rtrnflight_destination_ts','booking_dob', 'booking_passport_expiry_date'); $builtin_fields_to_import = array('nid'); + + // These are fields we can use to key the update + $unique_fields = array('nid', 'booking_tempid'); + $update_key = ""; + //$custom_fields_to_import = explode(";", variable_get('booking_import_include_fields', '')); $data = $form_state['input']; $import_fields = $data['booking_import_data_fields']; @@ -136,7 +141,20 @@ function booking_import_data_admin_submit($form, &$form_state) $studygroups_update_array = array(); $studygroup_roles_update_array = array(); - //do some error checking + // Check that there is a key we can use to perform the update + foreach ($unique_fields as $key) { + if (array_key_exists($key, $record)) { + $update_key = $key; + } + } + + // Check that we found a key, otherwise the update can't be processed + if ($key === "") { + drupal_set_message("Error: Unable to locate any unique identifier in input file for record number $update_counter.", 'error', FALSE); + break; + } + + // Pre-process some data foreach($fields_to_import as $field) { //make sure to skip the nid field since we can't update that if ($field == 'nid') { @@ -175,14 +193,15 @@ function booking_import_data_admin_submit($form, &$form_state) } } + // Perform the actual update if (count($update_array) > 0) { $query = db_update('booking_person') ->fields($update_array) - ->condition('nid', $record['nid']); + ->condition($key, $record[$key]); $rows = $query->execute(); - - $update_messages[] = t("Update Query:
@info
Condition: @nid
Rows affected: @rows
Values:
@values
", array('@info' => (string) $query, '@nid' => $record['nid'], '@rows' => $rows, '@values' => print_r( $update_array, true) )); + $update_messages[] = t("Update Query:
@info
Condition: @nid
Rows affected: @rows
Values:
@values
", + array('@info' => (string) $query, '@nid' => $record[$key], '@rows' => $rows, '@values' => print_r( $update_array, true) )); } //handle study group processing