diff --git a/README.md b/README.md index 66098bc..436eadd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/bundle/EventListener/NotifyUserActionListener.php b/src/bundle/EventListener/NotifyUserActionListener.php new file mode 100644 index 0000000..5f0472e --- /dev/null +++ b/src/bundle/EventListener/NotifyUserActionListener.php @@ -0,0 +1,149 @@ +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; + } +} diff --git a/src/bundle/Resources/config/services/action_listeners.yaml b/src/bundle/Resources/config/services/action_listeners.yaml new file mode 100644 index 0000000..4e3f59d --- /dev/null +++ b/src/bundle/Resources/config/services/action_listeners.yaml @@ -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 }