Skip to content

EspoCRM – Open Source CRM Application

Ref

Yuriie87e6635f401
diff --git a/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php b/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php
index 2f63041b77..d634cd6821 100644
--- a/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php
+++ b/application/Espo/Core/FieldProcessing/EmailAddress/Saver.php
@@ -46,17 +46,17 @@ use Espo\Core\ORM\EntityManager;
*/
class Saver implements SaverInterface
{
- private const ATTR_EMAIL_ADDRESS = 'emailAddress';
- private const ATTR_EMAIL_ADDRESS_DATA = 'emailAddressData';
- private const ATTR_EMAIL_ADDRESS_IS_OPTED_OUT = 'emailAddressIsOptedOut';
- private const ATTR_EMAIL_ADDRESS_IS_INVALID = 'emailAddressIsInvalid';
+ private const string ATTR_EMAIL_ADDRESS = 'emailAddress';
+ private const string ATTR_EMAIL_ADDRESS_DATA = 'emailAddressData';
+ private const string ATTR_EMAIL_ADDRESS_IS_OPTED_OUT = 'emailAddressIsOptedOut';
+ private const string ATTR_EMAIL_ADDRESS_IS_INVALID = 'emailAddressIsInvalid';
- private const LINK_EMAIL_ADDRESSES = Link::EMAIL_ADDRESSES;
+ private const string LINK_EMAIL_ADDRESSES = Link::EMAIL_ADDRESSES;
public function __construct(
private EntityManager $entityManager,
private ApplicationState $applicationState,
- private AccessChecker $accessChecker
+ private AccessChecker $accessChecker,
) {}
public function process(Entity $entity, Params $params): void
@@ -405,29 +405,33 @@ class Saver implements SaverInterface
return;
}
- $emailAddressValue = $entity->get(self::ATTR_EMAIL_ADDRESS);
+ $address = $entity->get(self::ATTR_EMAIL_ADDRESS);
- if (is_string($emailAddressValue)) {
- $emailAddressValue = trim($emailAddressValue);
+ if (is_string($address)) {
+ $address = trim($address);
}
- if ($emailAddressValue) {
- $this->storePrimaryNotEmpty($entity, $emailAddressValue);
+ if ($address) {
+ $this->storePrimaryNotEmpty($entity, $address);
return;
}
- $emailAddressValueOld = $entity->getFetched(self::ATTR_EMAIL_ADDRESS);
+ $fetchedAddress = $entity->getFetched(self::ATTR_EMAIL_ADDRESS);
- if (!empty($emailAddressValueOld)) {
- $emailAddressOld = $this->getByAddress($emailAddressValueOld);
+ if (!$fetchedAddress) {
+ return;
+ }
- if ($emailAddressOld) {
- $this->entityManager
- ->getRelation($entity, self::LINK_EMAIL_ADDRESSES)
- ->unrelate($emailAddressOld, [SaveOption::SKIP_HOOKS => true]);
- }
+ $emailAddressOld = $this->getByAddress($fetchedAddress);
+
+ if (!$emailAddressOld) {
+ return;
}
+
+ $this->entityManager
+ ->getRelation($entity, self::LINK_EMAIL_ADDRESSES)
+ ->unrelate($emailAddressOld, [SaveOption::SKIP_HOOKS => true]);
}
private function storePrimaryNotEmpty(Entity $entity, string $emailAddressValue): void
@@ -466,15 +470,14 @@ class Saver implements SaverInterface
$entityRepository = $this->entityManager->getRDBRepository($entity->getEntityType());
$emailAddressNew = $this->entityManager
- ->getRDBRepository(EmailAddress::ENTITY_TYPE)
+ ->getRDBRepositoryByClass(EmailAddress::class)
->where([
'lower' => strtolower($emailAddressValue),
])
->findOne();
if (!$emailAddressNew) {
- /** @var EmailAddress $emailAddressNew */
- $emailAddressNew = $this->entityManager->getNewEntity(EmailAddress::ENTITY_TYPE);
+ $emailAddressNew = $this->entityManager->getRDBRepositoryByClass(EmailAddress::class)->getNew();
$emailAddressNew->setAddress($emailAddressValue);
@@ -491,7 +494,7 @@ class Saver implements SaverInterface
$emailAddressValueOld = $entity->getFetched(self::ATTR_EMAIL_ADDRESS);
- if (!empty($emailAddressValueOld)) {
+ if ($emailAddressValueOld) {
$emailAddressOld = $this->getByAddress($emailAddressValueOld);
if ($emailAddressOld) {
@@ -516,7 +519,7 @@ class Saver implements SaverInterface
$updateQuery = $this->entityManager
->getQueryBuilder()
->update()
- ->in('EntityEmailAddress')
+ ->in(EmailAddress::RELATION_ENTITY_EMAIL_ADDRESS)
->set(['primary' => true])
->where([
'entityId' => $entity->getId(),
diff --git a/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php b/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php
index 67d809e87e..d95e2f610d 100644
--- a/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php
+++ b/application/Espo/Core/FieldProcessing/PhoneNumber/Saver.php
@@ -47,16 +47,16 @@ use Espo\Core\Utils\Metadata;
*/
class Saver implements SaverInterface
{
- private const ATTR_PHONE_NUMBER = 'phoneNumber';
- private const ATTR_PHONE_NUMBER_DATA = 'phoneNumberData';
- private const ATTR_PHONE_NUMBER_IS_OPTED_OUT = 'phoneNumberIsOptedOut';
- private const ATTR_PHONE_NUMBER_IS_INVALID = 'phoneNumberIsInvalid';
+ private const string ATTR_PHONE_NUMBER = 'phoneNumber';
+ private const string ATTR_PHONE_NUMBER_DATA = 'phoneNumberData';
+ private const string ATTR_PHONE_NUMBER_IS_OPTED_OUT = 'phoneNumberIsOptedOut';
+ private const string ATTR_PHONE_NUMBER_IS_INVALID = 'phoneNumberIsInvalid';
public function __construct(
private EntityManager $entityManager,
private ApplicationState $applicationState,
private AccessChecker $accessChecker,
- private Metadata $metadata
+ private Metadata $metadata,
) {}
public function process(Entity $entity, Params $params): void
@@ -409,57 +409,37 @@ class Saver implements SaverInterface
return;
}
- $phoneNumberValue = trim($entity->get(self::ATTR_PHONE_NUMBER) ?? '');
+ $number = trim($entity->get(self::ATTR_PHONE_NUMBER) ?? '');
$entityRepository = $this->entityManager->getRDBRepository($entity->getEntityType());
- if ($phoneNumberValue) {
- if ($phoneNumberValue !== $entity->getFetched(self::ATTR_PHONE_NUMBER)) {
- $this->storePrimaryNotEmpty($phoneNumberValue, $entity);
+ if ($number) {
+ if ($number !== $entity->getFetched(self::ATTR_PHONE_NUMBER)) {
+ $this->storePrimaryNotEmpty($number, $entity);
return;
}
- if (
- $entity->has(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT) &&
- (
- $entity->isNew() ||
- (
- $entity->hasFetched(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT) &&
- $entity->isAttributeChanged(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT)
- )
- )
- ) {
- $this->markNumberOptedOut($phoneNumberValue, (bool) $entity->get(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT));
- }
-
- if (
- $entity->has(self::ATTR_PHONE_NUMBER_IS_INVALID) &&
- (
- $entity->isNew() ||
- (
- $entity->hasFetched(self::ATTR_PHONE_NUMBER_IS_INVALID) &&
- $entity->isAttributeChanged(self::ATTR_PHONE_NUMBER_IS_INVALID)
- )
- )
- ) {
- $this->markNumberInvalid($phoneNumberValue, (bool) $entity->get(self::ATTR_PHONE_NUMBER_IS_INVALID));
- }
+ $this->storePrimaryNotChanged($entity, $number);
return;
}
- $phoneNumberValueOld = $entity->getFetched(self::ATTR_PHONE_NUMBER);
+ $fetchedNumber = $entity->getFetched(self::ATTR_PHONE_NUMBER);
- if (!empty($phoneNumberValueOld)) {
- $phoneNumberOld = $this->getByNumber($phoneNumberValueOld);
+ if (!$fetchedNumber) {
+ return;
+ }
- if ($phoneNumberOld) {
- $entityRepository
- ->getRelation($entity, 'phoneNumbers')
- ->unrelate($phoneNumberOld, [SaveOption::SKIP_HOOKS => true]);
- }
+ $phoneNumberOld = $this->getByNumber($fetchedNumber);
+
+ if (!$phoneNumberOld) {
+ return;
}
+
+ $entityRepository
+ ->getRelation($entity, 'phoneNumbers')
+ ->unrelate($phoneNumberOld, [SaveOption::SKIP_HOOKS => true]);
}
private function getByNumber(string $number): ?PhoneNumber
@@ -504,24 +484,23 @@ class Saver implements SaverInterface
$entityRepository = $this->entityManager->getRDBRepository($entity->getEntityType());
$phoneNumberNew = $this->entityManager
- ->getRDBRepository(PhoneNumber::ENTITY_TYPE)
+ ->getRDBRepositoryByClass(PhoneNumber::class)
->where([
'name' => $phoneNumberValue,
])
->findOne();
if (!$phoneNumberNew) {
- /** @var PhoneNumber $phoneNumberNew */
- $phoneNumberNew = $this->entityManager->getNewEntity(PhoneNumber::ENTITY_TYPE);
+ $phoneNumberNew = $this->entityManager->getRDBRepositoryByClass(PhoneNumber::class)->getNew();
$phoneNumberNew->setNumber($phoneNumberValue);
if ($entity->has(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT)) {
- $phoneNumberNew->setOptedOut((bool)$entity->get(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT));
+ $phoneNumberNew->setOptedOut((bool) $entity->get(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT));
}
if ($entity->has(self::ATTR_PHONE_NUMBER_IS_INVALID)) {
- $phoneNumberNew->setInvalid((bool)$entity->get(self::ATTR_PHONE_NUMBER_IS_INVALID));
+ $phoneNumberNew->setInvalid((bool) $entity->get(self::ATTR_PHONE_NUMBER_IS_INVALID));
}
$defaultType = $this->metadata
@@ -559,7 +538,7 @@ class Saver implements SaverInterface
$update = $this->entityManager
->getQueryBuilder()
->update()
- ->in('EntityPhoneNumber')
+ ->in(PhoneNumber::RELATION_ENTITY_PHONE_NUMBER)
->set(['primary' => true])
->where([
'entityId' => $entity->getId(),
@@ -570,4 +549,33 @@ class Saver implements SaverInterface
$this->entityManager->getQueryExecutor()->execute($update);
}
+
+ private function storePrimaryNotChanged(Entity $entity, string $phoneNumberValue): void
+ {
+ if (
+ $entity->has(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT) &&
+ (
+ $entity->isNew() ||
+ (
+ $entity->hasFetched(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT) &&
+ $entity->isAttributeChanged(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT)
+ )
+ )
+ ) {
+ $this->markNumberOptedOut($phoneNumberValue, (bool) $entity->get(self::ATTR_PHONE_NUMBER_IS_OPTED_OUT));
+ }
+
+ if (
+ $entity->has(self::ATTR_PHONE_NUMBER_IS_INVALID) &&
+ (
+ $entity->isNew() ||
+ (
+ $entity->hasFetched(self::ATTR_PHONE_NUMBER_IS_INVALID) &&
+ $entity->isAttributeChanged(self::ATTR_PHONE_NUMBER_IS_INVALID)
+ )
+ )
+ ) {
+ $this->markNumberInvalid($phoneNumberValue, (bool) $entity->get(self::ATTR_PHONE_NUMBER_IS_INVALID));
+ }
+ }
}