assigned users notifications

This commit is contained in:
yuri
2018-04-24 17:02:14 +03:00
parent 391c0dcaf8
commit 5cd03312e1
5 changed files with 101 additions and 52 deletions

View File

@@ -92,11 +92,27 @@ class Base implements Injectable
public function process(Entity $entity)
{
if (!$entity->get('assignedUserId')) return;
if (!$entity->isAttributeChanged('assignedUserId')) return;
if ($entity->hasLinkMultipleField('assignedUsers')) {
$userIdList = $entity->getLinkMultipleIdList('assignedUsers');
$fetchedAssignedUserIdList = $entity->getFetched('assignedUsersIds');
if (!is_array($fetchedAssignedUserIdList)) {
$fetchedAssignedUserIdList = [];
}
$assignedUserId = $entity->get('assignedUserId');
foreach ($userIdList as $userId) {
if (in_array($userId, $fetchedAssignedUserIdList)) continue;
$this->processForUser($entity, $userId);
}
} else {
if (!$entity->get('assignedUserId')) return;
if (!$entity->isAttributeChanged('assignedUserId')) return;
$assignedUserId = $entity->get('assignedUserId');
$this->processForUser($entity, $assignedUserId);
}
}
protected function processForUser(Entity $entity, $assignedUserId)
{
if ($entity->hasAttribute('createdById') && $entity->hasAttribute('modifiedById')) {
if ($entity->isNew()) {
$isNotSelfAssignment = $assignedUserId !== $entity->get('createdById');

View File

@@ -41,27 +41,49 @@ class AssignmentEmailNotification extends \Espo\Core\Hooks\Base
if (
$this->getConfig()->get('assignmentEmailNotifications')
&&
$entity->has('assignedUserId')
(
$entity->has('assignedUserId')
||
$entity->hasLinkMultipleField('assignedUsers') && $entity->has('assignedUsersIds')
)
&&
in_array($entity->getEntityType(), $this->getConfig()->get('assignmentEmailNotificationsEntityList', []))
) {
if ($entity->has('assignedUsersIds')) {
$userIdList = $entity->getLinkMultipleIdList('assignedUsers');
$fetchedAssignedUserIdList = $entity->getFetched('assignedUsersIds');
if (!is_array($fetchedAssignedUserIdList)) {
$fetchedAssignedUserIdList = [];
}
$userId = $entity->get('assignedUserId');
if (!empty($userId) && $userId != $this->getUser()->id && $entity->isAttributeChanged('assignedUserId')) {
$job = $this->getEntityManager()->getEntity('Job');
$job->set(array(
'serviceName' => 'EmailNotification',
'methodName' => 'notifyAboutAssignmentJob',
'data' => json_encode(array(
'userId' => $userId,
'assignerUserId' => $this->getUser()->id,
'entityId' => $entity->id,
'entityType' => $entity->getEntityType()
)),
'executeTime' => date('Y-m-d H:i:s'),
));
$this->getEntityManager()->saveEntity($job);
foreach ($userIdList as $userId) {
if (in_array($userId, $fetchedAssignedUserIdList)) continue;
if ($this->getUser()->id === $userId) continue;
$this->createJob($entity, $userId);
}
} else {
$userId = $entity->get('assignedUserId');
if (!empty($userId) && $userId != $this->getUser()->id && $entity->isAttributeChanged('assignedUserId')) {
$this->createJob($entity, $userId);
}
}
}
}
protected function createJob(Entity $entity, $userId)
{
$job = $this->getEntityManager()->getEntity('Job');
$job->set(array(
'serviceName' => 'EmailNotification',
'methodName' => 'notifyAboutAssignmentJob',
'data' => json_encode(array(
'userId' => $userId,
'assignerUserId' => $this->getUser()->id,
'entityId' => $entity->id,
'entityType' => $entity->getEntityType()
)),
'executeTime' => date('Y-m-d H:i:s'),
));
$this->getEntityManager()->saveEntity($job);
}
}

View File

@@ -92,7 +92,7 @@ class Notifications extends \Espo\Core\Hooks\Base
$entityType = $entity->getEntityType();
if (!$this->checkHasStream($entityType)) {
if (!$this->checkHasStream($entityType) || $entity->hasLinkMultipleField('assignedUsers')) {
if (in_array($entityType, $this->getConfig()->get('assignmentNotificationsEntityList', []))) {
$notificator = $this->getNotificator($entityType);
$notificator->process($entity);

View File

@@ -121,43 +121,48 @@ class EmailNotification extends \Espo\Core\Services\Base
$assignerUser = $this->getEntityManager()->getEntity('User', $assignerUserId);
$entity = $this->getEntityManager()->getEntity($entityType, $entityId);
if ($entity && $assignerUser && $entity->get('assignedUserId') == $userId) {
$emailAddress = $user->get('emailAddress');
if (!empty($emailAddress)) {
$email = $this->getEntityManager()->getEntity('Email');
if (!$entity) return true;
if (!$assignerUser) return true;
$subjectTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'subject', $entity->getEntityType());
$bodyTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'body', $entity->getEntityType());
if (!$entity->hasLinkMultipleField('assignedUsers')) {
if ($entity->get('assignedUserId') !== $userId) return true;
}
$subjectTpl = str_replace(array("\n", "\r"), '', $subjectTpl);
$emailAddress = $user->get('emailAddress');
if (!empty($emailAddress)) {
$email = $this->getEntityManager()->getEntity('Email');
$recordUrl = rtrim($this->getConfig()->get('siteUrl'), '/') . '/#' . $entity->getEntityType() . '/view/' . $entity->id;
$subjectTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'subject', $entity->getEntityType());
$bodyTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'body', $entity->getEntityType());
$data = array(
'userName' => $user->get('name'),
'assignerUserName' => $assignerUser->get('name'),
'recordUrl' => $recordUrl,
'entityType' => $this->getLanguage()->translate($entity->getEntityType(), 'scopeNames')
);
$data['entityTypeLowerFirst'] = lcfirst($data['entityType']);
$subjectTpl = str_replace(array("\n", "\r"), '', $subjectTpl);
$subject = $this->getHtmlizer()->render($entity, $subjectTpl, 'assignment-email-subject-' . $entity->getEntityType(), $data, true);
$body = $this->getHtmlizer()->render($entity, $bodyTpl, 'assignment-email-body-' . $entity->getEntityType(), $data, true);
$recordUrl = rtrim($this->getConfig()->get('siteUrl'), '/') . '/#' . $entity->getEntityType() . '/view/' . $entity->id;
$email->set(array(
'subject' => $subject,
'body' => $body,
'isHtml' => true,
'to' => $emailAddress,
'isSystem' => true,
'parentId' => $entity->id,
'parentType' => $entity->getEntityType()
));
try {
$this->getMailSender()->send($email);
} catch (\Exception $e) {
$GLOBALS['log']->error('EmailNotification: [' . $e->getCode() . '] ' .$e->getMessage());
}
$data = array(
'userName' => $user->get('name'),
'assignerUserName' => $assignerUser->get('name'),
'recordUrl' => $recordUrl,
'entityType' => $this->getLanguage()->translate($entity->getEntityType(), 'scopeNames')
);
$data['entityTypeLowerFirst'] = lcfirst($data['entityType']);
$subject = $this->getHtmlizer()->render($entity, $subjectTpl, 'assignment-email-subject-' . $entity->getEntityType(), $data, true);
$body = $this->getHtmlizer()->render($entity, $bodyTpl, 'assignment-email-body-' . $entity->getEntityType(), $data, true);
$email->set(array(
'subject' => $subject,
'body' => $body,
'isHtml' => true,
'to' => $emailAddress,
'isSystem' => true,
'parentId' => $entity->id,
'parentType' => $entity->getEntityType()
));
try {
$this->getMailSender()->send($email);
} catch (\Exception $e) {
$GLOBALS['log']->error('EmailNotification: [' . $e->getCode() . '] ' .$e->getMessage());
}
}

View File

@@ -33,8 +33,14 @@ Espo.define('views/settings/fields/assignment-notifications-entity-list', 'views
this.params.options = Object.keys(this.getMetadata().get('scopes')).filter(function (scope) {
if (this.getMetadata().get('scopes.' + scope + '.disabled')) return;
if (
this.getMetadata().get(['scopes', scope, 'stream'])
&&
!this.getMetadata().get(['entityDefs', scope, 'fields', 'assignedUsers'])
) return;
return this.getMetadata().get('scopes.' + scope + '.notifications') &&
!this.getMetadata().get('scopes.' + scope + '.stream') &&
this.getMetadata().get('scopes.' + scope + '.entity');
}, this).sort(function (v1, v2) {
return this.translate(v1, 'scopeNamesPlural').localeCompare(this.translate(v2, 'scopeNamesPlural'));