src/Controller/Bo/DealController.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Bo;
  3. use App\Entity\Club;
  4. use App\Entity\Deal;
  5. use App\Form\Bo\DealType;
  6. use App\Repository\CompanyRepository;
  7. use App\Repository\DealRepository;
  8. use App\Service\ClubHandler;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. #[Route('/bo/deal'name'bo_deal_')]
  15. class DealController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private readonly EntityManagerInterface $em,
  19.         private readonly ClubHandler $clubHandler,
  20.     ) {
  21.     }
  22.     #[Route('/'name'index'methods: ['GET'])]
  23.     public function index(): Response
  24.     {
  25.         $club $this->clubHandler->getCurrentBoClub();
  26.         return $this->render('bo/deal/index.html.twig', [
  27.             'deals' => $club->getDeals(),
  28.         ]);
  29.     }
  30.     #[Route('/new'name'new'methods: ['GET''POST'])]
  31.     public function new(Request $request): Response
  32.     {
  33.         $club $this->clubHandler->getCurrentBoClub();
  34.         $deal = new Deal();
  35.         $form $this->createForm(DealType::class, $deal);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $deal->setClub($club);
  39.             $this->em->persist($deal);
  40.             $this->em->flush();
  41.             return $this->redirectToRoute('bo_deal_show', ['id' => $deal->getId()], Response::HTTP_SEE_OTHER);
  42.         }
  43.         return $this->renderForm('bo/deal/new.html.twig', [
  44.             'club' => $club,
  45.             'form' => $form,
  46.         ]);
  47.     }
  48.     #[Route('/{id}'name'show'options: ['expose' => true], methods: ['GET'])]
  49.     public function show(Deal $deal): Response
  50.     {
  51.         return $this->render('bo/deal/show.html.twig', [
  52.             'deal' => $deal,
  53.         ]);
  54.     }
  55.     #[Route('/{id}/edit'name'edit'methods: ['GET''POST'])]
  56.     public function edit(Request $requestDeal $dealDealRepository $dealRepository): Response
  57.     {
  58.         $club $this->clubHandler->getCurrentBoClub();
  59.         $form $this->createForm(DealType::class, $deal);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted() && $form->isValid()) {
  62.             $deal->setClub($club);
  63.             $this->em->persist($deal);
  64.             $this->em->flush();
  65.             return $this->redirectToRoute('bo_deal_show', ['id' => $deal->getId()], Response::HTTP_SEE_OTHER);
  66.         }
  67.         return $this->renderForm('bo/deal/edit.html.twig', [
  68.             'deal' => $deal,
  69.             'club' => $club,
  70.             'form' => $form,
  71.         ]);
  72.     }
  73.     #[Route('/{id}/gallery'name'gallery'methods: ['GET''POST'])]
  74.     public function gallery(Deal $deal): Response
  75.     {
  76.         return $this->renderForm('bo/deal/gallery.html.twig', [
  77.             'deal' => $deal,
  78.         ]);
  79.     }
  80.     #[Route('/{id}/delete'name'delete'options: ['expose' => true], methods: ['POST'])]
  81.     public function delete(Deal $dealDealRepository $dealRepository): Response
  82.     {
  83.         $dealRepository->remove($dealtrue);
  84.         return $this->json([
  85.             'success' => true,
  86.         ]);
  87.     }
  88.     #[Route('/{id}/fetch-all-companies'name'fetch_all_companies'options: ['expose' => true])]
  89.     public function fetchAllCompanies(Club $clubCompanyRepository $companyRepositoryRequest $request): Response
  90.     {
  91.         $companies $companyRepository->findAllByFti($request->query->get('q'), $club);
  92.         $data = [];
  93.         foreach ($companies as $company) {
  94.             $data[] = [
  95.                 'id' => $company->getId(),
  96.                 'text' => $company->getName(),
  97.             ];
  98.         }
  99.         return $this->json([
  100.             'data' => $data,
  101.         ]);
  102.     }
  103. }