<?php
namespace App\Controller;
use App\Entity\Activite;
use App\Entity\Category;
use App\Entity\Extension;
use App\Entity\Cgv;
use App\Entity\InfoLine;
use App\Entity\Legal;
use App\Entity\Promote;
use App\Entity\Rgpd;
use App\Entity\Slider;
use App\Repository\InfoLineRepository;
use App\Repository\PromoteRepository;
use App\Repository\SliderRepository;
use App\Repository\ProductRepository;
use App\Entity\Cart;
use App\Service\Order;
use App\Service\Pwa;
use App\Service\Sitemap;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
use App\Entity\User;
use Symfony\Component\VarDumper\VarDumper;
class AppController extends AbstractController
{
/**
* @Route("/", name="homepage", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function indexAction(Request $request, Pwa $pwa, EntityManagerInterface $em)
{
$categories = $em->getRepository('App:Category')->findBy(["parent" => null, "deletedAt" => null], ['sort' => 'ASC']);
return $this->render('app/pages/index.html.twig', [
'categories' => $categories,
]);
}
public function renderMenuAction(Request $request, EntityManagerInterface $em)
{
$categories = $em->getRepository('App:Category')->findBy(["parent" => null, "deletedAt" => null], ['sort' => 'ASC']);
$pages = $em->getRepository('App:Page')->findBy(["deletedAt" => null], ['sort' => 'ASC']);
return $this->render('app/_inc/_menu.html.twig', ['categories' => $categories, 'pages' => $pages]);
}
public function renderFooterAction(Request $req, EntityManagerInterface $em)
{
$categories = $em->getRepository('App:Category')->findBy(["parent" => null, "deletedAt" => null], ['sort' => 'ASC']);
$pages = $em->getRepository('App:Page')->findBy(["deletedAt" => null], ['sort' => 'ASC']);
return $this->render('app/_inc/_footer.html.twig', ['categories' => $categories, 'pages' => $pages,'req'=>$req,'locales'=>$this->getParameter('a2lix_translation_form.locales')]);
}
public function renderCartAction(Request $request, Order $orderService, EntityManagerInterface $em)
{
// $repoCart = $em->getRepository('App:Cart');
// $orderService->updateCart($request);
// /** @var Cart $cart */
// $cart = $repoCart->findOneBy(array(
// "session" => User::getSessionId($request),
// "ordered" => false
// ));
// if ($cart instanceof Cart) {
// $cartProducts = $cart->getCartProducts();
// } else {
// $cartProducts = null;
// }
return $this->render('app/_inc/_cart.html.twig', []);
}
public function renderSliderAction(EntityManagerInterface $em)
{
/** @var SliderRepository $repoSlider */
$repoSlider = $em->getRepository('App:Slider');
$slider = $repoSlider->find(1);
if (!$slider instanceof Slider) {
$slider = new Slider();
$em->persist($slider);
$em->flush();
}
return $this->render("app/_inc/_slider.html.twig", array(
'slides' => $slider->getSlides()
));
}
public function renderInfoAction(EntityManagerInterface $em)
{
/** @var InfoLineRepository $repoInfoLine */
$repoInfoLine = $em->getRepository('App:InfoLine');
$infoLine = $repoInfoLine->find(1);
if (!$infoLine instanceof InfoLine) {
$infoLine = new InfoLine();
$em->persist($infoLine);
$em->flush();
}
return $this->render("app/_inc/_info.html.twig", array(
'infos' => $infoLine->getInfos()
));
}
public function renderHighlightAction(Request $request, EntityManagerInterface $em)
{
/** @var ProductRepository $productRepository */
$productRepository = $em->getRepository('App:Product');
$newProducts = $productRepository->newProducts();
$promoProducts = $productRepository->promoProducts();
return $this->render('app/_inc/_highlight.html.twig', [
'newProducts' => $newProducts,
'promoProducts' => $promoProducts,
]);
}
public function renderPromoteAction(Request $request, EntityManagerInterface $em)
{
/** @var PromoteRepository $repoPromote */
$repoPromote = $em->getRepository('App:Promote');
$promote = $repoPromote->find(1);
if (!$promote instanceof Promote) {
$promote = new Promote();
$em->persist($promote);
$em->flush();
}
return $this->render("app/_inc/_promote.html.twig", array(
'cards' => $promote->getCards()
));
}
/**
* @Route("/search", name="search", methods={"POST"}, options={"sitemap": false})
*/
public function searchAction(Request $request, EntityManagerInterface $em)
{
$repoProduct = $em->getRepository('App:Product');
$products = $repoProduct->filterProduct($request->getLocale(), null, $request->get("search"), [], "name", [], 1, 10);
$html = $this->render('app/_inc/_res.html.twig', ['products' => $products["products"]])->getContent();
return new JsonResponse(array('html' => $html));
}
/**
* @Route("/mentions-legales", name="legal", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function legalAction(Request $request)
{
return $this->render('app/legal/legal.html.twig');
}
/**
* @Route("/protection-des-donnees", name="rgpd", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function rgpdAction(Request $request)
{
return $this->render('app/legal/rgpd.html.twig');
}
/**
* @Route("activite/{id}/show", name="activiteShow", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function activiteShowAction(Request $request, Category $category)
{
$realisations = $category->getActivites();
return $this->render('app/activites/activite_show.html.twig', ['category' => $category, 'realisations' => $realisations]);
}
/**
* @Route("/sablage", name="sablage", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function sablageAction(Request $request)
{
return $this->render('app/activites/sablage.html.twig');
}
/**
* @Route("/hydrosablage", name="hydrosablage", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function hydrosablageAction(Request $request)
{
return $this->render('app/activites/hydrosablage.html.twig');
}
/**
* @Route("/aerogommage", name="aerogommage", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function aerogommageAction(Request $request)
{
return $this->render('app/activites/aerogommage.html.twig');
}
/**
* @Route("/hydrogommage", name="hydrogommage", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function hydrogommageAction(Request $request)
{
return $this->render('app/activites/hydrogommage.html.twig');
}
/**
* @Route("/demolition-interieure", name="demolition", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function demolitionAction(Request $request)
{
return $this->render('app/activites/demolition.html.twig');
}
/**
* @Route("/joints-de-pierre", name="joints", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function jointsAction(Request $request)
{
return $this->render('app/activites/joints.html.twig');
}
/**
* @Route("/nos-realisations", name="realisations", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function realisationsAction(Request $request, EntityManagerInterface $em)
{
$realisations = $em->getRepository('App:Activite')->findBy(['deletedAt'=>null]);
return $this->render('app/pages/realist.html.twig', ['realisations' => $realisations]);
}
/**
* @Route("realisation/{id}/show", name="realisationshow", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function realisationshowAction(Activite $realisation, Request $request, EntityManagerInterface $em)
{
// $other = $realisation->getCategory()->getActivites();
$qb = $em->createQueryBuilder();
$qb->select('a')
->from('App:Activite', 'a')
->where('a.deletedAt IS NULL')
->andWhere('a.category_rea = :category')
->setParameter('category', $realisation->getCategoryRea())
->orderBy('a.id', 'DESC');
$other = $qb->getQuery()->getResult();
return $this->render('app/pages/realisationshow.html.twig', ['realisation' => $realisation, 'other' => $other]);
}
/**
* @Route("{id}/nos-activites", name="activiteslist", options={"sitemap": false, "priority": 1, "changefreq": "yearly"})
*/
public function activiteslistAction(Category $category, Request $request)
{
return $this->render('app/activites/activiteslist.html.twig', ['category' => $category]);
}
/**
* @Route("/conditions-generales-de-vente", name="cgv", options={"sitemap": false, "priority": 1, "changefreq": "yearly"})
*/
public function cgvAction(Request $request)
{
return $this->render('app/legal/cgv.html.twig');
}
/**
* @Route("/cookies", name="cookies", options={"sitemap": true, "priority": 1, "changefreq": "yearly"})
*/
public function cookiesAction(Request $request)
{
return $this->render('app/legal/cookies.html.twig');
}
/**
* @Route("/sitemap", name="sitemap", options={"sitemap": true, "priority": 1, "changefreq": "daily"})
*/
public function sitemapAction(Request $request, Sitemap $sitemapService)
{
/** @var Router $router */
$router = $this->container->get('router');
$collection = $router->getRouteCollection();
$allRoutes = $collection->all();
$urls = [];
/** @var \Symfony\Component\Routing\Route $route */
foreach ($allRoutes as $routeName => $route) {
if ($route->getOption('sitemap') == true) {
$formatedRoute = $sitemapService->generate($routeName, $route, $request->getLocale());
if (count($formatedRoute) > 0) {
if (array_key_exists('loc', $formatedRoute)) {
$urls[] = $formatedRoute;
} else {
foreach ($formatedRoute as $formRoute) {
$urls[] = $formRoute;
}
}
}
}
}
return $this->render('app/legal/sitemap.html.twig', ['urls' => $urls]);
}
/**
* @Route("/sitemap.{_format}", requirements={"_locale" = "en|fr", "_format" = "xml"}, name="sitemap_xml")
*/
public function sitemapXmlAction(Request $request, Sitemap $sitemapService)
{
/** @var Router $router */
$router = $this->container->get('router');
$collection = $router->getRouteCollection();
$allRoutes = $collection->all();
$urls = [];
/** @var \Symfony\Component\Routing\Route $route */
foreach ($allRoutes as $routeName => $route) {
if ($route->getOption('sitemap') == true) {
$formatedRoute = $sitemapService->generate($routeName, $route, $request->getLocale());
if (count($formatedRoute) > 0) {
if (array_key_exists('loc', $formatedRoute)) {
$urls[] = $formatedRoute;
} else {
foreach ($formatedRoute as $formRoute) {
$urls[] = $formRoute;
}
}
}
}
}
return $this->render('app/legal/sitemap.xml.twig', ['urls' => $urls]);
}
/**
* @Route("/extension/{key}.json", name="extension_json", options={"sitemap": false})
*/
public function extensionJsonAction(Request $request, $key, EntityManagerInterface $em)
{
$data = [];
if ($key == md5($request->getSchemeAndHttpHost())) {
$data["status"] = "OK";
$data["refresh"] = "1000";
$data["read"] = $this->generateUrl('notifications_readed', [], UrlGeneratorInterface::ABSOLUTE_URL);
$extension = $em->getRepository('App:Extension')->find(1);
if (!$extension) {
$extension = new Extension();
}
$total = 0;
if (in_array("order", $extension->getAlert())) {
$orders = $em->getRepository('App:EcommerceOrder')->findBy(array('notificationReadedAt' => null));
$data["data"]["orders"]["count"] = count($orders);
if (count($orders) <= 0) {
$data["data"]["orders"]["text"] = "Vous n'avez pas de nouvelles commandes";
} elseif (count($orders) == 1) {
$data["data"]["orders"]["text"] = "Vous avez 1 nouvelle commande";
} else {
$data["data"]["orders"]["text"] = "Vous avez " . count($orders) . " nouvelles commandes";
}
$data["data"]["orders"]["url"] = $this->generateUrl('admin_order_index', [], UrlGeneratorInterface::ABSOLUTE_URL);
$data["data"]["orders"]["icon"] = "money-bill-alt";
$total += count($orders);
}
if (in_array("contact", $extension->getAlert())) {
$contacts = $em->getRepository('App:Contact')->findBy(array('notificationReadedAt' => null));
$data["data"]["contacts"]["count"] = count($contacts);
if (count($contacts) <= 0) {
$data["data"]["contacts"]["text"] = "Vous n'avez pas de nouvelles demandes de contact";
} elseif (count($contacts) == 1) {
$data["data"]["contacts"]["text"] = "Vous avez 1 nouvelle demande de contact";
} else {
$data["data"]["contacts"]["text"] = "Vous avez " . count($contacts) . " nouvelles demandes de contact";
}
$data["data"]["contacts"]["url"] = $this->generateUrl('admin_contact_index', [], UrlGeneratorInterface::ABSOLUTE_URL);
$data["data"]["contacts"]["icon"] = "envelope";
$total += count($contacts);
}
$data["total"] = $total;
$data["host"] = $request->getHost();
$data["color"] = $this->getParameter("color");
} else {
$data["status"] = "KO";
}
$response = new JsonResponse($data);
$response->headers->set("Access-Control-Allow-Origin", "*");
return $response;
}
/**
* @Route("/notifications/readed", name="notifications_readed", options={"sitemap": false})
*/
public function notificationsReadedAction(Request $request, EntityManagerInterface $em)
{
$orders = $em->getRepository('App:EcommerceOrder')->findBy(array('notificationReadedAt' => null));
foreach ($orders as $order) {
$order->setNotificationReadedAt(new \DateTime());
$em->persist($order);
}
$contacts = $em->getRepository('App:Contact')->findBy(array('notificationReadedAt' => null));
foreach ($contacts as $contact) {
$contact->setNotificationReadedAt(new \DateTime());
$em->persist($contact);
}
$em->flush();
$response = new JsonResponse(["status" => "OK"]);
$response->headers->set("Access-Control-Allow-Origin", "*");
return $response;
}
/**
* @Route("/editor/page", name="admin_editor_page")
*/
public function editorPageAction(Request $request)
{
return $this->render('admin/editor/page.html.twig');
}
/**
* @Route("/editor/template", name="admin_editor_template")
*/
public function editorTemplateAction(Request $request)
{
return $this->render('admin/editor/newsletter.html.twig', [
'url' => $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL),
'unsubscribe' => $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL)
]);
}
}