Posted by digital006 on March 16, 2010 at 10:49pm
Hi,
I'm using the batch api to process users from a csv file. Each line of the csv should be parsed separately but at the moment the batch is running once for the whole file rather than say 14 times for the number of rows in the csv file. I'd like each row parsed separately because there will be thousands once the module is complete.
The code below is the module, I'd be grateful if anyone could point me in the right direction.
<?php
/*
@todo
When search form is submitted to itsefl, detect the submission form values and execute the SQL search.
present the results using theme('table') and append it to the form.
*/
/**
* Display help and module information
*
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function test_crmimport_help($path, $arg)
{
$output = ''; //declare your output variable
switch ($path) {
case "admin/help#test_crmimport":
$output = '<p>'. t("Imports CRM data") .'</p>';
break;
}
return $output;
}
/**
* Valid permissions for this module
*
* @return array An array of valid permissions for the onthisdate module
*/
function test_crmimport_perm()
{
return array('import data');
}
/**
* Implementation of hook_menu
*/
function test_crmimport_menu()
{
$items = array();
$items['admin/settings/crmimport'] = array(
'title' => 'CRM Import',
'description' => 'Import CRM data',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_crmimport_admin_settings'),
'access arguments' => array('import data'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Administration page
*/
function test_crmimport_admin_settings()
{
$form = array();
$form['testfile'] = array(
'#type' => 'file',
'#title' => 'CRM Data File',
);
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit') );
return $form;
}
/*function test_gtassearch_settings_form_validate($form_id, $form_values) {
//Needs some form of validation here
}*/
function test_crmimport_admin_settings_submit($form, &$form_state)
{
$dir = file_directory_path() . '/dataimport';
if (file_check_directory($dir)) {
$save = file_save_upload('testfile', array(), $dir, FILE_EXISTS_REPLACE);
if (!$save) {
drupal_set_message('Failed upload to ' . $dir);
} else {
drupal_set_message('Upload complete'); // we know this works now
$test_filename = $save->filename;
test_crmimport_callback_csv_import($test_filename); // Call batch csv function
}
variable_set('gtas_import_file', $dir . '/' . $file);
} else {
drupal_set_message(t('FAIL'));
}
}
function test_crmimport_callback_csv_import($test_filename)
{
// define path to CSV file
$csv_file_path = file_directory_path() . '/dataimport/'.$test_filename;
// define a redirect path upon batch completion
$redirect_path = 'admin/settings/crmimport';
// define batch array structure
// NOTE: minimal parameters defined to simplify code
$batch = array(
'title' => t('Reading File'),
'progress_message' => t('Processed @current out of @total'),
'operations' => array(
array(
'test_crmimport_batch_read', array($csv_file_path),
),
),
);
// set batch
batch_set($batch);
// process batch
batch_process($redirect_path);
}
function test_crmimport_batch_read($csv_file_path, &$context)
{
// define batch limit
$batch_limit = 1;
$row = 0;
static $final_row = 0;
// assume the batch process has not completed
$context['finished'] = 0;
// open the file for reading
$file_handle = fopen($csv_file_path, 'r');
// check if file pointer position exists in the sandbox, and jump to location in file
if ($context['sandbox']['file_pointer_position']) {
fseek($file_handle, $context['sandbox']['file_pointer_position']);
}
// loop through the file and stop at batch limit
for ($i = 0; $i < $batch_limit; $i++) {
// get file line as csv
$csv_line = fgetcsv($file_handle);
// NOTE: at this point, do what ever you'd like with the CSV array data!
if (is_array($csv_line)) {
if ($csv_line[0] != '' && $csv_line[0] != 'ContactID') {
// SOME STUFF HERE THATS NOT IMPORTANT ;)
$final_row++;
}
}
// retain current file pointer position
$context['sandbox']['file_pointer_position'] = ftell($file_handle);
// check for EOF
if (feof($file_handle)) {
// complete the batch process
$context['finished'] = 1;
drupal_set_message('Processed ' . $final_row . ' items');
// end loop
break;
}
}
}
?>