Part-DB.Part-DB-server/src/Controller/AdminPages/CurrencyController.php

164 lines
6.4 KiB
PHP
Raw Normal View History

2019-08-12 23:45:21 +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);
2019-08-12 23:45:21 +02:00
namespace App\Controller\AdminPages;
use App\Entity\Attachments\CurrencyAttachment;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Parameters\CurrencyParameter;
2019-08-12 23:45:21 +02:00
use App\Entity\PriceInformations\Currency;
use App\Form\AdminPages\CurrencyAdminForm;
use App\Services\Attachments\AttachmentSubmitHandler;
2022-12-18 17:28:42 +01:00
use App\Services\ImportExportSystem\EntityExporter;
use App\Services\ImportExportSystem\EntityImporter;
use App\Services\Tools\ExchangeRateUpdater;
use App\Services\LabelSystem\LabelExampleElementsGenerator;
use App\Services\LabelSystem\LabelGenerator;
use App\Services\LogSystem\EventCommentHelper;
use App\Services\LogSystem\HistoryHelper;
use App\Services\LogSystem\TimeTravel;
2022-12-18 17:28:42 +01:00
use App\Services\Trees\StructuralElementRecursionHelper;
2019-08-12 23:45:21 +02:00
use Doctrine\ORM\EntityManagerInterface;
use Exchanger\Exception\Exception;
use Exchanger\Exception\UnsupportedCurrencyPairException;
use Omines\DataTablesBundle\DataTableFactory;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface;
2020-01-05 22:49:00 +01:00
use Symfony\Component\HttpFoundation\RedirectResponse;
2019-08-12 23:45:21 +02:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
2021-10-02 20:41:14 +02:00
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
2019-08-12 23:45:21 +02:00
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
2019-08-12 23:45:21 +02:00
/**
* Class CurrencyController
*/
2023-05-28 01:21:05 +02:00
#[Route(path: '/currency')]
2019-08-12 23:45:21 +02:00
class CurrencyController extends BaseAdminController
{
protected string $entity_class = Currency::class;
protected string $twig_template = 'admin/currency_admin.html.twig';
protected string $form_class = CurrencyAdminForm::class;
protected string $route_base = 'currency';
protected string $attachment_class = CurrencyAttachment::class;
protected ?string $parameter_class = CurrencyParameter::class;
2019-08-12 23:45:21 +02:00
public function __construct(
TranslatorInterface $translator,
2021-10-02 20:41:14 +02:00
UserPasswordHasherInterface $passwordEncoder,
AttachmentSubmitHandler $attachmentSubmitHandler,
EventCommentHelper $commentHelper,
HistoryHelper $historyHelper,
TimeTravel $timeTravel,
DataTableFactory $dataTableFactory,
EventDispatcherInterface $eventDispatcher,
LabelExampleElementsGenerator $barcodeExampleGenerator,
LabelGenerator $labelGenerator,
EntityManagerInterface $entityManager,
protected ExchangeRateUpdater $exchangeRateUpdater
) {
parent::__construct(
$translator,
$passwordEncoder,
$attachmentSubmitHandler,
$commentHelper,
$historyHelper,
$timeTravel,
$dataTableFactory,
$eventDispatcher,
$barcodeExampleGenerator,
$labelGenerator,
$entityManager
);
}
2023-05-28 01:21:05 +02:00
#[Route(path: '/{id}', name: 'currency_delete', methods: ['DELETE'])]
2020-02-02 14:05:36 +01:00
public function delete(Request $request, Currency $entity, StructuralElementRecursionHelper $recursionHelper): RedirectResponse
{
return $this->_delete($request, $entity, $recursionHelper);
}
public function additionalActionEdit(FormInterface $form, AbstractNamedDBElement $entity): bool
{
if (!$entity instanceof Currency) {
return false;
}
2020-06-07 23:00:27 +02:00
//@phpstan-ignore-next-line
if ($form->get('update_exchange_rate')->isClicked()) {
$this->denyAccessUnlessGranted('edit', $entity);
try {
$this->exchangeRateUpdater->update($entity);
$this->addFlash('info', 'currency.edit.exchange_rate_updated.success');
} catch (Exception $exception) {
//$exception = $exception->getExceptions()[0];
2020-08-21 21:36:22 +02:00
if ($exception instanceof UnsupportedCurrencyPairException || false !== stripos($exception->getMessage(), 'supported')) {
$this->addFlash('error', 'currency.edit.exchange_rate_update.unsupported_currency');
} else {
$this->addFlash('error', 'currency.edit.exchange_rate_update.generic_error');
}
}
}
return true;
}
2024-03-03 19:57:31 +01:00
#[Route(path: '/{id}/edit/{timestamp}', name: 'currency_edit', requirements: ['id' => '\d+'])]
2023-05-28 01:21:05 +02:00
#[Route(path: '/{id}', requirements: ['id' => '\d+'])]
public function edit(Currency $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response
2019-08-12 23:45:21 +02:00
{
return $this->_edit($entity, $request, $em, $timestamp);
2019-08-12 23:45:21 +02:00
}
2023-05-28 01:21:05 +02:00
#[Route(path: '/new', name: 'currency_new')]
#[Route(path: '/{id}/clone', name: 'currency_clone')]
#[Route(path: '/')]
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer, ?Currency $entity = null): Response
2019-08-12 23:45:21 +02:00
{
return $this->_new($request, $em, $importer, $entity);
2019-08-12 23:45:21 +02:00
}
2023-05-28 01:21:05 +02:00
#[Route(path: '/export', name: 'currency_export_all')]
2020-02-02 14:05:36 +01:00
public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request): Response
2019-08-12 23:45:21 +02:00
{
return $this->_exportAll($em, $exporter, $request);
}
2023-05-28 01:21:05 +02:00
#[Route(path: '/{id}/export', name: 'currency_export')]
2020-02-02 14:05:36 +01:00
public function exportEntity(Currency $entity, EntityExporter $exporter, Request $request): Response
2019-08-12 23:45:21 +02:00
{
return $this->_exportEntity($entity, $exporter, $request);
}
public function deleteCheck(AbstractNamedDBElement $entity): bool
{
2020-08-21 22:43:37 +02:00
if (($entity instanceof Currency) && $entity->getPricedetails()->count() > 0) {
$this->addFlash('error', 'entity.delete.must_not_contain_prices');
2020-08-21 21:36:22 +02:00
2020-08-21 22:43:37 +02:00
return false;
}
return true;
}
}