<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Address;
use OlaSoft\Common;
use App\Entity\Subscribers;
use App\Entity\Services;
use App\Entity\Articles;
use App\Entity\ArticlesCategories;
use App\Entity\Structures;
use App\Entity\StructuresCategories;
use App\Entity\Resources;
use App\Entity\ResourcesPhases;
use App\Entity\ResourcesCategories;
use App\Entity\Pages;
use App\Entity\Banners;
use App\Entity\Videos;
use App\Entity\Messages;
use App\Form\MessagesType;
use App\Form\SubscribersType;
use App\Entity\Members;
use App\Entity\Formations;
use App\Entity\Registrations;
use App\Form\RegistrationsType;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index()
{
$em = $this->getDoctrine()->getManager();
$banner = $em->getRepository(Banners::class)->findOneBy(['isEnabled'=>1], ['id'=>'desc']);
$articles = $em->getRepository(Articles::class)->findBy(['isEnabled'=>1], ['date'=>'desc'], 3);
$formations = $em->getRepository(Formations::class)->findBy(['isEnabled'=>1]);
// $docs = $em->getRepository(Resources::class)->findBy(['isEnabled' => 1], ['id'=>'desc'], 3);
// $videos = $em->getRepository(Videos::class)->findBy(['isEnabled'=>1], ['id'=>'desc'], 3);
$page = $em->getRepository(Pages::class)->findOneBySlug('a-propos');
return $this->render('Default/index.html.twig',[
'banner' => $banner,
'articles' => $articles,
// 'videos' => $videos,
// 'docs' => $docs,
'page' => $page,
'formations'=>$formations,
]);
}
/**
* @Route("/sitemap.xml", name="sitemap", defaults={"xml"=true, "_format"="xml"})
*/
public function menu($activeMenu = null, $isFooter = false, $xml = false)
{
$em = $this->getDoctrine()->getManager();
$formations = $em->getRepository(Formations::class)->findBy(['isEnabled'=>1]);
$structuresCats = $em->getRepository(StructuresCategories::class)->findBy(['isEnabled'=>1]);
$articlesCats = $em->getRepository(ArticlesCategories::class)->findAll();
$resourcesCats = $em->getRepository(ResourcesCategories::class)->findAll();
return $this->render($xml ? 'Default/sitemap.xml.twig' : ($isFooter ? 'Default/footer.html.twig' : 'Default/menu.html.twig'),[
'formations'=>$formations,
'structuresCats'=>$structuresCats,
'articlesCats'=>$articlesCats,
'resourcesCats'=>$resourcesCats,
'activeMenu'=>$activeMenu,
]);
}
/**
* @Route("/recherche/", name="search")
*/
public function search(Request $request, Sectors $sector = null)
{
$keywords = $request->query->get('keywords');
$em = $this->getDoctrine()->getManager();
$articles = $em->getRepository(Articles::class)->createQueryBuilder('a')
->leftjoin('a.banner','b')
->leftjoin('a.category','c')
->leftjoin('a.type','t')
->leftjoin('a.album','aa')
->leftjoin('a.biblio','ab')
->leftjoin('a.sector','s')
->orderBy('a.date','DESC')
->andWhere("a.date <= '".(new \DateTime())->format('Y-m-d H:i:s')."'")
->andWhere('a.isEnabled = 1')
->andWhere('(a.title like :k or a.description like :k or c.name like :k or s.label like :k )')
->setParameter('k','%'.$keywords.'%')
->getQuery()->getResult();
$docs = $em->getRepository(Resources::class)->createQueryBuilder('o')
->leftjoin('o.category','c')
->leftjoin('o.sector','s')
->where('o.title like :k or o.description like :k or c.label like :k or s.label like :k ')
->andWhere('o.isEnabled = 1')
->setParameter('k','%'.$keywords.'%')
->getQuery()->getResult();
$pages = $em->getRepository(Pages::class)->createQueryBuilder('o')
->where('o.title like :k or o.description like :k or o.titleAlt like :k or o.pictureTitle like :k or o.pictureSubtitle like :k or o.content like :k')
->andWhere('o.isEnabled = 1')
->setParameter('k','%'.$keywords.'%')
->getQuery()->getResult();
$structs = $em->getRepository(Structures::class)->createQueryBuilder('o')
->leftjoin('o.sector','s')
->where('o.name like :k or o.abbreviation like :k or s.label like :k or o.director like :k or o.description like :k or o.content like :k or o.address like :k or o.website like :k')
->andWhere('o.isEnabled = 1')
->setParameter('k','%'.$keywords.'%')
->getQuery()->getResult();
return $this->render('Default/search.html.twig',[
'articles'=>$articles,
'docs'=>$docs,
'pages'=>$pages,
'keywords'=>$keywords
]);
}
/**
* @Route("/formations/", name="formations")
*/
public function formations(Request $request)
{
$em = $this->getDoctrine()->getManager();
$list = $em->getRepository(Formations::class)->findBy(['isEnabled'=>1]);
return $this->render('Default/formations.html.twig',[
'list'=>$list
]);
}
/**
* @Route("/formation/{id}/{slug}/", name="formation", requirements={"id": "\d+"})
* @Route("/formation/{id}/", requirements={"id": "\d+"})
*/
public function project(Formations $item, $slug = null)
{
return $this->render('Default/formation.html.twig',[
'item'=>$item,
]);
}
/**
* @Route("/articles/", name="articles")
* @Route("/articles/{offset}/", name="articles-page", requirements={"offset": "\d+"})
* @Route("/articles/{cat}/", name="articles-cat")
* @Route("/articles/{cat}/{offset}/", name="articles-cat-page", requirements={"offset": "\d+"})
*/
public function articles($offset = 1, $cat = null)
{
$offset--;
$limit = 30;
$of = $offset*$limit;
$params = [];
$em = $this->getDoctrine()->getManager();
$cat = $cat ? $em->getRepository(ArticlesCategories::class)->findOneBySlug($cat) : null;
$list = $em->getRepository(Articles::class)->createQueryBuilder('a')
->leftjoin('a.banner','b')
->leftjoin('a.category','c')
->leftjoin('a.type','t')
->leftjoin('a.album','aa')
->leftjoin('a.biblio','ab')
->orderBy('a.date','DESC')
->andWhere("a.date <= '".(new \DateTime())->format('Y-m-d H:i:s')."'")
->andWhere('a.isEnabled = 1')
;
if($cat) {
$list = $list->andWhere('c.id = '.$cat->getId());
$params['cat'] = $cat->getSlug();
}
$count = $list->select('count(distinct(a.id))')->getQuery()->getResult()[0][1];
$list = $list->select('a')
->setMaxResults($limit)
->setFirstResult($of)
->getQuery()->getResult();
return $this->render('Default/articles.html.twig',[
'articles'=>$list,
'cat'=>$cat,
'count'=>$count,
'table_limit'=>$limit,
'offset'=>$offset,
'params'=>$params
]);
}
/**
* @Route("/article/{id}/{slug}/", name="article", requirements={"id": "\d+"})
* @Route("/article/{id}/", requirements={"id": "\d+"})
*/
public function article(Articles $article, $slug = null)
{
if(!$article->getIsEnabled() && !$this->isGranted('ROLE_USER'))
return $this->redirectToRoute('articles');
if($article->getURL())
return $this->redirect($article->getURL());
$em = $this->getDoctrine()->getManager();
$list = $em->getRepository(Articles::class)->createQueryBuilder('a')
->leftjoin('a.banner','b')
->leftjoin('a.category','c')
->leftjoin('a.type','t')
->leftjoin('a.album','aa')
->leftjoin('a.biblio','ab')
->orderBy('a.date','DESC')
->andWhere('a.id <> '.$article->getId())
->andWhere("a.date <= '".(new \DateTime())->format('Y-m-d H:i:s')."'")
->andWhere('a.isEnabled = 1')
->setMaxResults(4)
->getQuery()
->getResult();
return $this->render('Default/article.html.twig',[
'article'=>$article,
'list'=>$list,
]);
}
/**
* @Route("/videos/", name="videos")
*/
public function videos()
{
$em = $this->getDoctrine()->getManager();
$videos = $em->getRepository(Videos::class)->findBy(['isEnabled'=>true], ['id'=>'desc']);
return $this->render('Default/videos.html.twig',[
'videos'=>$videos
]);
}
/**
* @Route("/video/{id}/", name="video", requirements={"id": "\d+"})
*/
public function video(Videos $video)
{
return $this->render('Default/video.html.twig',[
'video'=>$video
]);
}
/**
* @Route("/conseil-pedagogique/", name="advisors", defaults={"title":"Conseil pédagogique"})
* @Route("/comite-direction/", name="members", defaults={"title":"Comité de direction"})
* @Route("/conseil-administration/", name="administrators", defaults={"title":"Conseil d'administration"})
*/
public function members($title)
{
$em = $this->getDoctrine()->getManager();
$list = $em->getRepository(Members::class)->findBy(['isEnabled' => 1], ['id'=>'asc']);
return $this->render('Default/members.html.twig',[
'list'=>$list,
'title'=>$title
]);
}
/**
* @Route("/formateurs/", name="teachers")
*/
public function teachers()
{
$em = $this->getDoctrine()->getManager();
$list = $em->getRepository(Members::class)->findBy(['isEnabled' => 1], ['id'=>'asc']);
return $this->render('Default/teachers.html.twig',[
'list'=>$list,
]);
}
/**
* @Route("/directions/", name="structures")
* @Route("/directions/{slug}/", name="structures-cat")
*/
public function structures(StructuresCategories $cat = null)
{
$em = $this->getDoctrine()->getManager();
$filter = ['isEnabled' => 1];
if($cat) $filter['category'] = $cat->getId();
$list = $em->getRepository(Structures::class)->findBy($filter, ['id'=>'asc']);
$groups = $cat ? null : $em->getRepository(StructuresCategories::class)->findBy(['isEnabled'=>1], ['id'=>'asc']);
return $this->render('Default/links.html.twig',[
'list'=>$list,
'groups'=>$groups,
'cat'=>$cat,
]);
}
/**
* @Route("/structure/{id}/{slug}/", name="structure")
*/
public function structure(Structures $struct)
{
return $this->render('Default/structure.html.twig',[
'struct'=>$struct,
]);
}
/**
* @Route("/documents/", name="resources")
* @Route("/documents/{slug}/", name="resources-cat")
*/
public function resources(Request $request, ResourcesCategories $cat = null)
{
$keyword = $request->get('q');
$offset = $request->get('offset') ?? 1;
$offset--;
$limit = 50;
$of = $offset*$limit;
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository(Resources::class)->createQueryBuilder('r')
->leftjoin('r.category','c')
->where('r.isEnabled = 1')
->orderBy('r.date','desc');
if($cat) $query = $query->andWhere('c.id = '.$cat->getId())->addOrderBy('r.date','desc');
if($keyword)
$query = $query->andWhere('(r.title like :k or r.description like :k or c.label like :k)')
->setParameter('k','%'.$keyword.'%');
$count = $query->select('count(r.id)')->getQuery()->getResult()[0][1];
$list = $query->select('r')->setMaxResults($limit)->setFirstResult($of)->getQuery()->getResult();
return $this->render('Default/resources.html.twig',[
'cat'=>$cat,
'list'=>$list,
'count'=>$count,
'table_limit'=>$limit,
'offset'=>$offset,
'keyword'=>$keyword
]);
}
/**
* @Route("/a-propos/", name="about", defaults={"slug":"a-propos"})
* @Route("/mot-directeur/", name="word", defaults={"slug":"mot-directeur"})
* @Route("/attributions/", name="attributions", defaults={"slug":"attributions"})
* @Route("/organisation/", name="organisation", defaults={"slug":"organisation"})
* @Route("/reglement-interieur/", name="statuses", defaults={"slug":"statuts-reglement-interieur"})
* @Route("/commissariat-comptes/", name="auditors", defaults={"slug":"commissariat-comptes"})
* @Route("/page/{slug}/", name="page")
*/
public function page(Pages $page = null, $menu = null, $subtitle = null)
{
return $this->render('Default/page.html.twig',[
'page'=>$page,
'menu'=>$menu,
'subtitle'=>$subtitle,
]);
}
/**
* @Route("/contacts/", name="contacts")
*/
public function contact(MailerInterface $mailer)
{
$request = $this->get('request_stack')->getCurrentRequest();
$em = $this->getDoctrine()->getManager();
$msg = new Messages();
$form = $this->createForm(MessagesType::class, $msg);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$browser = Common::getBrowser(null, true);
$msg->setIP($request->getClientIP());
$msg->setBrowser($browser['name']);
$msg->setOS($browser['platform']);
$msg->setDate(new \DateTime);
$em->persist($msg);
$em->flush();
$author = $msg->getFName().' '.$msg->getLName();
$message= "<b>Nom et prénoms : </b>".$author.
"<br/><b>Adresse électronique : </b>".$msg->getEmail().
"<br/><b>Object : </b>".$msg->getObject().
"<br/><b>Message :</b><p style='border-left: 3px solid #ccc; padding-left: 10px; padding-top: 5px; padding-bottom: 5px;'>".nl2br($msg->getContent())."</p>";
$email = (new TemplatedEmail())
->from('noreply@'.$_SERVER['HTTP_HOST'])
->to(Common::params('contacts')['email'])
->replyTo(new Address($msg->getEmail(),$author))
->priority(Email::PRIORITY_HIGH)
->subject(Common::params('sitename').' - '.$msg->getObject())
->html($message);
$mailer->send($email);
$this->addFlash("notice","Votre message a été envoyé avec succès.");
return $this->redirectToRoute('contacts');
}
return $this->render('Default/contacts.html.twig',['form'=>$form->createView()]);
}
/**
* @Route("/inscription/", name="subscribers", defaults={"isEmbed"=false})
*/
public function subscribers(Request $request, $isEmbed = true)
{
$em = $this->getDoctrine()->getManager();
$subs = new Subscribers();
$form = $this->createForm(SubscribersType::class, $subs);
$form->handleRequest($request);
if ($form->isSubmitted()){
if($form->isValid()){
$exist = $em->getRepository(Subscribers::class)->findOneByEmail($subs->getEmail());
if(!$exist){
$browser=Common::getBrowser(null, true);
$subs->setIP($request->getClientIP());
$subs->setBrowser($browser['name']);
$subs->setOS($browser['platform']);
$subs->setDate(new \DateTime);
$em->persist($subs);
$em->flush();
}
}
$this->addFlash('notice','Félicitations.<br>Votre inscription a été effectuée avec succès.');
return $this->redirectToRoute('home');
}
return $this->render($isEmbed ? 'Default/subscribers-form.html.twig' : 'Default/subscribers.html.twig',['form'=>$form->createView()]);
}
}