<?php
namespace App\Controller;
use App\Entity\Price;
use App\Form\PriceType;
use App\Annotation\CmsAdminDash;
use App\Annotation\CmsComponent;
use App\Repository\PriceRepository;
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/price')]
class PriceController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager
) {}
/**
* @CmsAdminDash("Prices", active=true, routeName="app_price_index", icon="fa fa-money", menuPosition=40, parentRouteName="control_content_index")
*/
#[Route('/', name: 'app_price_index', methods: ['GET'])]
public function index(PriceRepository $priceRepository): Response
{
return $this->render('price/index.html.twig', [
'prices' => $priceRepository->findBy(['deleted' => false]),
]);
}
#[Route('/new', name: 'app_price_new', methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
$price = new Price();
$form = $this->createForm(PriceType::class, $price);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// REQUIRED IF USING UPLOAD FILE TRAIT
// $price->uploadFile();
$this->entityManager->persist($price);
$this->entityManager->flush();
return $this->redirectToRoute('app_price_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('price/new.html.twig', [
'price' => $price,
'form' => $form->createView(),
]);
}
#[Route('/{id}', name: 'app_price_show', methods: ['GET'])]
public function show(Price $price): Response
{
return $this->render('price/show.html.twig', [
'price' => $price,
]);
}
#[Route('/{id}/edit', name: 'app_price_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Price $price): Response
{
$form = $this->createForm(PriceType::class, $price);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// REQUIRED IF USING UPLOAD FILE TRAIT
// $price->uploadFile();
$this->entityManager->flush();
return $this->redirectToRoute('app_price_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('price/edit.html.twig', [
'price' => $price,
'form' => $form->createView(),
]);
}
#[Route('/{id}', name: 'app_price_delete', methods: ['POST'])]
public function delete(Request $request, Price $price): Response
{
if ($this->isCsrfTokenValid('delete'.$price->getId(), $request->request->get('_token'))) {
$price->setActive(false);
$price->setDeleted(true);
$this->entityManager->persist($price);
$this->entityManager->flush();
$this->addFlash('success', 'Success - Price deleted');
}
return $this->redirectToRoute('app_price_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @CmsComponent("Prices (Employer)", active=true, routeName="embed_employer_prices_component")
*
* @param mixed $request
*/
#[Route('/prices', name: 'embed_employer_prices_component')]
public function pricesEmployer(): Response
{
$prices = $this->entityManager->getRepository(Price::class)->findBy(['deleted' => false, 'active' => true, 'serviceCategory' => 1], ['sortOrder' => 'ASC']);
if (! $prices) {
return new Response('');
}
return $this->render('@theme/price/index.html.twig', [
'prices' => $prices
]);
}
/**
* @CmsComponent("Prices (Employee)", active=true, routeName="embed_employee_prices_component")
*
* @param mixed $request
*/
#[Route('/prices', name: 'embed_employee_prices_component')]
public function pricesEmployee(): Response
{
$prices = $this->entityManager->getRepository(Price::class)->findBy(['deleted' => false, 'active' => true, 'serviceCategory' => 2], ['sortOrder' => 'ASC']);
if (! $prices) {
return new Response('');
}
return $this->render('@theme/price/index.html.twig', [
'prices' => $prices
]);
}
}