View source
<?php
namespace Drupal\social_user_export;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Url;
use Drupal\csv_serialization\Encoder\CsvEncoder;
use Drupal\user\UserInterface;
use League\Csv\Writer;
use Drupal\Core\Link;
class ExportUser extends ContentEntityBase {
public static function exportUserOperation(UserInterface $entity, array &$context) {
$type = \Drupal::service('plugin.manager.user_export_plugin');
$plugin_definitions = $type
->getDefinitions();
if (empty($context['results']['headers'])) {
$headers = [];
foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
$instance = $type
->createInstance($plugin_id);
$headers[] = $instance
->getHeader();
}
$context['results']['headers'] = $headers;
}
if (empty($context['results']['file_path'])) {
$context['results']['file_path'] = self::getFileTemporaryPath();
$csv = Writer::createFromPath($context['results']['file_path'], 'w');
$csv
->setDelimiter(',');
$csv
->setEnclosure('"');
$csv
->setEscape('\\');
$csv
->insertOne($context['results']['headers']);
}
else {
$csv = Writer::createFromPath($context['results']['file_path'], 'a');
}
$csv
->addFormatter([
new CsvEncoder(),
'formatRow',
]);
$row = [];
foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
$instance = $type
->createInstance($plugin_id);
$row[] = $instance
->getValue($entity);
}
$csv
->insertOne($row);
$context['message'] = t('Exporting: @name', [
'@name' => $entity
->getAccountName(),
]);
}
public static function exportUsersAllOperation(array $conditions, array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$view = _social_user_export_get_view($conditions);
$context['sandbox']['max'] = $view->total_rows;
}
$view = _social_user_export_get_view($conditions, FALSE);
$view
->initQuery();
$view->query->orderby = [
[
'field' => 'users_field_data.uid',
'direction' => 'ASC',
],
];
$view
->setOffset($context['sandbox']['progress']);
$view
->setItemsPerPage(1);
$view
->preExecute();
$view
->execute();
if (empty($view->result[0])) {
$context['finished'] = 1;
return;
}
$account = $view->result[0]->_entity;
if ($account) {
self::exportUserOperation($account, $context);
}
$context['sandbox']['progress']++;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public static function finishedCallback($success, array $results, array $operations) {
if ($success && !empty($results['file_path'])) {
$data = @file_get_contents($results['file_path']);
$name = basename($results['file_path']);
$path = 'private://csv';
if (file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS) && file_save_data($data, $path . '/' . $name)) {
$url = Url::fromUri(file_create_url($path . '/' . $name));
$link = Link::fromTextAndUrl(t('Download file'), $url);
drupal_set_message(t('Export is complete. @link', [
'@link' => $link
->toString(),
]));
}
else {
drupal_set_message(t('When saving the file an error occurred'), 'error');
}
}
else {
drupal_set_message(t('An error occurred', 'error'));
}
}
public static function getFileTemporaryPath() {
$hash = md5(microtime(TRUE));
$filename = 'export-users-' . substr($hash, 20, 12) . '.csv';
$file_path = file_directory_temp() . '/' . $filename;
return $file_path;
}
}