src/Controller/ApiController.php line 865

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.                     unlink(dirname(__DIR__2) . '/uploads/logos/' $oldLogo);
  935.                 }
  936.                 file_put_contents(dirname(__DIR__2) . '/uploads/logos/' $logoName$logo);
  937.                 
  938.                 $user->setLogo($logoName);
  939.             }
  940.             
  941.             if (!empty($data['newPassword']) && !empty($data['confirmNewPassword']) && $data['newPassword'] == $data['confirmNewPassword']) {
  942.                 $pass password_hash($data['newPassword'], PASSWORD_BCRYPT);
  943.                 
  944.                 $user->setPassword($pass);
  945.             }
  946.             
  947.             $userRepository->add($usertrue);
  948.             
  949.             
  950.             //logo
  951.             
  952.             $res->setStatusCode(200)
  953.                 ->setContent(json_encode(array(
  954.                     'message' => 'Le compte est mis à jour avec succès'
  955.                 )));
  956.             
  957.             
  958.             return $res;
  959.             
  960.         }
  961.         
  962.         
  963.         /**
  964.          * @Route("/api/addAnnonce", name="addAnnonce")
  965.          */
  966.         public function addAnnonce(Request $requestUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  967.         {
  968.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  969.             header('Access-Control-Allow-Origin: *');
  970.             header('Content-Type: application/json');
  971.             header('Access-Control-Allow-Methods: GET, POST');
  972.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  973.             
  974.             
  975.             $res = new Response();
  976.             
  977.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  978.                 $res->setStatusCode(200)
  979.                     ->setContent(json_encode(array(
  980.                         'message' => 'OK'
  981.                     )));
  982.                 return $res;
  983.             }
  984.             
  985.             $credit 1;
  986.             $token $_GET['token'];
  987.             $data json_decode(file_get_contents('php://input'), true);
  988.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  989.             $ville $user->getVilles();
  990.             
  991.             $cat $categoriesRepository->findOneById($data['categorie']);
  992.             
  993.             $annonce = new Annonces();
  994.             $annonce->setCategories($cat);
  995.             if (in_array("pro"$data['client'])) {
  996.                 $annonce->setVisiblePro(1);
  997.             }
  998.             $annonce->setNbVues(0);
  999.             $dDebut str_replace('T'' '$data['dateHeureDebut']);
  1000.             $dFin str_replace('T'' '$data['dateHeureFin']);
  1001.             $annonce->setDateDebut(new Datetime($dDebut));
  1002.             $annonce->setDateFin(new Datetime($dFin));
  1003.             $annonce->setUser($user);
  1004.             $annonce->setVilles($ville);
  1005.             if (isset($data['booster']) && $data["booster"] == true) {
  1006.                 $annonce->setBooster(1);
  1007.                 $credit $credit 4;
  1008.             }
  1009.             
  1010.             
  1011.             
  1012.             if ($user->getCredits() >= $credit) {
  1013.                 $newCredit $user->getCredits() - $credit;
  1014.                 
  1015.                 //Les champs mis en commentaire n'existe pas dans la maquette de l'application mobile.
  1016.                 
  1017.                 $annonce->setTitre($data['titre']);
  1018.                 $annonce->setPrix($data['prix']);
  1019.                 $annonce->setDescription($data['descriptif']);
  1020.                 $annonce->setPourcentRemise($data['pourcent']);
  1021.                 $annonce->setQuantite($data['quantite']);
  1022.                 
  1023.                 $annoncesRepository->add($annoncetrue);
  1024.             // METTRE LES IMAGES DANS public/uploads
  1025.                 
  1026.                 
  1027.                 /*$file = $form->get('image')->getData();
  1028.                 if ($file != 'null') {
  1029.                     $path = '/';
  1030.                     $fileName = uniqid() . '-' . $file->getClientOriginalName();
  1031.         
  1032.                     $file->move(
  1033.                         $this->getParameter('annonces_directory') . $path,
  1034.                         $fileName
  1035.                     );
  1036.         
  1037.                 }*/
  1038.                 
  1039.                 $image $data['logo'];
  1040.                 [$imgType$image] = explode(';'$image);
  1041.                 [, $image] = explode(','$image);
  1042.                 [, $extension] = explode('/'$imgType);
  1043.                 $image base64_decode($image);
  1044.                 $fileName uniqid('image') . '.' $extension;
  1045.                 file_put_contents(dirname(__DIR__2) . '/public/uploads/annonces/' $fileName$image);
  1046.                 
  1047.                 $dbImage = new Images();
  1048.                 $dbImage->setLien($fileName);
  1049.                 $dbImage->setAnnonces($annonce);
  1050.                 $imagesRepository->add($dbImagetrue);
  1051.                 
  1052.                 $user->setCredits($newCredit);
  1053.                 $userRepository->add($usertrue);
  1054.                 
  1055.                 $res->setStatusCode(200)
  1056.                     ->setContent(json_encode(array(
  1057.                         'message' => 'Annonce ajoutée'
  1058.                     )));
  1059.             } else {
  1060.                 $res->setStatusCode(301)
  1061.                     ->setContent(json_encode(array(
  1062.                         'message' => 'Pas assez de crédit pour cette annonce'
  1063.                     )));
  1064.                 
  1065.             }
  1066.             
  1067.             return $res;
  1068.             
  1069.         }
  1070.         
  1071.         
  1072.         /**
  1073.          * @Route("/api/updateAnnonce", name="updateAnnonce")
  1074.          */
  1075.         public function updateAnnonce(Request $requestUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1076.         {
  1077.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1078.             header('Access-Control-Allow-Origin: *');
  1079.             header('Content-Type: application/json');
  1080.             header('Access-Control-Allow-Methods: GET, POST');
  1081.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1082.             
  1083.             
  1084.             $credit 1;
  1085.             $ville $villesRepository->findOneById($_GET['ville']);
  1086.             
  1087.             $cat $categoriesRepository->findOneById($_GET['categorie']);
  1088.             
  1089.             $annonce $annoncesRepository->findOneById($_GET['id']);
  1090.             $annonce->setCategories($cat);
  1091.             if (isset($_GET['pro'])) {
  1092.                 $annonce->setVisiblePro(1);
  1093.             }
  1094.             $annonce->setNbVues(0);
  1095.             
  1096.             $annonce->setVilles($ville);
  1097.             if (isset($_GET['booster'])) {
  1098.                 $annonce->setBooster(1);
  1099.                 $credit $credit 4;
  1100.             }
  1101.             
  1102.             
  1103.             $newCredit $user->getCredits() - $credit;
  1104.             
  1105.             if ($user->getCredits() >= $credit) {
  1106.                 
  1107.                 $annonce->setTitre($_GET['titre']);
  1108.                 $annonce->setPrix($_GET['prix']);
  1109.                 $annonce->setDescription($_GET['description']);
  1110.                 $annonce->setPourcentRemise($_GET['pourcent']);
  1111.                 $annonce->setQuantite($_GET['quantite']);
  1112.                 
  1113.                 $annoncesRepository->add($annoncetrue);
  1114. // METTRE LES IMAGES DANS public/uploads
  1115.                 
  1116.                 
  1117.                 // POUR AJOUT DE L'IMAGE
  1118.                 /*$file = $form->get('image')->getData();
  1119.         if ($file != 'null') {
  1120.             $path = '/';
  1121.             $fileName = uniqid() . '-' . $file->getClientOriginalName();
  1122.             $file->move(
  1123.                 $this->getParameter('annonces_directory') . $path,
  1124.                 $fileName
  1125.             );
  1126.         }*/
  1127.                 /*$image = new Images();
  1128.                 $image->setLien($fileName);
  1129.                 $image->setAnnonces($annonce);
  1130.                 $imagesRepository->add($image, true);*/
  1131.                 
  1132.                 
  1133.                 $user->setCredits($newCredit);
  1134.                 $userRepository->add($usertrue);
  1135.                 
  1136.                 $res->setStatusCode(200)
  1137.                     ->setContent(json_encode(array(
  1138.                         'message' => 'Annonce modifiée'
  1139.                     )));
  1140.             } else {
  1141.                 $res->setStatusCode(301)
  1142.                     ->setContent(json_encode(array(
  1143.                         'message' => 'Pas assez de crédit pour modifier cette annonce'
  1144.                     )));
  1145.                 
  1146.             }
  1147.             
  1148.             
  1149.         }
  1150.         
  1151.         
  1152.         /**
  1153.          * @Route("/api/deleteAnnonce", name="deleteAnnonce")
  1154.          */
  1155.         public function deleteAnnonce(Request $requestNotesAnnoncesRepository $notesAnnoncesRepositoryUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1156.         {
  1157.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1158.             header('Access-Control-Allow-Origin: *');
  1159.             header('Content-Type: application/json');
  1160.             header('Access-Control-Allow-Methods: GET, POST');
  1161.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1162.             
  1163.             
  1164.             $annonce $annoncesRepository->findOneById($_GET['id']);
  1165.             
  1166.             $notes $notesAnnoncesRepository->findBy(array('Annonces' => $annonce));
  1167.             foreach ($notes as $res) {
  1168.                 $notesAnnoncesRepository->remove($restrue);
  1169.             }
  1170.             
  1171.             $images $imagesRepository->findBy(array('Annonces' => $annonce));
  1172.             foreach ($images as $res) {
  1173.                 $imagesRepository->remove($restrue);
  1174.             }
  1175.             
  1176.             $annoncesRepository->remove($annoncetrue);
  1177.             
  1178.             $res->setStatusCode(200)
  1179.                 ->setContent(json_encode(array(
  1180.                     'message' => 'Annonce supprimée'
  1181.                 )));
  1182.             
  1183.             
  1184.             return new Response(json_encode($res));
  1185.             
  1186.         }
  1187.         
  1188.         
  1189.         /**
  1190.          * @Route("/api/annoncesProfil", name="annoncesProfil")
  1191.          */
  1192.         public function annoncesProfil(Request $requestNotesAnnoncesRepository $notesAnnoncesRepositoryUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1193.         {
  1194.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1195.             header('Access-Control-Allow-Origin: *');
  1196.             header('Content-Type: application/json');
  1197.             header('Access-Control-Allow-Methods: GET, POST');
  1198.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1199.             
  1200.             $token $_GET['token'] ?? 'test';
  1201.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  1202.             $annoncesReq $annoncesRepository->findBy(array('User' => $user));
  1203.             
  1204.             
  1205.             $i = -1;
  1206.             $annonces = array();
  1207.             
  1208.             foreach ($annoncesReq as $res) {
  1209.                 
  1210.                 
  1211.                 $i++;
  1212.                 $annonces[$i]['id'] = $res->getId();
  1213.                 $annonces[$i]['boost'] = $res->isBooster();
  1214.                 $annonces[$i]['titre'] = $res->getTitre();
  1215.                 $annonces[$i]['enseigne'] = $res->getUser()->getRaisonSociale();
  1216.                 $annonces[$i]['logo'] = 'https://modixom.fr/uploads/logos/' $res->getUser()->getLogo();
  1217.                 $annonces[$i]['images'] = sizeof($res->getImages()) > 'https://modixom.fr/uploads/annonces/' $res->getImages()[0]->getLien() : 'https://modixom.fr/uploads/annonces/default.jpg';
  1218.                 $annonces[$i]['pourcentRemise'] = $res->getPourcentRemise();
  1219.                 $annonces[$i]['prix'] = $res->getPrix();
  1220.                 $annonces[$i]['description'] = $res->getDescription();
  1221.                 $annonces[$i]["quantity"] = $res->getQuantite();
  1222.                 
  1223.                 $pourcent $res->getPourcentRemise() / 100;
  1224.                 $annonces[$i]['newprix'] = $res->getPrix() * $pourcent;
  1225.                 
  1226.                 $fin strtotime($res->getDateFin()->format('Y-m-d H:i:s'));
  1227.                 $debut strtotime(date('Y-m-d H:i:s'));
  1228.                 
  1229.                 
  1230.                 $diff abs($debut $fin); // abs pour avoir la valeur absolute, ainsi éviter d'avoir une différence négative
  1231.                 $retour = array();
  1232.                 
  1233.                 $tmp $diff;
  1234.                 $retour['second'] = $tmp 60;
  1235.                 
  1236.                 $tmp floor(($tmp $retour['second']) / 60);
  1237.                 $retour['minute'] = $tmp 60;
  1238.                 
  1239.                 $tmp floor(($tmp $retour['minute']) / 60);
  1240.                 $retour['hour'] = $tmp 24;
  1241.                 
  1242.                 $tmp floor(($tmp $retour['hour']) / 24);
  1243.                 $retour['day'] = $tmp 24;
  1244.                 
  1245.                 $annonces[$i]['heures'] = $tmp 24 $retour['hour'];
  1246.                 $annonces[$i]['minutes'] = $retour['minute'];
  1247.                 $annonces[$i]['secondes'] = $retour['second'];
  1248.                 
  1249.                 // CALCUL DE LA MOYENNE DE LANNONCE
  1250.                 $sum 0;
  1251.                 $moy 0;
  1252.                 $y 0;
  1253.                 foreach ($res->getNotesAnnonces() as $res2) {
  1254.                     $y++;
  1255.                     $sum $sum $res2->getNote();
  1256.                 }
  1257.                 if ($y 0) {
  1258.                     $moy $sum $y;
  1259.                     
  1260.                     $moyAnnonce = (round($moy 2) / 2);
  1261.                 } else {
  1262.                     $moyAnnonce 0;
  1263.                 }
  1264.                 
  1265.                 $annonces[$i]['moyAnnonce'] = $moyAnnonce;
  1266.                 
  1267.                 
  1268.                 // CALCUL DE LA MOYENNE DE ENSEIGNE
  1269.                 $sum 0;
  1270.                 $moy 0;
  1271.                 $y 0;
  1272.                 
  1273.                 
  1274.                 $notesEns $notesEnseignesRepository->findBy(array('Enseigne' => $res->getUser()));
  1275.                 foreach ($notesEns as $res2) {
  1276.                     $y++;
  1277.                     $sum $sum $res2->getNote();
  1278.                 }
  1279.                 
  1280.                 $maxNote $y 5;
  1281.                 
  1282.                 
  1283.                 if ($maxNote 0) {
  1284.                     $noteEnseigne = ($sum 100) / $maxNote;
  1285.                 } else {
  1286.                     $noteEnseigne 0;
  1287.                 }
  1288.                 
  1289.                 
  1290.                 $annonces[$i]['moyEnseigne'] = $noteEnseigne;
  1291.                 
  1292.                 
  1293.                 if (strtotime($res->getDateDebut()->format('Y-m-d H:i:s')) <= time()
  1294.                     && strtotime($res->getDateFin()->format('Y-m-d H:i:s')) > time()) {
  1295.                     $annonces[$i]['statut'] = 'active';
  1296.                 } else {
  1297.                     $annonces[$i]['statut'] = 'expire';
  1298.                 }
  1299.                 
  1300.             }
  1301.             
  1302.             return new Response(json_encode($annonces));
  1303.         }
  1304.         
  1305.         
  1306.         /**
  1307.          * @Route("/api/noteProduit", name="noteProduit")
  1308.          */
  1309.         public function noteProduit(Request $requestNotesAnnoncesRepository $notesAnnoncesRepositoryUserRepository $userRepositoryImagesRepository $imagesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryAnnoncesRepository $annoncesRepositoryNotesEnseignesRepository $notesEnseignesRepository): Response
  1310.         {
  1311.             //Ajoute une mairie en favoris en fonction de l'id de la mairie et de l'UUID
  1312.             header('Access-Control-Allow-Origin: *');
  1313.             header('Content-Type: application/json');
  1314.             header('Access-Control-Allow-Methods: GET, POST');
  1315.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1316.             
  1317.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1318.                 $res = new Response();
  1319.                 $res->setStatusCode(200)
  1320.                     ->setContent(json_encode(array(
  1321.                         'message' => 'OK'
  1322.                     )));
  1323.                 return $res;
  1324.             }
  1325.             
  1326.             
  1327.             $token $_GET['token'];
  1328.             $data json_decode(file_get_contents('php://input'), true);
  1329.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  1330.             
  1331.             $produit $data['produit'];
  1332.             $note $data['note'];
  1333.             $produit $annoncesRepository->findOneById($produit);
  1334.             $noteAnnonce $notesAnnoncesRepository->findOneBy(['User'=>$user'Annonces'=>$produit]);
  1335.             if(empty($noteAnnonce))
  1336.             {
  1337.             
  1338.             $prod = new NotesAnnonces();
  1339.             
  1340.             //$prod = $notesAnnoncesRepository->findOneById($produit);
  1341.             $prod->setAnnonces($produit);
  1342.             $prod->setNote($note);
  1343.             $prod->setDate(new Datetime());
  1344.             $prod->setUser($user);
  1345.             
  1346.             $notesAnnoncesRepository->add($prodtrue);
  1347.             
  1348.             
  1349.             return new Response('ok');
  1350.             }
  1351.             else
  1352.             {
  1353.                 return new Response('produit déjà noté');
  1354.             }
  1355.         }
  1356.         
  1357.         /**
  1358.          * @Route("/api/addAlertes", name="addAlertesApi")
  1359.          */
  1360.         public function addAlertes(Request $requestAnnoncesRepository $annoncesRepositoryVillesRepository $villesRepositoryCategoriesRepository $categoriesRepositoryUserRepository $userRepository): Response
  1361.         {
  1362.             header('Access-Control-Allow-Origin: *');
  1363.             header('Content-Type: application/json');
  1364.             header('Access-Control-Allow-Methods: GET, POST');
  1365.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1366.             
  1367.             $res = new Response();
  1368.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1369.                 $res->setStatusCode(200)
  1370.                     ->setContent(json_encode(array(
  1371.                         'message' => 'OK'
  1372.                     )));
  1373.                 return $res;
  1374.             }
  1375.             $entityManager $this->getDoctrine()->getManager();
  1376.             $data json_decode(file_get_contents('php://input'), true);
  1377.             $villeId $data['ville'];
  1378.             $rayon $data['rayon'];
  1379.             $categorieId $data['category'];
  1380.             
  1381.             $mail $data['mail'];
  1382.             // Récupérer les instances des entités liées (Villes, Categories, User)
  1383.             $ville $villesRepository->find($villeId);
  1384.             $user $userRepository->findOneBy(['email' => $mail]);
  1385.             foreach($categorieId as $i_value) {
  1386.                 $categorie $categoriesRepository->find($i_value);
  1387.                 // Créer une nouvelle alerte avec les données du formulaire
  1388.                 $alerte = new Alertes();
  1389.                 $alerte->setVilles($ville);
  1390.                 $alerte->setRayon($rayon);
  1391.                 $alerte->setCategories($categorie);
  1392.                 $alerte->setUser($user);
  1393.                 // Enregistrer l'alerte dans la base de données
  1394.                 $entityManager->persist($alerte);
  1395.                 $entityManager->flush();
  1396.             }
  1397.             // Retourner une réponse indiquant que l'alerte a été ajoutée avec succès
  1398.             return new Response("Alerte ajoutée avec succès"Response::HTTP_CREATED);
  1399.         }
  1400.         
  1401.         /**
  1402.          * @Route("/api/getAlertes", name="getAlertes")
  1403.          */
  1404.         public function getAlertes(Request $requestAlertesRepository $alerteRepositoryUserRepository $userRepository): Response {
  1405.             header('Access-Control-Allow-Origin: *');
  1406.             header('Content-Type: application/json');
  1407.             header('Access-Control-Allow-Methods: GET, POST');
  1408.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1409.             
  1410.             $res = new Response();
  1411.             
  1412.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1413.                 $res->setStatusCode(200)
  1414.                     ->setContent(json_encode(array(
  1415.                         'message' => 'OK'
  1416.                     )));
  1417.                 return $res;
  1418.             }
  1419.             
  1420.             $token $_GET['token'] ?? null;
  1421.             $user $userRepository->findOneBy(array('TokenMobile' => $token));
  1422.             $rawAlertes $alerteRepository->findBy(array("User" => $user));
  1423.             $hydratedAlertes = [];
  1424.             foreach ($rawAlertes as $alerte) {
  1425.                 $alerteArray = [];
  1426.                 $alerteArray["id"] = $alerte->getId();
  1427.                 $alerteArray["ville"] = $alerte->getVilles()->getVilleNomReel();
  1428.                 $alerteArray["rayon"] = $alerte->getRayon();
  1429.                 $alerteArray["category"] = $alerte->getCategories()->getLibelle();
  1430.                 $hydratedAlertes[] = $alerteArray;
  1431.             }
  1432.             
  1433.             $res->setStatusCode(200)
  1434.                 ->setContent(json_encode($hydratedAlertes));
  1435.             return $res;
  1436.         }
  1437.         
  1438.         
  1439.         /**
  1440.          * @Route("/api/updateFavoris", name="updateFavoris")
  1441.          */
  1442.         public function updateFavoris(Request $requestAnnoncesRepository $annoncesRepositoryFavorisRepository $favorisRepositoryUserRepository $userRepository): Response {
  1443.             
  1444.             header('Access-Control-Allow-Origin: *');
  1445.             header('Content-Type: application/json');
  1446.             header('Access-Control-Allow-Methods: GET, POST');
  1447.             header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  1448.             
  1449.             $res = new Response();
  1450.             
  1451.             if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
  1452.                 $res->setStatusCode(200)
  1453.                     ->setContent(json_encode(array(
  1454.                         'message' => 'OK'
  1455.                     )));
  1456.                 return $res;
  1457.             }
  1458.             
  1459.             $data json_decode(file_get_contents("php://input"), true);
  1460.             
  1461.             $favorisId $data["favorisId"] ?? null;
  1462.             
  1463.             if ($favorisId) {
  1464.                 $favoris $favorisRepository->findOneById($favorisId);
  1465.                 $favorisRepository->remove($favoristrue);
  1466.                 return new Response("favoris retiré"Response::HTTP_CREATED);
  1467.             }
  1468.             $user $userRepository->findOneById($data["userId"]);
  1469.             $annonce $annoncesRepository->findOneById($data["annonceId"]);
  1470.             $favoris = new Favoris();
  1471.             $favoris->setAnnonces($annonce);
  1472.             $favoris->setUser($user);
  1473.             $favoris->setDate(new Datetime());
  1474.             $favorisRepository->add($favoristrue);
  1475.             
  1476.             return new Response("favoris ajouté"Response::HTTP_CREATED);
  1477.             
  1478.         }
  1479.     }