vendor/pimcore/pimcore/bundles/AdminBundle/Controller/Admin/NotificationController.php line 161

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. declare(strict_types=1);
  15. namespace Pimcore\Bundle\AdminBundle\Controller\Admin;
  16. use Pimcore\Bundle\AdminBundle\Controller\AdminController;
  17. use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
  18. use Pimcore\Model\Element\Service;
  19. use Pimcore\Model\Notification\Service\NotificationService;
  20. use Pimcore\Model\Notification\Service\NotificationServiceFilterParser;
  21. use Pimcore\Model\Notification\Service\UserService;
  22. use Pimcore\Model\User;
  23. use Pimcore\Translation\Translator;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. /**
  27.  * @Route("/notification")
  28.  */
  29. class NotificationController extends AdminController
  30. {
  31.     /**
  32.      * @Route("/recipients", name="pimcore_admin_notification_recipients", methods={"GET"})
  33.      *
  34.      * @param UserService $service
  35.      * @param Translator $translator
  36.      *
  37.      * @return JsonResponse
  38.      */
  39.     public function recipientsAction(UserService $serviceTranslator $translator): JsonResponse
  40.     {
  41.         $this->checkPermission('notifications_send');
  42.         $data = [];
  43.         foreach ($service->findAll($this->getAdminUser()) as $recipient) {
  44.             $group $translator->trans('group');
  45.             $prefix $recipient->getType() == 'role' $group ' - ' '';
  46.             $data[] = [
  47.                 'id' => $recipient->getId(),
  48.                 'text' => $prefix $recipient->getName()
  49.             ];
  50.         }
  51.         return $this->adminJson($data);
  52.     }
  53.     /**
  54.      * @Route("/send", name="pimcore_admin_notification_send", methods={"POST"})
  55.      *
  56.      * @param Request $request
  57.      * @param NotificationService $service
  58.      *
  59.      * @return JsonResponse
  60.      */
  61.     public function sendAction(Request $requestNotificationService $service): JsonResponse
  62.     {
  63.         $this->checkPermission('notifications_send');
  64.         $recipientId = (int) $request->get('recipientId'0);
  65.         $fromUser = (int) $this->getAdminUser()->getId();
  66.         $title $request->get('title''');
  67.         $message $request->get('message''');
  68.         $elementId $request->get('elementId');
  69.         $elementType $request->get('elementType''');
  70.         $element Service::getElementById($elementType$elementId);
  71.         if (User::getById($recipientId) instanceof User) {
  72.             $service->sendToUser($recipientId$fromUser$title$message$element);
  73.         } else {
  74.             $service->sendToGroup($recipientId$fromUser$title$message$element);
  75.         }
  76.         return $this->adminJson(['success' => true]);
  77.     }
  78.     /**
  79.      * @Route("/find", name="pimcore_admin_notification_find")
  80.      *
  81.      * @param Request $request
  82.      * @param NotificationService $service
  83.      *
  84.      * @return JsonResponse
  85.      */
  86.     public function findAction(Request $requestNotificationService $service): JsonResponse
  87.     {
  88.         $this->checkPermission('notifications');
  89.         $id = (int) $request->get('id'0);
  90.         $notification $service->findAndMarkAsRead($id$this->getAdminUser()->getId());
  91.         $data $service->format($notification);
  92.         return $this->adminJson([
  93.             'success' => true,
  94.             'data' => $data,
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/find-all", name="pimcore_admin_notification_findall")
  99.      *
  100.      * @param Request $request
  101.      * @param NotificationService $service
  102.      *
  103.      * @return JsonResponse
  104.      */
  105.     public function findAllAction(Request $requestNotificationService $service): JsonResponse
  106.     {
  107.         $this->checkPermission('notifications');
  108.         $filter = ['recipient = ?' => (int) $this->getAdminUser()->getId()];
  109.         $parser = new NotificationServiceFilterParser($request);
  110.         foreach ($parser->parse() as $key => $val) {
  111.             $filter[$key] = $val;
  112.         }
  113.         $options = [
  114.             'offset' => $request->get('start'0),
  115.             'limit' => $request->get('limit'40)
  116.         ];
  117.         $result $service->findAll($filter$options);
  118.         $data = [];
  119.         foreach ($result['data'] as $notification) {
  120.             $data[] = $service->format($notification);
  121.         }
  122.         return $this->adminJson([
  123.             'success' => true,
  124.             'total' => $result['total'],
  125.             'data' => $data,
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route("/find-last-unread", name="pimcore_admin_notification_findlastunread")
  130.      *
  131.      * @param Request $request
  132.      * @param NotificationService $service
  133.      *
  134.      * @return JsonResponse
  135.      */
  136.     public function findLastUnreadAction(Request $requestNotificationService $service): JsonResponse
  137.     {
  138.         $this->checkPermission('notifications');
  139.         $user $this->getAdminUser();
  140.         $lastUpdate = (int) $request->get('lastUpdate'time());
  141.         $result $service->findLastUnread((int) $user->getId(), $lastUpdate);
  142.         $unread $service->countAllUnread((int) $user->getId());
  143.         $data = [];
  144.         foreach ($result['data'] as $notification) {
  145.             $data[] = $service->format($notification);
  146.         }
  147.         return $this->adminJson([
  148.             'success' => true,
  149.             'total' => $result['total'],
  150.             'data' => $data,
  151.             'unread' => $unread,
  152.         ]);
  153.     }
  154.     /**
  155.      * @Route("/mark-as-read", name="pimcore_admin_notification_markasread")
  156.      *
  157.      * @param Request $request
  158.      * @param NotificationService $service
  159.      *
  160.      * @return JsonResponse
  161.      */
  162.     public function markAsReadAction(Request $requestNotificationService $service): JsonResponse
  163.     {
  164.         $this->checkPermission('notifications');
  165.         $id = (int) $request->get('id'0);
  166.         $service->findAndMarkAsRead($id$this->getAdminUser()->getId());
  167.         return $this->adminJson(['success' => true]);
  168.     }
  169.     /**
  170.      * @Route("/delete", name="pimcore_admin_notification_delete")
  171.      *
  172.      * @param Request $request
  173.      * @param NotificationService $service
  174.      *
  175.      * @return JsonResponse
  176.      */
  177.     public function deleteAction(Request $requestNotificationService $service): JsonResponse
  178.     {
  179.         $this->checkPermission('notifications');
  180.         $id = (int) $request->get('id'0);
  181.         $service->delete($id$this->getAdminUser()->getId());
  182.         return $this->adminJson(['success' => true]);
  183.     }
  184.     /**
  185.      * @Route("/delete-all", name="pimcore_admin_notification_deleteall")
  186.      *
  187.      * @param Request $request
  188.      * @param NotificationService $service
  189.      *
  190.      * @return JsonResponse
  191.      */
  192.     public function deleteAllAction(Request $requestNotificationService $service): JsonResponse
  193.     {
  194.         $this->checkPermission('notifications');
  195.         $user $this->getAdminUser();
  196.         $service->deleteAll((int) $user->getId());
  197.         return $this->adminJson(['success' => true]);
  198.     }
  199. }