<?php
namespace App\Controller\Admin;
use App\Entity\Information;
use App\Form\Admin\InformationType;
use App\Repository\InformationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
#[Route('/admin/information', name: 'hs_training_center_information_')]
class InformationController extends AbstractController
{
public function __construct(
private readonly InformationRepository $informationRepository,
private readonly RequestStack $requestStack,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly RequestContext $requestContext
){
}
#[Route('/', name: 'index', options:['expose'=> 'true'], methods: ['GET'])]
public function index(): Response
{
$items = $this->informationRepository->findAll();
return $this->render('Admin/information/index.html.twig', [
'items' => $items,
'genre'=>'info_institute'
]);
}
#[Route('/new', name: 'new', options:['expose'=> 'true'], methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
$items=$this->informationRepository->findAll();
$information = new Information();
return $this->getForm($information, $request, $items);
}
#[Route('/{id}', name: 'show', options:['expose'=> 'true'], methods: ['GET'])]
public function show(Information $information): Response
{
return $this->render('Admin/information/show.html.twig', [
'information' => $information,
]);
}
#[Route('/{id}/edit', name: 'edit', options:['expose'=> 'true'], methods: ['GET', 'POST'])]
public function edit(Information $information, Request $request): Response
{
$items=$this->informationRepository->findAll();
return $this->getForm($information, $request, $items);
}
#[Route('/remove/{id}', name: 'remove', options: ['expose'=> 'true'])]
public function delete(Information $information): Response
{
$this->informationRepository->remove($information, true);
$this->addFlash('success','Information deleted with success.');
return $this->redirectToRoute('hs_training_center_information_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/remove_prompt/{ids}', name: 'remove_prompt', options: ['expose'=> 'true'])]
public function deletePromptAction($ids, Request $request){
$items=explode("|",(string) $ids);
if (empty($items)) {
$this->addFlash('warning','No items selected !!!! ');
return $this->redirectToRoute('hs_training_center_information_index');
}
$count=0;
foreach ($items as $item){
/** @var Information $item */
$item=$this->informationRepository->find($item);
if (!empty($item)) {
$this->informationRepository->remove($item, true);
$count++;
}
}
$this->addFlash('success', $count.' items successfully deleted.');
return $this->redirectToRoute('hs_training_center_information_index');
}
#[Route('/statut/{id}', name: 'statut', options:['expose'=> 'true'])]
public function statutAction(Information $information)
{
if($information->isPublished()) {
$information->setPublished(false);
} else {
$information->setPublished(true);
}
$button=$information->getButtonEnable();
$this->informationRepository->save($information, true);
return new Response($button);
}
/**
* @return RedirectResponse|Response
*/
public function getForm(Information $information, Request $request, array $items): Response|RedirectResponse
{
$form = $this->createForm(InformationType::class, $information);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->informationRepository->save($information, true);
$this->addFlash('success', 'Successfully executed operation.');
if ($form->get('SaveList')->isClicked()) return $this->redirectToRoute('hs_training_center_information_index', [], Response::HTTP_SEE_OTHER);
if ($form->get('SaveNew')->isClicked()) return $this->redirectToRoute('hs_training_center_information_new', [], Response::HTTP_SEE_OTHER);
return $this->redirectToRoute('hs_training_center_information_edit', ['id' => $information->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('Admin/information/form.html.twig', [
'item' => $information,
'form' => $form,
'type' => 'New',
'items' => $items,
'genre' => 'info_institute'
]);
}
#[Route('/rest/{groupe}', name: 'rest', options:['expose'=> 'true'])]
public function rest(string $groupe){
$list=[
'Diplome'=>[
'sitting_diplome_auto_num' => "Numéro d'Autorisation",
'sitting_diplome_auto_date' => "Date d'Autorisation",
'sitting_diplome_acc_num' => "Numéro d'Accréditation",
'sitting_diplome_acc_date' => "Date d'Accréditation",
'sitting_diplome_pve_date' => "Date du procès verbal",
'sitting_assurance_police_num' => "N° POLICE D’ASSURANCE",
'sitting_assurance_start_date' => "Date de début",
'sitting_assurance_end_date' => "Date de fin",
],
'Assurance'=>[
'sitting_assurance_police_num' => "N° POLICE D’ASSURANCE",
'sitting_assurance_start_date' => "Date de début",
'sitting_assurance_end_date' => "Date de fin",
],
'Informations'=>[
'sitting_institute_name' => 'Nom de l’Institut',
'sitting_institute_director' => 'Nom du Directeur',
'sitting_institute_num_docs' => 'Numéro de document',
],
'Coordonnées'=>[
'sitting_institute_phone1' => 'Téléphone 1',
'sitting_institute_phone2' => 'Téléphone 2',
'sitting_institute_email' => 'E-mail',
'sitting_institute_adress' => 'Adresse',
'sitting_institute_website' => 'Site Web',
],
'Réseaux sociaux'=>[
'sitting_social_link_facebook' => 'Facebook',
'sitting_social_link_twitter' => 'Twitter',
'sitting_social_link_plus' => 'Google +',
'sitting_social_link_instagram' => 'Instagram',
'sitting_social_link_youtube' => 'Youtube',
],
'Accueil'=>[
'sitting_accueil_01' => 'Soutien',
'sitting_accueil_02' => 'Langue',
'sitting_accueil_03' => 'Contest'
],
];
if ($groupe=='all'){
foreach ($list as $key1 => $value1){
$groupe=$key1;
$infos=$value1;
foreach ($infos as $key => $value){
$information=$this->informationRepository->findOneBy(['slug'=>$key]);
if (empty($information)) {
$information = new Information();
$information->setSlug($key);
}
$information->setCategory($groupe);
$information->setType('Autre');
$information->setName($value);
$information->setVal('');
$this->informationRepository->save($information, true);
}
}
}
$infos=$list[$groupe]??[];
foreach ($infos as $key => $value){
$information=$this->informationRepository->findOneBy(['slug'=>$key]);
if (empty($information)) {
$information = new Information();
$information->setCategory($groupe);
$information->setType('Autre');
$information->setName($value);
$information->setSlug($key);
}
$information->setVal('');
$this->informationRepository->save($information, true);
}
return $this->redirectToRoute('hs_training_center_information_index');
}
public function redirectToPreviousRoute()
{
$referer = $this->requestStack->getMainRequest()->headers->get('referer');
// Modifiez le contexte de la requête pour générer des URL relatives
$this->requestContext->fromRequest($this->requestStack->getMainRequest());
$this->urlGenerator->setContext($this->requestContext);
// Générez une URL absolue à partir de l'URL de référence
$url = $this->urlGenerator->generate($referer);
return new RedirectResponse($url);
}
public function widgetAction($widget, $pole='gestion')
{
$infoParms = $this->informationRepository->findByCategory('Informations');
$coorParms = $this->informationRepository->findByCategory('Coordonnées');
$info_soc = [];
foreach ($infoParms as $parm) {
$info_soc[$parm->getSlug()] = $parm->getVal();
}
foreach ($coorParms as $parm) {
$info_soc[$parm->getSlug()] = $parm->getVal();
}
return $this->render(
'Admin/Widget/' . $widget . '.html.twig',
['parms' => $info_soc, 'pole' => $pole]
);
}
}