src/Controller/Admin/NotificationController.php line 220

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Admin;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use EADPlataforma\Entity\Notification;
  7. use EADPlataforma\Enum\NotificationEnum;
  8. use EADPlataforma\Enum\ErrorEnum;
  9. /**
  10.  * @Route(
  11.  *      schemes         = {"http|https"}
  12.  * )
  13.  * @Cache(
  14.  *      maxage          = "0",
  15.  *      smaxage         = "0",
  16.  *      expires         = "now",
  17.  *      public          = false
  18.  * )
  19.  */
  20. class NotificationController extends AbstractController {
  21.     public function getEntityClass(){
  22.         return Notification::class;
  23.     }
  24.     /**
  25.      * @Route(
  26.      *      path          = "/admin/v2/user/{userId}/notifications/read",
  27.      *      methods       = {"GET"}
  28.      * )
  29.      */
  30.     public function getNotificationReadPaginateNew(Request $request) {
  31.         $this->requestUtil->setRequest($request)->setData();
  32.         $columns = [
  33.             "n.id",
  34.             "n.origin",
  35.             "n.reference",
  36.             "n.status",
  37.             "DATE_FORMAT(n.date, '%Y-%m-%d %H:%i:%s') AS date",
  38.             "n.title",
  39.             "n.description",
  40.             "n.link",
  41.             "n.icon",
  42.         ];
  43.         $joins = [];
  44.         $userId $this->user->getId();
  45.         $orderParam $this->requestUtil->getField('order');
  46.         $searchText $this->requestUtil->getField('searchText');   
  47.         $limit = (int)$this->requestUtil->getField('limit');
  48.         $offset = (int)$this->requestUtil->getField('offset');
  49.         $filter = [
  50.             "n.userTo" => $userId,
  51.             "n.status" => NotificationEnum::VIEWED
  52.         ];
  53.         $order = [ "n.id" => "DESC" ];
  54.         if(!empty($orderParam)){
  55.             $order json_decode($orderParamtrue);
  56.         }
  57.         $data $this->repository->paginate(
  58.             "n",
  59.             $searchText,
  60.             $columns,
  61.             $joins,
  62.             $filter,
  63.             $order,
  64.             $limit,
  65.             $offset,
  66.             null,
  67.             false,
  68.             [],
  69.             false
  70.         );
  71.         return $this->eadResponseNew($data);
  72.     }
  73.     /**
  74.      * @Route(
  75.      *      path          = "/admin/v2/user/{userId}/notifications/unread",
  76.      *      methods       = {"GET"}
  77.      * )
  78.      */
  79.     public function getNotificationUnreadPaginateNew(Request $request) {
  80.         $this->requestUtil->setRequest($request)->setData();
  81.         $columns = [
  82.             "n.id",
  83.             "n.origin",
  84.             "n.reference",
  85.             "n.status",
  86.             "DATE_FORMAT(n.date, '%Y-%m-%d %H:%i:%s') AS date",
  87.             "n.title",
  88.             "n.description",
  89.             "n.link",
  90.             "n.icon",
  91.         ];
  92.         $joins = [];
  93.         $userId $this->user->getId();
  94.         $orderParam $this->requestUtil->getField('order');
  95.         $searchText $this->requestUtil->getField('searchText');   
  96.         $limit = (int)$this->requestUtil->getField('limit');
  97.         $offset = (int)$this->requestUtil->getField('offset');
  98.         $filter = [
  99.             "n.userTo" => $userId,
  100.             "n.status" => NotificationEnum::NOT_VIEWED
  101.         ];
  102.         $order = [ "n.id" => "DESC" ];
  103.         if(!empty($orderParam)){
  104.             $order json_decode($orderParamtrue);
  105.         }
  106.         $data $this->repository->paginate(
  107.             "n",
  108.             $searchText,
  109.             $columns,
  110.             $joins,
  111.             $filter,
  112.             $order,
  113.             $limit,
  114.             $offset,
  115.             null,
  116.             false,
  117.             [],
  118.             false
  119.         );
  120.         return $this->eadResponseNew($data);
  121.     }
  122.     /**
  123.      * @Route(
  124.      *      path          = "/admin/v2/user/{userId}/notifications/unread/total",
  125.      *      methods       = {"GET"}
  126.      * )
  127.      */
  128.     public function getUserNumberUnreadNotificationNew(Request $request) {
  129.         $userId $this->user->getId();
  130.         $number $this->repository->count([ 
  131.             "userTo" => $userId
  132.             "status" =>  NotificationEnum::NOT_VIEWED,
  133.         ]);
  134.         return $this->eadResponseNew([ "total" => $number ]);
  135.     }
  136.     /**
  137.      * @Route(
  138.      *      path          = "/v2/user/{userId}/notifications/{notificationId}",
  139.      *      methods       = {"GET"},
  140.      *      name          = "seeNotificationNew"
  141.      * )
  142.      */
  143.     public function seeNotificationNew(Request $request) {
  144.         $this->checkUserSession($request);
  145.         $notificationId $request->get('notificationId');
  146.         $notification $this->repository->find($notificationId);
  147.     
  148.         if(!$notification){
  149.             return $this->redirectToRoute('notFound');
  150.         }
  151.         if($this->user->getId() != $notification->getUserTo()->getId()){
  152.             return $this->redirectToRoute('notFound');
  153.         }
  154.         $notification->setStatus(NotificationEnum::VIEWED);
  155.         $this->em->flush();
  156.         return $this->redirect("//{$request->getHost()}{$notification->getLink()}"301);
  157.     }
  158.     /**
  159.      * @Route(
  160.      *      path          = "/admin/v2/user/{userId}/notifications",
  161.      *      methods       = {"PATCH"}
  162.      * )
  163.      */
  164.     public function readAllNotificationNew(Request $request) {
  165.         $userId $this->user->getId();
  166.         
  167.         $notifications $this->repository->findBy([ 
  168.             "userTo" => $userId,
  169.             "status" => NotificationEnum::NOT_VIEWED
  170.         ]);
  171.         foreach ($notifications as $key => $notification) {
  172.             $notification->setStatus(NotificationEnum::VIEWED);
  173.         }
  174.         $this->em->flush();
  175.         return $this->eadResponseNew([ "message" => "Success" ]);
  176.     }
  177.     /**
  178.      * @Route(
  179.      *      path          = "/admin/notification/user/list/paginate",
  180.      *      methods       = {"GET"}
  181.      * )
  182.      */
  183.     public function getNotificationPaginate(Request $request) {
  184.         $this->requestUtil->setRequest($request)->setData();
  185.         $columns = [
  186.             "n.id",
  187.             "n.origin",
  188.             "n.reference",
  189.             "n.status",
  190.             "DATE_FORMAT(n.date, '%Y-%m-%d %H:%i:%s') AS date",
  191.             "n.title",
  192.             "n.description",
  193.             "n.link",
  194.             "n.icon",
  195.         ];
  196.         $joins = [];
  197.         $userId $this->user->getId();
  198.         $status $this->requestUtil->getField('status');
  199.         $orderParam $this->requestUtil->getField('order');
  200.         $searchText $this->requestUtil->getField('searchText');   
  201.         $limit = (int)$this->requestUtil->getField('limit');
  202.         $offset = (int)$this->requestUtil->getField('offset');
  203.         $joins = [];
  204.         $filter = [
  205.             "n.userTo" => $userId
  206.         ];
  207.         if(!is_null($status)){
  208.             $filter["n.status"] = (int)$status;
  209.         }
  210.         $order = [ "n.status" => "ASC""n.id" => "DESC" ];
  211.         if(!empty($orderParam)){
  212.             $order json_decode($orderParamtrue);
  213.         }
  214.         $data $this->repository->paginate(
  215.             "n",
  216.             $searchText,
  217.             $columns,
  218.             $joins,
  219.             $filter,
  220.             $order,
  221.             $limit,
  222.             $offset,
  223.             null,
  224.             false,
  225.             [],
  226.             false
  227.         );
  228.         $numberTotal $this->repository->count([ 
  229.             "userTo" => $userId
  230.         ]);
  231.         $numberNotRead $this->repository->count([ 
  232.             "userTo" => $userId,
  233.             "status" =>  NotificationEnum::NOT_VIEWED,
  234.         ]);
  235.         $data['total']   = $numberTotal;
  236.         $data['notRead'] = $numberNotRead;
  237.         return $this->eadResponse($data);
  238.     }
  239.     /**
  240.      * @Route(
  241.      *      path          = "/admin/notification/user",
  242.      *      methods       = {"GET"}
  243.      * )
  244.      */
  245.     public function getUserNotification(Request $request) {
  246.         $userId $this->user->getId();
  247.         $data $this->repository->findBy([ 
  248.             "userTo" => $userId
  249.         ]);
  250.         return $this->eadResponse($data);
  251.     }
  252.     /**
  253.      * @Route(
  254.      *      path          = "/admin/notification/user/number",
  255.      *      methods       = {"GET"}
  256.      * )
  257.      */
  258.     public function getUserNumberNotification(Request $request) {
  259.         $userId $this->user->getId();
  260.         $number $this->repository->count([ 
  261.             "userTo" => $userId
  262.             "status" =>  NotificationEnum::NOT_VIEWED,
  263.         ]);
  264.         return $this->eadResponse([ "total" => $number ]);
  265.     }
  266.     /**
  267.      * @Route(
  268.      *      path          = "/admin/notification/user/new",
  269.      *      methods       = {"GET"}
  270.      * )
  271.      */
  272.     public function getUserNewNotification(Request $request) {
  273.         $userId $this->user->getId();
  274.         
  275.         $data $this->repository->findBy(
  276.             [ 
  277.                 "userTo" => $userId
  278.             ], 
  279.             [ 
  280.                 "status" => "ASC"
  281.                 "date" => "DESC" 
  282.             ], 
  283.             5
  284.         );
  285.         return $this->eadResponse($data);
  286.     }
  287.     /**
  288.      * @Route(
  289.      *      path          = "/notification/see/{notificationId}",
  290.      *      methods       = {"GET"},
  291.      *      name          = "seeNotification"
  292.      * )
  293.      */
  294.     public function seeNotification(Request $request) {
  295.         $this->checkUserSession($request);
  296.         $notificationId $request->get('notificationId');
  297.         $notification $this->repository->find($notificationId);
  298.     
  299.         if(!$notification){
  300.             return $this->redirectToRoute('notFound');
  301.         }
  302.         if($this->user->getId() != $notification->getUserTo()->getId()){
  303.             return $this->redirectToRoute('notFound');
  304.         }
  305.         $notification->setStatus(NotificationEnum::VIEWED);
  306.         $this->em->flush();
  307.         return $this->redirect("//{$request->getHost()}{$notification->getLink()}"301);
  308.     }
  309.     /**
  310.      * @Route(
  311.      *      path          = "/admin/notification/read/{notificationId}",
  312.      *      methods       = {"PUT"}
  313.      * )
  314.      */
  315.     public function readNotification(Request $request) {
  316.         $notificationId $request->get('notificationId');
  317.         $notification $this->repository->find($notificationId);
  318.         if(!$notification){
  319.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  320.         }
  321.         $notification->setStatus(NotificationEnum::VIEWED);
  322.         $this->em->flush();
  323.         return $this->eadResponse([ "message" => "Success" ]);
  324.     }
  325.     /**
  326.      * @Route(
  327.      *      path          = "/admin/notification/user/read/all",
  328.      *      methods       = {"PUT"}
  329.      * )
  330.      */
  331.     public function readAllNotification(Request $request) {
  332.         $userId $this->user->getId();
  333.         
  334.         $notifications $this->repository->findBy([ 
  335.             "userTo" => $userId,
  336.             "status" => NotificationEnum::NOT_VIEWED
  337.         ]);
  338.         foreach ($notifications as $key => $notification) {
  339.             $notification->setStatus(NotificationEnum::VIEWED);
  340.         }
  341.         $this->em->flush();
  342.         return $this->eadResponse([ "message" => "Success" ]);
  343.     }
  344. }