Upload csv file containing data to import. Minimum fields present should be nid and booking_status, " . " along with user-specified fields of !config. CSV Column names should match exactly (case sensitive).

", array('!config' => variable_get('booking_import_include_fields', ''))); $form = array(); $form['file'] = array( '#type' => 'file', '#title' => t('CSV data'), '#description' => t('Upload CSV data to be processed'), ); $form['submit'] = array ( '#type' => 'submit', '#value' => t('Submit'), ); return array ( 'first_para' => array ( '#type' => 'markup', '#markup' => $prefix, ), 'form' => $form, ); } function booking_import_data_admin_validate($form, &$form_state) { $file = file_save_upload('file', array( 'file_validate_extensions' => array('txt csv'), // Validate extensions. )); // If the file passed validation: if ($file) { // Move the file, into the Drupal file system if ($file = file_move($file, 'public://', FILE_EXISTS_REPLACE )) { // Save the file for use in the submit handler. $form_state['storage']['file'] = $file; } else { form_set_error('file', t('Failed to write the uploaded file to the site\'s file folder.')); } } else { form_set_error('file', t('No file was uploaded.')); } } function booking_import_data_admin_submit($form, &$form_state) { global $event; $array = array(); //$expected_fields = array('nid', 'booking_amount_paid', 'booking_total_pay_reqd', 'booking_status'); $error = false; $update_counter = 0; $delimiter = ","; $result_array = array(); $builtin_fields_to_import = array('nid', 'booking_status'); $custom_fields_to_import = explode(";", variable_get('booking_import_include_fields', '')); $fields_to_import = array_merge($builtin_fields_to_import, $custom_fields_to_import); //get the file name from temporary storage field $file = $form_state['storage']['file']; // We are done with the file, remove it from storage. unset($form_state['storage']['file']); drupal_set_message(t('Input file uploaded successfully, filename: "@filename"', array('@filename' => $file->filename))); $filename = drupal_realpath($file->uri); //convert 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) { if( 0 === $rowCounter) { $headerRecord = $rowData; } else { foreach( $rowData as $key => $value) { $array[$rowCounter - 1][ $headerRecord[$key] ] = $value; } } $rowCounter++; } fclose($handle); } else //unable to open file { drupal_set_message("Error: Unable to open input file " . $filename, 'error', FALSE); return; } //watchdog('booking', "
Import data:\n@info
", array('@info' => print_r( $array, true))); //process the input data foreach ($array as $record) { //watchdog('booking', "
Processing row data:\n@info
", array('@info' => print_r( $record, true))); $update_counter++; $update_text = ""; $update_array = array(); //do some error checking foreach($fields_to_import as $field) { /* if (! isset($record[$field])) watchdog('booking', 'Non-set field !field: !info', array ('!field' => $field, '!info' => var_export($record, TRUE))); if ($record[$field] == '') watchdog('booking', 'Blank field !field: !info', array ('!field' => $field, '!info' => var_export($record, TRUE))); */ //make sure to skip the nid field since we can't update that if ($field == 'nid') { //do nothing } //convert the booking status to the number used internally elseif ($field == 'booking_status') { $update_text .= " set booking status to '" . $record[$field] . "'; "; $update_array[$field] = _booking_status_lookup($record[$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); //watchdog('booking', 'Processing user record: @info', array ('@info' => $record['nid'])); //$error = true; //skip to the next record //continue 2; } else { $update_text .= " set '" . $field . "' to '" . $record[$field] . "'; "; $update_array[$field] = $record[$field]; } } drupal_set_message(t("Updating record !nid as follows: !update", array('!nid' => $record['nid'], '!update' => $update_text))); $query = db_update('booking_person') ->fields($update_array) ->condition('nid', $record['nid']); $rows = $query->execute(); //$args = $query->getArguments(); watchdog('booking', "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) )); /* $result_array[] = t('Setting payment for id !nid to $!price of total required $!total and status to !status', array('!nid' => $record['nid'], '!price' => $record['booking_amount_paid'], '!total' => $record['booking_total_pay_reqd'], '!status' => _booking_status_lookup($record['booking_status']) ) ); */ /* //TODO: output this from $result_array drupal_set_message(t('Setting payment for id !nid to $!price of total required $!total and status to !status', array('!nid' => $record['nid'], '!price' => $record['booking_amount_paid'], '!total' => $record['booking_total_pay_reqd'], '!status' => _booking_status_lookup($record['booking_status'])) )); */ // watchdog('booking', 'Setting payment for regn id !nid to $!price and status to !status', // array('!nid' => $record['nid'], '!price' => $record['booking_amount_paid'], '!status' => _booking_status_lookup($record['booking_status']))); /* db_update('booking_person') ->fields(array( 'booking_amount_paid' => $record['booking_amount_paid'], 'booking_total_pay_reqd' => $record['booking_total_pay_reqd'], 'booking_status' => _booking_status_lookup($record['booking_status']), )) ->condition('nid', $record['nid']) ->execute(); */ } //end processing input data //output our results to watchdog //watchdog('booking', '
@print_r
', array('@print_r', print_r( $result_array, TRUE))); //delete the uploaded file file_delete($file); //let the user know we finished drupal_set_message(t("Finished processing @count records from input file \"@filename\"", array('@count' => $update_counter, '@filename' => $file->filename))); /* // Make the storage of the file permanent $file->status = FILE_STATUS_PERMANENT; // Save file status. file_save($file); // Set a response to the user. drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename))); */ }