2019-04-11 19:22:13 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
namespace App\Services\ImportExportSystem;
|
2019-04-11 19:22:13 +02:00
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
2023-03-11 22:40:53 +01:00
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use function is_array;
|
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionException;
|
2019-04-11 19:22:13 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2023-03-11 22:40:53 +01:00
|
|
|
use function Symfony\Component\String\u;
|
2019-04-11 19:22:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this class to export an entity to multiple file formats.
|
|
|
|
*/
|
|
|
|
class EntityExporter
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected SerializerInterface $serializer)
|
2019-04-11 19:22:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-11 22:40:53 +01:00
|
|
|
protected function configureOptions(OptionsResolver $resolver): void
|
|
|
|
{
|
|
|
|
$resolver->setDefault('format', 'csv');
|
|
|
|
$resolver->setAllowedValues('format', ['csv', 'json', 'xml', 'yaml']);
|
|
|
|
|
2023-03-12 00:27:04 +01:00
|
|
|
$resolver->setDefault('csv_delimiter', ';');
|
2023-03-11 22:40:53 +01:00
|
|
|
$resolver->setAllowedTypes('csv_delimiter', 'string');
|
|
|
|
|
|
|
|
$resolver->setDefault('level', 'extended');
|
|
|
|
$resolver->setAllowedValues('level', ['simple', 'extended', 'full']);
|
|
|
|
|
|
|
|
$resolver->setDefault('include_children', false);
|
|
|
|
$resolver->setAllowedTypes('include_children', 'bool');
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:22:13 +02:00
|
|
|
/**
|
2023-03-11 22:40:53 +01:00
|
|
|
* Export the given entities using the given options.
|
|
|
|
* @param AbstractNamedDBElement|AbstractNamedDBElement[] $entities The data to export
|
|
|
|
* @param array $options The options to use for exporting
|
|
|
|
* @return string The serialized data
|
|
|
|
*/
|
2023-06-11 14:15:46 +02:00
|
|
|
public function exportEntities(\App\Entity\Base\AbstractNamedDBElement|array $entities, array $options): string
|
2023-03-11 22:40:53 +01:00
|
|
|
{
|
|
|
|
if (!is_array($entities)) {
|
|
|
|
$entities = [$entities];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Ensure that all entities are of type AbstractNamedDBElement
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
if (!$entity instanceof AbstractNamedDBElement) {
|
|
|
|
throw new InvalidArgumentException('All entities must be of type AbstractNamedDBElement!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
|
|
|
//If include children is set, then we need to add the include_children group
|
|
|
|
$groups = [$options['level']];
|
|
|
|
if ($options['include_children']) {
|
|
|
|
$groups[] = 'include_children';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->serializer->serialize($entities, $options['format'],
|
|
|
|
[
|
|
|
|
'groups' => $groups,
|
|
|
|
'as_collection' => true,
|
|
|
|
'csv_delimiter' => $options['csv_delimiter'],
|
|
|
|
'xml_root_node_name' => 'PartDBExport',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports an Entity or an array of entities to multiple file formats.
|
2019-04-11 19:22:13 +02:00
|
|
|
*
|
2020-04-10 13:05:08 +02:00
|
|
|
* @param Request $request the request that should be used for option resolving
|
2023-03-11 22:40:53 +01:00
|
|
|
* @param AbstractNamedDBElement|object[] $entities
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @return Response the generated response containing the exported data
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @throws ReflectionException
|
2019-04-11 19:22:13 +02:00
|
|
|
*/
|
2023-06-11 14:15:46 +02:00
|
|
|
public function exportEntityFromRequest(\App\Entity\Base\AbstractNamedDBElement|array $entities, Request $request): Response
|
2019-04-11 19:22:13 +02:00
|
|
|
{
|
2023-03-11 22:40:53 +01:00
|
|
|
$options = [
|
|
|
|
'format' => $request->get('format') ?? 'json',
|
|
|
|
'level' => $request->get('level') ?? 'extended',
|
|
|
|
'include_children' => $request->request->getBoolean('include_children') ?? false,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!is_array($entities)) {
|
|
|
|
$entities = [$entities];
|
2019-04-11 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 22:40:53 +01:00
|
|
|
//Do the serialization with the given options
|
|
|
|
$serialized_data = $this->exportEntities($entities, $options);
|
2019-04-11 19:22:13 +02:00
|
|
|
|
2023-03-11 22:40:53 +01:00
|
|
|
$response = new Response($serialized_data);
|
2019-04-11 19:22:13 +02:00
|
|
|
|
2023-03-11 22:40:53 +01:00
|
|
|
//Resolve the format
|
|
|
|
$optionsResolver = new OptionsResolver();
|
|
|
|
$this->configureOptions($optionsResolver);
|
|
|
|
$options = $optionsResolver->resolve($options);
|
|
|
|
|
|
|
|
//Determine the content type for the response
|
2019-04-11 19:22:13 +02:00
|
|
|
|
|
|
|
//Plain text should work for all types
|
2019-11-09 00:47:20 +01:00
|
|
|
$content_type = 'text/plain';
|
2019-04-11 19:22:13 +02:00
|
|
|
|
|
|
|
//Try to use better content types based on the format
|
2023-03-11 22:40:53 +01:00
|
|
|
$format = $options['format'];
|
2019-04-11 19:22:13 +02:00
|
|
|
switch ($format) {
|
|
|
|
case 'xml':
|
2019-11-09 00:47:20 +01:00
|
|
|
$content_type = 'application/xml';
|
2019-04-11 19:22:13 +02:00
|
|
|
break;
|
|
|
|
case 'json':
|
2019-11-09 00:47:20 +01:00
|
|
|
$content_type = 'application/json';
|
2019-04-11 19:22:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$response->headers->set('Content-Type', $content_type);
|
|
|
|
|
|
|
|
//If view option is not specified, then download the file.
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$request->get('view')) {
|
2023-03-11 22:40:53 +01:00
|
|
|
|
|
|
|
//Determine the filename
|
|
|
|
//When we only have one entity, then we can use the name of the entity
|
|
|
|
if (count($entities) === 1) {
|
|
|
|
$entity_name = $entities[0]->getName();
|
2019-04-11 19:22:13 +02:00
|
|
|
} else {
|
2023-03-11 22:40:53 +01:00
|
|
|
//Use the class name of the first element for the filename otherwise
|
|
|
|
$reflection = new ReflectionClass($entities[0]);
|
|
|
|
$entity_name = $reflection->getShortName();
|
2019-04-11 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
2023-03-11 22:40:53 +01:00
|
|
|
$level = $options['level'];
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$filename = 'export_'.$entity_name.'_'.$level.'.'.$format;
|
2019-04-11 19:22:13 +02:00
|
|
|
|
|
|
|
// Create the disposition of the file
|
|
|
|
$disposition = $response->headers->makeDisposition(
|
|
|
|
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
|
2020-02-23 00:44:52 +01:00
|
|
|
$filename,
|
2023-03-11 22:40:53 +01:00
|
|
|
u($filename)->ascii()->toString(),
|
2019-04-11 19:22:13 +02:00
|
|
|
);
|
|
|
|
// Set the content disposition
|
|
|
|
$response->headers->set('Content-Disposition', $disposition);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|