<?php
namespace App\Controller;
use App\Entity\Accreditation;
use App\Form\AccreditationType;
use App\Annotation\CmsAdminDash;
use App\Annotation\CmsComponent;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\AccreditationRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/takeflight/accreditation')]
class AccreditationController extends AbstractController
{
/**
* @CmsAdminDash("Accreditations", active=true, routeName="app_accreditation_index", icon="fa fa-certificate", menuPosition=40, parentRouteName="control_content_index")
*/
#[Route('/', name: 'app_accreditation_index', methods: ['GET'])]
public function index(AccreditationRepository $accreditationRepository): Response
{
return $this->render('accreditation/index.html.twig', [
'accreditations' => $accreditationRepository->findBy(['deleted' => false]),
]);
}
#[Route('/new', name: 'app_accreditation_new', methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
$accreditation = new Accreditation();
$form = $this->createForm(AccreditationType::class, $accreditation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// REQUIRED IF USING UPLOAD FILE TRAIT
// $accreditation->uploadFile();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($accreditation);
$entityManager->flush();
return $this->redirectToRoute('app_accreditation_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('accreditation/new.html.twig', [
'accreditation' => $accreditation,
'form' => $form->createView(),
]);
}
#[Route('/{id}', name: 'app_accreditation_show', methods: ['GET'])]
public function show(Accreditation $accreditation): Response
{
return $this->render('accreditation/show.html.twig', [
'accreditation' => $accreditation,
]);
}
#[Route('/{id}/edit', name: 'app_accreditation_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Accreditation $accreditation): Response
{
$form = $this->createForm(AccreditationType::class, $accreditation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// REQUIRED IF USING UPLOAD FILE TRAIT
// $accreditation->uploadFile();
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('app_accreditation_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('accreditation/edit.html.twig', [
'accreditation' => $accreditation,
'form' => $form->createView(),
]);
}
#[Route('/{id}', name: 'app_accreditation_delete', methods: ['POST'])]
public function delete(Request $request, Accreditation $accreditation): Response
{
if ($this->isCsrfTokenValid('delete'.$accreditation->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$accreditation->setActive(false);
$accreditation->setDeleted(true);
$entityManager->persist($accreditation);
$entityManager->flush();
$this->addFlash('success', 'Success - Accreditation deleted');
}
return $this->redirectToRoute('app_accreditation_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @CmsComponent("Accreditations (All)", active=true, routeName="embed_accreditations_all")
*
* @param mixed $request
*/
#[Route(path: '/test-accreditations/all', name: 'embed_accreditations_all')]
public function embedAccreditationsAll(EntityManagerInterface $em, PaginatorInterface $paginator, Request $request): Response
{
$perpage = 10;
$query = $em->createQuery('SELECT e FROM App:Accreditation e WHERE e.deleted = 0 AND e.active = 1 ORDER BY e.sortOrder ASC');
$paginatedAccreditations = $paginator->paginate($query, $request->query->getInt('page', 1), $perpage);
return $this->render('@theme/accreditation/accreditations.html.twig', [
'accreditations' => $paginatedAccreditations,
]);
}
/**
* @CmsComponent("Accreditations (Featured Only)", active=true, routeName="embed_accreditations_featured")
*
* @param mixed $request
*/
#[Route(path: '/test-accreditations/featured', name: 'embed_accreditations_featured')]
public function embedAccreditationsFeatured(EntityManagerInterface $em, PaginatorInterface $paginator, Request $request): Response
{
$perpage = 10;
$query = $em->createQuery('SELECT e FROM App:Accreditation e WHERE e.deleted = 0 AND e.active = 1 AND e.featured = 1 ORDER BY e.sortOrder ASC');
$paginatedAccreditations = $paginator->paginate($query, $request->query->getInt('page', 1), $perpage);
return $this->render('@theme/accreditation/featured.html.twig', [
'accreditations' => $paginatedAccreditations,
]);
}
}