Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workflows #15

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Features
- `ezuser:enable` to enable a user
- `ezplatform:language:add` to add a language to the database
- `ezplatform:slug:test` to test transformation of text into slug
* Workflows
- `notify_user` action: Notify users passed as a data list of user IDs, user group IDs, user logins and/or user emails.


Contribute
Expand Down
149 changes: 149 additions & 0 deletions src/bundle/EventListener/NotifyUserActionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace AdrienDupuis\EzPlatformAdminBundle\EventListener;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\NotificationService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\UserService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Notification\CreateStruct;
use eZ\Publish\API\Repository\Values\User\User;
use eZ\Publish\API\Repository\Values\User\UserGroup;
use EzSystems\EzPlatformWorkflow\Event\Action\AbstractStageWorkflowActionListener;
use EzSystems\EzPlatformWorkflow\MarkingStore\ContextualMarking;
use Symfony\Component\Workflow\Event\EnteredEvent;

class NotifyUserActionListener extends AbstractStageWorkflowActionListener
{
/** @var UserService */
private $userService;

/** @var NotificationService */
private $notificationService;

/** @var PermissionResolver */
private $permissionResolver;

public function __construct(
UserService $userService,
NotificationService $notificationService,
PermissionResolver $permissionResolver
) {
$this->userService = $userService;
$this->notificationService = $notificationService;
$this->permissionResolver = $permissionResolver;
}

public function getIdentifier(): string
{
return 'notify_user';
}

public function onWorkflowEvent(EnteredEvent $event): void
{
$marking = $event->getWorkflow()->getMarkingStore()->getMarking($event->getSubject());

if (!$marking instanceof ContextualMarking) {
return;
}

/** @var Content $content */
$content = $event->getSubject();
$versionInfo = $content->getVersionInfo();

$context = $marking->getContext();

$sender = $this->userService->loadUser(
$this->permissionResolver->getCurrentUserReference()->getUserId()
);

$userIdList = [];

if (!empty($context->reviewerId)) {
$userIdList[] = $context->reviewerId;
}

foreach (array_keys($event->getMarking()->getPlaces()) as $place) {
foreach ($event->getWorkflow()->getMetadataStore()->getPlaceMetadata($place)['actions'][$this->getIdentifier()]['data'] as $id) {
foreach ($this->getUserList($id) as $user) {
$userIdList[] = $user->id;
}
}
}

foreach (array_unique($userIdList, SORT_NUMERIC) as $userId) {
$notification = new CreateStruct();
$notification->ownerId = $userId;
$notification->type = 'Workflow:NotifyReviewer';
$notification->data = [
'content_id' => $content->id,
'content_name' => $content->getName(),
'version_number' => $versionInfo->versionNo,
'language_code' => $versionInfo->initialLanguageCode,
'sender_id' => $sender->id,
'sender_name' => $sender->getName(),
'message' => $context->message,
];

$this->notificationService->createNotification($notification);
}
}

/**
* @param $id int|string User ID, user group ID, user login or user email
*
* @return User[]
*/
public function getUserList($id)
{
$userList = [];

if (is_int($id)) {
try {
$userGroup = $this->userService->loadUserGroup($id);
$userList = array_merge($userList, $this->loadUsersOfUserGroup($userGroup));
} catch (NotFoundException $groupNotFoundException) {
try {
$userList[] = $this->userService->loadUser($id);
} catch (NotFoundException $userNotFoundException) {
//TODO
}
}
} elseif (is_string($id)) {
if (false === strpos($user, '@')) {
try {
$userList[] = $this->userService->loadUserByLogin($user);
} catch (NotFoundException $userNotFoundException) {
//TODO
}
} else {
try {
$userList[] = $this->userService->loadUserByEmail($user);
} catch (NotFoundException $userNotFoundException) {
//TODO
}
}
}

return $userList;
}

/** @return User[] */
public function loadUsersOfUserGroup(UserGroup $userGroup): array
{
$users = [];

for ($offset = 0, $limit = 25; count($usersSlice = $this->userService->loadUsersOfUserGroup($userGroup, $offset, $limit)); $offset += $limit) {
$users = array_merge($users, $usersSlice);
}

for ($offset = 0, $limit = 25; count($subUserGroupsSlice = $this->userService->loadSubUserGroups($userGroup, $offset, $limit)); $offset += $limit) {
foreach ($subUserGroupsSlice as $subUserGroup) {
$users = array_merge($users, $this->loadUsersOfUserGroup($subUserGroup));
}
}

return $users;
}
}
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/services/action_listeners.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
AdrienDupuis\EzPlatformAdminBundle\EventListener\NotifyUserActionListener:
arguments:
- '@ezpublish.api.service.user'
- '@ezpublish.api.service.notification'
- '@ezpublish.api.service.permissions_resolver'
tags:
- { name: ezplatform.workflow.action_listener }