Files
booking/booking.import_data.inc
2014-02-24 23:53:12 +11:00

173 lines
5.5 KiB
PHP

<?php
// $Id: booking.import_data.inc,v 0.1
/**
* @file
* Provide functionality to upload csv for bulk update of payment data
*/
function booking_import_data_admin()
{
global $event;
$prefix = "<p>Upload csv file containing data to import. Minimum fields present should be <strong>nid,booking_status,booking_total_pay_reqd,booking_amount_paid</strong> with column names matching that exactly (case sensitive).</p>";
$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();
//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;
}
//process the input data
foreach ($array as $record)
{
//watchdog('booking', 'Processing user record: @info', array ('@info' => $record['nid']));
$update_counter++;
//do some error checking
foreach($expected_fields 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)));
*/
if ( (! 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;
}
}
$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', '<pre>@print_r</pre>', 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)));
*/
}