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

201 lines
7.3 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).
*
* 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-08-12 23:45:21 +02:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
2019-08-12 23:45:21 +02:00
*
2019-11-01 13:40:30 +01:00
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
2019-08-12 23:45:21 +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\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;
2019-08-12 23:45:21 +02:00
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\ExchangeRateUpdater;
use App\Services\LabelSystem\Barcodes\BarcodeExampleElementsGenerator;
use App\Services\LabelSystem\LabelGenerator;
use App\Services\LogSystem\EventCommentHelper;
use App\Services\LogSystem\HistoryHelper;
use App\Services\LogSystem\TimeTravel;
2019-08-12 23:45:21 +02:00
use App\Services\StructuralElementRecursionHelper;
use Doctrine\ORM\EntityManagerInterface;
use Exchanger\Exception\ChainException;
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;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
2019-08-12 23:45:21 +02:00
/**
* @Route("/currency")
*
* Class CurrencyController
*/
class CurrencyController extends BaseAdminController
{
protected $entity_class = Currency::class;
protected $twig_template = 'AdminPages/CurrencyAdmin.html.twig';
protected $form_class = CurrencyAdminForm::class;
2019-08-20 18:39:57 +02:00
protected $route_base = 'currency';
protected $attachment_class = CurrencyAttachment::class;
protected $parameter_class = CurrencyParameter::class;
2019-08-12 23:45:21 +02:00
protected $exchangeRateUpdater;
public function __construct(
TranslatorInterface $translator,
UserPasswordEncoderInterface $passwordEncoder,
AttachmentSubmitHandler $attachmentSubmitHandler,
EventCommentHelper $commentHelper,
HistoryHelper $historyHelper,
TimeTravel $timeTravel,
DataTableFactory $dataTableFactory,
EventDispatcherInterface $eventDispatcher,
BarcodeExampleElementsGenerator $barcodeExampleGenerator,
LabelGenerator $labelGenerator,
EntityManagerInterface $entityManager,
ExchangeRateUpdater $exchangeRateUpdater
) {
$this->exchangeRateUpdater = $exchangeRateUpdater;
parent::__construct(
$translator,
$passwordEncoder,
$attachmentSubmitHandler,
$commentHelper,
$historyHelper,
$timeTravel,
$dataTableFactory,
$eventDispatcher,
$barcodeExampleGenerator,
$labelGenerator,
$entityManager
);
}
/**
* @Route("/{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 (ChainException $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;
}
2019-08-12 23:45:21 +02:00
/**
* @Route("/{id}/edit/{timestamp}", requirements={"id"="\d+"}, name="currency_edit")
* @Route("/{id}", requirements={"id"="\d+"})
2019-08-12 23:45:21 +02:00
*/
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
}
/**
* @Route("/new", name="currency_new")
* @Route("/{id}/clone", name="currency_clone")
2019-08-12 23:45:21 +02:00
* @Route("/")
*/
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
}
/**
* @Route("/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);
}
/**
* @Route("/{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
{
if ($entity instanceof Currency) {
if ($entity->getPricedetails()->count() > 0) {
$this->addFlash('error', 'entity.delete.must_not_contain_prices');
2020-08-21 21:36:22 +02:00
return false;
}
}
return true;
}
}