email address phone number tools

This commit is contained in:
Yuri Kuznetsov
2023-02-24 13:30:25 +02:00
parent e416dac56f
commit 7b2526430a
8 changed files with 420 additions and 88 deletions
+48 -45
View File
@@ -35,18 +35,27 @@ use Espo\ORM\Entity;
use Espo\Entities\EmailAddress as EmailAddressEntity;
use Espo\Core\Di;
use stdClass;
/**
* Not to be used directly. Use utilities from `Espo\Tools\EmailAddress` instead.
* @internal
* @extends Database<EmailAddressEntity>
*/
class EmailAddress extends Database implements
Di\ApplicationStateAware,
Di\AclManagerAware
Di\AclManagerAware,
Di\ConfigAware
{
use Di\ApplicationStateSetter;
use Di\AclManagerSetter;
use Di\ConfigSetter;
protected $hooksDisabled = true;
private const LOOKUP_SMALL_MAX_SIZE = 20;
private const LOOKUP_MAX_SIZE = 50;
/**
* @param string[] $addressList
* @return string[]
@@ -63,45 +72,44 @@ class EmailAddress extends Database implements
*/
public function getIds(array $addressList = []): array
{
if (empty($addressList)) {
return [];
}
$ids = [];
if (!empty($addressList)) {
$lowerAddressList = [];
$lowerAddressList = [];
foreach ($addressList as $address) {
$lowerAddressList[] = trim(strtolower($address));
foreach ($addressList as $address) {
$lowerAddressList[] = trim(strtolower($address));
}
$eaCollection = $this
->where(['lower' => $lowerAddressList])
->find();
$exist = [];
foreach ($eaCollection as $ea) {
$ids[] = $ea->getId();
$exist[] = $ea->get('lower');
}
foreach ($addressList as $address) {
$address = trim($address);
if (empty($address) || !filter_var($address, FILTER_VALIDATE_EMAIL)) {
continue;
}
$eaCollection = $this
->where([
['lower' => $lowerAddressList]
])
->find();
if (!in_array(strtolower($address), $exist)) {
$ea = $this->getNew();
$ids = [];
$exist = [];
$ea->set('name', $address);
$this->save($ea);
foreach ($eaCollection as $ea) {
$ids[] = $ea->getId();
$exist[] = $ea->get('lower');
}
foreach ($addressList as $address) {
$address = trim($address);
if (empty($address) || !filter_var($address, FILTER_VALIDATE_EMAIL)) {
continue;
}
if (!in_array(strtolower($address), $exist)) {
$ea = $this->getNew();
$ea->set('name', $address);
$this->save($ea);
$ids[] = $ea->getId();
}
}
}
@@ -109,7 +117,7 @@ class EmailAddress extends Database implements
}
/**
* @return \stdClass[]
* @return stdClass[]
*/
public function getEmailAddressData(Entity $entity): array
{
@@ -193,6 +201,7 @@ class EmailAddress extends Database implements
->sth()
->select(['entityType', 'entityId'])
->where($where)
->limit(0, self::LOOKUP_MAX_SIZE)
->find();
foreach ($itemList as $item) {
@@ -257,7 +266,7 @@ class EmailAddress extends Database implements
->sth()
->select(['entityType', 'entityId'])
->where($where)
->limit(0, 20)
->limit(0, self::LOOKUP_SMALL_MAX_SIZE)
->order([
['primary', 'DESC'],
['LIST:entityType:User,Contact,Lead,Account'],
@@ -310,16 +319,9 @@ class EmailAddress extends Database implements
/**
* @param string[] $order
*/
public function getEntityByAddress(
string $address,
?string $entityType = null,
array $order = [
'User',
'Contact',
'Lead',
'Account',
]
): ?Entity {
public function getEntityByAddress(string $address, ?string $entityType = null, ?array $order = null): ?Entity
{
$order ??= $this->config->get('emailAddressEntityLookupDefaultOrder') ?? [];
$selectBuilder = $this->entityManager
->getRDBRepository(EmailAddressEntity::RELATION_ENTITY_EMAIL_ADDRESS)
@@ -337,7 +339,8 @@ class EmailAddress extends Database implements
->order([
['LIST:entityType:' . implode(',', $order)],
['primary', 'DESC'],
]);
])
->limit(0, self::LOOKUP_MAX_SIZE);
if ($entityType) {
$selectBuilder->where('entityType=', $entityType);
+57 -43
View File
@@ -35,63 +35,69 @@ use Espo\Entities\PhoneNumber as PhoneNumberEntity;
use Espo\Core\Repositories\Database;
use Espo\Core\Di;
use stdClass;
/**
* Not to be used directly. Use utilities from `Espo\Tools\PhoneNumber` instead.
* @internal
* @extends Database<PhoneNumberEntity>
*/
class PhoneNumber extends Database implements
Di\ApplicationStateAware,
Di\AclManagerAware
Di\AclManagerAware,
Di\ConfigAware
{
use Di\ApplicationStateSetter;
use Di\AclManagerSetter;
use Di\ConfigSetter;
protected $hooksDisabled = true;
const ERASED_PREFIX = 'ERASED:';
private const ERASED_PREFIX = 'ERASED:';
private const LOOKUP_SMALL_MAX_SIZE = 20;
private const LOOKUP_MAX_SIZE = 50;
/**
* @param string [] $numberList
* @param string[] $numberList
* @return string[]
*/
public function getIds($numberList = []): array
{
if (empty($numberList)) {
return [];
}
$ids = [];
if (!empty($numberList)) {
$phoneNumbers = $this
->where([
[
'name' => $numberList,
'hash' => null,
]
])
->find();
$phoneNumbers = $this
->where([
'name' => $numberList,
'hash' => null,
])
->find();
$ids = [];
$exist = [];
$exist = [];
foreach ($phoneNumbers as $phoneNumber) {
$ids[] = $phoneNumber->getId();
$exist[] = $phoneNumber->get('name');
foreach ($phoneNumbers as $phoneNumber) {
$ids[] = $phoneNumber->getId();
$exist[] = $phoneNumber->get('name');
}
foreach ($numberList as $number) {
$number = trim($number);
if (empty($number)) {
continue;
}
foreach ($numberList as $number) {
$number = trim($number);
if (!in_array($number, $exist)) {
$phoneNumber = $this->getNew();
$phoneNumber->set('name', $number);
$this->save($phoneNumber);
if (empty($number)) {
continue;
}
if (!in_array($number, $exist)) {
$phoneNumber = $this->getNew();
$phoneNumber->set('name', $number);
$this->save($phoneNumber);
$ids[] = $phoneNumber->getId();
}
$ids[] = $phoneNumber->getId();
}
}
@@ -99,7 +105,7 @@ class PhoneNumber extends Database implements
}
/**
* @return array<int,\stdClass>
* @return array<int, stdClass>
*/
public function getPhoneNumberData(Entity $entity): array
{
@@ -172,6 +178,7 @@ class PhoneNumber extends Database implements
->sth()
->select(['entityType', 'entityId'])
->where($where)
->limit(0, self::LOOKUP_MAX_SIZE)
->find();
foreach ($itemList as $item) {
@@ -198,29 +205,36 @@ class PhoneNumber extends Database implements
return $entityList;
}
public function getEntityByPhoneNumberId(string $phoneNumberId, ?string $entityType = null): ?Entity
{
$where = [
'phoneNumberId' => $phoneNumberId,
];
/**
* @param string[] $order
*/
public function getEntityByPhoneNumberId(
string $phoneNumberId,
?string $entityType = null,
?array $order = null
): ?Entity {
$order ??= $this->config->get('phoneNumberEntityLookupDefaultOrder') ?? [];
$where = ['phoneNumberId' => $phoneNumberId];
if ($entityType) {
$where[] = ['entityType' => $entityType];
}
$itemList = $this->entityManager
$collection = $this->entityManager
->getRDBRepository(PhoneNumberEntity::RELATION_ENTITY_PHONE_NUMBER)
->sth()
->select(['entityType', 'entityId'])
->where($where)
->limit(0, 20)
->limit(0, self::LOOKUP_SMALL_MAX_SIZE)
->order([
['LIST:entityType:' . implode(',', $order)],
['primary', 'DESC'],
['LIST:entityType:User,Contact,Lead,Account'],
])
->find();
foreach ($itemList as $item) {
foreach ($collection as $item) {
$itemEntityType = $item->get('entityType');
$itemEntityId = $item->get('entityId');
@@ -255,7 +269,7 @@ class PhoneNumber extends Database implements
if ($entity->has('name')) {
$number = $entity->get('name');
if (is_string($number) && strpos($number, self::ERASED_PREFIX) !== 0) {
if (is_string($number) && !str_starts_with($number, self::ERASED_PREFIX)) {
$numeric = preg_replace('/[^0-9]/', '', $number);
}
else {
@@ -145,6 +145,8 @@ return [
'personalEmailMaxPortionSize' => 50,
'inboundEmailMaxPortionSize' => 50,
'emailAddressLookupEntityTypeList' => ['User', 'Contact', 'Lead', 'Account'],
'emailAddressEntityLookupDefaultOrder' => ['User', 'Contact', 'Lead', 'Account'],
'phoneNumberEntityLookupDefaultOrder' => ['User', 'Contact', 'Lead', 'Account'],
'authTokenLifetime' => 0,
'authTokenMaxIdleTime' => 48,
'userNameRegularExpression' => '[^a-z0-9\-@_\.\s]',
@@ -188,6 +188,8 @@ return [
'outboundSmsFromNumber',
'currencyNoJoinMode',
'authAnotherUserDisabled',
'emailAddressEntityLookupDefaultOrder',
'phoneNumberEntityLookupDefaultOrder',
'latestVersion',
],
'superAdminItems' => [
@@ -0,0 +1,90 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\EmailAddress;
use Espo\Entities\EmailAddress;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use RuntimeException;
/**
* Entity lookup by an email address.
*/
class EntityLookup
{
private EmailAddressRepository $internalRepository;
public function __construct(
private Repository $repository,
EntityManager $entityManager
) {
$repository = $entityManager->getRDBRepository(EmailAddress::ENTITY_TYPE);
if (!$repository instanceof EmailAddressRepository) {
throw new RuntimeException();
}
$this->internalRepository = $repository;
}
/**
* Find entities by an email address.
*
* @param string $address An email address.
* @return Entity[]
*/
public function find(string $address): array
{
$emailAddress = $this->repository->getByAddress($address);
if (!$emailAddress) {
return [];
}
return $this->internalRepository->getEntityListByAddressId($emailAddress->getId());
}
/**
* Find a first entity by an email address.
*
* @param string $address An email address.
* @param string[] $order An order entity type list.
*/
public function findOne(string $address, ?array $order = null): ?Entity
{
if ($order) {
$this->internalRepository->getEntityByAddress($address, null, $order);
}
return $this->internalRepository->getEntityByAddress($address);
}
}
@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\EmailAddress;
use Espo\Entities\EmailAddress;
use Espo\ORM\EntityManager;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use RuntimeException;
/**
* An email address repository.
*/
class Repository
{
private EmailAddressRepository $repository;
public function __construct(EntityManager $entityManager)
{
$repository = $entityManager->getRDBRepository(EmailAddress::ENTITY_TYPE);
if (!$repository instanceof EmailAddressRepository) {
throw new RuntimeException();
}
$this->repository = $repository;
}
/**
* Get an email address entity by a string address.
*/
public function getByAddress(string $address): ?EmailAddress
{
return $this->repository->getByAddress($address);
}
}
@@ -0,0 +1,96 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\PhoneNumber;
use Espo\Entities\PhoneNumber;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Repositories\PhoneNumber as PhoneNumberRepository;
use RuntimeException;
/**
* Entity lookup by a phone number.
*/
class EntityLookup
{
private PhoneNumberRepository $internalRepository;
public function __construct(
private Repository $repository,
EntityManager $entityManager
) {
$repository = $entityManager->getRDBRepository(PhoneNumber::ENTITY_TYPE);
if (!$repository instanceof PhoneNumberRepository) {
throw new RuntimeException();
}
$this->internalRepository = $repository;
}
/**
* Find entities by a phone number.
*
* @param string $number A phone number.
* @return Entity[]
*/
public function find(string $number): array
{
$phoneNumber = $this->repository->getByNumber($number);
if (!$phoneNumber) {
return [];
}
return $this->internalRepository->getEntityListByPhoneNumberId($phoneNumber->getId());
}
/**
* Find a first entity by a phone number.
*
* @param string $number A phone number.
* @param string[] $order An order entity type list.
*/
public function findOne(string $number, ?array $order = null): ?Entity
{
$phoneNumber = $this->repository->getByNumber($number);
if (!$phoneNumber) {
return null;
}
if ($order) {
$this->internalRepository->getEntityByPhoneNumberId($phoneNumber->getId(), null, $order);
}
return $this->internalRepository->getEntityByPhoneNumberId($phoneNumber->getId());
}
}
@@ -0,0 +1,63 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\PhoneNumber;
use Espo\Entities\PhoneNumber;
use Espo\ORM\EntityManager;
use Espo\Repositories\PhoneNumber as PhoneNumberRepository;
use RuntimeException;
/**
* A phone number repository.
*/
class Repository
{
private PhoneNumberRepository $repository;
public function __construct(EntityManager $entityManager)
{
$repository = $entityManager->getRDBRepository(PhoneNumber::ENTITY_TYPE);
if (!$repository instanceof PhoneNumberRepository) {
throw new RuntimeException();
}
$this->repository = $repository;
}
/**
* Get a phone number entity by a string number.
*/
public function getByNumber(string $number): ?PhoneNumber
{
return $this->repository->getByNumber($number);
}
}