feat: upload in behalf of teamer for administrative users
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Administrative\Disposition;
|
||||
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\Upload;
|
||||
use App\Event\DocumentUploadedEvent;
|
||||
use App\Htmx\HxRedirectResponse;
|
||||
use App\Service\Upload\UploadHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Target;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
use Symfony\Component\Workflow\WorkflowInterface;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class DocumentUploadController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UploadHandler $uploadHandler,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
#[Target('disposition')]
|
||||
private readonly WorkflowInterface $workflow,
|
||||
private readonly EventDispatcherInterface $eventDispatcher,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route(
|
||||
path: '/administrative/disposition/document-upload/{uuid}',
|
||||
name: 'app_administrative_disposition_document_upload'
|
||||
)]
|
||||
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
||||
public function index(Disposition $disposition, Request $request): Response
|
||||
{
|
||||
if (true === $this->workflow->can($disposition, 'upload_contract')) {
|
||||
$uploadType = Upload::TYPE_CONTRACT;
|
||||
$uploadTypeLabel = 'Honorarvertrag';
|
||||
$uploaderRoute = '_uploader_upload_contract';
|
||||
} elseif (true === $this->workflow->can($disposition, 'upload_invoice')) {
|
||||
$uploadType = Upload::TYPE_INVOICE;
|
||||
$uploadTypeLabel = 'Honoranote';
|
||||
$uploaderRoute = '_uploader_upload_invoice';
|
||||
} else {
|
||||
$this->addFlash('error', 'Kein Upload möglich beim aktuellen Status der Einteilung');
|
||||
|
||||
return $this->getRedirectResponse($disposition);
|
||||
}
|
||||
|
||||
$teamer = $disposition->getTeamer();
|
||||
$modalTitle = sprintf('%s für %s hochladen', $uploadTypeLabel, $teamer);
|
||||
|
||||
$form = $this->createFormBuilder()->getForm();
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->processUpload($disposition, $uploadType);
|
||||
|
||||
$this->addFlash('success', sprintf('%s wurde hochgeladen', $uploadTypeLabel));
|
||||
|
||||
return $this->getRedirectResponse($disposition);
|
||||
}
|
||||
|
||||
return $this->render('administrative/disposition/modal_document_upload.html.twig', [
|
||||
'disposition' => $disposition,
|
||||
'modalTitle' => $modalTitle,
|
||||
'uploaderRoute' => $uploaderRoute,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function processUpload(Disposition $disposition, string $uploadType): void
|
||||
{
|
||||
if (null !== $existingUpload = $disposition->getDocumentByType($uploadType)) {
|
||||
$this->entityManager->remove($existingUpload);
|
||||
}
|
||||
|
||||
$teamer = $disposition->getTeamer();
|
||||
$user = $teamer->getUser();
|
||||
|
||||
$uploadSession = $this->uploadHandler->getUploadSession();
|
||||
|
||||
$upload = Upload::fromUploadDto($uploadSession->getUploads()->first(), $user, $uploadType);
|
||||
$upload->setStatus(Upload::STATUS_NEW);
|
||||
|
||||
$disposition->addDocument($upload);
|
||||
|
||||
$transitionName = sprintf('upload_%s', $uploadType);
|
||||
$this->workflow->apply($disposition, $transitionName);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->uploadHandler->moveUploadSessionFilesFromOrphanage($uploadType, $uploadSession);
|
||||
$this->uploadHandler->destroyUploadSession();
|
||||
|
||||
$this->eventDispatcher->dispatch(new DocumentUploadedEvent($upload), DocumentUploadedEvent::NAME);
|
||||
}
|
||||
|
||||
private function getRedirectResponse(Disposition $disposition): HxRedirectResponse
|
||||
{
|
||||
return new HxRedirectResponse($this->generateUrl('app_administrative_assignment_detail', [
|
||||
'uuid' => $disposition->getAssignment()->getUuid(),
|
||||
]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user