<?php
namespace App\Controller;
use App\Service\Location;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Post;
use App\Repository\PostRepository;
use App\Entity\Images;
use App\Repository\ImagesRepository;
use App\Entity\Rate;
use App\Repository\RateRepository;
use App\Service\Helper;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Doctrine\Persistence\ManagerRegistry;
class MainController extends AbstractController
{
#[Route('/', name: 'app_main')]
public function main(): Response
{
$location = new Location;
$records = $location->getLocation();
return $this->render('main/index.html.twig', [
'controller_name' => 'MainController',
'records' => $records,
]);
}
#[Route('/data/getposts', name: 'app_data_getposts_post', methods: ['POST'])]
public function dataGetPostsPost(Request $request, PostRepository $postRepository, ImagesRepository $imagesRepository): Response
{
$coordLatLo = $request->get('latlo');
$coordLatHi = $request->get('lathi');
$coordLonLo = $request->get('lonlo');
$coordLonHi = $request->get('lonhi');
$filter = $request->get('filter');
$postJson = array();
$iPost = 0;
$posts = $postRepository->findByCoord($coordLatLo, $coordLatHi, $coordLonLo, $coordLonHi);
if (!$posts) {
return $this->json(null);
}
foreach($posts as $post) {
$skip = 0;
foreach ($_COOKIE as $key=>$val) {
$post_id = str_replace("Post", "", $key);
if ($post["id"] == $post_id) $skip = 1;
}
if ($filter != "new" || $skip == 0) {
$postJson[$iPost]["Id"] = $post["id"];
$postJson[$iPost]["Latitude"] = $post["Latitude"];
$postJson[$iPost]["Longitude"] = $post["Longitude"];
if ($filter == "new" || $skip == 0) {
$postJson[$iPost]["Rated"] = 0;
}
else {
$postJson[$iPost]["Rated"] = 1;
}
$image = $imagesRepository->findOneByPost($post["id"]);
$filepath = Helper::getFilepath($image->getId(), 'thumbnails');
$filename = 'thumbnail-'.Helper::getFilename($image->getId());
$postJson[$iPost]["Image"] = $filepath.str_replace('.jpg', '.png', $filename);
$iPost = $iPost + 1;
}
}
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$jsonContent = $serializer->serialize($postJson, 'json');
return new Response($jsonContent);
}
#[Route('/data/getposts', name: 'app_data_getposts_get', methods: ['GET'])]
public function dataGetPostsGet(Request $request, PostRepository $postRepository): Response
{
return $this->json('Must POST to this URL');
}
#[Route('/data/getpost/{id}', name: 'app_data_getpost_post', methods: ['POST'])]
public function dataGetPostPost(Request $request, PostRepository $postRepository, ImagesRepository $imagesRepository, RateRepository $rateRepository, $id): Response
{
$post = $postRepository->find($id);
if (!$post) {
return $this->json(null);
}
$cookie_name = "Post".$id;
$cookie_value = "Rated";
$rated = 1;
if(!isset($_COOKIE[$cookie_name])) {
$rated = 0;
}
$avgRating = $rateRepository->avgRating($id);
$postJson = '{ "id": '.$id.', "infraction": "'.$post->getInfraction()->getInfraction().'", "rated": '.$rated.', "rating": '.$avgRating.', "images": [ ';
foreach($post->getImages() as $image) {
$filepath = Helper::getFilepath($image->getId(), 'postimages');
$filename = Helper::getFilename($image->getId());
$postJson .= ' { "url": "'.$filepath.$filename.'" },';
}
$postJson = substr($postJson, 0, -1);
$postJson .= '] }';
$jsonContent = $postJson;
return new Response($jsonContent);
}
#[Route('/data/setrate/{id}', name: 'app_data_setrate_post', methods: ['POST'])]
public function dataSetRatePost(Request $request, PostRepository $postRepository, RateRepository $rateRepository, ManagerRegistry $doctrine, $id): Response
{
$post = $postRepository->find($id);
if (!$post) {
return new Response('Post does not exist');
}
$rating = $_POST['rate'];
$clientIp = $_SERVER['REMOTE_ADDR'];
$cookie_name = "Post".$id;
$cookie_value = "Rated";
$rated = 0;
if(!isset($_COOKIE[$cookie_name])) {
$entityManager = $doctrine->getManager();
$rate = new Rate();
$rate->setRate($rating);
$rate->setIPAddress($clientIp);
$rate->setPost($post);
$rate->setCreateDate(new \DateTime());
$entityManager->persist($rate);
$entityManager->flush();
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
$rated = 1;
}
else {
// This "user" has already rated this post
$rated = 1;
}
$avgRating = $rateRepository->avgRating($id);
return new Response('{ "id": '.$id.', "rated":'.$rated.', "avgrating": '.$avgRating.' }');
}
#[Route('/data/cookies/show', name: 'app_data_cookies_show', methods: ['GET'])]
public function dataCookiesShowGet(Request $request): Response
{
return new Response(print_r($_COOKIE));
}
#[Route('/data/cookies/delete', name: 'app_data_cookies_delete', methods: ['GET'])]
public function dataCookiesDeleteGet(Request $request): Response
{
$ret = "";
foreach ($_COOKIE as $key=>$val)
{
$ret .= 'Delete '.$key.' is '.$val."<br>\n";
unset($_COOKIE[$key]);
setcookie($key, null, -1, "/");
}
return new Response($ret);
}
}