<?php 
 
namespace App\Controller; 
 
use App\Form\ContactType; 
 
use App\Entity\Stats; 
use App\Form\StatsType; 
use App\Repository\AdminStatsRepository; 
 
use App\Entity\Upartistik; 
use App\Repository\UpartistikRepository; 
 
use App\Entity\MobilierCategories; 
use App\Repository\MobilierCategoriesRepository; 
 
use App\Entity\Mobilier; 
use App\Repository\MobilierRepository; 
 
use App\Entity\MobilierTechniques; 
use App\Repository\MobilierTechniquesRepository; 
 
use App\Entity\MobilierImages; 
use App\Repository\MobilierImagesRepository; 
 
use App\Entity\Contact; 
use App\Repository\ContactRepository; 
 
use App\Entity\Couv; 
use App\Repository\CouvRepository; 
 
use App\Entity\Apropos; 
use App\Repository\AproposRepository; 
 
use App\Entity\Accmobilier; 
use App\Repository\AccmobilierRepository; 
 
use App\Entity\AccEspPro; 
use App\Repository\AccEspProRepository; 
 
use App\Entity\AccContact; 
use App\Repository\AccContactRepository; 
 
use App\Entity\ProImages; 
use App\Repository\ProImagesRepository; 
use App\Entity\ProTexte; 
use App\Repository\ProTexteRepository; 
 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 
use Symfony\Component\HttpFoundation\Request; 
 
use Symfony\Component\Mailer\MailerInterface; 
use Symfony\Component\Mime\Email; 
use Symfony\Component\Mime\Address; 
use Symfony\Bridge\Twig\Mime\TemplatedEmail; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
 
use Webgiciel\RecaptchaBundle\Type\RecaptchaSubmitType; 
use Webgiciel\RecaptchaBundle\RecaptchaBundle; 
use Webgiciel\RecaptchaBundle\RecaptchaCompilerPass; 
 
use Symfony\Component\HttpFoundation\RequestStack; 
use Symfony\Component\HttpFoundation\Session\SessionInterface; 
 
use Doctrine\ORM\EntityManagerInterface; 
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; 
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; 
use Symfony\Component\String\Slugger\SluggerInterface; 
use Doctrine\Common\Collections\ArrayCollection; 
 
 
 
class FrontController extends AbstractController 
{ 
 
 
  // 
  public $mailAdmin = "admin@webgiciel.com"; 
  public $mailExpe = "contact@up-paris.com"; 
  public $mailContact = "contact@up-paris.com"; 
 
 
  private $repUser; 
  private $passwordEncoder; 
 
  public function __construct(MobilierImagesRepository $mobImgrepo,ProTexteRepository $protexterepo, ProImagesRepository $proimagerepo, AccContactRepository $acccontactrepo, AccEspProRepository $accespaceprorepo, AccmobilierRepository $accmobilierrepo, AproposRepository $aproposrepo, CouvRepository $couvrepo, MobilierTechniquesRepository $mobTech, ContactRepository $contactRepo, MobilierRepository $mob, AdminStatsRepository $stats, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, UpartistikRepository $upartRepo, MobilierCategoriesRepository $mobCat) 
  { 
    $this->stats = $stats; 
    $this->em = $em; 
    $this->passwordEncoder = $passwordEncoder; 
    $this->upartRepo = $upartRepo; 
    $this->mobCat = $mobCat; 
    $this->mob = $mob; 
    $this->contactRepo = $contactRepo; 
    $this->mobTech = $mobTech; 
    $this->mobImgrepo = $mobImgrepo; 
    $this->couvrepo = $couvrepo; 
    $this->aproposrepo = $aproposrepo; 
    $this->accmobilierrepo = $accmobilierrepo; 
    $this->accespaceprorepo = $accespaceprorepo; 
    $this->acccontactrepo = $acccontactrepo; 
    $this->proimagerepo = $proimagerepo; 
    $this->protexterepo = $protexterepo; 
  } 
 
 
 
