<?php
namespace App\Controller;
use App\Entity\Service;
use App\Form\ServiceType;
use App\Entity\ServiceCategory;
use App\Annotation\CmsAdminDash;
use App\Annotation\CmsComponent;
use App\Service\ServiceController as BaseServiceController;
use App\Repository\ServiceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/takeflight/service')]
class ServiceController extends AbstractController
{
public function __construct(private readonly BaseServiceController $serviceController) {}
/**
* Simple Parent Route for admin side-bar.
*
* @CmsAdminDash("Manage Services", active=true, role="ROLE_CMS_ACCESS", routeName="control_service_index", icon="fa fa-cogs", dontLink=true, menuPosition=10)
*/
#[Route(path: '/index', name: 'control_service_index', methods: ['GET'])]
public function contentIndex()
{
return false;
}
/**
* @CmsAdminDash("Services", active=true, routeName="app_service_index",icon="glyphicon glyphicon-picture", menuPosition=30, parentRouteName="control_service_index")
*/
#[Route('/', name: 'app_service_index', methods: ['GET'])]
public function index(ServiceRepository $serviceRepository): Response
{
return $this->render('service/index.html.twig', [
'services' => $serviceRepository->findBy(['deleted' => false]),
]);
}
#[Route('/new', name: 'app_service_new', methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
$service = new Service();
$form = $this->createForm(ServiceType::class, $service);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// REQUIRED IF USING UPLOAD FILE TRAIT
// $service->uploadFile();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($service);
$entityManager->flush();
return $this->redirectToRoute('app_service_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('service/new.html.twig', [
'service' => $service,
'form' => $form->createView(),
]);
}
#[Route('/{id}', name: 'app_service_show', methods: ['GET'])]
public function show(Service $service): Response
{
return $this->render('service/show.html.twig', [
'service' => $service,
]);
}
#[Route('/{id}/edit', name: 'app_service_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Service $service): Response
{
$form = $this->createForm(ServiceType::class, $service);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// REQUIRED IF USING UPLOAD FILE TRAIT
// $service->uploadFile();
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('app_service_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('service/edit.html.twig', [
'service' => $service,
'form' => $form->createView(),
]);
}
#[Route('/{id}', name: 'app_service_delete', methods: ['POST'])]
public function delete(Request $request, Service $service): Response
{
if ($this->isCsrfTokenValid('delete'.$service->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$service->setActive(false);
$service->setDeleted(true);
$entityManager->persist($service);
$entityManager->flush();
$this->addFlash('success', 'Success - Service deleted');
}
return $this->redirectToRoute('app_service_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @CmsComponent("Service Categories", active=true, routeName="embed_service_categories")
*/
#[Route(path: '/test-service-categories', name: 'embed_service_categories')]
public function embedServiceCategories(EntityManagerInterface $em, Request $request): Response
{
$categories = $em->getRepository(ServiceCategory::class)->findAllWithServices();
return $this->render('@theme/service/index.html.twig', [
'categories' => $categories,
]);
}
/**
* @CmsComponent("Service Category", slug="{service_category_slug}", slugEntity="ServiceCategory", active=true, routeName="embed_service_category")
*/
#[Route(path: '/test-service-categories/{service_category_slug}', name: 'embed_service_category')]
public function embedServiceCategory1(EntityManagerInterface $em, Request $request, mixed $service_category_slug)
{
$serviceCategory = $em->getRepository(ServiceCategory::class)->findOneWithServices($service_category_slug);
if (!$serviceCategory) {
return new Response('Not Found');
}
$employerServices = $serviceCategory->getServices()->filter(function($service) {
return $service->getEmployerOrEmployee() === 'Employer';
});
$employeeServices = $serviceCategory->getServices()->filter(function($service) {
return $service->getEmployerOrEmployee() === 'Employee';
});
return $this->render('@theme/service/category1.html.twig', [
'category' => $serviceCategory,
'employerServices' => $employerServices,
'employeeServices' => $employeeServices,
]);
}
/**
* @CmsComponent("Service", slug="{service_slug}", slugEntity="Service", active=true, routeName="embed_service")
*/
#[Route(path: '/test-service-categories/category/{service_slug}', name: 'embed_service')]
public function embedService(EntityManagerInterface $em, Request $request, mixed $service_slug)
{
if ($this->isGranted('ROLE_ADMIN')) {
$service = $em->getRepository(Service::class)->findOneWithCategory($service_slug, false);
} else {
$service = $em->getRepository(Service::class)->findOneWithCategory($service_slug);
}
if (!$service) {
return $this->serviceController->componentRedirect('/service-not-found');
}
return $this->render('@theme/service/service.html.twig', [
'service' => $service,
]);
}
}