Part-DB.Part-DB-server/src/Controller/PartController.php

258 lines
9.1 KiB
PHP
Raw Normal View History

<?php
2019-03-05 14:37:41 +01:00
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
2019-03-05 14:37:41 +01:00
*
2019-11-01 13:40:30 +01:00
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
2019-03-05 14:37:41 +01: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;
use App\Entity\Parts\Category;
use App\Entity\Parts\Part;
use App\Exceptions\AttachmentDownloadException;
use App\Form\Part\PartBaseType;
use App\Services\Attachments\AttachmentManager;
use App\Services\Attachments\AttachmentSubmitHandler;
use App\Services\Attachments\PartPreviewGenerator;
use App\Services\PricedetailHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
2020-01-05 22:49:00 +01:00
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
2020-01-05 22:49:00 +01:00
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
2019-09-02 17:42:13 +02:00
/**
* @Route("/part")
*/
class PartController extends AbstractController
{
/**
2019-09-02 17:42:13 +02:00
* @Route("/{id}/info", name="part_info")
* @Route("/{id}", requirements={"id"="\d+"})
*
2020-02-02 14:05:36 +01:00
* @param Part $part
* @param AttachmentManager $attachmentHelper
* @param PricedetailHelper $pricedetailHelper
* @param PartPreviewGenerator $previewGenerator
2020-01-05 22:49:00 +01:00
* @return Response
*/
2020-02-02 14:05:36 +01:00
public function show(Part $part, AttachmentManager $attachmentHelper, PricedetailHelper $pricedetailHelper, PartPreviewGenerator $previewGenerator): Response
{
$this->denyAccessUnlessGranted('read', $part);
return $this->render(
'Parts/info/show_part_info.html.twig',
[
2019-03-20 22:53:06 +01:00
'part' => $part,
'attachment_helper' => $attachmentHelper,
'pricedetail_helper' => $pricedetailHelper,
'pictures' => $previewGenerator->getPreviewAttachments($part),
]
);
}
/**
2019-09-02 17:42:13 +02:00
* @Route("/{id}/edit", name="part_edit")
*
2020-02-02 14:05:36 +01:00
* @param Part $part
* @param Request $request
* @param EntityManagerInterface $em
* @param TranslatorInterface $translator
* @param AttachmentManager $attachmentHelper
* @param AttachmentSubmitHandler $attachmentSubmitHandler
2020-01-05 22:49:00 +01:00
* @return Response
*/
public function edit(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator,
2020-02-02 14:05:36 +01:00
AttachmentManager $attachmentHelper, AttachmentSubmitHandler $attachmentSubmitHandler): Response
{
$this->denyAccessUnlessGranted('edit', $part);
$form = $this->createForm(PartBaseType::class, $part);
$form->handleRequest($request);
2019-03-13 15:10:42 +01:00
if ($form->isSubmitted() && $form->isValid()) {
//Upload passed files
$attachments = $form['attachments'];
foreach ($attachments as $attachment) {
2020-01-05 15:46:58 +01:00
/** @var FormInterface $attachment */
$options = [
'secure_attachment' => $attachment['secureFile']->getData(),
'download_url' => $attachment['downloadURL']->getData(),
];
2020-01-05 15:46:58 +01:00
try {
$attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options);
2020-01-05 22:49:00 +01:00
} catch (AttachmentDownloadException $attachmentDownloadException) {
$this->addFlash(
'error',
2020-01-05 22:49:00 +01:00
$translator->trans('attachment.download_failed').' '.$attachmentDownloadException->getMessage()
);
}
}
2019-03-13 15:10:42 +01:00
$em->persist($part);
$em->flush();
$this->addFlash('info', 'part.edited_flash');
//Reload form, so the SIUnitType entries use the new part unit
$form = $this->createForm(PartBaseType::class, $part);
2020-01-05 15:46:58 +01:00
} elseif ($form->isSubmitted() && ! $form->isValid()) {
$this->addFlash('error', 'part.edited_flash.invalid');
2019-03-13 15:10:42 +01:00
}
return $this->render('Parts/edit/edit_part_info.html.twig',
[
2019-03-20 22:53:06 +01:00
'part' => $part,
'form' => $form->createView(),
'attachment_helper' => $attachmentHelper,
]);
}
/**
2019-09-02 17:42:13 +02:00
* @Route("/{id}/delete", name="part_delete", methods={"DELETE"})
*
2020-02-02 14:05:36 +01:00
* @param Request $request
* @param Part $part
2020-01-05 22:49:00 +01:00
* @return RedirectResponse
2019-09-02 17:42:13 +02:00
*/
2020-02-02 14:05:36 +01:00
public function delete(Request $request, Part $part): RedirectResponse
2019-09-02 17:42:13 +02:00
{
$this->denyAccessUnlessGranted('delete', $part);
if ($this->isCsrfTokenValid('delete'.$part->getId(), $request->request->get('_token'))) {
2019-09-02 17:42:13 +02:00
$entityManager = $this->getDoctrine()->getManager();
//Remove part
$entityManager->remove($part);
//Flush changes
$entityManager->flush();
$this->addFlash('success', 'part.deleted');
}
return $this->redirectToRoute('homepage');
}
/**
* @Route("/new", name="part_new")
*
2020-02-02 14:05:36 +01:00
* @param Request $request
* @param EntityManagerInterface $em
* @param TranslatorInterface $translator
* @param AttachmentManager $attachmentHelper
* @param AttachmentSubmitHandler $attachmentSubmitHandler
2020-01-05 22:49:00 +01:00
* @return Response
*/
public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator,
2020-02-02 14:05:36 +01:00
AttachmentManager $attachmentHelper, AttachmentSubmitHandler $attachmentSubmitHandler): Response
{
$new_part = new Part();
$this->denyAccessUnlessGranted('create', $new_part);
$cid = $request->get('cid', 1);
$category = $em->find(Category::class, $cid);
2020-01-04 20:24:09 +01:00
if (null !== $category) {
2019-11-10 14:00:56 +01:00
$new_part->setCategory($category);
}
$form = $this->createForm(PartBaseType::class, $new_part);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//Upload passed files
$attachments = $form['attachments'];
foreach ($attachments as $attachment) {
2020-01-05 15:46:58 +01:00
/** @var FormInterface $attachment */
$options = [
'secure_attachment' => $attachment['secureFile']->getData(),
'download_url' => $attachment['downloadURL']->getData(),
];
2020-01-05 15:46:58 +01:00
try {
$attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options);
2020-01-05 22:49:00 +01:00
} catch (AttachmentDownloadException $attachmentDownloadException) {
$this->addFlash(
'error',
2020-01-05 22:49:00 +01:00
$translator->trans('attachment.download_failed').' '.$attachmentDownloadException->getMessage()
);
}
}
$em->persist($new_part);
$em->flush();
$this->addFlash('success', 'part.created_flash');
return $this->redirectToRoute('part_edit', ['id' => $new_part->getID()]);
2019-09-16 22:04:59 +02:00
}
2020-01-05 15:46:58 +01:00
if ($form->isSubmitted() && ! $form->isValid()) {
$this->addFlash('error', 'part.created_flash.invalid');
}
return $this->render('Parts/edit/new_part.html.twig',
2019-03-19 19:53:23 +01:00
[
2019-03-20 22:53:06 +01:00
'part' => $new_part,
'form' => $form->createView(),
'attachment_helper' => $attachmentHelper,
2019-03-19 19:53:23 +01:00
]);
}
/**
2019-09-02 17:42:13 +02:00
* @Route("/{id}/clone", name="part_clone")
*
2020-02-02 14:05:36 +01:00
* @param Part $part
* @param Request $request
* @param EntityManagerInterface $em
* @param TranslatorInterface $translator
2020-01-05 22:49:00 +01:00
* @return RedirectResponse|Response
2019-03-19 19:53:23 +01:00
*/
public function clone(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
{
/** @var Part $new_part */
2019-03-20 22:53:06 +01:00
$new_part = clone $part;
2019-03-19 19:53:23 +01:00
$this->denyAccessUnlessGranted('create', $new_part);
$form = $this->createForm(PartBaseType::class, $new_part);
2019-03-19 19:53:23 +01:00
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($new_part);
$em->flush();
$this->addFlash('success', 'part.created_flash');
2019-03-19 19:53:23 +01:00
return $this->redirectToRoute('part_edit', ['id' => $new_part->getID()]);
}
2019-03-19 19:53:23 +01:00
2019-08-20 18:39:57 +02:00
return $this->render('Parts/edit/new_part.html.twig',
[
2019-03-20 22:53:06 +01:00
'part' => $new_part,
'form' => $form->createView(),
]);
}
}