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

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. * @internal
  30. */
  31. class NotificationController extends AdminController
  32. {
  33. /**
  34. * @Route("/recipients", name="pimcore_admin_notification_recipients", methods={"GET"})
  35. *
  36. * @param UserService $service
  37. * @param Translator $translator
  38. *
  39. * @return JsonResponse
  40. */
  41. public function recipientsAction(UserService $service, Translator $translator): JsonResponse
  42. {
  43. $this->checkPermission('notifications_send');
  44. $data = [];
  45. foreach ($service->findAll($this->getAdminUser()) as $recipient) {
  46. $group = $translator->trans('group');
  47. $prefix = $recipient->getType() == 'role' ? $group . ' - ' : '';
  48. $data[] = [
  49. 'id' => $recipient->getId(),
  50. 'text' => $prefix . $recipient->getName(),
  51. ];
  52. }
  53. return $this->adminJson($data);
  54. }
  55. /**
  56. * @Route("/send", name="pimcore_admin_notification_send", methods={"POST"})
  57. *
  58. * @param Request $request
  59. * @param NotificationService $service
  60. *
  61. * @return JsonResponse
  62. */
  63. public function sendAction(Request $request, NotificationService $service): JsonResponse
  64. {
  65. $this->checkPermission('notifications_send');
  66. $recipientId = (int) $request->get('recipientId', 0);
  67. $fromUser = (int) $this->getAdminUser()->getId();
  68. $title = $request->get('title', '');
  69. $message = $request->get('message', '');
  70. $elementId = $request->get('elementId');
  71. $elementType = $request->get('elementType', '');
  72. $element = Service::getElementById($elementType, $elementId);
  73. if (User::getById($recipientId) instanceof User) {
  74. $service->sendToUser($recipientId, $fromUser, $title, $message, $element);
  75. } else {
  76. $service->sendToGroup($recipientId, $fromUser, $title, $message, $element);
  77. }
  78. return $this->adminJson(['success' => true]);
  79. }
  80. /**
  81. * @Route("/find", name="pimcore_admin_notification_find")
  82. *
  83. * @param Request $request
  84. * @param NotificationService $service
  85. *
  86. * @return JsonResponse
  87. */
  88. public function findAction(Request $request, NotificationService $service): JsonResponse
  89. {
  90. $this->checkPermission('notifications');
  91. $id = (int) $request->get('id', 0);
  92. try {
  93. $notification = $service->findAndMarkAsRead($id, $this->getAdminUser()->getId());
  94. } catch (\UnexpectedValueException $e) {
  95. return $this->adminJson(
  96. [
  97. 'success' => false,
  98. ]
  99. );
  100. }
  101. $data = $service->format($notification);
  102. return $this->adminJson([
  103. 'success' => true,
  104. 'data' => $data,
  105. ]);
  106. }
  107. /**
  108. * @Route("/find-all", name="pimcore_admin_notification_findall")
  109. *
  110. * @param Request $request
  111. * @param NotificationService $service
  112. *
  113. * @return JsonResponse
  114. */
  115. public function findAllAction(Request $request, NotificationService $service): JsonResponse
  116. {
  117. $this->checkPermission('notifications');
  118. $filter = ['recipient' => (int) $this->getAdminUser()->getId()];
  119. $parser = new NotificationServiceFilterParser($request);
  120. foreach ($parser->parse() as $key => $val) {
  121. $filter[$key] = $val;
  122. }
  123. $options = [
  124. 'offset' => $request->get('start', 0),
  125. 'limit' => $request->get('limit', 40),
  126. ];
  127. $result = $service->findAll($filter, $options);
  128. $data = [];
  129. foreach ($result['data'] as $notification) {
  130. $data[] = $service->format($notification);
  131. }
  132. return $this->adminJson([
  133. 'success' => true,
  134. 'total' => $result['total'],
  135. 'data' => $data,
  136. ]);
  137. }
  138. /**
  139. * @Route("/find-last-unread", name="pimcore_admin_notification_findlastunread")
  140. *
  141. * @param Request $request
  142. * @param NotificationService $service
  143. *
  144. * @return JsonResponse
  145. */
  146. public function findLastUnreadAction(Request $request, NotificationService $service): JsonResponse
  147. {
  148. $this->checkPermission('notifications');
  149. $user = $this->getAdminUser();
  150. $lastUpdate = (int) $request->get('lastUpdate', time());
  151. $result = $service->findLastUnread((int) $user->getId(), $lastUpdate);
  152. $unread = $service->countAllUnread((int) $user->getId());
  153. $data = [];
  154. foreach ($result['data'] as $notification) {
  155. $data[] = $service->format($notification);
  156. }
  157. return $this->adminJson([
  158. 'success' => true,
  159. 'total' => $result['total'],
  160. 'data' => $data,
  161. 'unread' => $unread,
  162. ]);
  163. }
  164. /**
  165. * @Route("/mark-as-read", name="pimcore_admin_notification_markasread", methods={"PUT"})
  166. *
  167. * @param Request $request
  168. * @param NotificationService $service
  169. *
  170. * @return JsonResponse
  171. */
  172. public function markAsReadAction(Request $request, NotificationService $service): JsonResponse
  173. {
  174. $this->checkPermission('notifications');
  175. $id = (int) $request->get('id', 0);
  176. $service->findAndMarkAsRead($id, $this->getAdminUser()->getId());
  177. return $this->adminJson(['success' => true]);
  178. }
  179. /**
  180. * @Route("/delete", name="pimcore_admin_notification_delete", methods={"DELETE"})
  181. *
  182. * @param Request $request
  183. * @param NotificationService $service
  184. *
  185. * @return JsonResponse
  186. */
  187. public function deleteAction(Request $request, NotificationService $service): JsonResponse
  188. {
  189. $this->checkPermission('notifications');
  190. $id = (int) $request->get('id', 0);
  191. $service->delete($id, $this->getAdminUser()->getId());
  192. return $this->adminJson(['success' => true]);
  193. }
  194. /**
  195. * @Route("/delete-all", name="pimcore_admin_notification_deleteall", methods={"DELETE"})
  196. *
  197. * @param Request $request
  198. * @param NotificationService $service
  199. *
  200. * @return JsonResponse
  201. */
  202. public function deleteAllAction(Request $request, NotificationService $service): JsonResponse
  203. {
  204. $this->checkPermission('notifications');
  205. $user = $this->getAdminUser();
  206. $service->deleteAll((int) $user->getId());
  207. return $this->adminJson(['success' => true]);
  208. }
  209. }