<?php
namespace App\Controller\Bo;
use App\Entity\Club;
use App\Entity\ClubManager;
use App\Entity\ClubPresentation;
use App\Entity\User;
use App\Entity\Widget;
use App\Form\Bo\AddUserType;
use App\Form\Bo\ClubPresentationsType;
use App\Form\Bo\ClubType;
use App\Form\Bo\HomeFormType;
use App\Repository\ClubManagerRepository;
use App\Repository\ClubPresentationRepository;
use App\Repository\ClubRepository;
use App\Repository\PhotoRepository;
use App\Repository\UserRepository;
use App\Repository\WidgetRepository;
use App\Service\ClubHandler;
use App\Service\DocumentHandler;
use App\Service\WidgetHandler;
use App\Utils\ImagePrototype;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/bo/cm', name: 'bo_cm_')]
class ClubManagementController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly DocumentHandler $dh,
private readonly ClubHandler $clubHandler,
private readonly UserPasswordHasherInterface $passwordHasher,
private readonly DocumentHandler $documentHandler,
) {
}
#[Route('/infos/edit-photo-club', name: 'infos_edit_photo_club')]
public function editPhoto(): Response
{
$club = $this->clubHandler->getCurrentBoClub();
return $this->render('bo/cm/gallery.html.twig', [
'club' => $club,
]);
}
#[Route('/infos/edit-infos-club', name: 'infos_edit_club')]
public function editInfos(Request $request): Response
{
$club = $this->clubHandler->getCurrentBoClub();
$form = $this->createForm(ClubType::class, $club);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($club);
$this->em->flush();
return $this->redirectToRoute('bo_cm_infos', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bo/cm/edit.html.twig', [
'club' => $club,
'form' => $form
]);
}
#[Route('/infos', name: 'infos')]
public function infos(): Response
{
$club = $this->clubHandler->getCurrentBoClub();
return $this->render('bo/cm/show.html.twig', [
"club" => $club,
]);
}
#[Route('/homepage-management', name: 'homepage_management')]
public function homepage(Request $request): Response
{
$club = $this->clubHandler->getCurrentBoClub();
return $this->renderForm('bo/cm/homepage_management.html.twig', [
'club' => $club,
]);
}
#[Route('/homepage-carousel', name: 'homepage_carousel')]
public function homepageCarousel(Request $request)
{
$club = $this->clubHandler->getCurrentBoClub();
$form = $this->createForm(HomeFormType::class);
$form->handleRequest($request);
$collectionForms = $this->createForm(ClubPresentationsType::class, $club);
$collectionForms->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$clubPre = new ClubPresentation();
$clubPre->setTitle($data['title'] ?? null);
$clubPre->setLink($data['link']);
$clubPre->setMessage($data['message'] ?? null);
$clubPre->setClub($club);
$photo = $this->documentHandler->createPhoto(ImagePrototype::TYPE_CLUB_PRESENTATION, $data['photo']);
$photo->setMain(1);
$this->em->persist($photo);
$clubPre->addPhoto($photo);
$this->em->persist($clubPre);
$this->em->flush();
return $this->redirectToRoute('bo_cm_homepage_carousel', [], Response::HTTP_SEE_OTHER);
}
if ($collectionForms->isSubmitted() && $collectionForms->isValid()) {
$this->em->persist($club);
$this->em->flush();
return $this->redirectToRoute('bo_cm_homepage_carousel', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('bo/cm/carousel_manager.html.twig', [
'club' => $club,
'collectionForms' => $collectionForms,
'form' => $form,
]);
}
#[Route('/homepage-preview', name: 'homepage_preview')]
public function homepagePreview(WidgetHandler $widgetHandler)
{
$club = $this->clubHandler->getCurrentBoClub();
$widgets = $club->getWidgets();
$slides = $club->getClubPresentations();
$sortedWidgets = $widgetHandler->formatWidgetsArray($widgets);
return $this->renderForm('bo/cm/preview.html.twig', [
'club' => $club,
'widgets' => $sortedWidgets,
'slides' => $slides
]);
}
#[Route('/{id}/delete-carousel', name: 'delete_carousel')]
public function deleteCarousel(ClubPresentation $clubPresentation, ClubPresentationRepository $clubPresentationRepository): RedirectResponse
{
$clubPresentationRepository->remove($clubPresentation, true);
return new RedirectResponse($this->generateUrl('bo_cm_homepage_carousel'));
}
#[Route('/{id}/get-widgets', name: 'get_widgets', options: ['expose' => true])]
public function homepageGetData(Club $club): Response
{
$widgets = $club->getWidgets();
$response = [];
for ($i = 0; $i < count($widgets); $i++) {
if ($widgets[$i]->getPhotos()[0]) {
$widget['image'] = $widgets[$i]->getPhotos()[0]->getMediumImageUrl();
} else {
$widget['image'] = "";
}
$widget['positionX'] = $widgets[$i]->getPositionX();
$widget['positionY'] = $widgets[$i]->getPositionY();
$widget['width'] = $widgets[$i]->getWidth();
$widget['height'] = $widgets[$i]->getHeight();
$widget['content'] = $widgets[$i]->getContent();
$widget['title'] = $widgets[$i]->getTitle();
$widget['link'] = $widgets[$i]->getLink();
$widget['id'] = $widgets[$i]->getId();
$response[] = [...$widget];
}
return $this->json([
'data' => $response,
]);
}
#[Route('/{id}/delete-widget', name: 'delete_widget', options: ['expose' => true])]
public function deleteWidget(WidgetRepository $widgetRepository, Widget $widget): Response
{
$widgetRepository->remove($widget);
$this->em->flush();
return $this->json([
'success' => true,
]);
}
#[Route('/{id}/add-widget-image', name: 'add_widget_image', options: ['expose' => true])]
public function addWidgetImage(Widget $widget, Request $request, PhotoRepository $photoRepository): Response
{
$data = $request->getContent();
$dataArray = explode(",", $data);
$base64 = $dataArray[1];
$mimeType = substr($dataArray[0], 11, -7);
if ($mimeType === 'png' || $mimeType === 'jpg' || $mimeType === 'jpeg') {
$oldPhoto = $widget->getPhotos()[0];
if ($oldPhoto) {
$photoRepository->remove($oldPhoto);
}
$photo = $this->dh->createPhoto(
ImagePrototype::TYPE_WIDGET,
$base64,
DocumentHandler::MODE_BASE64
);
$widget->addPhoto($photo);
$this->em->persist($widget);
$this->em->flush();
}
return $this->json([]);
}
#[Route('/{id}/reserve-widget-id', name: 'reserve_widget_id', options: ['expose' => true])]
public function reserveWidgetId(Club $club): Response
{
$widgetToSave = new Widget();
$widgetToSave->setContent("Contenu");
$widgetToSave->setLink("");
$widgetToSave->setTitle("Titre");
$widgetToSave->setWidth(1);
$widgetToSave->setHeight(1);
$widgetToSave->setClub($club);
$widgetToSave->setPositionX(1);
$widgetToSave->setPositionY(1);
$this->em->persist($widgetToSave);
$this->em->flush();
return $this->json([
'id' => $widgetToSave->getId(),
]);
}
#[Route('/{id}/set-widgets', name: 'set_widgets', options: ['expose' => true])]
public function homepageSetData(Request $request, Club $club, WidgetRepository $widgetRepository): Response
{
$data = json_decode($request->getContent(), true);
foreach ($data as $widget) {
if (array_key_exists('id', $widget)) {
$widgetToSave = $widgetRepository->find($widget['id']);
} else {
$widgetToSave = new Widget();
}
foreach ($widget as $key => $value) {
if ($key === "id") {
continue;
}
if (($key === "title" || $key === "link") && strlen($value) > 255) {
$setterMethod = 'set'.ucfirst($key);
$widgetToSave->$setterMethod("");
} else {
if ($key === "content" && strlen($value) > 65535) {
$setterMethod = 'set'.ucfirst($key);
$widgetToSave->$setterMethod("");
} else {
$setterMethod = 'set'.ucfirst($key);
$widgetToSave->$setterMethod(htmlspecialchars($value));
}
}
}
$widgetToSave->setClub($club);
$this->em->persist($widgetToSave);
$this->em->flush();
}
return $this->json([
'success' => $data,
]);
}
#[Route('/theme', name: 'theme')]
public function theme(): Response
{
$club = $this->clubHandler->getCurrentBoClub();
return $this->render('bo/cm/theme.html.twig', [
"club" => $club,
]);
}
#[Route('/{id}/get-colors', name: 'get_colors', options: ['expose' => true])]
public function getColors(Request $request, Club $club): Response
{
$response = $club->getTheme();
return $this->json([
'success' => $response,
]);
}
#[Route('/{id}/save-colors', name: 'save_colors', options: ['expose' => true])]
public function saveColors(Request $request, Club $club): Response
{
$data = json_decode($request->getContent(), true);
$club->setTheme($data["body"]);
$this->em->persist($club);
$this->em->flush();
return $this->json([
]);
}
/**
* @throws RuntimeException
* @throws LogicException
*/
#[Route('/admins', name: 'admins')]
public function admins(Request $request, ClubManagerRepository $clubManagerRepository, UserRepository $userRepository): Response
{
$club = $this->clubHandler->getCurrentBoClub();
$managers = $clubManagerRepository->findManagersByClub($club);
$form = $this->createForm(AddUserType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$ifUser = $userRepository->findBy(['email' => $user->getEmail()]);
$userNotExist = true;
if ($ifUser) {
$userNotExist = false;
}
if ($userNotExist) {
$user->setAcceptNotification(0);
$user->setPassword($this->passwordHasher->hashPassword($user, $user->getPassword()));
$this->em->persist($user);
$this->em->flush();
$clubManager = new ClubManager();
$clubManager->setClub($club);
$clubManager->setUser($user);
$this->em->persist($clubManager);
$this->em->flush();
return $this->redirectToRoute('bo_cm_admins', [], Response::HTTP_SEE_OTHER);
} else {
$error = new FormError('Cet email existe déjà !');
$form->addError($error);
}
$this->addFlash('success', "L'administrateur a bien été ajouté.");
}
return $this->render('bo/cm/admins.html.twig', [
'managers' => $managers,
'club' => $club,
'form' => $form->createView(),
]);
}
#[Route('{id}/add-admin', name: 'add_admin', options: ['expose' => true])]
public function addAdmin(User $user): Response
{
$club = $this->clubHandler->getCurrentBoClub();
$ifClubManager = $this->em->getRepository(ClubManager::class)->findBy(['club' => $club, 'user' => $user]);
if (!$ifClubManager) {
$clubManager = new ClubManager();
$clubManager->setClub($club);
$clubManager->setUser($user);
$this->em->persist($clubManager);
$this->em->flush();
}
return $this->json([
'success' => true,
]);
}
#[Route('/{id}/delete-admin', name: 'delete_admin', options: ['expose' => true], methods: ['POST'])]
public function deleteAdmin(ClubManagerRepository $clubManagerRepository, User $user): Response
{
$club = $this->clubHandler->getCurrentBoClub();
$clubManager = $clubManagerRepository->findOneBy(['user' => $user, 'club' => $club]);
$clubManagerRepository->remove($clubManager, true);
return $this->json([
'success' => true,
]);
}
#[Route('/delete-logo', name: 'delete_logo', options: ['expose' => true])]
public function deletePhoto(Request $request, PhotoRepository $photoRepository): response
{
$data = json_decode($request->getContent(), true);
$photo = $photoRepository->findOneBy(['club' => $data["query"]]);
$photoRepository->remove($photo, true);
return $this->json([
]);
}
#[Route('/club-menu', name: 'clubs_menu')]
public function clubsMenu(ClubRepository $clubRepository): Response
{
$user = $this->getUser();
return $this->render('bo/layout/_clubs_menu.html.twig', [
'clubs' => $clubRepository->findManagedClubsByUser($user),
'currentBoClub' => $this->clubHandler->getCurrentBoClub(),
]);
}
#[Route('/set-current-bo-club/{boClubId}', name: 'set_current_bo_club')]
public function setCurrentBoClub(int $boClubId): Response
{
$this->clubHandler->setCurrentBoClub($boClubId);
return $this->redirectToRoute('bo_home');
}
#[Route('/{id}/update-preview', name: 'update_preview', options: ['expose' => true])]
public function updatePreview(Club $club): Response
{
$widgets = $club->getWidgets();
$clubPresentation = $club->getClubPresentations()->getValues();
return $this->json([
'widgets' => $widgets,
'carousel' => array_map(static fn($cp) => [
'id' => $cp->getId(),
'title' => $cp->getTitle(),
'message' => $cp->getMessage(),
'link' => $cp->getLink(),
'mainPhoto' => $cp->getImageUrl(),
], $clubPresentation),
]);
}
}