src/Controller/MainController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\Location;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Entity\Post;
  9. use App\Repository\PostRepository;
  10. use App\Entity\Images;
  11. use App\Repository\ImagesRepository;
  12. use App\Entity\Rate;
  13. use App\Repository\RateRepository;
  14. use App\Service\Helper;
  15. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  16. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  17. use Symfony\Component\Serializer\Serializer;
  18. use Doctrine\Persistence\ManagerRegistry;
  19. class MainController extends AbstractController
  20. {
  21.     #[Route('/'name'app_main')]
  22.     public function main(): Response
  23.     {    
  24.         $location = new Location;
  25.         $records $location->getLocation();
  26.     
  27.         return $this->render('main/index.html.twig', [
  28.             'controller_name' => 'MainController',
  29.             'records' => $records,
  30.         ]);
  31.     }
  32.     
  33.     #[Route('/data/getposts'name'app_data_getposts_post'methods: ['POST'])]
  34.     public function dataGetPostsPost(Request $requestPostRepository $postRepositoryImagesRepository $imagesRepository): Response
  35.     {
  36.         $coordLatLo $request->get('latlo');
  37.         $coordLatHi $request->get('lathi');
  38.         $coordLonLo $request->get('lonlo');
  39.         $coordLonHi $request->get('lonhi');
  40.         $filter $request->get('filter');
  41.         
  42.         $postJson = array();
  43.         $iPost 0;
  44.         
  45.         $posts $postRepository->findByCoord($coordLatLo$coordLatHi$coordLonLo$coordLonHi);
  46.         if (!$posts) {
  47.             return $this->json(null);
  48.         }
  49.         
  50.         foreach($posts as $post) {
  51.             $skip 0;
  52.             foreach ($_COOKIE as $key=>$val) {
  53.                 $post_id str_replace("Post"""$key);
  54.                 if ($post["id"] == $post_id$skip 1;
  55.             }
  56.             if ($filter != "new" || $skip == 0) {
  57.                 $postJson[$iPost]["Id"] = $post["id"];
  58.                 $postJson[$iPost]["Latitude"] = $post["Latitude"];
  59.                 $postJson[$iPost]["Longitude"] = $post["Longitude"];
  60.                 if ($filter == "new" || $skip == 0) {
  61.                     $postJson[$iPost]["Rated"] = 0;
  62.                 }
  63.                 else {
  64.                     $postJson[$iPost]["Rated"] = 1;
  65.                 }
  66.                 $image $imagesRepository->findOneByPost($post["id"]);
  67.                 $filepath Helper::getFilepath($image->getId(), 'thumbnails');
  68.                 $filename 'thumbnail-'.Helper::getFilename($image->getId());
  69.                 $postJson[$iPost]["Image"] = $filepath.str_replace('.jpg''.png'$filename);
  70.                 $iPost $iPost 1;
  71.             }
  72.         }
  73.         $encoders = [new JsonEncoder()];
  74.         $normalizers = [new ObjectNormalizer()];
  75.         $serializer = new Serializer($normalizers$encoders);
  76.         
  77.         $jsonContent $serializer->serialize($postJson'json');
  78.         
  79.         return new Response($jsonContent);
  80.     }
  81.     
  82.     #[Route('/data/getposts'name'app_data_getposts_get'methods: ['GET'])]
  83.     public function dataGetPostsGet(Request $requestPostRepository $postRepository): Response
  84.     {
  85.         return $this->json('Must POST to this URL');
  86.     }
  87.     
  88.     #[Route('/data/getpost/{id}'name'app_data_getpost_post'methods: ['POST'])]
  89.     public function dataGetPostPost(Request $requestPostRepository $postRepositoryImagesRepository $imagesRepositoryRateRepository $rateRepository$id): Response
  90.     {
  91.         $post $postRepository->find($id);
  92.         if (!$post) {
  93.             return $this->json(null);
  94.         }
  95.         
  96.         $cookie_name "Post".$id;
  97.         $cookie_value "Rated";
  98.         
  99.         $rated 1;
  100.         if(!isset($_COOKIE[$cookie_name])) {
  101.             $rated 0;
  102.         }
  103.         
  104.         $avgRating $rateRepository->avgRating($id);
  105.         
  106.         $postJson '{ "id": '.$id.', "infraction": "'.$post->getInfraction()->getInfraction().'", "rated": '.$rated.', "rating": '.$avgRating.', "images": [ ';
  107.         
  108.         foreach($post->getImages() as $image) {
  109.             $filepath Helper::getFilepath($image->getId(), 'postimages');
  110.             $filename Helper::getFilename($image->getId());
  111.             $postJson .= ' { "url": "'.$filepath.$filename.'" },';
  112.         }
  113.         $postJson substr($postJson0, -1);
  114.         $postJson .= '] }';
  115.         $jsonContent $postJson;
  116.         
  117.         return new Response($jsonContent);
  118.     }
  119.     
  120.     #[Route('/data/setrate/{id}'name'app_data_setrate_post'methods: ['POST'])]
  121.     public function dataSetRatePost(Request $requestPostRepository $postRepositoryRateRepository $rateRepositoryManagerRegistry $doctrine$id): Response
  122.     {
  123.         $post $postRepository->find($id);
  124.         if (!$post) {
  125.             return new Response('Post does not exist');
  126.         }
  127.         
  128.         $rating $_POST['rate'];
  129.         $clientIp $_SERVER['REMOTE_ADDR'];
  130.         
  131.         $cookie_name "Post".$id;
  132.         $cookie_value "Rated";
  133.         
  134.         $rated 0;
  135.         
  136.         if(!isset($_COOKIE[$cookie_name])) {
  137.             $entityManager $doctrine->getManager();
  138.             
  139.             $rate = new Rate();
  140.             $rate->setRate($rating);
  141.             $rate->setIPAddress($clientIp);
  142.             $rate->setPost($post);
  143.             $rate->setCreateDate(new \DateTime());
  144.             
  145.             $entityManager->persist($rate);
  146.             $entityManager->flush();
  147.             
  148.             setcookie($cookie_name$cookie_valuetime() + (86400 30), "/");
  149.             $rated 1;
  150.         }
  151.         else {
  152.             // This "user" has already rated this post
  153.             $rated 1;
  154.         }
  155.         
  156.         $avgRating $rateRepository->avgRating($id);
  157.         
  158.         return new Response('{ "id": '.$id.', "rated":'.$rated.', "avgrating": '.$avgRating.' }');
  159.     }
  160.     
  161.     #[Route('/data/cookies/show'name'app_data_cookies_show'methods: ['GET'])]
  162.     public function dataCookiesShowGet(Request $request): Response
  163.     {
  164.         return new Response(print_r($_COOKIE));
  165.     }
  166.     
  167.     #[Route('/data/cookies/delete'name'app_data_cookies_delete'methods: ['GET'])]
  168.     public function dataCookiesDeleteGet(Request $request): Response
  169.     {
  170.         $ret "";
  171.         foreach ($_COOKIE as $key=>$val)
  172.         {
  173.             $ret .= 'Delete '.$key.' is '.$val."<br>\n";
  174.             unset($_COOKIE[$key]);
  175.             setcookie($keynull, -1"/");
  176.         }
  177.         return new Response($ret);
  178.     }
  179. }