Last active
December 7, 2017 16:24
-
-
Save arfaram/b8d54754a96a31d85ce0210aacde9085 to your computer and use it in GitHub Desktop.
Add a backend user notification everytime when user is added in eZPlatform CMS. In this example the Admin will be notified and data will also added to a system file. You could add other users or notify a user group. Note that all Signal will be sent to the Slot, you can avoid that when you limit the signal only to `Signal\UserService\CreateUserS…
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
<?php | |
namespace AppBundle\Slot; | |
use eZ\Publish\API\Repository\LocationService; | |
use eZ\Publish\Core\SignalSlot\SignalDispatcher; | |
use eZ\Publish\Core\SignalSlot\Slot as BaseSlot; | |
use eZ\Publish\Core\SignalSlot\Signal; | |
use eZ\Publish\Core\SignalSlot\UserService; | |
use EzSystems\Notification\Core\SignalSlot\Signal\NotificationSignal; | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Logger; | |
class OnUserPublishSlot extends BaseSlot | |
{ | |
/** @var UserService */ | |
private $userService; | |
/** @var LocationService */ | |
private $locationService; | |
/** @var SignalDispatcher */ | |
private $signalDispatcher; | |
public $logFile = "../app/logs/slot.log"; | |
public function __construct( | |
UserService $userService, | |
LocationService $locationService, | |
SignalDispatcher $signalDispatcher | |
) | |
{ | |
$this->userService = $userService; | |
$this->locationService = $locationService; | |
$this->signalDispatcher = $signalDispatcher; | |
} | |
public function receive( Signal $signal ) | |
{ | |
if ( $signal instanceof Signal\UserService\CreateUserSignal ) | |
{ | |
$user = $this->userService->loadUser($signal->userId); | |
$data = [ | |
'user_notification' => 'UserNotification', | |
'sender_firstname' => (string)$user->getFieldValue('first_name'), | |
'sender_lastname' => (string)$user->getFieldValue('last_name'), | |
'message' => 'New User was created:'.(string)$user->getFieldValue('first_name') .' '.(string)$user->getFieldValue('last_name') .'('.$user->versionInfo->initialLanguageCode.') user contentID:'.$signal->userId .', LocationID:'. $user->versionInfo->contentInfo->mainLocationId.', E-Mail:' .$user->getFieldValue('user_account')->email, | |
'content_name' => $user->contentInfo->name, | |
]; | |
//Step1: write data to log file | |
$log = new Logger("SlotNotification"); | |
$log->pushHandler(new StreamHandler($this->logFile, Logger::INFO)); | |
$log->addInfo('Signal:' . print_r($data, 1)); | |
//(Step 2)Add Administrator Notification using Notification Signal and FlexWorkflow | |
$location = $this->locationService->loadLocation($user->versionInfo->contentInfo->mainLocationId); | |
$platformUrl = sprintf( | |
'/api/ezp/v2/content/locations%s', | |
substr($location->pathString,0,-1) | |
); | |
$data = [ | |
'user_notification' => 'UserNotification', | |
'sender_firstname' => (string)$user->getFieldValue('first_name'), | |
'sender_lastname' => (string)$user->getFieldValue('last_name'), | |
'message' => 'New User was created:'.(string)$user->getFieldValue('first_name') .' '.(string)$user->getFieldValue('last_name') .'('.$user->versionInfo->initialLanguageCode.') user contentID:'.$signal->userId .', LocationID:'. $user->versionInfo->contentInfo->mainLocationId.', E-Mail:' .$user->getFieldValue('user_account')->email, | |
'content_name' => $user->contentInfo->name, | |
'link' => '/ez#/view/'.urlencode($platformUrl).'/'.$user->versionInfo->initialLanguageCode, | |
]; | |
$this->signalDispatcher->emit(new NotificationSignal([ | |
'ownerId' => 14, | |
'type' => 'FlexWorkflow:Review', | |
'data' => $data, | |
])); | |
} | |
} | |
} |
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
parameters: | |
appbundle.onuserpublish_slot.class: AppBundle\Slot\OnUserPublishSlot | |
services: | |
appbundle.onuserpublish_slot: | |
class: %appbundle.onuserpublish_slot.class% | |
arguments: | |
- @ezpublish.api.service.user | |
- @ezpublish.api.service.location | |
- @ezpublish.signalslot.signal_dispatcher | |
tags: | |
- { name: ezpublish.api.slot, signal: '*' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment