src/Controller/Api/NotificationsController.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Controller\Response;
  4. use App\Entity\ScheduledNotifications;
  5. use App\Entity\UserNotifications;
  6. use App\Entity\UserViewedNotifications;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\ORM\Query;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @Route("/api/notifications", name="api_notifications_")
  14.  */
  15. class NotificationsController extends AbstractController
  16. {
  17.     use \App\Controller\Request;
  18.     use Response;
  19.     const MAX_NOTIFICATION_COUNT 5;
  20.     /**
  21.      * @Route("/mark_as_read", name="mark_as_read", methods={"POST"})
  22.      */
  23.     public function markAsReadAction(Request $requestEntityManagerInterface $em)
  24.     {
  25.         $user $this->getUser(); // TOKEN
  26.         # get Request json
  27.         $content $request->getContent();
  28.         # parse JSON to Array
  29.         $data \json_decode($contenttrue);
  30.         if (!empty($user->getId()) && !empty($data['notification_id'])) {
  31.             $view $em->getRepository(UserViewedNotifications::class)->findOneUserNotification(
  32.                 $user->getId(),
  33.                 $data['notification_id']
  34.             );
  35.             if (!$view) {
  36.                 $view = new UserViewedNotifications();
  37.                 $view->setUserId($user->getId());
  38.                 $view->setNotificationId($data['notification_id']);
  39.                 $view->setCreatedAt(new \DateTime());
  40.             }
  41.             $em->persist($view);
  42.             $em->flush();
  43.             return $this->jsonResponse();
  44.         }
  45.         return $this->errorJsonResponse();
  46.     }
  47.     /**
  48.      * @Route("/user/{id}", name="user", methods={"GET"})
  49.      */
  50.     public function userNotificationsAction($idRequest $requestEntityManagerInterface $em)
  51.     {
  52.         $user $this->getUser(); // TOKEN
  53.         $lang $this->getQueryLang($request);
  54.         $lang = (strtolower($lang) == 'en' 'En' '');
  55.         $qb $em->createQueryBuilder();
  56.         $qb->select(
  57.             [
  58.                 'sn.id',
  59.                 'sn.notificationTitle' $lang ' as notificationTitle',
  60.                 'sn.notificationPreview' $lang ' as notificationPreview',
  61.                 'sn.scheduledAt',
  62.                 'uwn.createdAt as viewed_at',
  63.             ]
  64.         );
  65.         $qb->from(ScheduledNotifications::class, 'sn');
  66.         $qb->leftJoin(
  67.             UserViewedNotifications::class,
  68.             'uwn',
  69.             'WITH',
  70.             'sn.id = uwn.notificationId AND uwn.userId = :user'
  71.         );
  72.     $qb->andWhere('sn.removedAt IS NULL AND sn.runnedAt IS NOT NULL');
  73.         if (!$user->hasValidSubscription()) {
  74.             $qb->andWhere('sn.pro_users = 0');
  75.         }
  76.         $qb->setParameter('user'$user->getId());
  77.     $qb->orderBy('sn.runnedAt''DESC');
  78.     $qb->setMaxResults(NotificationsController::MAX_NOTIFICATION_COUNT);
  79.         $notifications $qb->getQuery()->getArrayResult();
  80.         if (count($notifications) > 0) {
  81.             foreach ($notifications as &$notification) {
  82.                 $notification['notificationPreview'] = $this->removeLastWord($notification['notificationPreview']) . '...';
  83.             }
  84.         }
  85.         return $this->jsonResponse(['data' => $notifications]);
  86.     }
  87.     /**
  88.      * @Route("/view/{id}", name="view", methods={"GET"})
  89.      */
  90.     public function viewNotificationAction($idRequest $requestEntityManagerInterface $em)
  91.     {
  92.         $lang $this->getQueryLang($request);
  93.         $lang = (strtolower($lang) == 'en' 'En' '');
  94.         $notification $em->createQueryBuilder()
  95.             ->select(
  96.                 [
  97.                     'sn.id',
  98.                     'sn.notificationTitle' $lang ' as notificationTitle',
  99.                     'sn.notificationBody' $lang ' as notificationBody',
  100.                     'sn.notificationPreview' $lang ' as notificationPreview',
  101.                     'sn.forwardTo',
  102.                     'sn.scheduledAt',
  103.                 ]
  104.             )
  105.             ->from(ScheduledNotifications::class, 'sn')
  106.             ->where('sn.id = :id AND sn.removedAt IS NULL AND sn.runnedAt IS NOT NULL')
  107.             ->setParameter('id'$id)
  108.             ->orderBy('sn.runnedAt''DESC')
  109.             ->setMaxResults(1)
  110.             ->getQuery()
  111.             ->getOneOrNullResult(Query::HYDRATE_ARRAY);
  112.         return $this->jsonResponse(['data' => $notification]);
  113.     }
  114.     /**
  115.      * @Route("/unread/{id}", name="unread", methods={"GET"})
  116.      */
  117.     public function unreadNotificationsAction($idRequest $requestEntityManagerInterface $em)
  118.     {
  119.         $user $this->getUser(); // TOKEN
  120.         $count $em->getRepository(ScheduledNotifications::class)->createQueryBuilder('sn')
  121.             ->select('count(sn.id)')
  122.             ->leftJoin(
  123.                 UserViewedNotifications::class,
  124.                 'uwn',
  125.                 'WITH',
  126.                 'sn.id = uwn.notificationId AND uwn.userId = :user'
  127.             )
  128.             ->setParameter('user'$user->getId())
  129.             ->where('sn.removedAt IS NULL AND sn.runnedAt IS NOT NULL AND uwn.id IS NULL')
  130.             ->getQuery()
  131.             ->getSingleScalarResult();
  132.         $objective $em->getRepository(UserNotifications::class)
  133.             ->createQueryBuilder('un')
  134.             ->select('count(un.id)')
  135.             ->where('un.userId = :user AND un.seenAt IS NULL ')
  136.             ->setParameter('user'$user->getId())
  137.             ->getQuery()
  138.         ->getSingleScalarResult();
  139.     $count min(intval($count), NotificationsController::MAX_NOTIFICATION_COUNT);
  140.         return $this->jsonResponse(['data' => intval($count), 'objective' => intval($objective)]);
  141.     }
  142.     /*
  143.      * +---------------------------------------------------+
  144.      * |            OBJECTIVES                             |
  145.      * +---------------------------------------------------+
  146.      */
  147.     /**
  148.      * @Route("/objective/user/{id}", name="objective_user", methods={"GET"})
  149.      */
  150.     public function objectiveNotificationsAction($idRequest $requestEntityManagerInterface $em)
  151.     {
  152.         $user $this->getUser();
  153.         $notifications $em->createQueryBuilder()
  154.             ->select(['on.id''on.notificationTitle''on.notificationPreview''on.seenAt''on.createdAt'])
  155.             ->from(UserNotifications::class, 'on')
  156.             ->where('on.userId = :user')
  157.             ->setParameter('user'$user->getId())
  158.             ->orderBy('on.id''DESC')
  159.             ->getQuery()
  160.             ->getArrayResult();
  161.         if (count($notifications) > 0) {
  162.             foreach ($notifications as &$notification) {
  163.                 $notification['notificationPreview'] = $this->removeLastWord($notification['notificationPreview']) . '...';
  164.             }
  165.         }
  166.         return $this->jsonResponse(['data' => $notifications]);
  167.     }
  168.     /**
  169.      * @Route("/objective/mark_as_read", name="objective_mark_as_read", methods={"POST"})
  170.      */
  171.     public function markAsReadObjectiveNotificationAction(Request $requestEntityManagerInterface $em)
  172.     {
  173.         $user $this->getUser();
  174.         # get Request json
  175.         $content $request->getContent();
  176.         # parse JSON to Array
  177.         $data \json_decode($contenttrue);
  178.         if (!empty($data['notification_id'])) {
  179.             $notification $em->createQueryBuilder()
  180.                 ->select('on')
  181.                 ->from(UserNotifications::class, 'on')
  182.                 ->where('on.userId = :user AND on.id = :notification')
  183.                 ->setParameter('user'$user->getId())
  184.                 ->setParameter('notification'$data['notification_id'])
  185.                 ->setMaxResults(1)
  186.                 ->getQuery()
  187.                 ->getOneOrNullResult();
  188.             if ($notification) {
  189.                 $notification->setSeenAt(new \DateTime());
  190.                 $em->persist($notification);
  191.                 $em->flush();
  192.             }
  193.             return $this->jsonResponse([]);
  194.         }
  195.         return $this->errorJsonResponse([]);
  196.     }
  197.     /**
  198.      * @Route("/objective/view/{id}", name="objective_view", methods={"GET"})
  199.      */
  200.     public function viewObjectNotificationAction($idRequest $requestEntityManagerInterface $em)
  201.     {
  202.         $notification $em->createQueryBuilder()
  203.             ->select('on')
  204.             ->from(UserNotifications::class, 'on')
  205.             ->where('on.id = :id')
  206.             ->setParameter('id'$id)
  207.             ->setMaxResults(1)
  208.             ->getQuery()
  209.             ->getOneOrNullResult(Query::HYDRATE_ARRAY);
  210.         return $this->jsonResponse(['data' => $notification]);
  211.     }
  212.     private function removeLastWord($string)
  213.     {
  214.         $words explode(" "$string);
  215.         array_splice($words, -1);
  216.         return implode(" "$words);
  217.     }
  218. }