<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\User;
use App\Entity\Sejour;
use App\Entity\SejourWhitelist;
class WhiteListController extends AbstractController
{
/**
* @Route("/favoris", name="get_whitelist")
*/
public function index(Request $request)
{
$whitelist = [];
if (isset($_COOKIE['whitelist'])) {
$whitelist = json_decode($_COOKIE['whitelist'], true);
}
$sejours = [];
if (!empty($whitelist)) {
foreach ($whitelist as $id) {
$entityManager = $this->getDoctrine()->getManager();
$sejour = $entityManager->getRepository(Sejour::class)->find($id);
$sejours[] = $sejour;
}
}
return $this->render('white_list/index.html.twig', [
'whitelist' => $sejours
]);
}
/**
* @Route("/whitelist/new", name="add_whitelist", methods={"POST"})
*/
public function addToWhitelist(Request $request)
{
$sejourId = $request->request->get('sejourId');
// Get the current whitelist from the cookie
$whitelist = [];
if (isset($_COOKIE['whitelist'])) {
$whitelist = json_decode($_COOKIE['whitelist'], true);
}
// Add the new sejour to the whitelist
$whitelist[] = $sejourId;
// Create a new cookie to store the whitelist information
setcookie('whitelistLength', count($whitelist), time() + (86400 * 14), "/"); // 14 day
setcookie('whitelist', json_encode($whitelist), time() + (86400 * 14), "/"); // 14 day
return new JsonResponse(['success' => true]);
}
/**
* @Route("/whitelist/remove/{sejourId}", name="remove_whitelist", methods={"DELETE"})
*/
public function removeFromWhitelist($sejourId)
{
if (isset($_COOKIE['whitelist'])) {
$whitelist = json_decode($_COOKIE['whitelist'], true);
if (($key = array_search($sejourId, $whitelist)) !== false) {
unset($whitelist[$key]);
setcookie('whitelist', json_encode($whitelist), time() + (86400 * 14), "/");
setcookie('whitelistLength', count($whitelist), time() + (86400 * 14), "/"); // 14 day
}
}
return new JsonResponse(['success' => true,"message"=>"reomve Whitelist "]);
}
}