diff --git a/application/Espo/Core/Mail/Importer/DefaultImporter.php b/application/Espo/Core/Mail/Importer/DefaultImporter.php index 5c7d4d533d..da01fd2feb 100644 --- a/application/Espo/Core/Mail/Importer/DefaultImporter.php +++ b/application/Espo/Core/Mail/Importer/DefaultImporter.php @@ -167,7 +167,7 @@ class DefaultImporter implements Importer $email->set('addressNameMap', $addressNameMap); foreach ($folderData as $uId => $folderId) { - $email->setLinkMultipleColumn('users', Email::USERS_COLUMN_FOLDER_ID, $uId, $folderId); + $email->setUserColumnFolderId($uId, $folderId); } $matchedFilter = $this->filtersMatcher->findMatch($email, $filterList, true); @@ -501,7 +501,7 @@ class DefaultImporter implements Importer foreach ($folderData as $uId => $folderId) { if (!in_array($uId, $fetchedUserIdList)) { - $duplicate->setLinkMultipleColumn('users', Email::USERS_COLUMN_FOLDER_ID, $uId, $folderId); + $duplicate->setUserColumnFolderId($uId, $folderId); continue; } diff --git a/application/Espo/Entities/Email.php b/application/Espo/Entities/Email.php index f6974676f8..89d81fceca 100644 --- a/application/Espo/Entities/Email.php +++ b/application/Espo/Entities/Email.php @@ -636,6 +636,13 @@ class Email extends Entity return $this->getValueObject('parent'); } + public function setAccount(?Link $account): self + { + $this->setValueObject('account', $account); + + return $this; + } + public function setParent(LinkParent|Entity|null $parent): self { if ($parent instanceof LinkParent) { @@ -666,6 +673,18 @@ class Email extends Entity return $this->getValueObject('teams'); } + public function getUsers(): LinkMultiple + { + /** @var LinkMultiple */ + return $this->getValueObject('users'); + } + + public function getAssignedUser(): ?Link + { + /** @var ?Link */ + return $this->getValueObject('assignedUser'); + } + public function getCreatedBy(): ?Link { /** @var ?Link */ @@ -770,6 +789,27 @@ class Email extends Entity return $this; } + public function setUserColumnFolderId(string $userId, ?string $folderId): self + { + $this->setLinkMultipleColumn('users', self::USERS_COLUMN_FOLDER_ID, $userId, $folderId); + + return $this; + } + + public function setUserColumnIsRead(string $userId, bool $isRead): self + { + $this->setLinkMultipleColumn('users', self::USERS_COLUMN_IS_READ, $userId, $isRead); + + return $this; + } + + public function setUserColumnInTrash(string $userId, bool $inTrash): self + { + $this->setLinkMultipleColumn('users', self::USERS_COLUMN_IN_TRASH, $userId, $inTrash); + + return $this; + } + public function addTeamId(string $teamId): self { $this->addLinkMultipleId('teams', $teamId); diff --git a/application/Espo/Repositories/Email.php b/application/Espo/Repositories/Email.php index 503d1634dd..3783ad18c0 100644 --- a/application/Espo/Repositories/Email.php +++ b/application/Espo/Repositories/Email.php @@ -29,6 +29,7 @@ namespace Espo\Repositories; +use Espo\Core\Field\Link; use Espo\Core\ORM\Repository\Option\SaveOption; use Espo\Entities\EmailFilter; use Espo\Entities\InboundEmail; @@ -37,14 +38,11 @@ use Espo\Modules\Crm\Entities\Account; use Espo\ORM\Collection; use Espo\ORM\Entity; use Espo\Core\ORM\Entity as CoreEntity; - use Espo\Core\Repositories\Database; - -use Espo\Core\Di; - use Espo\Entities\Email as EmailEntity; use Espo\Repositories\EmailAddress as EmailAddressRepository; use Espo\Entities\EmailAddress; +use Espo\Core\Di; use stdClass; /** @@ -68,14 +66,8 @@ class Email extends Database implements $idList = []; if (!empty($addressValue)) { - $addressList = array_map( - fn ($item) => trim($item), - explode(';', $addressValue) - ); - - $addressList = array_filter($addressList, function ($item) { - return filter_var($item, FILTER_VALIDATE_EMAIL) !== false; - }); + $addressList = array_map(fn($item) => trim($item), explode(';', $addressValue)); + $addressList = array_filter($addressList, fn($item) => filter_var($item, FILTER_VALIDATE_EMAIL) !== false); $idList = $eaRepository->getIdListFormAddressList($addressList); @@ -100,10 +92,10 @@ class Email extends Database implements ->getEntityListByAddressId($emailAddressId, null, UserEntity::ENTITY_TYPE, true); foreach ($users as $user) { - $entity->addLinkMultipleId('users', $user->getId()); + $entity->addUserId($user->getId()); if ($addAssignedUser && !$user->isPortal()) { - $entity->addLinkMultipleId('assignedUsers', $user->getId()); + $entity->addAssignedUserId($user->getId()); } } } @@ -122,11 +114,11 @@ class Email extends Database implements $fromEmailAddressId = $entity->get('fromEmailAddressId'); if ($fromEmailAddressId) { - $ea = $this->getEmailAddressRepository()->getById($fromEmailAddressId); + $emailAddress = $this->getEmailAddressRepository()->getById($fromEmailAddressId); - if ($ea) { - $entity->set('from', $ea->get('name')); - $entity->setFetched('from', $ea->get('name')); + if ($emailAddress) { + $entity->setFromAddress($emailAddress->getAddress()); + $entity->setFetched('from', $emailAddress->getAddress()); return; } @@ -136,7 +128,7 @@ class Email extends Database implements return; } - $entity->set('from', null); + $entity->setFromAddress(null); $entity->setFetched('from', null); } @@ -329,18 +321,18 @@ class Email extends Database implements } if ($entity->has('from')) { - $from = trim($entity->get('from')); + $from = trim($entity->getFromAddress() ?? ''); if (!empty($from)) { $ids = $this->getEmailAddressRepository()->getIdListFormAddressList([$from]); - if (!empty($ids)) { + if ($ids !== []) { $entity->set('fromEmailAddressId', $ids[0]); $entity->set('fromEmailAddressName', $from); $this->addUserByEmailAddressId($entity, $ids[0], true); - if (!$entity->get('sentById')) { + if (!$entity->getSentBy()) { $user = $this->getEmailAddressRepository() ->getEntityByAddressId( $entity->get('fromEmailAddressId'), @@ -354,6 +346,7 @@ class Email extends Database implements } } } else { + /** @noinspection PhpRedundantOptionalArgumentInspection */ $entity->set('fromEmailAddressId', null); } } @@ -373,18 +366,17 @@ class Email extends Database implements if ($entity->has('replyTo')) { $this->prepareAddresses($entity, 'replyTo'); } + } - $assignedUserId = $entity->get('assignedUserId'); - if ($assignedUserId) { - $entity->addLinkMultipleId('users', $assignedUserId); - } + if ($entity->getAssignedUser()) { + $entity->addUserId($entity->getAssignedUser()->getId()); } parent::beforeSave($entity, $options); - if ($entity->getStatus() === EmailEntity::STATUS_SENDING && $entity->get('createdById')) { - $entity->addLinkMultipleId('users', $entity->get('createdById')); - $entity->setLinkMultipleColumn('users', 'isRead', $entity->get('createdById'), true); + if ($entity->getStatus() === EmailEntity::STATUS_SENDING && $entity->getCreatedBy()) { + $entity->addUserId($entity->getCreatedBy()->getId()); + $entity->setUserColumnIsRead($entity->getCreatedBy()->getId(), true); } if ($entity->isNew() || $entity->isAttributeChanged('parentId')) { @@ -395,6 +387,7 @@ class Email extends Database implements if (!$entity->has('from')) { $this->loadFromField($entity); } + if (!$entity->has('to')) { $this->loadToField($entity); } @@ -406,48 +399,48 @@ class Email extends Database implements public function fillAccount(EmailEntity $entity): void { if (!$entity->isNew()) { - $entity->set('accountId', null); + $entity->setAccount(null); } - $parentId = $entity->get('parentId'); - $parentType = $entity->get('parentType'); + if (!$entity->getParent()) { + return; + } - if ($parentId && $parentType) { - $parent = $this->entityManager->getEntity($parentType, $parentId); + $parent = $this->entityManager + ->getEntityById($entity->getParent()->getEntityType(), $entity->getParent()->getId()); - if ($parent) { - $accountId = null; + if (!$parent) { + return; + } - if ($parent->getEntityType() == Account::ENTITY_TYPE) { - $accountId = $parent->getId(); - } + $accountId = null; - if ( - !$accountId && - $parent->get('accountId') && - $parent instanceof CoreEntity && - $parent->getRelationParam('account', 'entity') === Account::ENTITY_TYPE - ) { - $accountId = $parent->get('accountId'); - } + if ($parent->getEntityType() == Account::ENTITY_TYPE) { + $accountId = $parent->getId(); + } - if ($accountId) { - $account = $this->entityManager->getEntityById(Account::ENTITY_TYPE, $accountId); + if ( + !$accountId && + $parent->get('accountId') && + $parent instanceof CoreEntity && + $parent->getRelationParam('account', 'entity') === Account::ENTITY_TYPE + ) { + $accountId = $parent->get('accountId'); + } - if ($account) { - $entity->set('accountId', $accountId); - $entity->set('accountName', $account->get('name')); - } - } + if ($accountId) { + /** @var ?Account $account */ + $account = $this->entityManager->getEntityById(Account::ENTITY_TYPE, $accountId); + + if ($account) { + $entity->setAccount(Link::create($accountId, $account->getName())); } } } public function applyUsersFilters(EmailEntity $entity): void { - $userIdList = $entity->getLinkMultipleIdList('users'); - - foreach ($userIdList as $userId) { + foreach ($entity->getUsers()->getIdList() as $userId) { if ( $entity->getStatus() === EmailEntity::STATUS_SENT && $entity->getSentBy()?->getId() === $userId @@ -462,18 +455,15 @@ class Email extends Database implements } if ($filter->getAction() === EmailFilter::ACTION_SKIP) { - $entity->setLinkMultipleColumn('users', EmailEntity::USERS_COLUMN_IN_TRASH, $userId, true); + $entity->setUserColumnInTrash($userId, true); } else if ($filter->getAction() === EmailFilter::ACTION_MOVE_TO_FOLDER) { - $folderId = $filter->getEmailFolderId(); - - if ($folderId) { - $entity - ->setLinkMultipleColumn('users', EmailEntity::USERS_COLUMN_FOLDER_ID, $userId, $folderId); + if ($filter->getEmailFolderId()) { + $entity->setUserColumnFolderId($userId, $filter->getEmailFolderId()); } } if ($filter->markAsRead()) { - $entity->setLinkMultipleColumn('users', EmailEntity::USERS_COLUMN_IS_READ, $userId, true); + $entity->setUserColumnIsRead($userId, true); } } } @@ -523,15 +513,16 @@ class Email extends Database implements $entity->isNew() ) ) { - $repliedId = $entity->get('repliedId'); + $repliedId = $entity->getReplied()?->getId(); if ($repliedId) { + /** @var ?EmailEntity $replied */ $replied = $this->entityManager->getEntityById(EmailEntity::ENTITY_TYPE, $repliedId); if ( $replied && $replied->getId() !== $entity->getId() && - !$replied->get('isReplied') + !$replied->getReplied() ) { $replied->set('isReplied', true);