src/Controller/HomeController.php line 317

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use App\Entity\Panel;
  8. use App\Repository\PanelRepository;
  9. use App\Entity\TypePanel;
  10. use App\Repository\TypePanelRepository;
  11. use App\Entity\HistoryViews;
  12. use App\Repository\HistoryViewsRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Doctrine\Common\Collections\Criteria;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use DateTime;
  18. class HomeController extends AbstractController
  19. {
  20.     /**
  21.      */
  22.     public function __construct(PanelRepository $panelRepositoryTypePanelRepository $typePanelRepositoryHistoryViewsRepository $historyViewsRepository)
  23.     {
  24.         $this->panelRepository $panelRepository;
  25.         $this->typePanelRepository $typePanelRepository;
  26.         $this->historyViewsRepository $historyViewsRepository;
  27.     }
  28.     public function sendEmail($data)
  29.     {
  30.         $curl curl_init();
  31.         curl_setopt_array($curl, array(
  32.             CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send",
  33.             CURLOPT_RETURNTRANSFER => true,
  34.             CURLOPT_ENCODING => "",
  35.             CURLOPT_MAXREDIRS => 10,
  36.             CURLOPT_TIMEOUT => 0,
  37.             CURLOPT_FOLLOWLOCATION => true,
  38.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  39.             CURLOPT_CUSTOMREQUEST => "POST",
  40.             CURLOPT_POSTFIELDS => json_encode($data),
  41.             CURLOPT_HTTPHEADER => array(
  42.                 "Authorization: Bearer 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",
  43.                 "Content-Type: application/json"
  44.             ),
  45.         ));
  46.         $response curl_exec($curl);
  47.         curl_close($curl);
  48.         return true;
  49.     }
  50.     /**
  51.      * @Route("/", name="home")
  52.      */
  53.     public function index(Request $request): Response
  54.     {
  55.         $panels $this->panelRepository->findBy(['free' => true]);
  56.         $images = Array();
  57.         $default "assets/images/panels/default.png";
  58.         $type $request->query->get("type");
  59.         foreach($panels as $key=>$panel)
  60.         {
  61.             if (!empty($panel->getImages()[0]->getPath()) && !$panel->getImages()[0]->getMain())
  62.                 $images[$key] = $panel->getImages()[0]->getPath();
  63.             else
  64.                 $images[$key] = $default;
  65.         }
  66.         $typesPanel $this->typePanelRepository->findAll();
  67.         return $this->render('index.html.twig', [
  68.             'panels' => $panels,
  69.             'typespanel' => $typesPanel,
  70.             'images' => $images,
  71.             'defaultimage' => $default
  72.         ]);
  73.     }
  74.      /**
  75.      * @Route("/list-panels/{type}", name="home_list_panels")
  76.      */
  77. /*    public function listPanels(int $type): Response
  78.     {
  79.         $typesPanel = $this->typePanelRepository->findAll();
  80.         $panels = $type === 0 ? $this->panelRepository->findBy(['free' => true]) : $this->typePanelRepository->find($type)->getPanels();
  81.         $listPanels = [];
  82.         $images = [];
  83.         foreach ($panels as $key => $panel) {
  84.             $panelFormat['name'] = $panel->getName();
  85.             $panelFormat['price'] = $panel->getPrice();
  86.             $panelFormat['image'] = $panel->getImages()[0]->getPath();
  87.             if (!$panelFormat['image'] || $panel->getImages()[0]->getMain())
  88.                 $panelFormat['image'] = 'assets/image/panels/default/png';
  89.             $listPanels[] = $panelFormat;
  90.         }
  91.         return new JsonResponse($listPanels);
  92.     } */
  93.      /**
  94.      * @Route("/contact", name="contact_page")
  95.      */
  96.     public function contact(): Response
  97.     {
  98.         return $this->render('page-contact.html.twig');
  99.     }
  100.      /**
  101.      * @Route("/contact/email", name="contact_page_email", methods={"POST"})
  102.      */
  103.     public function contactSendEmail(Request $request): Response
  104.     
  105.         $from $request->get('email');
  106.         $to $_ENV['MY_EMAIL'];
  107.         $name $request->get('name');
  108.         $msgSubject '';
  109.         $cmessage $request->get('message');
  110.         $mail = array(
  111.             "personalizations" => array(
  112.                 array(
  113.                     "to" => array(
  114.                         array(
  115.                             "email" => $to
  116.                         )
  117.                     ),
  118.                     "dynamic_template_data" => array(
  119.                         "mail" => $from,
  120.                         "nom" => $name,
  121.                         "sujet" => $msgSubject,
  122.                         "message" => $cmessage
  123.         
  124.                     ),
  125.                     "subject" => $msgSubject
  126.                 )
  127.             ),
  128.             "from" => array(
  129.                 "email" => "noreply@wecodx.com"
  130.             ),
  131.             "template_id" => "d-2ded55ea65d94ab984ecc72751425b16"
  132.         );
  133.         $this->sendEmail($mail);
  134.         $this->addFlash(
  135.             'info',
  136.             'Votre e-mail a été envoyé !'
  137.         );
  138.         return $this->redirectToRoute('contact_page');
  139.     }  
  140.     /**
  141.      * @Route("/listing-panels/", name="listing_panels_page")
  142.      */
  143.     public function listPanels(Request $request): Response
  144.     {
  145.         $data $request->query;
  146.         $typePanels $this->typePanelRepository->findAll();
  147.         $panels $this->panelRepository->findAll();
  148.         $maxPrice 0;
  149.         $maxArea 0;
  150.         $area $data->get("area");
  151.         foreach($panels as $panel)
  152.         {
  153.             if ($panel->getPrice() > $maxPrice)
  154.                 $maxPrice $panel->getPrice();
  155.             if ($panel->getArea() > $maxArea)
  156.                 $maxArea $panel->getArea();
  157.         }
  158.         $lowest $data->get('lowest');
  159.         if (!$lowest)
  160.             $lowest 0;
  161.         $highest $data->get('highest');
  162.         if (!$highest)
  163.             $highest $maxPrice;
  164.         $panels $this->panelRepository->findSearch($data);
  165.         if (!$panels)
  166.             return $this->render('page-listing-v1.html.twig',[
  167.                 'error' => "Aucun résultat pour cette recherche",
  168.                 'type_panels' => $typePanels,
  169.                 'panels' => null,
  170.                 'lowest' => $lowest,
  171.                 'highest' => $highest,
  172.                 'most_expensive' => $maxPrice,
  173.                 'max_area' => $maxArea,
  174.                 'area' => $area,
  175.                 'name' => $data->get('name'),
  176.                 'type' => $data->get('type')
  177.             ]);
  178.         $typePerPanel = Array();
  179.         $images = Array();
  180.         $default "../assets/images/panels/default.png";
  181.         foreach ($panels as $key => $panel)
  182.         {
  183.             if (!empty($panel->getImages()[0]->getPath()) && !$panel->getImages()[0]->getMain())
  184.                 $images[$key] = "../" $panel->getImages()[0]->getPath();
  185.             else
  186.                 $images[$key] = $default;
  187.         }
  188.         return $this->render('page-listing-v1.html.twig',[
  189.             'panels' => $panels,
  190.             'type_per_panel' => $typePerPanel,
  191.             'images' => $images,
  192.             'lowest' => $lowest,
  193.             'highest' => $highest,
  194.             'most_expensive' => $maxPrice,
  195.             'max_area' => $maxArea,
  196.             'area' => $area,
  197.             'type_panels' => $typePanels,
  198.             'name' => $data->get('name'),
  199.             'type' => $data->get('type')
  200.         ]);
  201.     }
  202.     /**
  203.      * @Route("/listing-panel-single/{id}", name="listing_panel_single_page")
  204.      */
  205.     public function singlePanel(EntityManagerInterface $em$id): Response
  206.     {
  207.         $panel $this->panelRepository->find($id);
  208.         date_default_timezone_set('Europe/Paris');
  209.         $date = new DateTime();
  210.         $view = new HistoryViews();
  211.         $view->setPanel($panel);
  212.         $view->setDate($date);
  213.         $em->persist($view);
  214.         $em->flush($view);
  215.         if ($panel->getTypePanel())
  216.             $typePanel $panel->getTypePanel()->getName();
  217.         else
  218.             $typePanel "";
  219.         $images = [];
  220.         foreach ($panel->getImages() as $key => $imageInstance)
  221.         {
  222.             $images[] = $imageInstance->getPath();
  223.             if (!empty($imageInstance->getPath()) && !$imageInstance->getMain())
  224.                 $images[$key] = "../" $imageInstance->getPath();
  225.             else
  226.                 $images[$key] = "../assets/images/panels/default.png";
  227.         }
  228.         $similars = [];
  229.         if ($panel->getTypePanel())
  230.             $similars $this->panelRepository->findAllInTypeExcept($panel->getTypePanel()->getId(), $panel->getId());
  231.         $similarImages = [];
  232.         foreach($similars as $key => $similar)
  233.         {
  234.             if (!empty($similar->getImages()[0]->getPath()) && !$similar->getImages()[0]->getMain())
  235.                 $similarImages[$key] = "../" $similar->getImages()[0]->getPath();
  236.             else
  237.                 $similarImages[$key] = "../assets/images/panels/default.png";
  238.         }
  239.         return $this->render('page-listing-single-v1.html.twig',[
  240.             'panel' => $panel,
  241.             'type_panel' => $typePanel,
  242.             'images' => $images,
  243.             'iframe' => $panel->getLinkGm(),
  244.             'similars' => $similars,
  245.             'similars_images' => $similarImages,
  246.         ]);
  247.     }
  248.     /**
  249.      * @Route("/listing-panel-single/{id}/email", name="listing_panel_single_page_email")
  250.      */
  251.     public function singlePanelEmail($idRequest $request): Response
  252.     {
  253.         $panelName $this->panelRepository->find($id)->getName();
  254.         $from $request->get('email');
  255.         $to $_ENV['MY_EMAIL'];
  256.         $name $request->get('name');
  257.         $msgSubject 'Nouvelle demande concernant le panneau ' $panelName;
  258.         $cmessage $request->get('message');
  259.         $mail = array(
  260.             "personalizations" => array(
  261.                 array(
  262.                     "to" => array(
  263.                         array(
  264.                             "email" => $to
  265.                         )
  266.                     ),
  267.                     "dynamic_template_data" => array(
  268.                         "mail" => $from,
  269.                         "nom" => $name,
  270.                         "sujet" => $msgSubject,
  271.                         "message" => $cmessage
  272.         
  273.                     ),
  274.                     "subject" => $msgSubject
  275.                 )
  276.             ),
  277.             "from" => array(
  278.                 "email" => "noreply@wecodx.com"
  279.             ),
  280.             "template_id" => "d-2ded55ea65d94ab984ecc72751425b16"
  281.         );
  282.         $this->sendEmail($mail);
  283.         $this->addFlash(
  284.             'info',
  285.             'Votre e-mail a été envoyé !'
  286.         );
  287.         return $this->redirect('/listing-panel-single/' $id);
  288.     }
  289.     /**
  290.      * @Route("/about-us", name="about_us_page")
  291.      */
  292.     public function aboutUs(): Response
  293.     {
  294.         return $this->render('page-agent-list.html.twig');
  295.     }
  296. }