    /** 
     * @Route("/", name="front_index") 
     */ 
    public function index(Request $request, MailerInterface $mailer, SessionInterface $session): Response 
    { 
      $maintenant = new \DateTime("now"); 
      $time = new \DateTime("2022-06-21 00:00:00"); 
      // dump($maintenant); 
      // dump($time); 
 
      if ($maintenant < $time ) 
      { 
        // dump("construction"); 
        // enregistre un log 
        $this->moucheUser("visite", "construction_index"); 
        // dump($this->upartRepo->find(1)); 
 
        // recupère les categories 
        $allMobCat = $this->mobCat->findAllParOrdre(); 
 
        return $this->render('front/construction.html.twig', [ 
          'controller_name' => 'construction_index', 
          'upartRepo' => $this->upartRepo->find(1), 
          'allMobCat' => $allMobCat, 
        ]); 
      } 
      else{ 
        // dump("site"); 
        // enregistre un log 
        $this->moucheUser("visite", "front_index"); 
        // dump($this->upartRepo->find(1)); 
 
        // recupère les categories 
        $allMobCat = $this->mobCat->findAllParOrdre(); 
 
        // form 
        $form = $this->createForm(ContactType::class); 
        $form->handleRequest($request); 
 
        // captcha 
        $name = date('Y_m_d_h_i_s'); 
 
        if ($form->isSubmitted() && $form->isValid()) 
        { 
          if ( $form['captcha']->getData() == $session->get('captcha') ) 
          { 
            // envoi un mail du contact 
            $email = (new TemplatedEmail()) 
              ->from($this->mailExpe) 
              ->to($this->mailAdmin) 
              ->subject('Envoi depuis page Contact') 
              ->text('Envoi depuis page Contact.') 
              ->htmlTemplate('mails/_Mail_Contact.html.twig') 
              ->context([ 
                'nom' => $form['nom']->getData(), 
                'emailx' => $form['email']->getData(), 
                'message' => $form['message']->getData(), 
              ]) 
            ; 
 
 
            $errorM1 = 0; 
            try { 
              $mailer->send($email); 
            } catch (TransportExceptionInterface $e) { 
              // some error prevented the email sending; display an 
              // error message or try to resend the message 
              $this->addFlash('warning', 'Erreur Message !'); 
              $errorM1 = 1; 
            } 
 
            if ($errorM1 == 0) 
            { 
              // dump("ok"); 
              $this->addFlash('success', 'Message envoyé !'); 
            } 
            else 
            { 
              // dump("false"); 
            } 
 
            //return $this->redirectToRoute('paul_contact'); 
          } 
          else 
          { 
            $this->addFlash('warning', 'Erreur de captcha!'); 
          } 
        } 
 
        return $this->render('front/index.html.twig', [ 
          'controller_name' => 'front_index', 
          'upartRepo' => $this->upartRepo->find(1), 
          'allMobCat' => $allMobCat, 
          'form' => $form->createView(), 
          'img' => $this->captcha($name, $session), 
          'name' => $name, 
          'couvs' => $this->couvrepo->findAll(), 
          'apropos' => $this->aproposrepo->find(1), 
          'accmobiliers' => $this->accmobilierrepo->findByOrdre(), 
          'accespacepros' => $this->accespaceprorepo->findByOrdre(), 
          'acccontact' => $this->acccontactrepo->findAll(), 
        ]); 
      } 
 
    } 
 
