I am learning hot to use Drupal for my physics PHD. I was studying how to insert a batch into my custom module. I wrote a simple module which allows to upload a CSV through a form, check the extension and save it into a directory. This is just a try because i want to use batch API for another custom module Here is the code:
/**
* Implementation of hook_menu()
*/
function rhmtts_csvimport_menu() {
$items['rhmtts/csvimport'] = array(
'title' => 'Import CSV files',
'page callback' => 'rhmtts_csvimport_page_default',
'type' => MENU_NORMAL_ITEM,
'access callback' => TRUE,
);
return $items;
}
function rhmtts_csvimport_uploadform() {
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['csvfile'] = array(
'#type' => 'file',
'#title' => t("Upload a CSV file"),
'#size' => 40,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/////// Setup a batch //////////////
function rhmtts_batch_submit(){
$batch=array(
'title' => t('Importing CSV ...'),
'operations' => array(),
'init_message' => t('Commencing'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('An error occurred during processing'),
'finished' => 'rhmtts_csvimport_finished',
);
batch_set($batch);
$batch['operations'][] = array('rhmtts_csvimport_uploadform_submit', array());
}
////// Define finished function ////////////////////////////
function rhmtts_csvimport_finished($success, $results, $operations) {
if ($success) {
$message = t('All users have had first and last names processed.');
}
else {
$message = t('Finished with error.');
}
drupal_set_message($message);
}
///// Methods definition ////
function rhmtts_csvimport_uploadform_submit($form, &$form_state) {
// create a csv upload directory in sites/default/files if it doesn't exist
// yet
$filepath = 'public://csvuploads';
file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);
// save the uploaded file
$file = file_save_upload('csvfile', array('file_validate_extensions' => array('CSV')), $filepath, FILE_EXISTS_REPLACE);
drupal_set_message('The file you have uploaded is: <pre>'.print_r($file, TRUE).'</pre>');
// now read from it
$realpath = drupal_realpath($file->uri);
drupal_set_message("The real path of the file is $realpath");
$fh = fopen($realpath, 'r');
$table_header = NULL;
$table = array();
while(($row = fgetcsv($fh, NULL,';')) !== FALSE){
if($table_header == NULL) {
foreach($row as $field) {
$table_header[] = $field;
};
continue;
};
$table_row = array();
for($i = 0; $i < count($table_header) ; $i++) {
$table_row[$table_header[$i]] = $row[$i];
};
$table[] = $table_row;
}
fclose($fh);
drupal_set_message("You have uploaded: <pre>".print_r($table, TRUE)."</pre>");
}
When i choose a file from the form it completed the operation, but the usual page showing batch progression is not showed. Is this because the operation is to fast? Or there are other problems in the code?
Many thanks
Alberto