66 lines
2.6 KiB
PHP
66 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Admin\System\Contact;
|
|
|
|
use App\Entity\Contact;
|
|
use App\Entity\Upload;
|
|
use App\Entity\User;
|
|
use App\Form\ContactType;
|
|
use App\Htmx\HxRedirectResponse;
|
|
use App\Service\Upload\UploadHandler;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
class EditController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
#[Route('/admin/system/contact/edit/{id}', name: 'app_admin_system_contact_edit')]
|
|
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
|
public function index(Contact $contact, Request $request): Response
|
|
{
|
|
// Handle upload independently from form submission to avoid issues with failing validation
|
|
$uploadSession = $this->uploadHandler->getUploadSession();
|
|
if (true === $request->isMethod('POST') && 0 < $uploadSession->getCount()) {
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
$photo = Upload::fromUploadDto($uploadSession->getUploadsByType(Upload::TYPE_PHOTO)->first(), $user, Upload::TYPE_PHOTO);
|
|
if (null !== $existingPhoto = $contact->getPhoto()) {
|
|
$this->entityManager->remove($existingPhoto);
|
|
}
|
|
$contact->setPhoto($photo);
|
|
$this->entityManager->flush();
|
|
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_PHOTO, $uploadSession);
|
|
$this->uploadHandler->removeUploadSessionUploadsByType(Upload::TYPE_PHOTO);
|
|
}
|
|
|
|
$form = $this->createForm(ContactType::class, $contact, ['upload_session' => $uploadSession]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$this->entityManager->flush();
|
|
$this->addFlash('success', 'Die Ansprechperson wurde aktualisiert');
|
|
$this->logger->info('Edit contact', [
|
|
'contact_id' => $contact->getId(),
|
|
'contact_name' => $contact->getName(),
|
|
]);
|
|
|
|
return new HxRedirectResponse($this->generateUrl('app_admin_system_contact_index'));
|
|
}
|
|
|
|
return $this->render('admin/system/contact/modal_edit.html.twig', [
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
}
|