    /** 
     * @Route("/site", name="construction_index") 
     */ 
    public function index2(Request $request, MailerInterface $mailer, SessionInterface $session): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "front_index"); 
      // dump($this->upartRepo->find(1)); 
 
      // recupère les categories 
      $allMobCat = $this->mobCat->findAllParOrdre(); 
 
      // form 
      $form = $this->createForm(ContactType::class); 
      $form->handleRequest($request); 
 
      // captcha 
      $name = date('Y_m_d_h_i_s'); 
 
      if ($form->isSubmitted() && $form->isValid()) 
      { 
        if ( $form['captcha']->getData() == $session->get('captcha') ) 
        { 
          // envoi un mail du contact 
          $email = (new TemplatedEmail()) 
            ->from($this->mailExpe) 
            ->to($this->mailAdmin) 
            ->subject('Envoi depuis page Contact') 
            ->text('Envoi depuis page Contact.') 
            ->htmlTemplate('mails/_Mail_Contact.html.twig') 
            ->context([ 
              'nom' => $form['nom']->getData(), 
              'emailx' => $form['email']->getData(), 
              'message' => $form['message']->getData(), 
            ]) 
          ; 
 
 
          $errorM1 = 0; 
          try { 
            $mailer->send($email); 
          } catch (TransportExceptionInterface $e) { 
            // some error prevented the email sending; display an 
            // error message or try to resend the message 
            $this->addFlash('warning', 'Erreur Message !'); 
            $errorM1 = 1; 
          } 
 
          if ($errorM1 == 0) 
          { 
            // dump("ok"); 
            $this->addFlash('success', 'Message envoyé !'); 
          } 
          else 
          { 
            // dump("false"); 
          } 
 
          //return $this->redirectToRoute('paul_contact'); 
        } 
        else 
        { 
          $this->addFlash('warning', 'Erreur de captcha!'); 
        } 
      } 
 
      return $this->render('front/index.html.twig', [ 
        'controller_name' => 'front_index', 
        'upartRepo' => $this->upartRepo->find(1), 
        'allMobCat' => $allMobCat, 
        'form' => $form->createView(), 
        'img' => $this->captcha($name, $session), 
        'name' => $name, 
        'couvs' => $this->couvrepo->findAll(), 
        'apropos' => $this->aproposrepo->find(1), 
        'accmobiliers' => $this->accmobilierrepo->findByOrdre(), 
        'accespacepros' => $this->accespaceprorepo->findByOrdre(), 
        'acccontact' => $this->acccontactrepo->findAll(), 
      ]); 
    } 
 
    /** 
    * @Route("/qui_sommes_nous", name="paul_quisommesnous") 
    */ 
    public function quisommesnous(): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_quisommesnous"); 
 
      return $this->render('front/quisommesnous.html.twig', [ 
        'controller_name' => 'paul_quisommesnous', 
        'upartRepo' => $this->upartRepo->find(1), 
      ]); 
    } 
 
    /** 
    * @Route("/espacePro", name="paul_espacePro") 
    */ 
    public function espacePro(): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_espacePro"); 
 
      return $this->render('front/espacePro.html.twig', [ 
        'controller_name' => 'paul_espacePro', 
        'upartRepo' => $this->upartRepo->find(1), 
        'accespacepros' => $this->accespaceprorepo->findByOrdre(), 
        'proimages' => $this->proimagerepo->findByOrdre(), 
        'protexte' => $this->protexterepo->find(1), 
      ]); 
    } 
 
    /** 
    * @Route("/mobilier", name="paul_mobilier") 
    */ 
    public function mobilier(): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_mobilier"); 
 
      // recupère les categories 
      //$allMobCat = $this->mobCat->findAllParOrdre(); 
 
      // recupere les mobiliers 
      $allMob = $this->mob->findAllParOrdre(); 
      // dump($allMob); 
 
      return $this->render('front/mobilier.html.twig', [ 
        'controller_name' => 'paul_mobilier', 
        'upartRepo' => $this->upartRepo->find(1), 
        /* 'allMobCat' => $allMobCat, */ 
        'allMob' => $allMob, 
      ]); 
    } 
 
    /** 
    * @Route("/mobilier2", name="paul_mobilier2") 
    */ 
    public function mobilier2(): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_mobilier2"); 
 
      // recupère les categories 
      $allMobCat = $this->mobCat->findAllParOrdre(); 
 
      // recupere les mobiliers 
      $allMob = $this->mob->findAllParOrdre(); 
 
      return $this->render('front/mobilier2.html.twig', [ 
        'controller_name' => 'paul_mobilier2', 
        'upartRepo' => $this->upartRepo->find(1), 
        'allMobCat' => $allMobCat, 
        'allMob' => $allMob, 
      ]); 
    } 
 
    /** 
    * @Route("/mobilier/{categorie}", name="paul_mobilier_cat") 
    */ 
    public function mobilierListe($categorie): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_mobilier_cat ".$categorie); 
 
      // recupère les objets 
      $allMob = $this->mob->findCatParOrdre($cat[0]->getId()); 
      // dump($allMob); 
 
 
      return $this->render('front/mobilierListe.html.twig', [ 
        'controller_name' => 'paul_mobilier_cat', 
        'upartRepo' => $this->upartRepo->find(1), 
        'categorie' => $categorie, 
        'allMob' => $allMob, 
      ]); 
    } 
 
    /** 
    * @Route("/mobilier/detail/{slug}", name="paul_mobilier_detail") 
    */ 
    public function paul_mobilier_detail($slug): Response 
    { 
      // recupère la categorie 
      // dump($categorie); 
      //$cat0 = $this->mobCat->findCat($categorie); 
      //$cat = $cat0[0]->getId(); 
      // // dump($cat); 
      // dump($slug); 
      // recupère les objets 
      $allMob = $this->mob->findMobParSlug($slug); 
      // dump($allMob[0]); 
 
      // recupere la fiche technique 
      $allTech = $this->mobTech->findOrderByMob($allMob[0]->getId()); 
      // dump($allTech); 
 
      // enregistre un log 
      $this->moucheUser("visite", "paul_mobilier_detail"); 
 
      /* 
      */ 
      // affiche 
      return $this->render('front/mobilierDetail.html.twig', [ 
        'controller_name' => 'paul_mobilier_detail', 
        'upartRepo' => $this->upartRepo->find(1), 
        //'categorie' => $categorie, 
        'allMob' => $allMob[0], 
        'allTech' => $allTech, 
      ]); 
    } 
 
 
    /** 
    * @Route("/mobilierC/{categorie}/{slug}", name="paul_mobilier_cat_detail") 
    */ 
    public function mobilierDetail($categorie, $slug): Response 
    { 
      // recupère la categorie 
      // // dump($categorie); 
      //$cat0 = $this->mobCat->findCat($categorie); 
      //$cat = $cat0[0]->getId(); 
      // // dump($cat); 
 
      // dump($slug); 
      // recupère les objets 
      $allMob = $this->mob->findMobParSlug($slug); 
      // dump($allMob); 
 
      // recupere la fiche technique 
      $allTech = $this->mobTech->findOrderByMob($allMob[0]->getId()); 
      // dump($allTech); 
 
      // recupere les images 
      $allImgs = $this->mobImgrepo->findAllImgDuMob($allMob[0]->getId()); 
      // dump($allImgs); 
 
      // enregistre un log 
      $this->moucheUser("visite", "paul_mobilier_cat_detail ".$allMob[0]->getTitre()); 
 
      // affiche 
      return $this->render('front/mobilierDetail.html.twig', [ 
        'controller_name' => 'paul_mobilier_cat_detail', 
        'upartRepo' => $this->upartRepo->find(1), 
        //'categorie' => $categorie, 
        'allMob' => $allMob[0], 
        'allTech' => $allTech, 
        'allImgs' => $allImgs, 
      ]); 
    } 
 
 
    /** 
    * @Route("/up-artistic", name="paul_upArtistic") 
    */ 
    public function upArtistic(): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_upArtistic"); 
 
      return $this->render('front/upArtistic.html.twig', [ 
        'controller_name' => 'paul_upArtistic', 
        'upartRepo' => $this->upartRepo->find(1), 
      ]); 
    } 
 
    /** 
    * @Route("/contact", name="paul_contact") 
    */ 
    public function contact(Request $request, MailerInterface $mailer, SessionInterface $session): Response 
    { 
      // enregistre un log 
      $this->moucheUser("visite", "paul_contact"); 
 
      $form = $this->createForm(ContactType::class); 
      $form->handleRequest($request); 
 
      // captcha 
      $name = date('Y_m_d_h_i_s'); 
 
      if ($form->isSubmitted() && $form->isValid()) 
      { 
        if ( $form['captcha']->getData() == $session->get('captcha') ) 
        { 
          // inscrit dans la base de données 
          $contactLog = new Contact(); 
          $contactLog->setIp($_SERVER['REMOTE_ADDR']); 
          $contactLog->setNom($form['nom']->getData()); 
          $contactLog->setEmail($form['email']->getData()); 
          $contactLog->setMessage($form['message']->getData()); 
          $contactLog->setDateCrea(new \DateTime()); 
 
          $this->em->persist($contactLog); 
          $this->em->flush(); 
 
          // envoi un mail du contact 
          $email = (new TemplatedEmail()) 
            ->from($this->mailExpe) 
            ->to($this->mailContact) 
            ->subject('Envoi depuis page Contact') 
            ->text('Envoi depuis page Contact.') 
            ->htmlTemplate('mails/_Mail_Contact.html.twig') 
            ->context([ 
              'nom' => $form['nom']->getData(), 
              'emailx' => $form['email']->getData(), 
              'message' => $form['message']->getData(), 
            ]) 
          ; 
          $emailTech = (new TemplatedEmail()) 
            ->from($this->mailExpe) 
            ->to($this->mailAdmin) 
            ->subject('Envoi depuis page Contact') 
            ->text('Envoi depuis page Contact.') 
            ->htmlTemplate('mails/_Mail_Contact.html.twig') 
            ->context([ 
              'nom' => $form['nom']->getData(), 
              'emailx' => $form['email']->getData(), 
              'message' => $form['message']->getData(), 
            ]) 
          ; 
 
 
          $errorM1 = 0; 
          try 
          { 
            $mailer->send($email); 
            $mailer->send($emailTech); 
          } 
          catch (TransportExceptionInterface $e) 
          { 
            // some error prevented the email sending; display an 
            // error message or try to resend the message 
            $this->addFlash('warning', 'Erreur Message !'); 
            $errorM1 = 1; 
          } 
 
          if ($errorM1 == 0) 
          { 
            // dump("ok"); 
            $this->addFlash('success', 'Message envoyé !'); 
          } 
          else 
          { 
            $this->addFlash('danger', 'Le message n\'a pas été envoyé !'); 
            // dump("false"); 
          } 
 
          //return $this->redirectToRoute('paul_contact'); 
        } 
        else 
        { 
          $this->addFlash('warning', 'Erreur de captcha!'); 
        } 
      } 
 
      return $this->render('front/contact.html.twig', [ 
        'controller_name' => 'paul_contact', 
        'upartRepo' => $this->upartRepo->find(1), 
        'form' => $form->createView(), 
        'img' => $this->captcha($name, $session), 
        'name' => $name, 
      ]); 
    } 
 
 
 
 
 
 
    /** 
    * @Route("/captcha/{name}", name="paul_captcha") 
    */ 
    public function captcha($name, SessionInterface $session): Response 
    { 
      // Création d'une image vide et ajout d'un texte 
      $session->set('captcha', mt_rand(10000, 99999)); 
      $im = imagecreate(110, 40); 
      $font = realpath('fonts/ASMAN.TTF'); 
      $bg = imagecolorallocate($im, 205, 215, 212); 
      $textcolor = imagecolorallocate($im, 75, 60, 80); 
      $code = $session->get('captcha'); 
      imagettftext($im, 23, 5, 15, 35, $textcolor, $font, $code); 
 
      // Définir le chemin de sauvegarde de l'image 
      $save = "captcha/".$name.".jpg"; 
 
      // Enregistrer l'image dans un fichier 
      imagejpeg($im, $save); 
 
      // Libération de la mémoire 
      imagedestroy($im); 
 
      // Lire le contenu du fichier image généré 
      $imageContent = file_get_contents($save); 
 
      // Définir les en-têtes HTTP 
      $headers = array( 
        'Content-Type' => 'image/jpeg', 
        'Content-Disposition' => 'inline; filename="'.$name.'.jpg"' 
      ); 
 
      // Retourner l'image en réponse HTTP 
      return new Response($imageContent, 200, $headers); 
 
      // // Définit le contenu de l'en-tête - dans ce cas, image/jpeg 
      // header('Content-Type: image/jpeg'); 
      // $save = "captcha/".$name.".jpg"; 
 
      // // Affichage de l'image 
      // imagejpeg($im, $save); 
 
      // // Libération de la mémoire 
      // imagedestroy($im); 
 
      // $headers = array( 
      //   'Content-Type' => 'image/jpeg', 
      //   'Content-Disposition' => 'inline; filename="'.$im.'"' 
      // ); 
 
      // return new Response($save, 200, $headers); 
    } 
 
    /** 
    * @Route("/captcha2/{name}", name="paul_captcha2") 
    */ 
    public function captcha2($name, SessionInterface $session): Response 
    { 
      // Création d'une image vide et ajout d'un texte 
      $session->set('captcha', mt_rand(10000, 99999)); 
      $im = imagecreate(110, 40); 
      $font = realpath('fonts/ASMAN.TTF'); 
      $bg = imagecolorallocate($im, 205, 215, 212); 
      $textcolor = imagecolorallocate($im, 75, 60, 80); 
      $code = $session->get('captcha'); 
      imagettftext($im, 23, 5, 15, 35, $textcolor, $font, $code); 
 
      // Définit le contenu de l'en-tête - dans ce cas, image/jpeg 
      header('Content-Type: image/jpeg'); 
      $save = "captcha/".$name.".jpg"; 
 
      // Affichage de l'image 
      imagejpeg($im, $save); 
 
      // Libération de la mémoire 
      imagedestroy($im); 
 
      $headers = array( 
        'Content-Type' => 'image/jpeg', 
        'Content-Disposition' => 'inline; filename="'.$im.'"' 
      ); 
 
      return new Response($save, 200, $headers); 
    } 
 
 
    public function moucheUser($action, $page){ 
      // $stat = new Stats(); 
 
 
      // // Get IP address 
      // $ip_address = getenv('HTTP_CLIENT_IP') ?: getenv('HTTP_X_FORWARDED_FOR') ?: getenv('HTTP_X_FORWARDED') ?: getenv('HTTP_FORWARDED_FOR') ?: getenv('HTTP_FORWARDED') ?: getenv('REMOTE_ADDR'); 
      // $stat->setIp($ip_address); 
      // // Get action 
      // $stat->setAction($action); 
      // $stat->setPage($page); 
      // $stat->setDateCrea(new \DateTime()); 
      // // Get JSON object 
      // $ip_data = unserialize(file_get_contents('http://ip-api.com/php/'.$_SERVER['REMOTE_ADDR'].'?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query')); 
      // $stat->setPays($ip_data['country']); 
      // $stat->setRegion($ip_data['region']); 
      // $stat->setVille($ip_data['city']); 
 
      // //$jsondata = file_get_contents("http://timezoneapi.io/api/ip/?" . $ip_address . '&token=aYJMQZkEKzSLtQwhCmzD'); 
      // // Decode 
      // //$data = json_decode($jsondata, true); 
      // // Request OK? 
      // // if(isset($data['meta']) && $data['meta']['code'] == '200'){ 
      // //   $stat->setPays($data['data']['country']); 
      // //   $stat->setRegion($data['data']['state']); 
      // //   $stat->setVille($data['data']['city']); 
      // // } 
 
      // $this->em->persist($stat); 
      // $this->em->flush(); 
    } 
 
 
 
 
 
 
    public function dessineCourbe(){ 
 
      $donnees = array(12,23,9,58,23,26,57,48,12); 
 
      $largeur = 250; 
      $hauteur = 200; 
 
      // Initialisation du graphique 
      $graphe = new \Graph($largeur, $hauteur); 
      // Echelle lineaire ('lin') en ordonnee et pas de valeur en abscisse ('text') 
      // Valeurs min et max seront determinees automatiquement 
      $graphe->setScale("textlin"); 
 
      // Creation de l'histogramme 
      $histo = new \BarPlot($donnees); 
      // Ajout de l'histogramme au graphique 
      $graphe->add($histo); 
 
      // Ajout du titre du graphique 
      $graphe->title->set("Histogramme"); 
 
      // Affichage du graphique 
      $graphe->stroke(); 
    } 
 
 
 
 
 
 
 
 
 
 
}