<?php
namespace App\Controller\Bo;
use App\Entity\Club;
use App\Entity\Deal;
use App\Form\Bo\DealType;
use App\Repository\CompanyRepository;
use App\Repository\DealRepository;
use App\Service\ClubHandler;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/bo/deal', name: 'bo_deal_')]
class DealController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly ClubHandler $clubHandler,
) {
}
#[Route('/', name: 'index', methods: ['GET'])]
public function index(): Response
{
$club = $this->clubHandler->getCurrentBoClub();
return $this->render('bo/deal/index.html.twig', [
'deals' => $club->getDeals(),
]);
}
#[Route('/new', name: 'new', methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
$club = $this->clubHandler->getCurrentBoClub();
$deal = new Deal();
$form = $this->createForm(DealType::class, $deal);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$deal->setClub($club);
$this->em->persist($deal);
$this->em->flush();
return $this->redirectToRoute('bo_deal_show', ['id' => $deal->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bo/deal/new.html.twig', [
'club' => $club,
'form' => $form,
]);
}
#[Route('/{id}', name: 'show', options: ['expose' => true], methods: ['GET'])]
public function show(Deal $deal): Response
{
return $this->render('bo/deal/show.html.twig', [
'deal' => $deal,
]);
}
#[Route('/{id}/edit', name: 'edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Deal $deal, DealRepository $dealRepository): Response
{
$club = $this->clubHandler->getCurrentBoClub();
$form = $this->createForm(DealType::class, $deal);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$deal->setClub($club);
$this->em->persist($deal);
$this->em->flush();
return $this->redirectToRoute('bo_deal_show', ['id' => $deal->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bo/deal/edit.html.twig', [
'deal' => $deal,
'club' => $club,
'form' => $form,
]);
}
#[Route('/{id}/gallery', name: 'gallery', methods: ['GET', 'POST'])]
public function gallery(Deal $deal): Response
{
return $this->renderForm('bo/deal/gallery.html.twig', [
'deal' => $deal,
]);
}
#[Route('/{id}/delete', name: 'delete', options: ['expose' => true], methods: ['POST'])]
public function delete(Deal $deal, DealRepository $dealRepository): Response
{
$dealRepository->remove($deal, true);
return $this->json([
'success' => true,
]);
}
#[Route('/{id}/fetch-all-companies', name: 'fetch_all_companies', options: ['expose' => true])]
public function fetchAllCompanies(Club $club, CompanyRepository $companyRepository, Request $request): Response
{
$companies = $companyRepository->findAllByFti($request->query->get('q'), $club);
$data = [];
foreach ($companies as $company) {
$data[] = [
'id' => $company->getId(),
'text' => $company->getName(),
];
}
return $this->json([
'data' => $data,
]);
}
}