Created
October 25, 2010 19:46
-
-
Save semperos/645593 to your computer and use it in GitHub Desktop.
Example of Drupal Batch API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Import submit function | |
*/ | |
function ret_user_import_upload_form_submit($form, &$form_state) { | |
if ($file = file_save_upload($source = 'ret_user_import_csv_file', $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_REPLACE)) { | |
$offset = 0; | |
$headers = _ret_user_import_get_row($file->filepath, $offset); | |
if ($headers === FALSE) { | |
// there needs to be consistency, so enforce the existence of a 'header' row | |
return; | |
} | |
$srcinfo = new stdClass; | |
$srcinfo->filepath = $file->filepath; | |
$srcinfo->offset = $offset; | |
$batch = array( | |
'title' => t('Importing user records from %filename', | |
array('%filename' => $file->filename)), | |
'init_message' => t('User import initializing'), | |
'error_message' => t('Import failed'), | |
'progress_message' => '', | |
'operations' => array( | |
array('_ret_user_import_import_csv', array($srcinfo)), | |
), | |
'finished' => '_ret_user_import_import_finish', | |
); | |
batch_set($batch); | |
$redirect_path = 'admin/ret/import/user/overview'; | |
batch_process($redirect_path); | |
} | |
else { | |
drupal_set_message(t('Everything failed; try again.'), 'error', FALSE); | |
$form_state['redirect'] = 'admin/ret/import/user/overview'; | |
} | |
} | |
function _ret_user_import_import_csv($srcinfo, &$context) { | |
if (!isset($context['sandbox']['max'])) { | |
$context['sandbox']['max'] = (count(file($srcinfo->filepath)) - 1); // -1 because we read the first line as headers | |
$context['sandbox']['done'] = $srcinfo->offset; | |
$context['sandbox']['rows'] = 0; | |
} | |
// Return for feedback every X seconds | |
$timelimit = time() + 10; | |
while ($values = _ret_user_import_get_row($srcinfo->filepath, $context['sandbox']['done'])) { | |
// do your work | |
$context['sandbox']['rows']++; | |
if (time() > $timelimit) { | |
break; | |
} | |
} | |
// provide a meaningful status message during the batch process | |
$context['message'] = t('%rows of %total records imported.', | |
array('%rows' => $context['sandbox']['rows'], | |
'%total' => $context['sandbox']['max'])); | |
// if not finished, give the Batch API a percentage of 'finished' to use for the progress bar | |
if ($context['sandbox']['rows'] != $context['sandbox']['max']) { | |
$context['finished'] = $context['sandbox']['rows'] / $context['sandbox']['max']; | |
} | |
else { | |
$context['finished'] = 1; | |
} | |
// if finished, wrap up | |
// the _finish function below will be called after this | |
if ($context['finished'] == 1) { | |
$context['results']['srcinfo'] = $srcinfo; | |
$context['message'] .= '<br/>' . t('Importation Complete.'); | |
} | |
} | |
/** | |
* Batch _finish function | |
* | |
* This function uses the success, results and operations parts of the $context variable, | |
* executing after the batch has completed successfully. | |
*/ | |
function _ret_user_import_import_finish($success, $results, $operations) { | |
if ($success) { | |
$srcinfo = $results['srcinfo']; | |
unlink($srcinfo->filepath); | |
drupal_set_message($srcinfo->success_msg); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment