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).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
|
2019-04-11 19:22:13 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-04-11 19:22:13 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-04-11 19:22:13 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
2020-01-05 22:49:00 +01:00
|
|
|
use function in_array;
|
|
|
|
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;
|
2020-03-15 13:56:31 +01:00
|
|
|
use Symfony\Component\Serializer\Encoder\CsvEncoder;
|
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
|
|
|
use Symfony\Component\Serializer\Encoder\YamlEncoder;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
|
|
use Symfony\Component\Serializer\Serializer;
|
2019-04-11 19:22:13 +02:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this class to export an entity to multiple file formats.
|
|
|
|
*/
|
|
|
|
class EntityExporter
|
|
|
|
{
|
|
|
|
protected $serializer;
|
|
|
|
|
|
|
|
public function __construct(SerializerInterface $serializer)
|
|
|
|
{
|
2020-02-23 19:45:06 +01:00
|
|
|
/*$encoders = [new XmlEncoder(), new JsonEncoder(), new CSVEncoder(), new YamlEncoder()];
|
|
|
|
$normalizers = [new ObjectNormalizer(), new DateTimeNormalizer()];
|
|
|
|
$this->serializer = new Serializer($normalizers, $encoders);
|
|
|
|
$this->serializer-> */
|
2019-04-11 19:22:13 +02:00
|
|
|
$this->serializer = $serializer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-29 23:13:25 +02:00
|
|
|
* 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
|
2020-03-29 23:13:25 +02:00
|
|
|
* @param AbstractNamedDBElement|object[] $entity
|
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
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function exportEntityFromRequest($entity, Request $request): Response
|
2019-04-11 19:22:13 +02:00
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
$format = $request->get('format') ?? 'json';
|
2019-04-11 19:22:13 +02:00
|
|
|
|
|
|
|
//Check if we have one of the supported formats
|
2020-01-05 22:49:00 +01:00
|
|
|
if (! in_array($format, ['json', 'csv', 'yaml', 'xml'], true)) {
|
|
|
|
throw new InvalidArgumentException('Given format is not supported!');
|
2019-04-11 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check export verbosity level
|
|
|
|
$level = $request->get('level') ?? 'extended';
|
2020-01-05 22:49:00 +01:00
|
|
|
if (! in_array($level, ['simple', 'extended', 'full'], true)) {
|
|
|
|
throw new InvalidArgumentException('Given level is not supported!');
|
2019-04-11 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check for include children option
|
|
|
|
$include_children = $request->get('include_children') ?? false;
|
|
|
|
|
|
|
|
//Check which groups we need to export, based on level and include_children
|
2019-11-09 00:47:20 +01:00
|
|
|
$groups = [$level];
|
2019-04-11 19:22:13 +02:00
|
|
|
if ($include_children) {
|
|
|
|
$groups[] = 'include_children';
|
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
|
|
|
switch ($format) {
|
|
|
|
case 'xml':
|
2019-11-09 00:47:20 +01:00
|
|
|
$content_type = 'application/xml';
|
2020-01-05 15:46:58 +01:00
|
|
|
|
2019-04-11 19:22:13 +02:00
|
|
|
break;
|
|
|
|
case 'json':
|
2019-11-09 00:47:20 +01:00
|
|
|
$content_type = 'application/json';
|
2020-01-05 15:46:58 +01:00
|
|
|
|
2019-04-11 19:22:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
//Ensure that we always serialize an array. This makes it easier to import the data again.
|
2020-01-05 22:49:00 +01:00
|
|
|
if (is_array($entity)) {
|
2019-04-11 22:41:21 +02:00
|
|
|
$entity_array = $entity;
|
|
|
|
} else {
|
|
|
|
$entity_array = [$entity];
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:45:06 +01:00
|
|
|
$serialized_data = $this->serializer->serialize($entity_array, $format,
|
|
|
|
[
|
|
|
|
'groups' => $groups,
|
|
|
|
'as_collection' => true,
|
|
|
|
'csv_delimiter' => ';', //Better for Excel
|
|
|
|
'xml_root_node_name' => 'PartDBExport',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = new Response($serialized_data);
|
2019-04-11 19:22:13 +02:00
|
|
|
|
|
|
|
$response->headers->set('Content-Type', $content_type);
|
|
|
|
|
|
|
|
//If view option is not specified, then download the file.
|
2020-01-05 15:46:58 +01:00
|
|
|
if (! $request->get('view')) {
|
2020-02-01 19:48:07 +01:00
|
|
|
if ($entity instanceof AbstractNamedDBElement) {
|
2019-04-11 19:22:13 +02:00
|
|
|
$entity_name = $entity->getName();
|
2020-01-05 22:49:00 +01:00
|
|
|
} elseif (is_array($entity)) {
|
2019-04-11 19:22:13 +02:00
|
|
|
if (empty($entity)) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$entity must not be empty!');
|
2019-04-11 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Use the class name of the first element for the filename
|
2020-01-05 22:49:00 +01:00
|
|
|
$reflection = new ReflectionClass($entity[0]);
|
2019-04-11 19:22:13 +02:00
|
|
|
$entity_name = $reflection->getShortName();
|
|
|
|
} else {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$entity type is not supported!');
|
2019-04-11 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
$string = preg_replace('![^'.preg_quote('-').'a-z0-_9\s]+!', '', strtolower($filename))
|
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
|
|
|
}
|