cmtracker/src/Controller/CommunityResourceController.php

139 lines
4.7 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\CommunityResource;
use App\Entity\User;
use App\Form\ResourceFormType;
use App\Libs\Breadcrumb;
use App\Libs\NavList;
use Doctrine\ORM\EntityManagerInterface;
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\CurrentUser;
use Symfony\Component\Validator\Constraints\Regex;
class CommunityResourceController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private array $navLinks = []
) {
$this->navLinks = NavList::LIST;
$this->navLinks['community_resources'] = NavList::PRESENT_LINK;
}
#[Route('/resource/list', name: 'app_community_resource')]
public function list(#[CurrentUser()] User $user): Response
{
$rsc = $this->entityManager->getRepository(CommunityResource::class)->findAll();
return $this->render(
'internal/community_resource/list.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
new Breadcrumb('#', 'Community Resources')
],
'resources' => $rsc,
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/resource/map', name: 'app_community_resource_map')]
public function map(): Response
{
return $this->render('internal/community_resource/map.html.twig', [
]);
}
#[Route('/resource/add', name: 'app_community_resource_add')]
public function add(#[CurrentUser()] User $user, Request $request): Response
{
$form = $this->createForm(ResourceFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$rsc = $form->getData();
$this->entityManager->persist($rsc);
$this->entityManager->flush();
return $this->redirectToRoute('app_community_resource');
}
return $this->render(
'internal/community_resource/add.html.twig',
array_merge(
$this->navLinks,
[
'form' => $form,
'breadcrumbs' => [
new Breadcrumb($this->generateUrl('app_community_resource'), 'List Resources'),
new Breadcrumb('#', 'Add Resource')
],
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/resource/edit/{id}', name: 'app_community_resource_edit')]
public function edit(string $id, #[CurrentUser()] User $user, Request $request): Response
{
$rsc = $this->entityManager->getRepository(CommunityResource::class)->find($id);
$form = $this->createForm(ResourceFormType::class, $rsc);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->flush();
return $this->redirectToRoute('app_community_resource');
}
return $this->render(
'internal/community_resource/edit.html.twig',
array_merge(
$this->navLinks,
[
'form' => $form,
'breadcrumbs' => [
new Breadcrumb($this->generateUrl('app_community_resource'), 'List Resources'),
new Breadcrumb('#', 'Edit Resource')
],
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/resource/download/{id}', name: 'app_community_resource_download')]
public function download(string $id): Response
{
/** @var CommunityResource $rsc */
$rsc = $this->entityManager->getRepository(CommunityResource::class)->find($id);
if (!$rsc) {
$this->addFlash('error', 'Resource not found.');
return $this->redirectToRoute('app_community_resource');
}
return new Response($rsc->generateVCard(), 200, [
'Content-Type' => 'text/vcf',
'Content-Disposition' => 'attachment; filename="' . str_replace(' ', '', $rsc->getName()) . '.vcf"',
'Content-Length' => strlen($rsc->generateVCard()),
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
'Pragma' => 'public',
'Content-Transfer-Encoding' => 'binary'
]);
}
}