src/Controller/Api/ApiBrowserNotificationsController.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * apiNotificationsEnableController.php.
  4.  *
  5.  * @copyright 2016-2017 Oecko
  6.  */
  7. namespace App\Controller\Api;
  8. use App\Entity\Notification;
  9. use App\Utils\ApiController;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use FOS\RestBundle\View\View;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * Api controller for user profile management.
  18.  */
  19. #[Route(path'/api')]
  20. class ApiBrowserNotificationsController extends ApiController
  21. {
  22.     public function __construct(private readonly ManagerRegistry $doctrine)
  23.     {
  24.     }
  25.     #[Route(path'/notifications/preferences'methods: ['POST'])]
  26.     #[IsGranted('ROLE_NOTIFICATION_CENTER')]
  27.     public function postNotificationsPreferences(Request $request): Response
  28.     {
  29.         $em $this->doctrine->getManager();
  30.         $user $this->getUser();
  31.         $notifType $request->get('notifType');
  32.         $isChecked $request->get('isChecked');
  33.         if (str_contains(strtolower((string) $notifType), 'tenant')) {
  34.             if (!$user->hasRole('ROLE_DOC_ADMIN_SPACE')) {
  35.                 return new View('Accès non authorisé'Response::HTTP_FORBIDDEN);
  36.             }
  37.         }
  38.         if (str_contains(strtolower((string) $notifType), 'publication')) {
  39.             if (!$user->hasRole('ROLE_PUBLICPAGE')) {
  40.                 return new View('Accès non authorisé'Response::HTTP_FORBIDDEN);
  41.             }
  42.         }
  43.         if (str_contains(strtolower((string) $notifType), 'privatemessage')) {
  44.             if (!$user->hasRole('ROLE_READ_MESSAGE')) {
  45.                 return new View('Accès non authorisé'Response::HTTP_FORBIDDEN);
  46.             }
  47.         }
  48.         if ($notifType) {
  49.             if ('true' == $isChecked) {
  50.                 $user->addNotifType($notifType);
  51.             } else {
  52.                 $user->removeNotifType($notifType);
  53.             }
  54.         }
  55.         $em->persist($user);
  56.         $em->flush();
  57.         return $this->res(['message' => 'Préférences enregistrées']);
  58.     }
  59.     /**
  60.      *
  61.      * @return Response
  62.      */
  63.     #[Route(path'/notifications')]
  64.     #[IsGranted('ROLE_NOTIFICATION_CENTER')]
  65.     public function getUserNotifications()
  66.     {
  67.         $notifications $this->doctrine->getRepository(Notification::class)->findBy(
  68.             ['recipient' => $this->getUser()],
  69.             ['date' => 'ASC']
  70.         );
  71.         return $this->res($notifications);
  72.     }
  73.     /**
  74.      *
  75.      * @return Response
  76.      */
  77.     #[Route(path'/notifications/{id}'methods: ['DELETE'])]
  78.     #[IsGranted('ROLE_NOTIFICATION_CENTER')]
  79.     public function deleteNotif($id)
  80.     {
  81.         $notificationDelete $this->doctrine->getRepository(Notification::class)->find($id);
  82.         if (null === $notificationDelete) {
  83.             return new $this->err('La notification n\'existe pas.');
  84.         }
  85.         $em $this->doctrine->getManager();
  86.         $em->remove($notificationDelete);
  87.         $em->flush();
  88.         return $this->res(['message' => 'La notification a bien été supprimée']);
  89.     }
  90. }