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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  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.         try {
  91.             $notification $service->findAndMarkAsRead($id$this->getAdminUser()->getId());
  92.         } catch (\UnexpectedValueException $e) {
  93.             return $this->adminJson(
  94.                 [
  95.                     'success' => false,
  96.                 ]
  97.             );
  98.         }
  99.         $data $service->format($notification);
  100.         return $this->adminJson([
  101.             'success' => true,
  102.             'data' => $data,
  103.         ]);
  104.     }
  105.     /**
  106.      * @Route("/find-all", name="pimcore_admin_notification_findall")
  107.      *
  108.      * @param Request $request
  109.      * @param NotificationService $service
  110.      *
  111.      * @return JsonResponse
  112.      */
  113.     public function findAllAction(Request $requestNotificationService $service): JsonResponse
  114.     {
  115.         $this->checkPermission('notifications');
  116.         $filter = ['recipient = ?' => (int) $this->getAdminUser()->getId()];
  117.         $parser = new NotificationServiceFilterParser($request);
  118.         foreach ($parser->parse() as $key => $val) {
  119.             $filter[$key] = $val;
  120.         }
  121.         $options = [
  122.             'offset' => $request->get('start'0),
  123.             'limit' => $request->get('limit'40),
  124.         ];
  125.         $result $service->findAll($filter$options);
  126.         $data = [];
  127.         foreach ($result['data'] as $notification) {
  128.             $data[] = $service->format($notification);
  129.         }
  130.         return $this->adminJson([
  131.             'success' => true,
  132.             'total' => $result['total'],
  133.             'data' => $data,
  134.         ]);
  135.     }
  136.     /**
  137.      * @Route("/find-last-unread", name="pimcore_admin_notification_findlastunread")
  138.      *
  139.      * @param Request $request
  140.      * @param NotificationService $service
  141.      *
  142.      * @return JsonResponse
  143.      */
  144.     public function findLastUnreadAction(Request $requestNotificationService $service): JsonResponse
  145.     {
  146.         $this->checkPermission('notifications');
  147.         $user $this->getAdminUser();
  148.         $lastUpdate = (int) $request->get('lastUpdate'time());
  149.         $result $service->findLastUnread((int) $user->getId(), $lastUpdate);
  150.         $unread $service->countAllUnread((int) $user->getId());
  151.         $data = [];
  152.         foreach ($result['data'] as $notification) {
  153.             $data[] = $service->format($notification);
  154.         }
  155.         return $this->adminJson([
  156.             'success' => true,
  157.             'total' => $result['total'],
  158.             'data' => $data,
  159.             'unread' => $unread,
  160.         ]);
  161.     }
  162.     /**
  163.      * @Route("/mark-as-read", name="pimcore_admin_notification_markasread")
  164.      *
  165.      * @param Request $request
  166.      * @param NotificationService $service
  167.      *
  168.      * @return JsonResponse
  169.      */
  170.     public function markAsReadAction(Request $requestNotificationService $service): JsonResponse
  171.     {
  172.         $this->checkPermission('notifications');
  173.         $id = (int) $request->get('id'0);
  174.         $service->findAndMarkAsRead($id$this->getAdminUser()->getId());
  175.         return $this->adminJson(['success' => true]);
  176.     }
  177.     /**
  178.      * @Route("/delete", name="pimcore_admin_notification_delete")
  179.      *
  180.      * @param Request $request
  181.      * @param NotificationService $service
  182.      *
  183.      * @return JsonResponse
  184.      */
  185.     public function deleteAction(Request $requestNotificationService $service): JsonResponse
  186.     {
  187.         $this->checkPermission('notifications');
  188.         $id = (int) $request->get('id'0);
  189.         $service->delete($id$this->getAdminUser()->getId());
  190.         return $this->adminJson(['success' => true]);
  191.     }
  192.     /**
  193.      * @Route("/delete-all", name="pimcore_admin_notification_deleteall")
  194.      *
  195.      * @param Request $request
  196.      * @param NotificationService $service
  197.      *
  198.      * @return JsonResponse
  199.      */
  200.     public function deleteAllAction(Request $requestNotificationService $service): JsonResponse
  201.     {
  202.         $this->checkPermission('notifications');
  203.         $user $this->getAdminUser();
  204.         $service->deleteAll((int) $user->getId());
  205.         return $this->adminJson(['success' => true]);
  206.     }
  207. }