Navigation Menu

Skip to content

Commit

Permalink
Fix EZP-24450: As an admin developer, I want to push notifications using
Browse files Browse the repository at this point in the history
a Pjax controller

> https://jira.ez.no/browse/EZP-24450

Added a new block in Pjax base template for notifications.
This block is automatically filled by flash session messages with
`notification` as identifier.
  • Loading branch information
lolautruche committed Jun 26, 2015
1 parent 2fad3a9 commit 367c6a9
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 3 deletions.
29 changes: 27 additions & 2 deletions Form/Processor/ContentTypeFormProcessor.php
Expand Up @@ -10,6 +10,8 @@
namespace EzSystems\PlatformUIBundle\Form\Processor;

use eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft;
use EzSystems\PlatformUIBundle\Notification\NotificationPoolInterface;
use EzSystems\PlatformUIBundle\Notification\TranslatableNotificationMessage;
use EzSystems\RepositoryForms\Event\FormActionEvent;
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -23,19 +25,31 @@ class ContentTypeFormProcessor implements EventSubscriberInterface
*/
private $router;

public function __construct(RouterInterface $router)
/**
* @var NotificationPoolInterface
*/
private $notificationPool;

public function __construct(RouterInterface $router, NotificationPoolInterface $notificationPool)
{
$this->router = $router;
$this->notificationPool = $notificationPool;
}

public static function getSubscribedEvents()
{
return [
RepositoryFormEvents::CONTENT_TYPE_UPDATE => ['processDefaultAction', -10],
RepositoryFormEvents::CONTENT_TYPE_PUBLISH => ['processPublishContentType', -10],
RepositoryFormEvents::CONTENT_TYPE_REMOVE_DRAFT => ['processRemoveContentTypeDraft', -10],
];
}

public function processDefaultAction(FormActionEvent $event)
{
$this->addNotification('content_type.notification.draft_updated');
}

public function processPublishContentType(FormActionEvent $event)
{
$event->setResponse(
Expand All @@ -44,7 +58,8 @@ public function processPublishContentType(FormActionEvent $event)
$event->getOption('languageCode')
)
);
// TODO: Add confirmation flash message.

$this->addNotification('content_type.notification.published');
}

public function processRemoveContentTypeDraft(FormActionEvent $event)
Expand All @@ -55,6 +70,8 @@ public function processRemoveContentTypeDraft(FormActionEvent $event)
$event->getOption('languageCode')
)
);

$this->addNotification('content_type.notification.draft_removed');
}

private function generateRedirectResponse(ContentTypeDraft $contentTypeDraft, $languageCode)
Expand All @@ -66,4 +83,12 @@ private function generateRedirectResponse(ContentTypeDraft $contentTypeDraft, $l

return new RedirectResponse($url);
}

private function addNotification($message)
{
$this->notificationPool->addNotification(new TranslatableNotificationMessage([
'message' => $message,
'domain' => 'content_type'
]));
}
}
42 changes: 42 additions & 0 deletions Notification/Notification.php
@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the eZ PlatformUI package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\PlatformUIBundle\Notification;

use eZ\Publish\API\Repository\Values\ValueObject;

/**
* A notification representation.
* Typical usage is storing an implementation in the session flash bag, with "notification" identifier.
* PJax display will automatically detect it and dispatch it to the main notification system.
*
* @property-read string $message
* @property-read string $state
*/
class Notification extends ValueObject
{
const STATE_DONE = 'done';
const STATE_ERROR = 'error';
const STATE_STARTED = 'started';

/**
* The notification message.
*
* @var string
*/
protected $message;

/**
* The notification state.
* See STATE_* constants.
*
* @var string
*/
protected $state;
}
25 changes: 25 additions & 0 deletions Notification/NotificationMessage.php
@@ -0,0 +1,25 @@
<?php
/**
* This file is part of the eZ PlatformUI package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\PlatformUIBundle\Notification;

use eZ\Publish\API\Repository\Values\ValueObject;

/**
* @property-read string $message
*/
class NotificationMessage extends ValueObject
{
/**
* The message id (may also be an object that can be cast to string).
*
* @var string
*/
public $message;
}
83 changes: 83 additions & 0 deletions Notification/NotificationPool.php
@@ -0,0 +1,83 @@
<?php
/**
* This file is part of the eZ PlatformUI package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\PlatformUIBundle\Notification;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Translation\TranslatorInterface;

class NotificationPool implements NotificationPoolInterface, EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var Session
*/
private $session;

/**
* @var Notification[]
*/
private $notifications = [];

public function __construct(TranslatorInterface $translator, Session $session)
{
$this->translator = $translator;
$this->session = $session;
}

public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
];
}

