Skip to content

Commit

Permalink
Move batch code to dedicated *.inc file
Browse files Browse the repository at this point in the history
(As seen in devel_generate.batch.inc and locale.batch.inc)
  • Loading branch information
leymannx committed Apr 11, 2017
1 parent cb637b6 commit 4e71489
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions csvimport.module → csvimport.batch.inc
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
<?php

/**
* Demonstration module.
*
* - Provide form for upload of a CSV file.
* - Trigger a batch task which iterates through each row in the file.
*/

use \Drupal\node\Entity\Node;

/**
* Handle batch completion.
*
* Creates a new CSV file containing all failed rows if any.
*/
function csvimport_import_finished($success, $results, $operations) {

if (!empty($results['failed_rows'])) {

$dir = 'public://csvimport';
if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {

$csv_filename = 'failed_rows-' . basename($results['uploaded_filename']); // we validated extension on upload
$csv_filepath = $dir . '/' . $csv_filename;
$targs = [

$targs = [
':csv_url' => file_create_url($csv_filepath),
'@csv_filename' => $csv_filename,
'@csv_filepath' => $csv_filepath,
];

if ($handle = fopen($csv_filepath, 'w+')) {

foreach ($results['failed_rows'] as $failed_row) {
fputcsv($handle, $failed_row);
}

fclose($handle);
drupal_set_message(t('Some rows failed to import. You may download a CSV of these rows: <a href=":csv_url">@csv_filename</a>', $targs), 'error');
}
Expand All @@ -37,23 +40,26 @@ function csvimport_import_finished($success, $results, $operations) {
drupal_set_message(t('Some rows failed to import, but unable to create directory for error CSV at @csv_directory', $targs), 'error');
}
}

return t('The CSV import has completed.');
}

/**
* Remember the uploaded CSV filename
* Remember the uploaded CSV filename.
*
* @TODO is there a better way to pass a value from inception of the
* batch to the finished function?
* @TODO Is there a better way to pass a value from inception of the batch to
* the finished function?
*/
function _csvimport_remember_filename($filename, &$context) {

$context['results']['uploaded_filename'] = $filename;
}

/**
* Process a single line.
*/
function _csvimport_import_line($line, &$context) {

$context['results']['rows_imported']++;
$line = $cleaned_line = array_map('base64_decode', $line);

Expand All @@ -77,6 +83,20 @@ function _csvimport_import_line($line, &$context) {
*/
usleep(2500);

/**
* Convert the line of the CSV file into a new node.
*
*/
// if ($context['results']['rows_imported'] > 1) { // Skip header line.
// $node = Node::create([
// 'type' => 'article',
// 'title' => $line[2],
// 'body' => $line[0],
// ]);
//
// $node->save();
// }

/**
* If the first two columns in the row are "ROW", "FAILS" then we
* will add that row to the CSV we'll return to the importing person
Expand All @@ -85,4 +105,5 @@ function _csvimport_import_line($line, &$context) {
if ($line[1] == 'ROW' && $line[2] == 'FAILS') {
$context['results']['failed_rows'][] = $line;
}

}

0 comments on commit 4e71489

Please sign in to comment.