<?phpnamespace App\Controller\Api;use App\Entity\Core\Notifications;use App\Services\Dossiers;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\HttpFoundation\JsonResponse;use Doctrine\ORM\EntityManagerInterface;use Knp\Component\Pager\PaginatorInterface;use Symfony\Component\Form\Extension\Core\Type\CheckboxType;use Symfony\Component\Form\Extension\Core\Type\ChoiceType;use Symfony\Component\Form\Extension\Core\Type\DateTimeType;use Symfony\Component\Form\Extension\Core\Type\EmailType;use Symfony\Component\Form\Extension\Core\Type\HiddenType;use Symfony\Component\Form\Extension\Core\Type\NumberType;use Symfony\Component\Form\Extension\Core\Type\SubmitType;use Symfony\Component\Form\Extension\Core\Type\TextareaType;use Symfony\Component\Form\Extension\Core\Type\TextType;use Symfony\Component\HttpFoundation\Session\Session;class NotificationsController extends AbstractController{ private $em; private $paginator; private $dossier; public function __construct(EntityManagerInterface $em, Dossiers $dossier, PaginatorInterface $paginator ) { $this->em = $em; $this->paginator = $paginator; $this->dossier = $dossier; } /** * Notifications * @param Request $request * @return JsonResponse */ public function recents(Request $request) { $user = $this->getUser(); $limit = $request->query->get('limit', 20); $queries = $this->em->getRepository(Notifications::class)->recents($user,$limit); $array = []; foreach ($queries as $query) { $createdAt = ""; if($query->getCreatedAt() !== null) { $createdAt = $query->getCreatedAt()->format('c'); } $array[] = [ "id" => (string)$query->getId(), "type" => (string)$query->getType(), "title" => (string)$query->getTitle(), "description" => (string)$query->getDescription(), "createdAt" => $createdAt, "viewed" => (bool)$query->isViewed(), ]; $query->setViewed(true); $this->em->persist($query); $this->em->flush(); } return new JsonResponse($array); } public function dashboardRecents(Request $request) { $user = $this->getUser(); $queries = $this->em->getRepository(Notifications::class)->dashboardRecents($user); $array = []; foreach ($queries as $query) { $createdAt = ""; if($query->getCreatedAt() !== null) { $createdAt = $query->getCreatedAt()->format('c'); } $array[] = [ "id" => (string)$query->getId(), "type" => (string)$query->getType(), "title" => (string)$query->getTitle(), "description" => (string)$query->getDescription(), "createdAt" => $createdAt, "viewed" => (bool)$query->isViewed(), ]; $query->setViewed(true); $this->em->persist($query); $this->em->flush(); } return new JsonResponse($array); } /** * Supprimer * @param Notifications $notification * @return JsonResponse */ public function delete(Notifications $notification) { $user = $this->getUser(); if($notification->getUser() !== $user) { return new JsonResponse(false); } $this->em->remove($notification); $this->em->flush(); return new JsonResponse(true); } /** * Supprimer toutes les notifications * @param Notifications $notification * @return JsonResponse */ public function clearAll(Request $request) { $user = $this->getUser(); $queries = $this->em->getRepository(Notifications::class)->findBy(["user" => $user]); foreach ($queries as $query) { $this->em->remove($query); $this->em->flush(); } return new JsonResponse(true); } public function readAll(Request $request) { $user = $this->getUser(); $queries = $this->em->getRepository(Notifications::class)->findBy(["user" => $user]); foreach ($queries as $query) { $query->setViewed(true); $this->em->persist($query); $this->em->flush(); } return new JsonResponse(true); } /** * Mise à jour de la lecture. * @param Notifications $notification * @return JsonResponse */ public function viewed(Notifications $notification) { $user = $this->getUser(); if($notification->getUser() !== $user) { return new JsonResponse(false); } $notification->setViewed(true); $this->em->persist($notification); $this->em->flush(); return new JsonResponse(true); }}