2023-03-12 00:27:04 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2023-03-12 19:53:55 +01:00
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Form\AdminPages\ImportType;
|
2023-03-12 00:27:04 +01:00
|
|
|
use App\Services\ImportExportSystem\EntityExporter;
|
2023-03-12 19:53:55 +01:00
|
|
|
use App\Services\ImportExportSystem\EntityImporter;
|
|
|
|
use App\Services\LogSystem\EventCommentHelper;
|
2023-03-12 00:27:04 +01:00
|
|
|
use App\Services\Parts\PartsTableActionHandler;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2023-03-12 19:53:55 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2023-03-12 00:27:04 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2023-03-15 21:33:18 +01:00
|
|
|
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
2023-03-12 21:43:40 +01:00
|
|
|
use UnexpectedValueException;
|
2023-03-12 00:27:04 +01:00
|
|
|
|
|
|
|
class PartImportExportController extends AbstractController
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(private readonly PartsTableActionHandler $partsTableActionHandler, private readonly EntityImporter $entityImporter, private readonly EventCommentHelper $commentHelper)
|
2023-03-12 00:27:04 +01:00
|
|
|
{
|
2023-03-12 19:53:55 +01:00
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/parts/import', name: 'parts_import')]
|
2023-03-12 19:53:55 +01:00
|
|
|
public function importParts(Request $request): Response
|
|
|
|
{
|
2023-03-13 22:02:55 +01:00
|
|
|
$this->denyAccessUnlessGranted('@parts.import');
|
|
|
|
|
2023-03-12 19:53:55 +01:00
|
|
|
$import_form = $this->createForm(ImportType::class, ['entity_class' => Part::class]);
|
|
|
|
$import_form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($import_form->isSubmitted() && $import_form->isValid()) {
|
|
|
|
/** @var UploadedFile $file */
|
|
|
|
$file = $import_form['file']->getData();
|
|
|
|
$data = $import_form->getData();
|
|
|
|
|
2023-03-12 21:43:40 +01:00
|
|
|
if ($data['format'] === 'auto') {
|
|
|
|
$format = $this->entityImporter->determineFormat($file->getClientOriginalExtension());
|
|
|
|
if (null === $format) {
|
|
|
|
$this->addFlash('error', 'parts.import.flash.error.unknown_format');
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$format = $data['format'];
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:53:55 +01:00
|
|
|
$options = [
|
2023-03-12 22:03:02 +01:00
|
|
|
'create_unknown_datastructures' => $data['create_unknown_datastructures'],
|
|
|
|
'path_delimiter' => $data['path_delimiter'],
|
2023-03-12 21:43:40 +01:00
|
|
|
'format' => $format,
|
2023-03-12 19:53:55 +01:00
|
|
|
'part_category' => $data['part_category'],
|
|
|
|
'class' => Part::class,
|
|
|
|
'csv_delimiter' => $data['csv_delimiter'],
|
2023-03-15 21:46:14 +01:00
|
|
|
'part_needs_review' => $data['part_needs_review'],
|
|
|
|
'abort_on_validation_error' => $data['abort_on_validation_error'],
|
2023-03-12 19:53:55 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->commentHelper->setMessage('Import '.$file->getClientOriginalName());
|
|
|
|
|
|
|
|
$entities = [];
|
|
|
|
|
2023-03-12 21:43:40 +01:00
|
|
|
try {
|
|
|
|
$errors = $this->entityImporter->importFileAndPersistToDB($file, $options, $entities);
|
|
|
|
} catch (UnexpectedValueException $e) {
|
|
|
|
$this->addFlash('error', 'parts.import.flash.error.invalid_file');
|
2023-03-15 21:33:18 +01:00
|
|
|
if ($e instanceof NotNormalizableValueException) {
|
|
|
|
$this->addFlash('error', $e->getMessage());
|
|
|
|
}
|
2023-03-12 21:43:40 +01:00
|
|
|
goto ret;
|
|
|
|
}
|
2023-03-12 19:53:55 +01:00
|
|
|
|
2023-03-15 21:05:30 +01:00
|
|
|
if (!isset($errors) || $errors) {
|
2023-03-12 19:53:55 +01:00
|
|
|
$this->addFlash('error', 'parts.import.flash.error');
|
|
|
|
} else {
|
|
|
|
$this->addFlash('success', 'parts.import.flash.success');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 21:43:40 +01:00
|
|
|
|
|
|
|
ret:
|
2023-05-28 01:21:05 +02:00
|
|
|
return $this->render('parts/import/parts_import.html.twig', [
|
2023-03-12 19:53:55 +01:00
|
|
|
'import_form' => $import_form,
|
|
|
|
'imported_entities' => $entities ?? [],
|
|
|
|
'import_errors' => $errors ?? [],
|
|
|
|
]);
|
2023-03-12 00:27:04 +01:00
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/parts/export', name: 'parts_export', methods: ['GET'])]
|
2023-03-12 00:27:04 +01:00
|
|
|
public function exportParts(Request $request, EntityExporter $entityExporter): Response
|
|
|
|
{
|
|
|
|
$ids = $request->query->get('ids', '');
|
|
|
|
$parts = $this->partsTableActionHandler->idStringToArray($ids);
|
|
|
|
|
2023-06-11 14:15:46 +02:00
|
|
|
if ($parts === []) {
|
2023-03-12 00:27:04 +01:00
|
|
|
throw new \RuntimeException('No parts found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
//Ensure that we have access to the parts
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$this->denyAccessUnlessGranted('read', $part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entityExporter->exportEntityFromRequest($parts, $request);
|
|
|
|
}
|
|
|
|
}
|