vendor/ibexa/admin-ui/src/lib/Form/Processor/ContentEditNotificationFormProcessor.php line 62

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\AdminUi\Form\Processor;
  8. use Ibexa\AdminUi\Specification\SiteAccess\IsAdmin;
  9. use Ibexa\ContentForms\Event\ContentFormEvents;
  10. use Ibexa\ContentForms\Event\FormActionEvent;
  11. use Ibexa\Contracts\AdminUi\Notification\TranslatableNotificationHandlerInterface;
  12. use JMS\TranslationBundle\Annotation\Desc;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. class ContentEditNotificationFormProcessor implements EventSubscriberInterface
  17. {
  18. /** @var \Ibexa\Contracts\AdminUi\Notification\TranslatableNotificationHandlerInterface */
  19. private $notificationHandler;
  20. /** @var \Symfony\Component\HttpFoundation\RequestStack */
  21. private $requestStack;
  22. /** @var array */
  23. private $siteAccessGroups;
  24. /**
  25. * @param \Ibexa\Contracts\AdminUi\Notification\TranslatableNotificationHandlerInterface $notificationHandler
  26. * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
  27. * @param array $siteAccessGroups
  28. */
  29. public function __construct(
  30. TranslatableNotificationHandlerInterface $notificationHandler,
  31. RequestStack $requestStack,
  32. array $siteAccessGroups
  33. ) {
  34. $this->notificationHandler = $notificationHandler;
  35. $this->requestStack = $requestStack;
  36. $this->siteAccessGroups = $siteAccessGroups;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function getSubscribedEvents()
  42. {
  43. return [
  44. ContentFormEvents::CONTENT_PUBLISH => ['addPublishMessage', 5],
  45. ContentFormEvents::CONTENT_SAVE_DRAFT => ['addSaveDraftMessage', 5],
  46. ];
  47. }
  48. /**
  49. * @param \Ibexa\ContentForms\Event\FormActionEvent $event
  50. *
  51. * @throws \Ibexa\AdminUi\Exception\InvalidArgumentException
  52. */
  53. public function addPublishMessage(FormActionEvent $event)
  54. {
  55. if (!$this->isAdminSiteAccess($this->requestStack->getCurrentRequest())) {
  56. return;
  57. }
  58. $this->notificationHandler->success(
  59. /** @Desc("Content published.") */
  60. 'content.published.success',
  61. [],
  62. 'ibexa_content_edit'
  63. );
  64. }
  65. /**
  66. * @param \Ibexa\ContentForms\Event\FormActionEvent $event
  67. *
  68. * @throws \Ibexa\AdminUi\Exception\InvalidArgumentException
  69. */
  70. public function addSaveDraftMessage(FormActionEvent $event)
  71. {
  72. if (!$this->isAdminSiteAccess($this->requestStack->getCurrentRequest())) {
  73. return;
  74. }
  75. $this->notificationHandler->success(
  76. /** @Desc("Content draft saved.") */
  77. 'content.draft_saved.success',
  78. [],
  79. 'ibexa_content_edit'
  80. );
  81. }
  82. /**
  83. * @param \Symfony\Component\HttpFoundation\Request $request
  84. *
  85. * @return bool
  86. *
  87. * @throws \Ibexa\AdminUi\Exception\InvalidArgumentException
  88. */
  89. protected function isAdminSiteAccess(Request $request): bool
  90. {
  91. return (new IsAdmin($this->siteAccessGroups))->isSatisfiedBy($request->attributes->get('siteaccess'));
  92. }
  93. }
  94. class_alias(ContentEditNotificationFormProcessor::class, 'EzSystems\EzPlatformAdminUi\Form\Processor\ContentEditNotificationFormProcessor');