src/Controller/WhiteListController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use App\Entity\User;
  9. use App\Entity\Sejour;
  10. use App\Entity\SejourWhitelist;
  11. class WhiteListController extends AbstractController
  12. {
  13. /**
  14. * @Route("/favoris", name="get_whitelist")
  15. */
  16. public function index(Request $request)
  17. {
  18. $whitelist = [];
  19. if (isset($_COOKIE['whitelist'])) {
  20. $whitelist = json_decode($_COOKIE['whitelist'], true);
  21. }
  22. $sejours = [];
  23. if (!empty($whitelist)) {
  24. foreach ($whitelist as $id) {
  25. $entityManager = $this->getDoctrine()->getManager();
  26. $sejour = $entityManager->getRepository(Sejour::class)->find($id);
  27. $sejours[] = $sejour;
  28. }
  29. }
  30. return $this->render('white_list/index.html.twig', [
  31. 'whitelist' => $sejours
  32. ]);
  33. }
  34. /**
  35. * @Route("/whitelist/new", name="add_whitelist", methods={"POST"})
  36. */
  37. public function addToWhitelist(Request $request)
  38. {
  39. $sejourId = $request->request->get('sejourId');
  40. // Get the current whitelist from the cookie
  41. $whitelist = [];
  42. if (isset($_COOKIE['whitelist'])) {
  43. $whitelist = json_decode($_COOKIE['whitelist'], true);
  44. }
  45. // Add the new sejour to the whitelist
  46. $whitelist[] = $sejourId;
  47. // Create a new cookie to store the whitelist information
  48. setcookie('whitelistLength', count($whitelist), time() + (86400 * 14), "/"); // 14 day
  49. setcookie('whitelist', json_encode($whitelist), time() + (86400 * 14), "/"); // 14 day
  50. return new JsonResponse(['success' => true]);
  51. }
  52. /**
  53. * @Route("/whitelist/remove/{sejourId}", name="remove_whitelist", methods={"DELETE"})
  54. */
  55. public function removeFromWhitelist($sejourId)
  56. {
  57. if (isset($_COOKIE['whitelist'])) {
  58. $whitelist = json_decode($_COOKIE['whitelist'], true);
  59. if (($key = array_search($sejourId, $whitelist)) !== false) {
  60. unset($whitelist[$key]);
  61. setcookie('whitelist', json_encode($whitelist), time() + (86400 * 14), "/");
  62. setcookie('whitelistLength', count($whitelist), time() + (86400 * 14), "/"); // 14 day
  63. }
  64. }
  65. return new JsonResponse(['success' => true,"message"=>"reomve Whitelist "]);
  66. }
  67. }