src/Controller/ApiController.php line 1498

Open in your IDE?
  1. <?php
  2.     
  3.     namespace App\Controller;
  4.     
  5.     use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6.     use Symfony\Component\HttpFoundation\Response;
  7.     use Symfony\Component\HttpFoundation\Request;
  8.     use Symfony\Component\Routing\Annotation\Route;
  9.     use App\Repository\UserRepository;
  10.     use App\Repository\VillesRepository;
  11.     use App\Repository\PaysRepository;
  12.     use App\Repository\CategoriesRepository;
  13.     use App\Repository\AnnoncesRepository;
  14.     use App\Repository\NotesEnseignesRepository;
  15.     use App\Repository\NotesAnnoncesRepository;
  16.     use App\Repository\ImagesRepository;
  17.     use App\Repository\FavorisRepository;
  18.     use App\Repository\FacturesRepository;
  19.     use App\Repository\CommandesRepository;
  20.     use App\Repository\AlertesRepository;
  21.     
  22.     use Datetime;
  23.     use App\Entity\User;
  24.     use App\Entity\Annonces;
  25.     use App\Entity\Images;
  26.     use App\Entity\Alertes;
  27.     use App\Entity\NotesAnnonces;
  28.     use App\Entity\Favoris;
  29.     
  30.     class ApiController extends AbstractController
  31.     {
  32.         /**
  33.          * @Route("/api/login", name="app_apiLogin")
  34.          */
  35.         public function app_apiLogin(UserRepository $userRepository): Response
  36.         {
  37.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  38.             header('Access-Control-Allow-Origin: *');
  39.             header('Content-Type: application/json');
  40.             header('Access-Control-Allow-Methods: GET, POST');
  41.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  42.             // Passer id de la mairie en GET['id']
  43.             
  44.             $reponse = array();
  45.             $data json_decode(file_get_contents('php://input'), true);
  46.             
  47.             
  48.             if ($data) {
  49.                 $email $data['mail'];
  50.                 $password $data['password'];
  51.                 
  52.                 $user $userRepository->findOneBy(['email' => $email]);
  53.                 if ($user && password_verify($password$user->getPassword())) {
  54.                     
  55.                     $token bin2hex(random_bytes(16));
  56.                     
  57.                     $user->setTokenMobile($token);
  58.                     $userRepository->add($usertrue);
  59.                     
  60.                     // Les informations d'identification sont valides, vous pouvez utiliser l'objet $user
  61.                     $reponse['user']['idUser'] = $user->getId();
  62.                     $reponse['user']['token'] = $user->getTokenMobile();
  63.                     $reponse['user']['type'] = $user->getType();
  64.                     $reponse['user']['mail'] = $user->getEmail();
  65.                     $reponse['message'] = 'Connexion réussie';
  66.                 } else {
  67.                     $reponse['user'] = null;
  68.                     $reponse['message'] = 'Identifiants invalides';
  69.                 }
  70.                 
  71.                 
  72.                 return new Response(json_encode($reponse));
  73.             } elseif ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  74.                 return (new Response())->setStatusCode(200);
  75.             }
  76.             
  77.             return (new Response())
  78.                 ->setStatusCode(401)
  79.                 ->withBody('Unauthorized');
  80.         }
  81.         /**
  82.          * @Route("/api/token", name="app_apiToken")
  83.          */
  84.         public function app_apiToken(UserRepository $userRepository): Response
  85.         {
  86.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  87.             header('Access-Control-Allow-Origin: *');
  88.             header('Content-Type: application/json');
  89.             header('Access-Control-Allow-Methods: GET, POST');
  90.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  91.             // Passer id de la mairie en GET['id']
  92.             
  93.             $reponse = array();
  94.             $data json_decode(file_get_contents('php://input'), true);
  95.             
  96.             
  97.             if ($data) {
  98.                 $token $data['token'];
  99.                 $email $data['mail'];
  100.                     $user $userRepository->findOneBy(['email' => $email]);
  101.                     if($user->getTokenMobile() != $token)
  102.                     {
  103.                         $user->setTokenMobile($token);
  104.                         $userRepository->add($usertrue);
  105.                     }
  106.                 return new Response(json_encode($reponse));
  107.             } elseif ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  108.                 return (new Response())->setStatusCode(200);
  109.             }
  110.             
  111.             return (new Response())
  112.                 ->setStatusCode(401)
  113.                 ->withBody('Unauthorized');
  114.         }
  115.         
  116.         
  117.         /**
  118.          * @Route("/api/inscription", name="app_apiInscription")
  119.          */
  120.         public function app_apiInscription(UserRepository $userRepositoryVillesRepository $villesRepositoryPaysRepository $paysRepository): Response
  121.         {
  122.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  123.             header('Access-Control-Allow-Origin: *');
  124.             header('Content-Type: application/json');
  125.             header('Access-Control-Allow-Methods: GET, POST');
  126.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  127.             // Passer id de la mairie en GET['id']
  128.             
  129.             
  130.             $client = new User();
  131.             
  132.             $res = new Response();
  133.             
  134.             $data json_decode(file_get_contents('php://input'), true);
  135.             
  136.             if ($data) {
  137.                 $ville $data['cityId'];
  138.                 $pays $data['country'];
  139.                 $email $data['mail'];
  140.                 $password $data['password'];
  141.                 $type $data['type'] ?? 'particuliers';
  142.                 $genre $data['gender'];
  143.                 $nom $data['name'];
  144.                 $prenom $data['surname'];
  145.                 $raisonSociale $data['socialReason'] ?? null;
  146.                 $siret $data['siret'] ?? null;
  147.                 $tva $data['tva'] ?? null;
  148.                 $adresse $data['address'];
  149.                 $codePostal $data['postalCode'];
  150.                 $telephone $data['phone'];
  151.                 
  152.                 $credit 0;
  153.                 
  154.                 if ($type == 'particuliers') {
  155.                     $role = array("ROLE_PART");
  156.                 } else {
  157.                     $role = array("ROLE_PRO");
  158.                     $credit 100;
  159.                 }
  160.                 
  161.                 $ville $villesRepository->findOneById($ville);
  162.                 $pays $paysRepository->findOneById($pays);
  163.                 
  164.                 $clientExist $userRepository->findOneBy(array('email' => $email));
  165.                 
  166.                 
  167.                 if ($type == 'professionnel') {
  168.                     // upload du logo dans le dossier public/uploads/logos
  169.                     $logoBase64 $data['logo'];
  170.                     [$imgType$logoBase64] = explode(';'$logoBase64);
  171.                     [, $logoBase64] = explode(','$logoBase64);
  172.                     [, $extension] = explode('/'$imgType);
  173.                     $logo base64_decode($logoBase64);
  174.                     $logoName uniqid('logo') . '.' $extension;
  175.                     file_put_contents(dirname(__DIR__2) . '/uploads/logos/' $logoName$logo);
  176.                     $client->setLogo($logoName);
  177.                 }
  178.                 
  179.                 
  180.                 if (!empty($clientExist)) {
  181.                     $res->setStatusCode(301)
  182.                         ->setContent(json_encode(array(
  183.                             'message' => 'L\'email existe déjà'
  184.                         )));
  185.                 } else {
  186.                     $pass password_hash($passwordPASSWORD_BCRYPT);
  187.                     
  188.                     $client->setPassword($pass);
  189.                     $client->setType($type);
  190.                     $client->setEmail($email);
  191.                     $client->setRoles($role);
  192.                     $client->setCivilite($genre);
  193.                     $client->setPays($pays);
  194.                     $client->setVilles($ville);
  195.                     $client->setCredits($credit);
  196.                     $client->setCreated(new DateTime());
  197.                     $client->setNom($nom);
  198.                     $client->setPrenom($prenom);
  199.                     $client->setRaisonSociale($raisonSociale);
  200.                     $client->setSiret($siret);
  201.                     $client->setTva($tva);
  202.                     $client->setAdresse($adresse);
  203.                     $client->setCodePostal($codePostal);
  204.                     $client->setTelephone($telephone);
  205.                     $client->setActif(1);
  206.                     
  207.                     $userRepository->add($clienttrue);
  208.                     
  209.                     $res->setStatusCode(200)
  210.                         ->setContent(json_encode(array(
  211.                             'message' => 'Le compte est ajouté avec succès'
  212.                         )));
  213.                 }
  214.             } else {
  215.                 if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  216.                     $res->setStatusCode(200)
  217.                         ->setContent(json_encode(array(
  218.                             'message' => 'OK'
  219.                         )));
  220.                 } else {
  221.                     $res->setStatusCode(301)
  222.                         ->setContent(json_encode(array(
  223.                             'message' => 'Aucune donnée reçue'
  224.                         )));
  225.                 }
  226.             }
  227.             
  228.             
  229.             return $res;
  230.             
  231.         }
  232.         
  233.         
  234.         /**
  235.          * @Route("/api/searchCities", name="searchCities")
  236.          */
  237.         public function searchCities(UserRepository $userRepositoryVillesRepository $villesRepositoryPaysRepository $paysRepository): Response
  238.         {
  239.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  240.             header('Access-Control-Allow-Origin: *');
  241.             header('Content-Type: application/json');
  242.             header('Access-Control-Allow-Methods: GET, POST');
  243.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  244.             // Passer id de la mairie en GET['id']
  245.             
  246.             $chaine $_GET["q"];
  247.             
  248.             $villes $villesRepository->findByExampleField($chaine);
  249.             $i = -1;
  250.             $ville = array();
  251.             
  252.             foreach ($villes as $res) {
  253.                 $i++;
  254.                 $ville[$i]['id'] = $res->getId();
  255.                 $ville[$i]['nom'] = $res->getVilleNom();
  256.                 $ville[$i]['cp'] = $res->getVilleCodePostal();
  257.             }
  258.             
  259.             
  260.             return new Response(json_encode($ville));
  261.         }
  262.         
  263.         
  264.         /**
  265.          * @Route("/api/categories", name="categoriesList")
  266.          */
  267.         public function categoriesList(UserRepository $userRepositoryCategoriesRepository $categoriesRepository): Response
  268.         {
  269.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  270.             header('Access-Control-Allow-Origin: *');
  271.             header('Content-Type: application/json');
  272.             header('Access-Control-Allow-Methods: GET, POST');
  273.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  274.             
  275.             
  276.             $cats $categoriesRepository->findBy([], ['Libelle' => 'asc']);
  277.             $i = -1;
  278.             $categories = array();
  279.             
  280.             foreach ($cats as $res) {
  281.                 $i++;
  282.                 $categories[$i]['id'] = $res->getId();
  283.                 $categories[$i]['libelle'] = $res->getLibelle();
  284.             }
  285.             
  286.             return new Response(json_encode($categories));
  287.         }
  288.         
  289.         
  290.         /**
  291.          * @Route("/api/searchAnnonces", name="searchAnnonces")
  292.          */
  293.         public function searchAnnonces(FavorisRepository $favorisRepositoryUserRepository $userRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  294.         {
  295.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  296.             header('Access-Control-Allow-Origin: *');
  297.             header('Content-Type: application/json');
  298.             header('Access-Control-Allow-Methods: GET, POST');
  299.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  300.             
  301.             
  302.             $categoriesSearch '';
  303.             $citiesRes '';
  304.             
  305.             if (isset($_GET['token'])) {
  306.                 $token $_GET['token'];
  307.                 $user $userRepository->findOneBy(['TokenMobile' => $token]);
  308.                 
  309.             } else {
  310.                 $user = array();
  311.                 $token '';
  312.             }
  313.             if (isset($_GET['tri'])) {
  314.                 $triSearch $_GET['tri'];
  315.             } else {
  316.                 $triSearch '';
  317.             }
  318.             
  319.             
  320.             if (isset($_GET['nbParPage'])) {
  321.                 $nbParPage $_GET['nbParPage'];
  322.             } else {
  323.                 $nbParPage 25;
  324.             }
  325.             
  326.             
  327.             if (isset($_GET['pro'])) {
  328.                 $pro $_GET['pro'] === "true" 0;
  329.             } else {
  330.                 $pro 0;
  331.             }
  332.             
  333.             
  334.             if (isset($_GET['motscles'])) {
  335.                 $motscles $_GET['motscles'];
  336.             } else {
  337.                 $motscles '';
  338.             }
  339.             
  340.             
  341.             $villes = array();
  342.             $citiesRes = array();
  343.             $em $this->getDoctrine()->getManager();
  344.             
  345.             
  346.             // Fin
  347.             
  348.             
  349.             if (isset($_GET['categories'])) {
  350.                 $categoriesSearch $_GET['categories'];
  351.             } else {
  352.                 $categoriesSearch = array();
  353.             }
  354.             
  355.             
  356.             if (isset($_GET['ville'])) {
  357.                 $ville $_GET['ville'];
  358.             } else {
  359.                 $ville '';
  360.             }
  361.             $villes $villesRepository->findOneById($ville);
  362.             
  363.             if (isset($_GET['rayon'])) {
  364.                 $distance $_GET['rayon'];
  365.             } else {
  366.                 $distance 50;
  367.             }
  368.             
  369.             
  370.             // Il faut ajouter le repertoire doctrine/dql dans src
  371.             // Il faut ajouter les lignes dans services.yaml
  372.             // Il faut ajouter les lignes dans doctrine.yaml
  373.             
  374.             
  375.             if (!empty($villes)) {
  376.                 // RECUPERER LES VILLES DANS UN RAYON
  377.                 $lat $villes->getVilleLatitudeDeg();
  378.                 $lng $villes->getVilleLongitudeDeg();
  379.                 
  380.                 $formule '(6371 * ACOS(COS(RADIANS(:lat)) * COS(RADIANS(c.VilleLatitudeDeg)) * COS(RADIANS(c.VilleLongitudeDeg) - RADIANS(:lng)) + SIN(RADIANS(:lat)) * SIN(RADIANS(c.VilleLatitudeDeg))))';
  381.                 $query $em->createQuery("
  382.                    SELECT c.id,c.VilleNom, $formule as dist
  383.                    FROM App\Entity\Villes c
  384.                    WHERE $formule  <= :distance order by dist asc  ")->setParameters([
  385.                     'lat' => $lat,
  386.                     'lng' => $lng,
  387.                     'distance' => $distance,
  388.                 
  389.                 ]);
  390.                 
  391.                 
  392.                 $cities $query->getResult();
  393.                 
  394.                 
  395.                 foreach ($cities as $resVille) {
  396.                     
  397.                     array_push($citiesRes$resVille['id']);
  398.                 }
  399.             }
  400.             
  401.             
  402.             $annoncesReq $annoncesRepository->findByMultiCriteresMobile($categoriesSearch$citiesRes$pro$triSearch$motscles);
  403.             
  404.             
  405.             $i = -1;
  406.             $annonces = array();
  407.             
  408.             foreach ($annoncesReq as $res) {
  409.                 
  410.                 $i++;
  411.                 $favoris $favorisRepository->findOneBy(['Annonces' => $res->getId(), 'User'=>$user]);
  412.                 if($favoris)
  413.                 {
  414.                     $annonces[$i]['favoris'] = true;
  415.                     $annonces[$i]['favorisId'] = $favoris->getId();
  416.                 }
  417.                 else
  418.                 {
  419.                     $annonces[$i]['favoris'] = false;
  420.                 }
  421.                 
  422.                 $annonces[$i]['id'] = $res->getId();
  423.                 $annonces[$i]['boost'] = $res->isBooster();
  424.                 $annonces[$i]['titre'] = $res->getTitre();
  425.                 $annonces[$i]['enseigne'] = $res->getUser()->getRaisonSociale();
  426.                 $annonces[$i]['logo'] = 'https://modixom.fr/uploads/logos/' $res->getUser()->getLogo();
  427.                 $images $res->getImages();
  428.                 $annonces[$i]['images'] = (count($images) > 0) ? 'https://modixom.fr/uploads/annonces/' $images[0]->getLien() : 'default.jpg'// Utilisez une image par défaut si aucune image n'est disponible
  429.                 $annonces[$i]['pourcentRemise'] = $res->getPourcentRemise();
  430.                 $annonces[$i]['prix'] = $res->getPrix();
  431.                 $annonces[$i]['description'] = $res->getDescription();
  432.                 
  433.                 $pourcent $res->getPourcentRemise() / 100;
  434.                 $annonces[$i]['newprix'] = $res->getPrix() * $pourcent;
  435.                 
  436.                 $fin strtotime($res->getDateFin()->format('Y-m-d H:i:s'));
  437.                 $debut strtotime(date('Y-m-d H:i:s'));
  438.                 
  439.                 
  440.                 $diff abs($debut $fin); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
  441.                 $retour = array();
  442.                 
  443.                 $tmp $diff;
  444.                 $retour['second'] = $tmp 60;
  445.                 
  446.                 $tmp floor(($tmp $retour['second']) / 60);
  447.                 $retour['minute'] = $tmp 60;
  448.                 
  449.                 $tmp floor(($tmp $retour['minute']) / 60);
  450.                 $retour['hour'] = $tmp 24;
  451.                 
  452.                 $tmp floor(($tmp $retour['hour']) / 24);
  453.                 $retour['day'] = $tmp 24;
  454.                 
  455.                 $annonces[$i]['heures'] = $tmp 24 $retour['hour'];
  456.                 $annonces[$i]['minutes'] = $retour['minute'];
  457.                 $annonces[$i]['secondes'] = $retour['second'];
  458.                 
  459.                 // CALCUL DE LA MOYENNE DE LANNONCE
  460.                 $sum 0;
  461.                 $moy 0;
  462.                 $y 0;
  463.                 foreach ($res->getNotesAnnonces() as $res2) {
  464.                     $y++;
  465.                     $sum $sum $res2->getNote();
  466.                 }
  467.                 if ($y 0) {
  468.                     $moy $sum $y;
  469.                     
  470.                     $moyAnnonce = (round($moy 2) / 2);
  471.                 } else {
  472.                     $moyAnnonce 0;
  473.                 }
  474.                 
  475.                 $annonces[$i]['moyAnnonce'] = $moyAnnonce;
  476.                 $annonces[$i]["nbNoteAnnonce"] = $y;
  477.                 
  478.                 
  479.                 // CALCUL DE LA MOYENNE DE ENSEIGNE
  480.                 $sum 0;
  481.                 $moy 0;
  482.                 $y 0;
  483.                 
  484.                 
  485.                 $notesEns $notesEnseignesRepository->findBy(array('Enseigne' => $res->getUser()));
  486.                 foreach ($notesEns as $res2) {
  487.                     $y++;
  488.                     $sum $sum $res2->getNote();
  489.                 }
  490.                 
  491.                 $maxNote $y 5;
  492.                 
  493.                 
  494.                 if ($maxNote 0) {
  495.                     $noteEnseigne = ($sum 100) / $maxNote;
  496.                 } else {
  497.                     $noteEnseigne 0;
  498.                 }
  499.                 
  500.                 
  501.                 $annonces[$i]['moyEnseigne'] = $noteEnseigne;
  502.                 
  503.             }
  504.             
  505.             return new Response(json_encode($annonces));
  506.         }
  507.         
  508. /**
  509.          * @Route("/api/getFactures", name="getFactures")
  510.          */
  511.         public function getFactures(FacturesRepository $facturesRepository,UserRepository $userRepositoryCommandesRepository $commandesRepository): Response
  512.         {
  513.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  514.             header('Access-Control-Allow-Origin: *');
  515.             header('Content-Type: application/json');
  516.             header('Access-Control-Allow-Methods: GET, POST');
  517.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  518.             
  519.             
  520.             $_GET['token'] = '095421a4e2fead6820d921862592f1ad';
  521.             if (isset($_GET['token'])) {
  522.                 $token $_GET['token'];
  523.                 $user $userRepository->findOneBy(['TokenMobile' => $token]);
  524.                 $factures $facturesRepository->findBy(['User'=>$user]);
  525.                 $i = -1;
  526.             $facts = array();
  527.             
  528.             foreach ($factures as $res) {
  529.                 $i++;
  530.                 $facts[$i]['id'] = $res->getId();
  531.                 $facts[$i]['date'] = $res->getCreated()->format('d-m-Y');
  532.                 $facts[$i]['fichier'] = 'https://modixom.fr/factures/'.$res->getLienDoc();
  533.             }
  534.                 
  535.                 return new Response(json_encode($facts));
  536.                 
  537.             }
  538.         }
  539.         /**
  540.          * @Route("/api/getFavoris", name="getFavoris")
  541.          */
  542.         public function getFavoris(FavorisRepository $favorisRepositoryUserRepository $userRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  543.         {
  544.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  545.             header('Access-Control-Allow-Origin: *');
  546.             header('Content-Type: application/json');
  547.             header('Access-Control-Allow-Methods: GET, POST');
  548.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  549.             
  550.             
  551.             $categoriesSearch '';
  552.             $citiesRes '';
  553.             
  554.             
  555.             if (isset($_GET['token'])) {
  556.                 $token $_GET['token'];
  557.                 $user $userRepository->findOneBy(['TokenMobile' => $token]);
  558.                 
  559.             } else {
  560.                 $user = array();
  561.                 $token '';
  562.             }
  563.         
  564.             if (isset($_GET['tri'])) {
  565.                 $triSearch $_GET['tri'];
  566.             } else {
  567.                 $triSearch '';
  568.             }
  569.             
  570.             
  571.             if (isset($_GET['nbParPage'])) {
  572.                 $nbParPage $_GET['nbParPage'];
  573.             } else {
  574.                 $nbParPage 25;
  575.             }
  576.             
  577.             
  578.             if (isset($_GET['pro'])) {
  579.                 $pro $_GET['pro'] === "true" 0;
  580.             } else {
  581.                 $pro 0;
  582.             }
  583.             
  584.             
  585.             if (isset($_GET['motscles'])) {
  586.                 $motscles $_GET['motscles'];
  587.             } else {
  588.                 $motscles '';
  589.             }
  590.             
  591.             
  592.             $villes = array();
  593.             $citiesRes = array();
  594.             $em $this->getDoctrine()->getManager();
  595.             
  596.             
  597.             // Fin
  598.             
  599.             
  600.             if (isset($_GET['categories'])) {
  601.                 $categoriesSearch $_GET['categories'];
  602.             } else {
  603.                 $categoriesSearch = array();
  604.             }
  605.             
  606.             
  607.         $citiesRes = array();
  608.             
  609.             
  610.             
  611.             $annoncesReq $annoncesRepository->findByMultiCriteresMobile($categoriesSearch$citiesRes$pro$triSearch$motscles);
  612.             
  613.             
  614.             $i = -1;
  615.             $annonces = array();
  616.             
  617.             foreach ($annoncesReq as $res) {
  618.                 
  619.                 
  620.                 $favoris $favorisRepository->findOneBy(['Annonces' => $res->getId(), 'User'=>$user]);
  621.                 if($favoris)
  622.                 {
  623.                     $i++;
  624.                     $annonces[$i]['favoris'] = true;
  625.                     $annonces[$i]["favorisId"] = $favoris->getId();
  626.                 
  627.                 
  628.                 $annonces[$i]['id'] = $res->getId();
  629.                 $annonces[$i]['boost'] = $res->isBooster();
  630.                 $annonces[$i]['titre'] = $res->getTitre();
  631.                 $annonces[$i]['enseigne'] = $res->getUser()->getRaisonSociale();
  632.                 $annonces[$i]['logo'] = 'https://modixom.fr/uploads/logos/' $res->getUser()->getLogo();
  633.                 $images $res->getImages();
  634.                 $annonces[$i]['images'] = (count($images) > 0) ? 'https://modixom.fr/uploads/annonces/' $images[0]->getLien() : 'default.jpg'// Utilisez une image par défaut si aucune image n'est disponible
  635.                 $annonces[$i]['pourcentRemise'] = $res->getPourcentRemise();
  636.                 $annonces[$i]['prix'] = $res->getPrix();
  637.                 $annonces[$i]['description'] = $res->getDescription();
  638.                 
  639.                 $pourcent $res->getPourcentRemise() / 100;
  640.                 $annonces[$i]['newprix'] = $res->getPrix() * $pourcent;
  641.                 
  642.                 $fin strtotime($res->getDateFin()->format('Y-m-d H:i:s'));
  643.                 $debut strtotime(date('Y-m-d H:i:s'));
  644.                 
  645.                 
  646.                 $diff abs($debut $fin); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
  647.                 $retour = array();
  648.                 
  649.                 $tmp $diff;
  650.                 $retour['second'] = $tmp 60;
  651.                 
  652.                 $tmp floor(($tmp $retour['second']) / 60);
  653.                 $retour['minute'] = $tmp 60;
  654.                 
  655.                 $tmp floor(($tmp $retour['minute']) / 60);
  656.                 $retour['hour'] = $tmp 24;
  657.                 
  658.                 $tmp floor(($tmp $retour['hour']) / 24);
  659.                 $retour['day'] = $tmp 24;
  660.                 
  661.                 $annonces[$i]['heures'] = $tmp 24 $retour['hour'];
  662.                 $annonces[$i]['minutes'] = $retour['minute'];
  663.                 $annonces[$i]['secondes'] = $retour['second'];
  664.                 
  665.                 // CALCUL DE LA MOYENNE DE LANNONCE
  666.                 $sum 0;
  667.                 $moy 0;
  668.                 $y 0;
  669.                 foreach ($res->getNotesAnnonces() as $res2) {
  670.                     $y++;
  671.                     $sum $sum $res2->getNote();
  672.                 }
  673.                 if ($y 0) {
  674.                     $moy $sum $y;
  675.                     
  676.                     $moyAnnonce = (round($moy 2) / 2);
  677.                 } else {
  678.                     $moyAnnonce 0;
  679.                 }
  680.                 
  681.                 $annonces[$i]['moyAnnonce'] = $moyAnnonce;
  682.                 $annonces[$i]["nbNoteAnnonce"] = $y;
  683.                 
  684.                 
  685.                 // CALCUL DE LA MOYENNE DE ENSEIGNE
  686.                 $sum 0;
  687.                 $moy 0;
  688.                 $y 0;
  689.                 
  690.                 
  691.                 $notesEns $notesEnseignesRepository->findBy(array('Enseigne' => $res->getUser()));
  692.                 foreach ($notesEns as $res2) {
  693.                     $y++;
  694.                     $sum $sum $res2->getNote();
  695.                 }
  696.                 
  697.                 $maxNote $y 5;
  698.                 
  699.                 
  700.                 if ($maxNote 0) {
  701.                     $noteEnseigne = ($sum 100) / $maxNote;
  702.                 } else {
  703.                     $noteEnseigne 0;
  704.                 }
  705.                 
  706.                 
  707.                 $annonces[$i]['moyEnseigne'] = $noteEnseigne;
  708.             }
  709.             
  710.         
  711.             
  712.             }
  713.             
  714.             return new Response(json_encode($annonces));
  715.         }
  716.         
  717.         
  718.         /**
  719.          * @Route("/api/annonceDetail", name="annonceDetail")
  720.          */
  721.         public function annonceDetail(UserRepository $userRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  722.         {
  723.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  724.             header('Access-Control-Allow-Origin: *');
  725.             header('Content-Type: application/json');
  726.             header('Access-Control-Allow-Methods: GET, POST');
  727.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  728.             
  729.             $id $_GET['id'] ?? 10;
  730.             
  731.             $annoncesReq $annoncesRepository->findOneById($id);
  732.             $annoncesReq->setNbvues($annoncesReq->getNbVues() + 1);
  733.             $annoncesRepository->add($annoncesReqtrue);
  734.             
  735.             $annonces['id'] = $annoncesReq->getId();
  736.             
  737.             $annonces['booste'] = $annoncesReq->isBooster();
  738.             $annonces['titre'] = $annoncesReq->getTitre();
  739.             $annonces['enseigne'] = $annoncesReq->getUser()->getRaisonSociale();
  740.             $annonces['logo'] = 'https://modixom.fr/uploads/logos/' $annoncesReq->getUser()->getLogo();
  741.             $annonces['adresse'] = $annoncesReq->getUser()->getAdresse();
  742.             $annonces['cp'] = $annoncesReq->getVilles()->getVilleCodePostal();
  743.             $annonces['telephone'] = $annoncesReq->getUser()->getTelephone();
  744.             $annonces['ville'] = $annoncesReq->getVilles()->getVilleNom();
  745.             $annonces['images'] = 'https://modixom.fr/uploads/annonces/' $annoncesReq->getImages()[0]->getLien();
  746.             $annonces['pourcentRemise'] = $annoncesReq->getPourcentRemise();
  747.             $annonces['prix'] = $annoncesReq->getPrix();
  748.             $annonces['description'] = $annoncesReq->getDescription();
  749.             $annonces['quantite'] = $annoncesReq->getQuantite();
  750.             $annonces['debut'] = $annoncesReq->getDateDebut()->format('d-m-Y H:i');
  751.             $annonces['fin'] = $annoncesReq->getDateFin()->format('d-m-Y H:i');
  752.             // $annonces['pourcentRemise'] = $annoncesReq->getPourcentRemise();
  753.             
  754.             
  755.             $pourcent $annoncesReq->getPourcentRemise() / 100;
  756.             $annonces['newprix'] = $annoncesReq->getPrix() * $pourcent;
  757.             
  758.             $fin strtotime($annoncesReq->getDateFin()->format('Y-m-d H:i:s'));
  759.             $debut strtotime(date('Y-m-d H:i:s'));
  760.             
  761.             
  762.             $diff abs($debut $fin); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
  763.             $retour = array();
  764.             
  765.             $tmp $diff;
  766.             $retour['second'] = $tmp 60;
  767.             
  768.             $tmp floor(($tmp $retour['second']) / 60);
  769.             $retour['minute'] = $tmp 60;
  770.             
  771.             $tmp floor(($tmp $retour['minute']) / 60);
  772.             $retour['hour'] = $tmp 24;
  773.             
  774.             $tmp floor(($tmp $retour['hour']) / 24);
  775.             $retour['day'] = $tmp 24;
  776.             
  777.             $annonces['heures'] = $tmp 24 $retour['hour'];
  778.             $annonces['minutes'] = $retour['minute'];
  779.             $annonces['secondes'] = $retour['second'];
  780.             
  781.             // CALCUL DE LA MOYENNE DE LANNONCE
  782.             $sum 0;
  783.             $moy 0;
  784.             $y 0;
  785.             foreach ($annoncesReq->getNotesAnnonces() as $res2) {
  786.                 $y++;
  787.                 $sum $sum $res2->getNote();
  788.             }
  789.             if ($y 0) {
  790.                 $moy $sum $y;
  791.                 
  792.                 $moyAnnonce = (round($moy 2) / 2);
  793.             } else {
  794.                 $moyAnnonce 0;
  795.             }
  796.             
  797.             $annonces['moyAnnonce'] = $moyAnnonce;
  798.             
  799.             
  800.             // CALCUL DE LA MOYENNE DE ENSEIGNE
  801.             $sum 0;
  802.             $moy 0;
  803.             $y 0;
  804.             
  805.             $notesEns $notesEnseignesRepository->findBy(array('Enseigne' => $annoncesReq->getUser()));
  806.             foreach ($notesEns as $res2) {
  807.                 $y++;
  808.                 $sum $sum $res2->getNote();
  809.             }
  810.             $maxNote $y 5;
  811.             
  812.             if ($maxNote 0) {
  813.                 $noteEnseigne = ($sum 100) / $maxNote;
  814.             } else {
  815.                 $noteEnseigne 0;
  816.             }
  817.             
  818.             
  819.             $annonces['moyEnseigne'] = $noteEnseigne;
  820.             
  821.             
  822.             return new Response(json_encode($annonces));
  823.         }
  824.         
  825.         
  826.         /**
  827.          * @Route("/api/profil", name="profilDetail")
  828.          */
  829.         public function profilDetail(Request $requestUserRepository $userRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  830.         {
  831.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  832.             header('Access-Control-Allow-Origin: *');
  833.             header('Content-Type: application/json');
  834.             header('Access-Control-Allow-Methods: GET, POST');
  835.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  836.             
  837.             
  838.             $token $_GET['token'];
  839.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  840.             
  841.             $userArray = array();
  842.             
  843.             $userArray['id'] = $user->getId();
  844.             $userArray['nom'] = $user->getNom();
  845.             $userArray['prenom'] = $user->getPrenom();
  846.             $userArray['civilite'] = $user->getCivilite();
  847.             $userArray['email'] = $user->getEmail();
  848.             $userArray['telephone'] = $user->getTelephone();
  849.             $userArray['raisonSociale'] = $user->getRaisonSociale();
  850.             $userArray['siret'] = $user->getSiret();
  851.             $userArray['tva'] = $user->getTva();
  852.             $userArray['adresse'] = $user->getAdresse();
  853.             $userArray['codePostal'] = $user->getCodePostal();
  854.             $userArray['ville'] = $user->getVilles()->getVilleNom();
  855.             $userArray['credit'] = $user->getCredits();
  856.             $userArray['type'] = $user->getType();
  857.             $userArray['logo'] = 'https://modixom.fr/uploads/logos/' $user->getLogo();
  858.             
  859.             
  860.             return new Response(json_encode($userArray));
  861.             
  862.         }
  863.         
  864.         
  865.         /**
  866.          * @Route("/api/profilUpdate", name="profilUpdate")
  867.          */
  868.         public function profilUpdate(Request $requestUserRepository $userRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  869.         {
  870.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  871.             header('Access-Control-Allow-Origin: *');
  872.             header('Content-Type: application/json');
  873.             header('Access-Control-Allow-Methods: GET, POST');
  874.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  875.             
  876.             $res = new Response();
  877.             
  878.             $token $_GET['token'];
  879.             $data json_decode(file_get_contents('php://input'), true);
  880.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  881.             
  882.             //Si la method est OPTIONS, on renvoie un code 200
  883.             
  884.             if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  885.                 $res->setStatusCode(200)
  886.                     ->setContent(json_encode(array(
  887.                         'message' => 'OK'
  888.                     )));
  889.                 return $res;
  890.             }
  891.             
  892.             if (!isset($data['password']) or empty($data['password'])) {
  893.                 $res->setStatusCode(301)
  894.                     ->setContent(json_encode(array(
  895.                         'message' => 'Le mot de passe est obligatoire'
  896.                     )));
  897.                 return $res;
  898.             } else {
  899.                 $passwordCheck password_verify($data['password'], $user->getPassword());
  900.                 if (!$passwordCheck) {
  901.                     $res->setStatusCode(301)
  902.                         ->setContent(json_encode(array(
  903.                             'message' => 'Le mot de passe est incorrect'
  904.                         )));
  905.                     return $res;
  906.                 }
  907.             }
  908.             
  909.             isset($data['name']) && $user->setNom($data['name']);
  910.             isset($data['surname']) && $user->setPrenom($data['surname']);
  911.             isset($data['gender']) && $user->setCivilite($data['gender']);
  912.             isset($data['phone']) && $user->setTelephone($data['phone']);
  913.             isset($data['socialReason']) && $user->setRaisonSociale($data['socialReason']);
  914.             isset($data['siret']) && $user->setSiret($data['siret']);
  915.             isset($data['tva']) && $user->setTva($data['tva']);
  916.             isset($data['address']) && $user->setAdresse($data['address']);
  917.             isset($data['postalCode']) && $user->setCodePostal($data['postalCode']);
  918.             
  919.             if (!empty($data['cityId'])) {
  920.                 $v $villesRepository->findOneById($data['cityId']);
  921.                 $user->setVilles($v);
  922.             }
  923.             
  924.             if (!empty($data['logo'])) {
  925.                 
  926.                 $logoBase64 $data['logo'];
  927.                 [$imgType$logoBase64] = explode(';'$logoBase64);
  928.                 [, $logoBase64] = explode(','$logoBase64);
  929.                 [, $extension] = explode('/'$imgType);
  930.                 $logo base64_decode($logoBase64);
  931.                 $logoName uniqid('logo') . '.' $extension;
  932.                 $oldLogo $user->getLogo();
  933.                 if (!empty($oldLogo)) {
  934.                     $oldLogoPath dirname(__DIR__2) . '/public/uploads/logos/' $oldLogo;
  935.                     if (file_exists($oldLogoPath)) {
  936.                         unlink($oldLogoPath);
  937.                     }
  938.                 }
  939.                 file_put_contents(dirname(__DIR__2) . '/public/uploads/logos/' $logoName$logo);
  940.                 
  941.                 $user->setLogo($logoName);
  942.             }
  943.             
  944.             if (!empty($data['newPassword']) && !empty($data['confirmNewPassword']) && $data['newPassword'] == $data['confirmNewPassword']) {
  945.                 $pass password_hash($data['newPassword'], PASSWORD_BCRYPT);
  946.                 
  947.                 $user->setPassword($pass);
  948.             }
  949.             
  950.             $userRepository->add($usertrue);
  951.             
  952.             
  953.             //logo
  954.             
  955.             $res->setStatusCode(200)
  956.                 ->setContent(json_encode(array(
  957.                     'message' => 'Le compte est mis à jour avec succès'
  958.                 )));
  959.             
  960.             
  961.             return $res;
  962.             
  963.         }
  964.         
  965.         
  966.         /**
  967.          * @Route("/api/addAnnonce", name="addAnnonce")
  968.          */
  969.         public function addAnnonce(Request $requestUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  970.         {
  971.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  972.             header('Access-Control-Allow-Origin: *');
  973.             header('Content-Type: application/json');
  974.             header('Access-Control-Allow-Methods: GET, POST');
  975.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  976.             
  977.             
  978.             $res = new Response();
  979.             
  980.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  981.                 $res->setStatusCode(200)
  982.                     ->setContent(json_encode(array(
  983.                         'message' => 'OK'
  984.                     )));
  985.                 return $res;
  986.             }
  987.             
  988.             $credit 1;
  989.             $token $_GET['token'];
  990.             $data json_decode(file_get_contents('php://input'), true);
  991.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  992.             $ville $user->getVilles();
  993.             
  994.             $cat $categoriesRepository->findOneById($data['categorie']);
  995.             
  996.             $annonce = new Annonces();
  997.             $annonce->setCategories($cat);
  998.             if (in_array("pro"$data['client'])) {
  999.                 $annonce->setVisiblePro(1);
  1000.             }
  1001.             $annonce->setNbVues(0);
  1002.             $dDebut str_replace('T'' '$data['dateHeureDebut']);
  1003.             $dFin str_replace('T'' '$data['dateHeureFin']);
  1004.             $annonce->setDateDebut(new Datetime($dDebut));
  1005.             $annonce->setDateFin(new Datetime($dFin));
  1006.             $annonce->setUser($user);
  1007.             $annonce->setVilles($ville);
  1008.             if (isset($data['booster']) && $data["booster"] == true) {
  1009.                 $annonce->setBooster(1);
  1010.                 $credit $credit 4;
  1011.             }
  1012.             
  1013.             
  1014.             
  1015.             if ($user->getCredits() >= $credit) {
  1016.                 $newCredit $user->getCredits() - $credit;
  1017.                 
  1018.                 //Les champs mis en commentaire n'existe pas dans la maquette de l'application mobile.
  1019.                 
  1020.                 $annonce->setTitre($data['titre']);
  1021.                 $annonce->setPrix($data['prix']);
  1022.                 $annonce->setDescription($data['descriptif']);
  1023.                 $annonce->setPourcentRemise($data['pourcent']);
  1024.                 $annonce->setQuantite($data['quantite']);
  1025.                 
  1026.                 $annoncesRepository->add($annoncetrue);
  1027.             // METTRE LES IMAGES DANS public/uploads
  1028.                 
  1029.                 
  1030.                 /*$file = $form->get('image')->getData();
  1031.                 if ($file != 'null') {
  1032.                     $path = '/';
  1033.                     $fileName = uniqid() . '-' . $file->getClientOriginalName();
  1034.         
  1035.                     $file->move(
  1036.                         $this->getParameter('annonces_directory') . $path,
  1037.                         $fileName
  1038.                     );
  1039.         
  1040.                 }*/
  1041.                 
  1042.                 $image $data['logo'];
  1043.                 [$imgType$image] = explode(';'$image);
  1044.                 [, $image] = explode(','$image);
  1045.                 [, $extension] = explode('/'$imgType);
  1046.                 $image base64_decode($image);
  1047.                 $fileName uniqid('image') . '.' $extension;
  1048.                 file_put_contents(dirname(__DIR__2) . '/public/uploads/annonces/' $fileName$image);
  1049.                 
  1050.                 $dbImage = new Images();
  1051.                 $dbImage->setLien($fileName);
  1052.                 $dbImage->setAnnonces($annonce);
  1053.                 $imagesRepository->add($dbImagetrue);
  1054.                 
  1055.                 $user->setCredits($newCredit);
  1056.                 $userRepository->add($usertrue);
  1057.                 
  1058.                 $res->setStatusCode(200)
  1059.                     ->setContent(json_encode(array(
  1060.                         'message' => 'Annonce ajoutée'
  1061.                     )));
  1062.             } else {
  1063.                 $res->setStatusCode(301)
  1064.                     ->setContent(json_encode(array(
  1065.                         'message' => 'Pas assez de crédit pour cette annonce'
  1066.                     )));
  1067.                 
  1068.             }
  1069.             
  1070.             return $res;
  1071.             
  1072.         }
  1073.         
  1074.         
  1075.         /**
  1076.          * @Route("/api/updateAnnonce", name="updateAnnonce")
  1077.          */
  1078.         public function updateAnnonce(Request $requestUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1079.         {
  1080.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1081.             header('Access-Control-Allow-Origin: *');
  1082.             header('Content-Type: application/json');
  1083.             header('Access-Control-Allow-Methods: GET, POST');
  1084.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1085.             
  1086.             
  1087.             $credit 1;
  1088.             $ville $villesRepository->findOneById($_GET['ville']);
  1089.             
  1090.             $cat $categoriesRepository->findOneById($_GET['categorie']);
  1091.             
  1092.             $annonce $annoncesRepository->findOneById($_GET['id']);
  1093.             $annonce->setCategories($cat);
  1094.             if (isset($_GET['pro'])) {
  1095.                 $annonce->setVisiblePro(1);
  1096.             }
  1097.             $annonce->setNbVues(0);
  1098.             
  1099.             $annonce->setVilles($ville);
  1100.             if (isset($_GET['booster'])) {
  1101.                 $annonce->setBooster(1);
  1102.                 $credit $credit 4;
  1103.             }
  1104.             
  1105.             
  1106.             $newCredit $user->getCredits() - $credit;
  1107.             
  1108.             if ($user->getCredits() >= $credit) {
  1109.                 
  1110.                 $annonce->setTitre($_GET['titre']);
  1111.                 $annonce->setPrix($_GET['prix']);
  1112.                 $annonce->setDescription($_GET['description']);
  1113.                 $annonce->setPourcentRemise($_GET['pourcent']);
  1114.                 $annonce->setQuantite($_GET['quantite']);
  1115.                 
  1116.                 $annoncesRepository->add($annoncetrue);
  1117. // METTRE LES IMAGES DANS public/uploads
  1118.                 
  1119.                 
  1120.                 // POUR AJOUT DE L'IMAGE
  1121.                 /*$file = $form->get('image')->getData();
  1122.         if ($file != 'null') {
  1123.             $path = '/';
  1124.             $fileName = uniqid() . '-' . $file->getClientOriginalName();
  1125.             $file->move(
  1126.                 $this->getParameter('annonces_directory') . $path,
  1127.                 $fileName
  1128.             );
  1129.         }*/
  1130.                 /*$image = new Images();
  1131.                 $image->setLien($fileName);
  1132.                 $image->setAnnonces($annonce);
  1133.                 $imagesRepository->add($image, true);*/
  1134.                 
  1135.                 
  1136.                 $user->setCredits($newCredit);
  1137.                 $userRepository->add($usertrue);
  1138.                 
  1139.                 $res->setStatusCode(200)
  1140.                     ->setContent(json_encode(array(
  1141.                         'message' => 'Annonce modifiée'
  1142.                     )));
  1143.             } else {
  1144.                 $res->setStatusCode(301)
  1145.                     ->setContent(json_encode(array(
  1146.                         'message' => 'Pas assez de crédit pour modifier cette annonce'
  1147.                     )));
  1148.                 
  1149.             }
  1150.             
  1151.             
  1152.         }
  1153.         
  1154.         
  1155.         /**
  1156.          * @Route("/api/deleteAnnonce", name="deleteAnnonce")
  1157.          */
  1158.         public function deleteAnnonce(Request $requestNotesAnnoncesRepository $notesAnnoncesRepositoryUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1159.         {
  1160.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1161.             header('Access-Control-Allow-Origin: *');
  1162.             header('Content-Type: application/json');
  1163.             header('Access-Control-Allow-Methods: GET, POST');
  1164.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1165.             
  1166.             
  1167.             $annonce $annoncesRepository->findOneById($_GET['id']);
  1168.             
  1169.             $notes $notesAnnoncesRepository->findBy(array('Annonces' => $annonce));
  1170.             foreach ($notes as $res) {
  1171.                 $notesAnnoncesRepository->remove($restrue);
  1172.             }
  1173.             
  1174.             $images $imagesRepository->findBy(array('Annonces' => $annonce));
  1175.             foreach ($images as $res) {
  1176.                 $imagesRepository->remove($restrue);
  1177.             }
  1178.             
  1179.             $annoncesRepository->remove($annoncetrue);
  1180.             
  1181.             $res->setStatusCode(200)
  1182.                 ->setContent(json_encode(array(
  1183.                     'message' => 'Annonce supprimée'
  1184.                 )));
  1185.             
  1186.             
  1187.             return new Response(json_encode($res));
  1188.             
  1189.         }
  1190.         
  1191.         
  1192.         /**
  1193.          * @Route("/api/annoncesProfil", name="annoncesProfil")
  1194.          */
  1195.         public function annoncesProfil(Request $requestNotesAnnoncesRepository $notesAnnoncesRepositoryUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1196.         {
  1197.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1198.             header('Access-Control-Allow-Origin: *');
  1199.             header('Content-Type: application/json');
  1200.             header('Access-Control-Allow-Methods: GET, POST');
  1201.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1202.             
  1203.             $token $_GET['token'] ?? 'test';
  1204.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  1205.             $annoncesReq $annoncesRepository->findBy(array('User' => $user));
  1206.             
  1207.             
  1208.             $i = -1;
  1209.             $annonces = array();
  1210.             
  1211.             foreach ($annoncesReq as $res) {
  1212.                 
  1213.                 
  1214.                 $i++;
  1215.                 $annonces[$i]['id'] = $res->getId();
  1216.                 $annonces[$i]['boost'] = $res->isBooster();
  1217.                 $annonces[$i]['titre'] = $res->getTitre();
  1218.                 $annonces[$i]['enseigne'] = $res->getUser()->getRaisonSociale();
  1219.                 $annonces[$i]['logo'] = 'https://modixom.fr/uploads/logos/' $res->getUser()->getLogo();
  1220.                 $annonces[$i]['images'] = sizeof($res->getImages()) > 'https://modixom.fr/uploads/annonces/' $res->getImages()[0]->getLien() : 'https://modixom.fr/uploads/annonces/default.jpg';
  1221.                 $annonces[$i]['pourcentRemise'] = $res->getPourcentRemise();
  1222.                 $annonces[$i]['prix'] = $res->getPrix();
  1223.                 $annonces[$i]['description'] = $res->getDescription();
  1224.                 $annonces[$i]["quantity"] = $res->getQuantite();
  1225.                 
  1226.                 $pourcent $res->getPourcentRemise() / 100;
  1227.                 $annonces[$i]['newprix'] = $res->getPrix() * $pourcent;
  1228.                 
  1229.                 $fin strtotime($res->getDateFin()->format('Y-m-d H:i:s'));
  1230.                 $debut strtotime(date('Y-m-d H:i:s'));
  1231.                 
  1232.                 
  1233.                 $diff abs($debut $fin); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
  1234.                 $retour = array();
  1235.                 
  1236.                 $tmp $diff;
  1237.                 $retour['second'] = $tmp 60;
  1238.                 
  1239.                 $tmp floor(($tmp $retour['second']) / 60);
  1240.                 $retour['minute'] = $tmp 60;
  1241.                 
  1242.                 $tmp floor(($tmp $retour['minute']) / 60);
  1243.                 $retour['hour'] = $tmp 24;
  1244.                 
  1245.                 $tmp floor(($tmp $retour['hour']) / 24);
  1246.                 $retour['day'] = $tmp 24;
  1247.                 
  1248.                 $annonces[$i]['heures'] = $tmp 24 $retour['hour'];
  1249.                 $annonces[$i]['minutes'] = $retour['minute'];
  1250.                 $annonces[$i]['secondes'] = $retour['second'];
  1251.                 
  1252.                 // CALCUL DE LA MOYENNE DE LANNONCE
  1253.                 $sum 0;
  1254.                 $moy 0;
  1255.                 $y 0;
  1256.                 foreach ($res->getNotesAnnonces() as $res2) {
  1257.                     $y++;
  1258.                     $sum $sum $res2->getNote();
  1259.                 }
  1260.                 if ($y 0) {
  1261.                     $moy $sum $y;
  1262.                     
  1263.                     $moyAnnonce = (round($moy 2) / 2);
  1264.                 } else {
  1265.                     $moyAnnonce 0;
  1266.                 }
  1267.                 
  1268.                 $annonces[$i]['moyAnnonce'] = $moyAnnonce;
  1269.                 
  1270.                 
  1271.                 // CALCUL DE LA MOYENNE DE ENSEIGNE
  1272.                 $sum 0;
  1273.                 $moy 0;
  1274.                 $y 0;
  1275.                 
  1276.                 
  1277.                 $notesEns $notesEnseignesRepository->findBy(array('Enseigne' => $res->getUser()));
  1278.                 foreach ($notesEns as $res2) {
  1279.                     $y++;
  1280.                     $sum $sum $res2->getNote();
  1281.                 }
  1282.                 
  1283.                 $maxNote $y 5;
  1284.                 
  1285.                 
  1286.                 if ($maxNote 0) {
  1287.                     $noteEnseigne = ($sum 100) / $maxNote;
  1288.                 } else {
  1289.                     $noteEnseigne 0;
  1290.                 }
  1291.                 
  1292.                 
  1293.                 $annonces[$i]['moyEnseigne'] = $noteEnseigne;
  1294.                 
  1295.                 
  1296.                 if (strtotime($res->getDateDebut()->format('Y-m-d H:i:s')) <= time()
  1297.                     && strtotime($res->getDateFin()->format('Y-m-d H:i:s')) > time()) {
  1298.                     $annonces[$i]['statut'] = 'active';
  1299.                 } else {
  1300.                     $annonces[$i]['statut'] = 'expire';
  1301.                 }
  1302.                 
  1303.             }
  1304.             
  1305.             return new Response(json_encode($annonces));
  1306.         }
  1307.         
  1308.         
  1309.         /**
  1310.          * @Route("/api/noteProduit", name="noteProduit")
  1311.          */
  1312.         public function noteProduit(Request $requestNotesAnnoncesRepository $notesAnnoncesRepositoryUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1313.         {
  1314.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1315.             header('Access-Control-Allow-Origin: *');
  1316.             header('Content-Type: application/json');
  1317.             header('Access-Control-Allow-Methods: GET, POST');
  1318.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1319.             
  1320.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1321.                 $res = new Response();
  1322.                 $res->setStatusCode(200)
  1323.                     ->setContent(json_encode(array(
  1324.                         'message' => 'OK'
  1325.                     )));
  1326.                 return $res;
  1327.             }
  1328.             
  1329.             
  1330.             $token $_GET['token'];
  1331.             $data json_decode(file_get_contents('php://input'), true);
  1332.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  1333.             
  1334.             $produit $data['produit'];
  1335.             $note $data['note'];
  1336.             $produit $annoncesRepository->findOneById($produit);
  1337.             $noteAnnonce $notesAnnoncesRepository->findOneBy(['User'=>$user'Annonces'=>$produit]);
  1338.             if(empty($noteAnnonce))
  1339.             {
  1340.             
  1341.             $prod = new NotesAnnonces();
  1342.             
  1343.             //$prod = $notesAnnoncesRepository->findOneById($produit);
  1344.             $prod->setAnnonces($produit);
  1345.             $prod->setNote($note);
  1346.             $prod->setDate(new Datetime());
  1347.             $prod->setUser($user);
  1348.             
  1349.             $notesAnnoncesRepository->add($prodtrue);
  1350.             
  1351.             
  1352.             return new Response('ok');
  1353.             }
  1354.             else
  1355.             {
  1356.                 return new Response('produit déjà noté');
  1357.             }
  1358.         }
  1359.         
  1360.         /**
  1361.          * @Route("/api/addAlertes", name="addAlertesApi")
  1362.          */
  1363.         public function addAlertes(Request $requestAnnoncesRepository $annoncesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryUserRepository $userRepository): Response
  1364.         {
  1365.             header('Access-Control-Allow-Origin: *');
  1366.             header('Content-Type: application/json');
  1367.             header('Access-Control-Allow-Methods: GET, POST');
  1368.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1369.             
  1370.             $res = new Response();
  1371.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1372.                 $res->setStatusCode(200)
  1373.                     ->setContent(json_encode(array(
  1374.                         'message' => 'OK'
  1375.                     )));
  1376.                 return $res;
  1377.             }
  1378.             $entityManager $this->getDoctrine()->getManager();
  1379.             $data json_decode(file_get_contents('php://input'), true);
  1380.             $villeId $data['ville'];
  1381.             $rayon $data['rayon'];
  1382.             $categorieId $data['category'];
  1383.             
  1384.             $mail $data['mail'];
  1385.             // Récupérer les instances des entités liées (Villes, Categories, User)
  1386.             $ville $villesRepository->find($villeId);
  1387.             $user $userRepository->findOneBy(['email' => $mail]);
  1388.             foreach($categorieId as $i_value) {
  1389.                 $categorie $categoriesRepository->find($i_value);
  1390.                 // Créer une nouvelle alerte avec les données du formulaire
  1391.                 $alerte = new Alertes();
  1392.                 $alerte->setVilles($ville);
  1393.                 $alerte->setRayon($rayon);
  1394.                 $alerte->setCategories($categorie);
  1395.                 $alerte->setUser($user);
  1396.                 // Enregistrer l'alerte dans la base de données
  1397.                 $entityManager->persist($alerte);
  1398.                 $entityManager->flush();
  1399.             }
  1400.             // Retourner une réponse indiquant que l'alerte a été ajoutée avec succès
  1401.             return new Response("Alerte ajoutée avec succès"Response::HTTP_CREATED);
  1402.         }
  1403.         
  1404.         /**
  1405.          * @Route("/api/getAlertes", name="getAlertes")
  1406.          */
  1407.         public function getAlertes(Request $requestAlertesRepository $alerteRepositoryUserRepository $userRepository): Response {
  1408.             header('Access-Control-Allow-Origin: *');
  1409.             header('Content-Type: application/json');
  1410.             header('Access-Control-Allow-Methods: GET, POST');
  1411.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1412.             
  1413.             $res = new Response();
  1414.             
  1415.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1416.                 $res->setStatusCode(200)
  1417.                     ->setContent(json_encode(array(
  1418.                         'message' => 'OK'
  1419.                     )));
  1420.                 return $res;
  1421.             }
  1422.             
  1423.             $token $_GET['token'] ?? null;
  1424.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  1425.             $rawAlertes $alerteRepository->findBy(array("User" => $user));
  1426.             $hydratedAlertes = [];
  1427.             foreach ($rawAlertes as $alerte) {
  1428.                 $alerteArray = [];
  1429.                 $alerteArray["id"] = $alerte->getId();
  1430.                 $alerteArray["ville"] = $alerte->getVilles()->getVilleNomReel();
  1431.                 $alerteArray["rayon"] = $alerte->getRayon();
  1432.                 $alerteArray["category"] = $alerte->getCategories()->getLibelle();
  1433.                 $hydratedAlertes[] = $alerteArray;
  1434.             }
  1435.             
  1436.             $res->setStatusCode(200)
  1437.                 ->setContent(json_encode($hydratedAlertes));
  1438.             return $res;
  1439.         }
  1440.         
  1441.         
  1442.         /**
  1443.          * @Route("/api/updateFavoris", name="updateFavoris")
  1444.          */
  1445.         public function updateFavoris(Request $requestAnnoncesRepository $annoncesRepositoryFavorisRepository $favorisRepositoryUserRepository $userRepository): Response {
  1446.             
  1447.             header('Access-Control-Allow-Origin: *');
  1448.             header('Content-Type: application/json');
  1449.             header('Access-Control-Allow-Methods: GET, POST');
  1450.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1451.             
  1452.             $res = new Response();
  1453.             
  1454.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1455.                 $res->setStatusCode(200)
  1456.                     ->setContent(json_encode(array(
  1457.                         'message' => 'OK'
  1458.                     )));
  1459.                 return $res;
  1460.             }
  1461.             
  1462.             $data json_decode(file_get_contents("php://input"), true);
  1463.             
  1464.             $favorisId $data["favorisId"] ?? null;
  1465.             
  1466.             if ($favorisId) {
  1467.                 $favoris $favorisRepository->findOneById($favorisId);
  1468.                 $favorisRepository->remove($favoristrue);
  1469.                 return new Response("favoris retiré"Response::HTTP_CREATED);
  1470.             }
  1471.             $user $userRepository->findOneById($data["userId"]);
  1472.             $annonce $annoncesRepository->findOneById($data["annonceId"]);
  1473.             $favoris = new Favoris();
  1474.             $favoris->setAnnonces($annonce);
  1475.             $favoris->setUser($user);
  1476.             $favoris->setDate(new Datetime());
  1477.             $favorisRepository->add($favoristrue);
  1478.             
  1479.             return new Response("favoris ajouté"Response::HTTP_CREATED);
  1480.             
  1481.         }
  1482.     }