forked from ezsystems/PlatformUIBundle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix EZP-24450: As an admin developer, I want to push notifications using
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
1 parent
2fad3a9
commit 367c6a9
Showing
10 changed files
with
457 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.