diff --git a/application/Espo/Repositories/EmailAddress.php b/application/Espo/Repositories/EmailAddress.php index 6a096e6e73..8c4571e015 100644 --- a/application/Espo/Repositories/EmailAddress.php +++ b/application/Espo/Repositories/EmailAddress.php @@ -33,6 +33,12 @@ use Espo\ORM\Entity; class EmailAddress extends \Espo\Core\ORM\Repositories\RDB { + protected function init() + { + parent::init(); + $this->addDependency('user'); + } + public function getIdListFormAddressList(array $arr = []) { return $this->getIds($arr); @@ -117,6 +123,15 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB WHERE entity_email_address.email_address_id = ".$pdo->quote($emailAddressId)." AND entity_email_address.deleted = 0 + "; + + if ($entityType) { + $sql .= " + AND entity_email_address.entity_type = " . $pdo->quote($entityType) . " + "; + } + + $sql .= " ORDER BY entity_email_address.primary DESC, FIELD(entity_email_address.entity_type, 'User', 'Contact', 'Lead', 'Account') "; @@ -124,11 +139,6 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $sth->execute(); while ($row = $sth->fetch()) { if (!empty($row['entityType']) && !empty($row['entityId'])) { - if (!empty($entityType)) { - if ($entityType != $row['entityType']) { - continue; - } - } $entity = $this->getEntityManager()->getEntity($row['entityType'], $row['entityId']); if ($entity) { return $entity; @@ -154,6 +164,12 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB entity_email_address.deleted = 0 "; + if ($entityType) { + $sql .= " + AND entity_email_address.entity_type = " . $pdo->quote($entityType) . " + "; + } + $sql .= " ORDER BY FIELD(entity_email_address.entity_type, ".join(',', array_reverse($a)).") DESC, entity_email_address.primary DESC "; @@ -162,11 +178,6 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $sth->execute(); while ($row = $sth->fetch()) { if (!empty($row['entityType']) && !empty($row['entityId'])) { - if (!empty($entityType)) { - if ($entityType != $row['entityType']) { - continue; - } - } $entity = $this->getEntityManager()->getEntity($row['entityType'], $row['entityId']); if ($entity) { return $entity; @@ -278,12 +289,20 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB foreach ($toUpdate as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { - $emailAddress->set(array( - 'optOut' => $hash[$address]['optOut'], - 'invalid' => $hash[$address]['invalid'], - 'name' => $hash[$address]['emailAddress'] - )); - $this->save($emailAddress); + $skipSave = false; + if (!$this->getInjection('user')->isAdmin()) { + if ($this->getEntityByAddressId($emailAddress->id, 'User')) { + $skipSave = true; + } + } + if (!$skipSave) { + $emailAddress->set(array( + 'optOut' => $hash[$address]['optOut'], + 'invalid' => $hash[$address]['invalid'], + 'name' => $hash[$address]['emailAddress'] + )); + $this->save($emailAddress); + } } } diff --git a/application/Espo/Repositories/PhoneNumber.php b/application/Espo/Repositories/PhoneNumber.php index 3cbdf94f68..3678b2e117 100644 --- a/application/Espo/Repositories/PhoneNumber.php +++ b/application/Espo/Repositories/PhoneNumber.php @@ -33,6 +33,12 @@ use Espo\ORM\Entity; class PhoneNumber extends \Espo\Core\ORM\Repositories\RDB { + protected function init() + { + parent::init(); + $this->addDependency('user'); + } + public function getIds($arr = array()) { $ids = array(); @@ -102,6 +108,39 @@ class PhoneNumber extends \Espo\Core\ORM\Repositories\RDB return $this->where(array('name' => $number))->findOne(); } + public function getEntityByPhoneNumberId($phoneNumberId, $entityType = null) + { + $pdo = $this->getEntityManager()->getPDO(); + $sql = " + SELECT entity_phone_number.entity_type AS 'entityType', entity_phone_number.entity_id AS 'entityId' + FROM entity_phone_number + WHERE + entity_phone_number.phone_number_id = ".$pdo->quote($phoneNumberId)." AND + entity_phone_number.deleted = 0 + "; + + if ($entityType) { + $sql .= " + AND entity_phone_number.entity_type = " . $pdo->quote($entityType) . " + "; + } + + $sql .= " + ORDER BY entity_phone_number.primary DESC, FIELD(entity_phone_number.entity_type, 'User', 'Contact', 'Lead', 'Account') + "; + + $sth = $pdo->prepare($sql); + $sth->execute(); + while ($row = $sth->fetch()) { + if (!empty($row['entityType']) && !empty($row['entityId'])) { + $entity = $this->getEntityManager()->getEntity($row['entityType'], $row['entityId']); + if ($entity) { + return $entity; + } + } + } + } + public function storeEntityPhoneNumber(Entity $entity) { $phoneNumberValue = trim($entity->get('phoneNumber')); @@ -197,10 +236,18 @@ class PhoneNumber extends \Espo\Core\ORM\Repositories\RDB foreach ($toUpdate as $number) { $phoneNumber = $this->getByNumber($number); if ($phoneNumber) { - $phoneNumber->set(array( - 'type' => $hash[$number]['type'], - )); - $this->save($phoneNumber); + $skipSave = false; + if (!$this->getInjection('user')->isAdmin()) { + if ($this->getEntityByPhoneNumberId($phoneNumber->id, 'User')) { + $skipSave = true; + } + } + if (!$skipSave) { + $phoneNumber->set(array( + 'type' => $hash[$number]['type'], + )); + $this->save($phoneNumber); + } } }