Skip to content

EspoCRM – Open Source CRM Application

add to collaborators notification

Yuri Kuznetsov3581736bd550
diff --git a/application/Espo/Core/Acl/AssignmentChecker/Helper.php b/application/Espo/Core/Acl/AssignmentChecker/Helper.php
index 11a04134cd..e0d634b458 100644
--- a/application/Espo/Core/Acl/AssignmentChecker/Helper.php
+++ b/application/Espo/Core/Acl/AssignmentChecker/Helper.php
@@ -318,6 +318,21 @@ class Helper
$entityDefs->getRelation(self::FIELD_ASSIGNED_USERS)->getForeignEntityType() === User::ENTITY_TYPE;
}
+ public function hasAssignedUserField(string $entityType): bool
+ {
+ $entityDefs = $this->ormDefs->getEntity($entityType);
+
+ $fieldDefs = $entityDefs->tryGetField(Field::ASSIGNED_USER);
+
+ if (!$fieldDefs) {
+ return false;
+ }
+
+ return $fieldDefs->getType() === FieldType::LINK &&
+ $entityDefs->hasRelation(Field::ASSIGNED_USER) &&
+ $entityDefs->getRelation(Field::ASSIGNED_USER)->getForeignEntityType() === User::ENTITY_TYPE;
+ }
+
public function hasCollaboratorsField(string $entityType): bool
{
if (!$this->metadata->get("scopes.$entityType.collaborators")) {
diff --git a/application/Espo/Entities/Notification.php b/application/Espo/Entities/Notification.php
index 111120dfcd..9ee3a57688 100644
--- a/application/Espo/Entities/Notification.php
+++ b/application/Espo/Entities/Notification.php
@@ -29,6 +29,7 @@
namespace Espo\Entities;
+use Espo\Core\Field\Link;
use Espo\Core\Field\LinkParent;
use Espo\Core\ORM\Entity;
@@ -40,6 +41,7 @@ class Notification extends Entity
public const TYPE_ENTITY_REMOVED = 'EntityRemoved';
public const TYPE_ASSIGN = 'Assign';
+ public const TYPE_COLLABORATING = 'Collaborating';
public const TYPE_EMAIL_RECEIVED = 'EmailReceived';
public const TYPE_NOTE = 'Note';
public const TYPE_MENTION_IN_POST = 'MentionInPost';
@@ -98,12 +100,21 @@ class Notification extends Entity
return $this;
}
+ public function getCreatedBy(): ?Link
+ {
+ /** @var ?Link */
+ return $this->getValueObject('createdBy');
+ }
+
public function getRelated(): ?LinkParent
{
/** @var ?LinkParent */
return $this->getValueObject('related');
}
+ /**
+ * Note: 'relatedName' is not loaded for a performance reason. Pass 'relatedName' in 'data'.
+ */
public function setRelated(LinkParent|Entity|null $related): self
{
if ($related instanceof LinkParent) {
diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json
index b664d18189..88797041af 100644
--- a/application/Espo/Resources/i18n/en_US/Global.json
+++ b/application/Espo/Resources/i18n/en_US/Global.json
@@ -541,6 +541,7 @@
"entityRemoved": "{user} removed {entityType} {entity}",
"emailInbox": "{user} added email {entity} to your inbox",
"userPostReaction": "{user} reacted to your {post}",
+ "addedToCollaborators": "{user} added you as a collaborator to {entityType} {entity}",
"userPostInParentReaction": "{user} reacted to your {post} in {entityType} {entity}"
},
"streamMessages": {
diff --git a/application/Espo/Tools/Notification/CollaboratorsNotificator.php b/application/Espo/Tools/Notification/CollaboratorsNotificator.php
new file mode 100644
index 0000000000..2aae2715b3
--- /dev/null
+++ b/application/Espo/Tools/Notification/CollaboratorsNotificator.php
@@ -0,0 +1,118 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Tools\Notification;
+
+use Espo\Core\Acl\AssignmentChecker\Helper;
+use Espo\Core\Field\LinkParent;
+use Espo\Core\Name\Field;
+use Espo\Core\Notification\AssignmentNotificator\Params;
+use Espo\Core\ORM\Entity;
+use Espo\Entities\Notification;
+use Espo\Entities\User;
+use Espo\ORM\EntityManager;
+
+class CollaboratorsNotificator
+{
+ public function __construct(
+ private Helper $helper,
+ private EntityManager $entityManager,
+ private User $user,
+ ) {}
+
+ public function process(Entity $entity, Params $params): void
+ {
+ if (!$this->toProcess($entity)) {
+ return;
+ }
+
+ $userIds = $entity->getLinkMultipleIdList(Field::COLLABORATORS);
+ $previousUserIds = $entity->getFetchedLinkMultipleIdList(Field::COLLABORATORS);
+
+ $addedUserIds = array_diff($userIds, $previousUserIds);
+
+ foreach ($addedUserIds as $userId) {
+ $this->processForUser($entity, $userId, $params);
+ }
+ }
+
+ private function processForUser(Entity $entity, string $userId, Params $params): void
+ {
+ if (!$this->toProcessUser($entity, $userId)) {
+ return;
+ }
+
+ $notification = $this->entityManager->getRDBRepositoryByClass(Notification::class)->getNew();
+
+ $notification
+ ->setType(Notification::TYPE_COLLABORATING)
+ ->setUserId($userId)
+ ->setData([
+ 'relatedName' => $entity->get(Field::NAME),
+ 'createdByName' => $this->user->getName(),
+ ])
+ ->setRelated(LinkParent::createFromEntity($entity))
+ ->setActionId($params->getActionId());
+
+ $this->entityManager->saveEntity($notification);
+ }
+
+ private function toProcessUser(Entity $entity, string $userId): bool
+ {
+ if ($userId === $this->user->getId()) {
+ return false;
+ }
+
+ if ($this->helper->hasAssignedUsersField($entity->getEntityType())) {
+ return !in_array($userId, $entity->getLinkMultipleIdList(Field::ASSIGNED_USERS));
+ }
+
+ if ($this->helper->hasAssignedUserField($entity->getEntityType())) {
+ return $userId !== $entity->get(Field::ASSIGNED_USER . 'Id');
+ }
+
+ return true;
+ }
+
+ private function toProcess(Entity $entity): bool
+ {
+ if (!$this->helper->hasCollaboratorsField($entity->getEntityType())) {
+ return false;
+ }
+
+ $idsAttr = Field::COLLABORATORS . 'Ids';
+
+ if (!$entity->isAttributeChanged($idsAttr)) {
+ return false;
+ }
+
+ return true;
+ }
+
+}
diff --git a/application/Espo/Tools/Notification/HookProcessor.php b/application/Espo/Tools/Notification/HookProcessor.php
index ad15b6fc7d..e0a96ab969 100644
--- a/application/Espo/Tools/Notification/HookProcessor.php
+++ b/application/Espo/Tools/Notification/HookProcessor.php
@@ -61,7 +61,8 @@ class HookProcessor
private EntityManager $entityManager,
private StreamService $streamService,
private AssignmentNotificatorFactory $notificatorFactory,
- private User $user
+ private User $user,
+ private CollaboratorsNotificator $collaboratorsNotificator,
) {}
/**
@@ -69,12 +70,22 @@ class HookProcessor
*/
public function afterSave(Entity $entity, array $options): void
{
- $entityType = $entity->getEntityType();
-
if (!$entity instanceof CoreEntity) {
return;
}
+ $this->processAssignment($entity, $options);
+
+ $this->collaboratorsNotificator->process($entity, $this->createParams($options));
+ }
+
+ /**
+ * @param array<string, mixed> $options
+ */
+ private function processAssignment(CoreEntity $entity, array $options): void
+ {
+ $entityType = $entity->getEntityType();
+
$hasStream = $this->checkHasStream($entityType);
$force = $this->forceAssignmentNotificator($entityType);
@@ -97,13 +108,7 @@ class HookProcessor
$notificator = $this->getNotificator($entityType);
- $params = AssignmentNotificatorParams::create()->withRawOptions($options);
-
- $saveContext = SaveContext::obtainFromRawOptions($options);
-
- if ($saveContext) {
- $params = $params->withActionId($saveContext->getActionId());
- }
+ $params = $this->createParams($options);
$notificator->process($entity, $params);
}
@@ -214,4 +219,20 @@ class HookProcessor
{
return (bool) $this->metadata->get(['notificationDefs', $entityType, 'forceAssignmentNotificator']);
}
+
+ /**
+ * @param array<string, mixed> $options
+ */
+ private function createParams(array $options): AssignmentNotificatorParams
+ {
+ $params = AssignmentNotificatorParams::create()->withRawOptions($options);
+
+ $saveContext = SaveContext::obtainFromRawOptions($options);
+
+ if ($saveContext) {
+ $params = $params->withActionId($saveContext->getActionId());
+ }
+
+ return $params;
+ }
}
diff --git a/application/Espo/Tools/Notification/RecordService.php b/application/Espo/Tools/Notification/RecordService.php
index 476847e24a..c3d42515aa 100644
--- a/application/Espo/Tools/Notification/RecordService.php
+++ b/application/Espo/Tools/Notification/RecordService.php
@@ -121,7 +121,7 @@ class RecordService
$ids = [];
$actionIds = [];
- foreach ($collection as $entity) {
+ foreach ($collection as $entity) {
$ids[] = $entity->getId();
$groupedCount = null;
@@ -132,6 +132,14 @@ class RecordService
$groupedCount = $groupedCountMap[$entity->getActionId()] ?? 0;
}
+ if ($entity->getRelated() && $entity->getData()?->relatedName) {
+ $entity->set('relatedName', $entity->getData()->relatedName);
+ }
+
+ if ($entity->getCreatedBy() && $entity->getData()?->createdByName) {
+ $entity->set('createdByName', $entity->getData()->createdByName);
+ }
+
$entity->set('groupedCount', $groupedCount);
}
diff --git a/client/src/views/notification/fields/container.js b/client/src/views/notification/fields/container.js
index ba5b7ca9f8..d6bf0bb124 100644
--- a/client/src/views/notification/fields/container.js
+++ b/client/src/views/notification/fields/container.js
@@ -36,6 +36,10 @@ class NotificationContainerFieldView extends BaseFieldView {
listTemplate = 'notification/fields/container'
detailTemplate = 'notification/fields/container'
+ /**
+ * @private
+ * @type {string[]}
+ */
types = [
'Assign',
'EmailReceived',
@@ -43,6 +47,7 @@ class NotificationContainerFieldView extends BaseFieldView {
'Message',
'System',
'UserReaction',
+ 'Collaborating',
]
inlineEditDisabled = true
diff --git a/client/src/views/notification/items/base.js b/client/src/views/notification/items/base.js
index b75eb5c6a8..fb9924aedf 100644
--- a/client/src/views/notification/items/base.js
+++ b/client/src/views/notification/items/base.js
@@ -127,6 +127,32 @@ class BaseNotificationItemView extends View {
return ['de_DE', 'nl_NL'].includes(language);
}
+
+ /**
+ * @protected
+ * @param entityType
+ * @param id
+ * @return {string|null}
+ */
+ getIconHtml(entityType, id) {
+ const iconClass = this.getMetadata().get(`clientDefs.${entityType}.iconClass`);
+ const color = this.getMetadata().get(`clientDefs.${entityType}.color`);
+
+ if (!iconClass) {
+ return null;
+ }
+
+ const span = document.createElement('span');
+ span.className = `action text-muted icon ${iconClass}`;
+ span.style.cursor = 'pointer';
+ span.style.color = color ? color : '';
+ span.title = this.translate('View');
+ span.dataset.action = 'quickView';
+ span.dataset.id = id;
+ span.dataset.scope = entityType;
+
+ return span.outerHTML;
+ }
}
export default BaseNotificationItemView;
diff --git a/client/src/views/notification/items/collaborating.js b/client/src/views/notification/items/collaborating.js
new file mode 100644
index 0000000000..ae9ae600c3
--- /dev/null
+++ b/client/src/views/notification/items/collaborating.js
@@ -0,0 +1,81 @@
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+import BaseNotificationItemView from 'views/notification/items/base';
+
+// noinspection JSUnusedGlobalSymbols
+export default class CollaboratingNotificationItemView extends BaseNotificationItemView {
+
+ // language=Handlebars
+ templateContent = `
+ <div class="stream-head-container">
+ <div class="pull-left">
+ {{{avatar}}}
+ </div>
+ <div class="stream-head-text-container text-muted">
+ {{#if iconHtml}}{{{iconHtml}}}{{/if}}<span class="message"">{{{message}}}</span>
+ </div>
+ </div>
+
+ <div class="stream-date-container">
+ <span class="text-muted small">{{{createdAt}}}</span>
+ </div>
+ `
+
+ messageName = 'addedToCollaborators'
+
+ data() {
+ const iconHtml = this.model.attributes.relatedType && this.model.attributes.relatedId ?
+ this.getIconHtml(this.model.attributes.relatedType, this.model.attributes.relatedId) : null;
+
+ return {
+ ...super.data(),
+ iconHtml: iconHtml,
+ };
+ }
+
+ setup() {
+ this.userId = this.model.attributes.createdById;
+
+ this.messageData['user'] = (() => {
+ const element = document.createElement('a');
+ element.href = `#User/view/${this.model.attributes.createdById}`;
+ element.dataset.id = this.model.attributes.createdById;
+ element.dataset.scope = 'User';
+ element.textContent = this.model.attributes.createdByName;
+
+ return element;
+ })();
+
+ this.messageData['entityType'] = this.translateEntityType(this.model.attributes.relatedType);
+
+ this.messageData['entity'] = 'field:related';
+
+ this.createMessage();
+ }
+}