<?php
namespace App\Controller;
use App\Entity\CBRCArticle;
use App\Entity\CBRCArticleCategory;
use App\Entity\CBRCPageContent;
use App\Entity\CBRCPageContentUpdate;
use App\Entity\CBRCUser;
use App\Form\Type\CBRCUserDescriptionType;
use App\Form\Type\CBRCUserType;
use App\Form\Type\ContactType;
use App\Form\Type\ModifyEmailType;
use App\Form\Type\ModifyPasswordType;
use App\Form\Type\PageContentType;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
class HomePagesController extends AbstractController
{
/**
* @Route ("/admin/pagecontent/{pagename}", name="admin_pagecontent")
*/
public function ShowAdminPageContent(string $pagename, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user=$this->getUser();
$pagecontent = $this->GetOrCreatePageContent("$pagename");
$newpagecontentupdate = new CBRCPageContentUpdate();
$newpagecontentupdate->setUser($user);
$newpagecontentupdate->setPage($pagecontent);
$newpagecontentupdate->setContent($pagecontent->getContent());
$formcontent = $this->createForm(PageContentType::class, $newpagecontentupdate);
$formcontent->handleRequest($request);
if($formcontent->isSubmitted() && $formcontent->isValid())
{
$pagecontent->setContent($newpagecontentupdate->getContent());
$em=$this->getDoctrine()->getManager();
$em->persist($pagecontent);
$em->persist($newpagecontentupdate);
$em->flush();
return $this->redirectToRoute('home_'.$pagename);
}
if ($formcontent->isSubmitted() && !$formcontent->isValid()) {
foreach ($formcontent->getErrors(true) as $error)
$this->addFlash('danger', "(" . $error->getOrigin()->getName() . ") " . $error->getMessage());
$formcontent->clearErrors(true);
}
return $this->render('adminpagecontent.html.twig', [
'formpagecontent' =>$formcontent->createView(),
'pagename' => $pagename,
]);
}
/**
* @Route ("/tournois", name="home_tournois")
*/
public function ShowTournois(Request $request)
{
$pagecontent = $this->GetOrCreatePageContent('tournois');
return $this->render('hometournois.html.twig',[
'pagecontent' => $pagecontent,
]);
}
/**
* @Route ("/cours", name="home_cours")
*/
public function ShowCours(Request $request)
{
$pagecontent = $this->GetOrCreatePageContent('cours');
return $this->render('homecours.html.twig',[
'pagecontent' => $pagecontent,
]);
}
/**
* @Route ("/competitions", name="home_competitions")
*/
public function ShowCompetitions(Request $request)
{
$pagecontent = $this->GetOrCreatePageContent('competitions');
return $this->render('homecomitecalendar.html.twig',[
'pagecontent' => $pagecontent,
]);
}
/**
* @Route ("/utilitiares", name="home_utilitaires")
*/
public function ShowUtilitiares(Request $request)
{
$pagecontent = $this->GetOrCreatePageContent('utilitaires');
return $this->render('homeutilitaires.html.twig',[
'pagecontent' => $pagecontent,
]);
}
/**
* @Route ("/partenaires", name="home_partenaires")
*/
public function ShowPartenaires(Request $request)
{
$pagecontent = $this->GetOrCreatePageContent('partenaires');
return $this->render('homepartenaires.html.twig',[
'pagecontent' => $pagecontent,
]);
}
/**
* @Route ("/festival", name="home_festival")
*/
public function ShowFestival(Request $request)
{
$pagecontent = $this->GetOrCreatePageContent('festival');
return $this->render('homefestival.html.twig',[
'pagecontent' => $pagecontent,
]);
}
private function GetOrCreatePageContent(string $name)
{
$pagecontent = $this->getDoctrine()->getRepository(CBRCPageContent::class)->findOneBy(['name'=>$name]);
if(!$pagecontent)
{
//create the page
$pagecontent = new CBRCPageContent();
$pagecontent->setName($name);
//persist the page
$em = $this->getDoctrine()->getManager();
$em->persist($pagecontent);
$em->flush();
}
return $pagecontent;
}
}