Feat: Add contacts

This commit is contained in:
Björn Fromme
2023-10-16 12:05:18 +02:00
parent f5fee787a0
commit 3fd4c6ca54
19 changed files with 658 additions and 2 deletions
@@ -0,0 +1,79 @@
<?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\Model\AjaxModalResponseDto;
use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class CreateController extends AbstractController
{
public function __construct(
private readonly UploadHandler $uploadHandler,
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger
) {
}
#[Route('/admin/system/contact/create', name: 'app_admin_system_contact_create')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): JsonResponse
{
$response = new AjaxModalResponseDto();
$contact = new Contact();
$formAction = $this->generateUrl('app_admin_system_contact_create');
// 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->getUploads()->first(), $user, Upload::TYPE_PHOTO);
$contact->setPhoto($photo);
$this->entityManager->flush();
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_PHOTO, $uploadSession);
$this->uploadHandler->destroyUploadSession();
}
$form = $this->createForm(
ContactType::class,
$contact,
[
'action' => $formAction,
'ajax_submit' => true,
'upload_session' => $uploadSession,
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($contact);
$this->entityManager->flush();
$this->addFlash('success', 'Der Ansprechpartner wurde hinzugefügt');
$this->logger->info('Create contact', [
'contact' => $contact->getName(),
]);
$response->setCloseAndRedirect($this->generateUrl('app_admin_system_contact_index'));
} else {
$response->setContent($this->renderView('admin/system/contact/create.html.twig', [
'form' => $form,
]));
}
return $this->json($response);
}
}
@@ -0,0 +1,35 @@
<?php
namespace App\Controller\Admin\System\Contact;
use App\Entity\Contact;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class DeleteController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger
) {
}
#[Route('/admin/system/contact/delete/{id}', name: 'app_admin_system_contact_delete')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Contact $contact): Response
{
$this->entityManager->remove($contact);
$this->entityManager->flush();
$this->addFlash('success', 'Der Ansprechpartner wurde gelöscht');
$this->logger->info('Delete contact', [
'contact' => $contact->getName(),
]);
return $this->redirectToRoute('app_admin_system_contact_index');
}
}
@@ -0,0 +1,76 @@
<?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\Model\AjaxModalResponseDto;
use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\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): JsonResponse
{
$response = new AjaxModalResponseDto();
$formAction = $this->generateUrl('app_admin_system_contact_edit', ['id' => $contact->getId()]);
// 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->getUploads()->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->destroyUploadSession();
}
$form = $this->createForm(
ContactType::class,
$contact,
[
'action' => $formAction,
'ajax_submit' => true,
'upload_session' => $uploadSession,
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->flush();
$this->addFlash('success', 'Der Ansprechpartner wurde aktualisiert');
$this->logger->info('Edit contact', [
'contact' => $contact->getName(),
]);
$response->setCloseAndRedirect($this->generateUrl('app_admin_system_contact_index'));
} else {
$response->setContent($this->renderView('admin/system/contact/edit.html.twig', [
'form' => $form,
]));
}
return $this->json($response);
}
}
@@ -0,0 +1,29 @@
<?php
namespace App\Controller\Admin\System\Contact;
use App\Repository\ContactRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class IndexController extends AbstractController
{
public function __construct(private readonly ContactRepository $contactRepository)
{}
#[Route('/admin/system/contact', name: 'app_admin_system_contact_index')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(): Response
{
$contacts = $this
->contactRepository
->findBy([], ['name' => 'ASC'])
;
return $this->render('admin/system/contact/index.html.twig', [
'contacts' => $contacts,
]);
}
}
+18 -1
View File
@@ -2,15 +2,32 @@
namespace App\Controller\Teamer;
use App\Repository\ContactRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
public function __construct(private readonly ContactRepository $contactRepository)
{}
#[Route('/teamer/contact', name: 'app_teamer_contact')]
public function index(): Response
{
return $this->render('teamer/contact/index.html.twig');
$generalContacts = $this
->contactRepository
->getGeneralContacts()
;
$destinationContacts = $this
->contactRepository
->getDestinationContacts()
;
return $this->render('teamer/contact/index.html.twig', [
'generalContacts' => $generalContacts,
'destinationContacts' => $destinationContacts,
]);
}
}