You are here

public static function PriceListItemExportForm::batchProcess in Commerce Pricelist 8.2

Batch process to export price list items to CSV.

Parameters

string $file_uri: The CSV file URI.

array $mapping: The mapping options.

array $csv_options: The CSV options.

string $price_list_id: The price list ID.

array $context: The batch context.

File

src/Form/PriceListItemExportForm.php, line 213

Class

PriceListItemExportForm

Namespace

Drupal\commerce_pricelist\Form

Code

public static function batchProcess($file_uri, array $mapping, array $csv_options, $price_list_id, array &$context) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $price_list_storage = $entity_type_manager
    ->getStorage('commerce_pricelist');
  $price_list_item_storage = $entity_type_manager
    ->getStorage('commerce_pricelist_item');

  /** @var \Drupal\commerce_pricelist\Entity\PriceList $price_list */
  $price_list = $price_list_storage
    ->load($price_list_id);

  /** @var \Drupal\Core\StreamWrapper\StreamWrapperInterface $stream_wrapper */
  $stream_wrapper = \Drupal::service('stream_wrapper.temporary');
  $stream_wrapper
    ->setUri($file_uri);
  try {
    $csv = new \SplFileObject($stream_wrapper
      ->realpath(), 'a+');
    $csv
      ->setCsvControl($csv_options['delimiter'], $csv_options['enclosure']);
  } catch (\Exception $e) {
    $context['results']['error_message'] = t('Cannot open the CSV file @filename for writing, aborting.', [
      '@filename' => $stream_wrapper
        ->realpath(),
    ]);
    $context['sandbox']['finished'] = 1;
    return;
  }
  if (empty($context['sandbox'])) {
    $price_list_item_count = $price_list_item_storage
      ->getQuery()
      ->condition('type', $price_list
      ->bundle())
      ->condition('price_list_id', $price_list
      ->id())
      ->count()
      ->execute();
    $context['sandbox']['header_mapping'] = static::buildHeader($mapping);

    // Append the configured headers to the CSV file.
    $csv
      ->fputcsv($context['sandbox']['header_mapping']);
    $context['sandbox']['export_total'] = (int) $price_list_item_count;
    $context['results']['external_url'] = $stream_wrapper
      ->getExternalUrl();
    $context['results']['export_count'] = 0;
  }
  $export_total = $context['sandbox']['export_total'];
  $export_count =& $context['results']['export_count'];
  $remaining = $export_total - $export_count;
  $limit = $remaining < self::BATCH_SIZE ? $remaining : self::BATCH_SIZE;
  $price_list_item_ids = $price_list_item_storage
    ->getQuery()
    ->condition('type', $price_list
    ->bundle())
    ->condition('price_list_id', $price_list
    ->id())
    ->range($export_count, $limit)
    ->execute();
  if (!$price_list_item_ids) {
    $context['finished'] = 1;
    return;
  }
  $price_list_items = $price_list_item_storage
    ->loadMultiple($price_list_item_ids);

  /** @var \Drupal\commerce_pricelist\Entity\PriceListItemInterface $price_list_item */
  foreach ($price_list_items as $price_list_item) {
    $row = static::buildRow($price_list_item, $context['sandbox']['header_mapping']);
    $csv
      ->fputcsv($row);
    $export_count++;
  }
  $context['message'] = t('Exporting @exported of @export_total price list items', [
    '@exported' => $export_count,
    '@export_total' => $export_total,
  ]);
  $context['finished'] = $export_count / $export_total;
}