public function addNotification(NotificationMessage $message, $state = Notification::STATE_DONE)
{
$translatedMessage = $message instanceof TranslatableNotificationMessage ? $this->translateMessage($message) : $message->message;
$this->notifications[] = new Notification([
'message' => $translatedMessage,
'state' => $state,
]);
}

/**
* @return Notification[]
*/
public function getNotifications()
{
return $this->notifications;
}

private function translateMessage(TranslatableNotificationMessage $message)
{
if ($message->number !== null) {
return $this->translator->transChoice(
$message->message,
(int)$message->number,
$message->translationParams,
$message->domain
);
}

return $this->translator->trans($message->message, $message->translationParams, $message->domain);
}

public function onKernelResponse(FilterResponseEvent $event)
{
$this->session->getFlashBag()->set('notification', $this->notifications);
}
}
21 changes: 21 additions & 0 deletions Notification/NotificationPoolInterface.php
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the eZ PlatformUI package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\PlatformUIBundle\Notification;

interface NotificationPoolInterface
{
/**
* Pushes a notification message to the registry.
*
* @param NotificationMessage $message The notification message
* @param string $state The notification state (see Notification::STATE_*)
*/
public function addNotification(NotificationMessage $message, $state = Notification::STATE_DONE);
}
40 changes: 40 additions & 0 deletions Notification/TranslatableNotificationMessage.php
@@ -0,0 +1,40 @@
<?php
/**
* This file is part of the eZ PlatformUI package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\PlatformUIBundle\Notification;

/**
* @property-read array $translationParams
* @property-read string|null $domain
* @property-read int|null $number
*/
class TranslatableNotificationMessage extends NotificationMessage
{
/**
* An array of parameters for the message.
*
* @var array
*/
public $translationParams = [];

/**
* The domain for the message or null to use the default.
*
* @var string|null
*/
public $domain;

/**
* The number to use to find the indice of the message.
* If provided, the message will be translated using TranslatorInterface::transChoice() instead of TranslatorInterface::trans().
*
* @var int|null
*/
public $number;
}
9 changes: 8 additions & 1 deletion Resources/config/services.yml
Expand Up @@ -12,6 +12,7 @@ parameters:
ezsystems.platformui.form.type.section_list.class: EzSystems\PlatformUIBundle\Form\Type\SectionListType
ezsystems.platformui.controller.pjax.class: EzSystems\PlatformUIBundle\Controller\PjaxController
ezsystems.platformui.controller.content_type.class: EzSystems\PlatformUIBundle\Controller\ContentTypeController
ezsystems.platforui.notification_pool.class: EzSystems\PlatformUIBundle\Notification\NotificationPool
ezsystems.platformui.form_processor.content_type.class: EzSystems\PlatformUIBundle\Form\Processor\ContentTypeFormProcessor

services:
Expand Down Expand Up @@ -109,8 +110,14 @@ services:
calls:
- [setPrioritizedLanguages, ["$languages$"]]

ezsystems.platformui.notification_pool:
class: %ezsystems.platforui.notification_pool.class%
arguments: [@translator, @session]
tags:
- { name: kernel.event_subscriber }

ezsystems.platformui.form_processor.content_type:
class: %ezsystems.platformui.form_processor.content_type.class%
arguments: [@router]
arguments: [@router, @ezsystems.platformui.notification_pool]
tags:
- { name: kernel.event_subscriber }
12 changes: 12 additions & 0 deletions Resources/translations/content_type.en.xlf
Expand Up @@ -94,6 +94,18 @@
<source>content_type.default_children_sorting</source>
<target>Default children sorting</target>
</trans-unit>
<trans-unit id="dc20866b714ed1642b9bce242bf460fd" resname="content_type.notification.draft_updated">
<source>content_type.notification.draft_updated</source>
<target>The ContentType draft was successfully updated.</target>
</trans-unit>
<trans-unit id="9706f09166fa56162a4558261b87a9e2" resname="content_type.notification.draft_removed">
<source>content_type.notification.draft_removed</source>
<target>The ContentType draft was successfully removed.</target>
</trans-unit>
<trans-unit id="ced84ba84fbde36bb688787c0562b1da" resname="content_type.notification.published">
<source>content_type.notification.published</source>
<target>The ContentType draft was successfully updated and published. Related Content has also been updated.</target>
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions Resources/views/pjax_admin.html.twig
Expand Up @@ -25,3 +25,13 @@
</header>
{% block content %}{% endblock %}
</div>

{% block notification %}
<ul data-name="notification">
{% set notifications = app.session.flashBag.get("notification") %}
{% for notification in notifications %}
<li data-state="{{ notification.state }}">{{ notification.message }}</li>

{% endfor %}
</ul>
{% endblock %}

0 comments on commit 367c6a9

Please sign in to comment.