You are here

function bibcite_export_batch_finished in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_export/bibcite_export.batch.inc \bibcite_export_batch_finished()

Batch export finished callback.

Parameters

bool $success: A boolean indicating whether the batch has completed successfully.

array $results: The value set in $context['results']. Must be an array and contain next elements: $results['file'] - URI for temporary file contains export result. $results['format'] - Instance of format plugin. $results['count'] - Count of exported entries. Optionally you can set $results['save_storage'] to TRUE and file info will be saved to private storage.

mixed $operations: If $success is FALSE, contains the operations that remained unprocessed.

2 string references to 'bibcite_export_batch_finished'
ExportAllForm::submitForm in modules/bibcite_export/src/Form/ExportAllForm.php
Form submission handler.
ExportMultipleForm::submitForm in modules/bibcite_export/src/Form/ExportMultipleForm.php
Form submission handler.

File

modules/bibcite_export/bibcite_export.batch.inc, line 124
Batch callbacks.

Code

function bibcite_export_batch_finished($success, array $results, $operations) {
  $messenger = \Drupal::messenger();
  if ($success && isset($results['file'], $results['format'], $results['count'])) {

    /** @var \Drupal\bibcite\Plugin\BibciteFormatInterface $format */
    $format = $results['format'];

    /** @var \Drupal\Core\TempStore\PrivateTempStore $tempstorage */
    $tempstorage = \Drupal::service('tempstore.private')
      ->get('bibcite_export');
    $current_user = \Drupal::currentUser();
    $file_schema = 'temporary://';
    $file_name = sprintf('bibcite-export-%s-%s.%s', date('mdYHis'), $current_user
      ->id(), $format
      ->getExtension());
    $file = File::create([
      'uid' => $current_user
        ->id(),
      'filename' => $file_name,
      'uri' => $results['file'],
      'status' => 0,
    ]);
    if ($file
      ->save() && ($file = file_move($file, $file_schema . $file_name))) {
      if (!empty($results['save_storage'])) {
        $file_data = [
          'id' => $file
            ->id(),
          'timestamp' => time(),
          'format' => $format
            ->getPluginId(),
        ];
        $data = $tempstorage
          ->get('export_files');
        $data = !empty($data) ? $data : [];
        array_unshift($data, $file_data);
        $tempstorage
          ->set('export_files', $data);
      }
      $message = t('Reference export is complete (@count entries has been exported). <a href="@download_url">Download</a>.', [
        '@count' => $results['count'],
        '@download_url' => Url::fromRoute('bibcite_export.download', [
          'file' => $file
            ->id(),
        ])
          ->toString(),
      ]);
      $messenger
        ->addStatus($message);
    }
    else {
      $messenger
        ->addError(t('Temporary file can not be saved'));
    }
  }
  else {
    $messenger
      ->addError(t('An error occurred while processing export'));
  }
}