src/Controller/PriceController.php line 104

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Price;
  4. use App\Form\PriceType;
  5. use App\Annotation\CmsAdminDash;
  6. use App\Annotation\CmsComponent;
  7. use App\Repository\PriceRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. #[Route('/takeflight/price')]
  14. class PriceController extends AbstractController
  15. {
  16.     public function __construct(
  17.         private readonly EntityManagerInterface $entityManager
  18.     ) {}
  19.     /**
  20.      * @CmsAdminDash("Prices", active=true, routeName="app_price_index", icon="fa fa-money", menuPosition=40, parentRouteName="control_content_index")
  21.      */
  22.     #[Route('/'name'app_price_index'methods: ['GET'])]
  23.     public function index(PriceRepository $priceRepository): Response
  24.     {
  25.         return $this->render('price/index.html.twig', [
  26.             'prices' => $priceRepository->findBy(['deleted' => false]),
  27.         ]);
  28.     }
  29.     #[Route('/new'name'app_price_new'methods: ['GET''POST'])]
  30.     public function new(Request $request): Response
  31.     {
  32.         $price = new Price();
  33.         $form $this->createForm(PriceType::class, $price);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             // REQUIRED IF USING UPLOAD FILE TRAIT
  37.             // $price->uploadFile();
  38.             $this->entityManager->persist($price);
  39.             $this->entityManager->flush();
  40.             return $this->redirectToRoute('app_price_index', [], Response::HTTP_SEE_OTHER);
  41.         }
  42.         return $this->render('price/new.html.twig', [
  43.             'price' => $price,
  44.             'form' => $form->createView(),
  45.         ]);
  46.     }
  47.     #[Route('/{id}'name'app_price_show'methods: ['GET'])]
  48.     public function show(Price $price): Response
  49.     {
  50.         return $this->render('price/show.html.twig', [
  51.             'price' => $price,
  52.         ]);
  53.     }
  54.     #[Route('/{id}/edit'name'app_price_edit'methods: ['GET''POST'])]
  55.     public function edit(Request $requestPrice $price): Response
  56.     {
  57.         $form $this->createForm(PriceType::class, $price);
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted() && $form->isValid()) {
  60.             // REQUIRED IF USING UPLOAD FILE TRAIT
  61.             // $price->uploadFile();
  62.             $this->entityManager->flush();
  63.             return $this->redirectToRoute('app_price_index', [], Response::HTTP_SEE_OTHER);
  64.         }
  65.         return $this->render('price/edit.html.twig', [
  66.             'price' => $price,
  67.             'form' => $form->createView(),
  68.         ]);
  69.     }
  70.     #[Route('/{id}'name'app_price_delete'methods: ['POST'])]
  71.     public function delete(Request $requestPrice $price): Response
  72.     {
  73.         if ($this->isCsrfTokenValid('delete'.$price->getId(), $request->request->get('_token'))) {
  74.             $price->setActive(false);
  75.             $price->setDeleted(true);
  76.             $this->entityManager->persist($price);
  77.             $this->entityManager->flush();
  78.             $this->addFlash('success''Success - Price deleted');
  79.         }
  80.         return $this->redirectToRoute('app_price_index', [], Response::HTTP_SEE_OTHER);
  81.     }
  82.     /**
  83.      * @CmsComponent("Prices (Employer)", active=true, routeName="embed_employer_prices_component")
  84.      *
  85.      * @param mixed $request
  86.      */
  87.     #[Route('/prices'name'embed_employer_prices_component')]
  88.     public function pricesEmployer(): Response
  89.     {
  90.         $prices $this->entityManager->getRepository(Price::class)->findBy(['deleted' => false'active' => true'serviceCategory' => 1], ['sortOrder' => 'ASC']);
  91.         if (! $prices) {
  92.             return new Response('');
  93.         }
  94.         return $this->render('@theme/price/index.html.twig', [
  95.             'prices' => $prices
  96.         ]);
  97.     }
  98.     /**
  99.      * @CmsComponent("Prices (Employee)", active=true, routeName="embed_employee_prices_component")
  100.      *
  101.      * @param mixed $request
  102.      */
  103.     #[Route('/prices'name'embed_employee_prices_component')]
  104.     public function pricesEmployee(): Response
  105.     {
  106.         $prices $this->entityManager->getRepository(Price::class)->findBy(['deleted' => false'active' => true'serviceCategory' => 2], ['sortOrder' => 'ASC']);
  107.         if (! $prices) {
  108.             return new Response('');
  109.         }
  110.         return $this->render('@theme/price/index.html.twig', [
  111.             'prices' => $prices
  112.         ]);
  113.     }
  114. }