View source
<?php
namespace Drupal\contact_storage_export;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Drupal\contact\Entity\Message;
use Drupal\file\Entity\File;
class ContactStorageExportBatches {
protected static $tempFile = NULL;
public static function processBatch(array $settings, array &$context) {
if (empty($context['sandbox'])) {
$context['results']['data'] = [];
$context['results']['settings'] = $settings;
$last_id = 0;
if ($settings['since_last_export']) {
$last_id = ContactStorageExport::getLastExportId($settings['contact_form']);
}
$file = self::getTempFile();
$context['results']['fid'] = $file
->id();
$context['sandbox']['settings'] = $settings;
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = self::getMax($settings, $last_id);
}
else {
$settings = $context['sandbox']['settings'];
}
if ($context['sandbox']['max'] == 0) {
$context['finished'] = 1;
}
else {
self::$tempFile = File::load($context['results']['fid']);
self::getContactFormData($settings, $context);
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
}
private static function getContactFormData(array $settings, array &$context) {
$last_id = 0;
if ($settings['since_last_export']) {
$last_id = ContactStorageExport::getLastExportId($settings['contact_form']);
}
$limit = 25;
$query = \Drupal::entityQuery('contact_message');
$query
->condition('contact_form', $settings['contact_form']);
$query
->condition('id', $last_id, '>');
$query
->range($context['sandbox']['progress'], $limit);
$query
->sort('id', 'ASC');
if ($message_ids = $query
->execute()) {
if ($messages = Message::loadMultiple($message_ids)) {
self::prepareMessages($messages, $settings, $context);
}
}
}
private static function getMax(array $settings, $last_id) {
$query = \Drupal::entityQuery('contact_message');
$query
->condition('contact_form', $settings['contact_form']);
$query
->condition('id', $last_id, '>');
$query
->count();
$result = $query
->execute();
return $result ? $result : 0;
}
private static function prepareMessages(array $messages, array $settings, array &$context) {
$exporter = \Drupal::service('contact_storage_export.exporter');
$message = reset($messages);
$settings['labels'] = $exporter
->getLabels($message);
$csv_data = [];
foreach ($messages as $contact_message) {
$id = $contact_message
->id();
$serialized_message = $exporter
->serialize($contact_message, $settings);
$csv_data[] = $serialized_message;
$context['results']['current_id'] = $id;
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $id;
$context['message'] = t('Processed up to Contact Message ID @id. Your file will download immediately when complete.', [
'@id' => $id,
]);
}
$csv_string = $exporter
->encodeData($csv_data);
self::writeToTempFile($csv_string);
}
public static function finishBatch($success, array $results, array $operations) {
if ($success) {
if ($results['settings']['since_last_export']) {
ContactStorageExport::setLastExportId($results['settings']['contact_form'], $results['current_id']);
}
$key = ContactStorageExportTempstore::setTempstore($results['fid'], $results['settings']['filename']);
$route = 'contact_storage_export.contact_storage_download_form';
$args = [
'contact_form' => $results['settings']['contact_form'],
'key' => $key,
];
$url = Url::fromRoute($route, $args);
$url_string = $url
->toString();
$response = new RedirectResponse($url_string);
return $response;
}
else {
$message = t('There was no data to export.');
$messenger = \Drupal::messenger();
$messenger
->addWarning($message);
}
$route = 'entity.contact_form.export_form';
$args = [
'contact_form' => $results['settings']['contact_form'],
];
$url = Url::fromRoute($route, $args);
$url_string = $url
->toString();
$response = new RedirectResponse($url_string);
return $response;
}
protected static function getTempFile() {
if (!self::$tempFile) {
self::$tempFile = self::createTempFile();
}
return self::$tempFile;
}
protected static function createTempFile() {
$dir = 'temporary://contact_storage_export';
$file_system = \Drupal::service('file_system');
$temp_nam = $file_system
->tempnam($dir, 'contact_storage_export');
$file = File::create([
'filename' => 'contact-storage-export.csv',
'uri' => $temp_nam,
]);
$file
->setTemporary();
$file
->save();
return $file;
}
protected static function writeToTempFile($data) {
$file = self::getTempFile();
if (file_put_contents($file
->getFileUri(), $data, FILE_APPEND) === FALSE) {
$url = Url::fromRoute('entity.contact_form.export_form', []);
$url_string = $url
->toString();
$response = new RedirectResponse($url_string);
$response
->send();
$message = t('The export was unsuccessful for an unknown reason. Please check your error logs.');
$messenger = \Drupal::messenger();
$messenger
->addWarning($message);
}
$file
->setSize($file
->getFileUri());
$file
->save();
}
}