Skip to content

EspoCRM – Open Source CRM Application

Phone number search add code

Yuriif0f6fea93206
diff --git a/application/Espo/Core/Select/Text/ConfigProvider.php b/application/Espo/Core/Select/Text/ConfigProvider.php
index f4c8ec6b9e..23943c9da6 100644
--- a/application/Espo/Core/Select/Text/ConfigProvider.php
+++ b/application/Espo/Core/Select/Text/ConfigProvider.php
@@ -30,6 +30,7 @@
namespace Espo\Core\Select\Text;
use Espo\Core\Utils\Config;
+use libphonenumber\PhoneNumberUtil;
class ConfigProvider
{
@@ -64,4 +65,26 @@ class ConfigProvider
{
return $this->config->get('phoneNumberNumericSearch') ?? false;
}
+
+ public function isPhoneNumberInternational(): bool
+ {
+ return $this->config->get('phoneNumberInternational') ?? false;
+ }
+
+ /**
+ * @return int[]
+ * @since 10.0.8
+ */
+ public function getPreferredPhoneNumberCountryCodes(): array
+ {
+ $regionCodes = $this->config->get('phoneNumberPreferredCountryList') ?? [];
+
+ $codes = array_map(function ($code) {
+ return PhoneNumberUtil::getInstance()->getCountryCodeForRegion($code);
+ }, $regionCodes);
+
+ $codes = array_filter($codes, fn ($it) => $it !== 0);
+
+ return array_values($codes);
+ }
}
diff --git a/application/Espo/Core/Select/Text/DefaultFilter.php b/application/Espo/Core/Select/Text/DefaultFilter.php
index 325a5f6554..729ebfb269 100644
--- a/application/Espo/Core/Select/Text/DefaultFilter.php
+++ b/application/Espo/Core/Select/Text/DefaultFilter.php
@@ -46,7 +46,7 @@ class DefaultFilter implements Filter
public function __construct(
private string $entityType,
private MetadataProvider $metadataProvider,
- private ConfigProvider $config
+ private ConfigProvider $config,
) {}
public function apply(SelectBuilder $queryBuilder, Data $data): void
@@ -118,6 +118,8 @@ class DefaultFilter implements Filter
return;
}
+ $hasPlus = str_starts_with($filter, '+');
+
if ($this->config->usePhoneNumberNumericSearch()) {
$attribute = $attribute . 'Numeric';
@@ -127,6 +129,30 @@ class DefaultFilter implements Filter
if (!$filter) {
return;
}
+
+ if (
+ $this->config->usePhoneNumberNumericSearch() &&
+ $this->config->isPhoneNumberInternational() &&
+ !$skipWildcards
+ ) {
+ $preferredCodes = $this->config->getPreferredPhoneNumberCountryCodes();
+
+ $expression = $filter . '%';
+
+ $orGroupBuilder->add(
+ Cmp::like(Expr::column($attribute), $expression)
+ );
+
+ if (!$hasPlus) {
+ foreach ($preferredCodes as $code) {
+ $orGroupBuilder->add(
+ Cmp::like(Expr::column($attribute), $code . $expression)
+ );
+ }
+ }
+
+ return;
+ }
}
$expression = $filter;
diff --git a/tests/integration/Espo/Core/Select/SelectBuilderTest.php b/tests/integration/Espo/Core/Select/SelectBuilderTest.php
index d4efa19f49..8bec7e3c3a 100644
--- a/tests/integration/Espo/Core/Select/SelectBuilderTest.php
+++ b/tests/integration/Espo/Core/Select/SelectBuilderTest.php
@@ -32,6 +32,7 @@ namespace tests\integration\Espo\Core\Select;
use Espo\Core\Application;
use Espo\Core\Container;
use Espo\Core\InjectableFactory;
+use Espo\Core\Name\Field;
use Espo\Core\Select\SearchParams;
use Espo\Core\Select\SelectBuilderFactory;
use Espo\Classes\Select\Email\AdditionalAppliers\Main as EmailAdditionalApplier;
@@ -936,6 +937,73 @@ class SelectBuilderTest extends BaseTestCase
$this->assertEquals($account1->getId(), $accounts[0]->getId());
}
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testPhoneNumberSearch(): void
+ {
+ $metadata = $this->getMetadata();
+ $metadata->set('entityDefs', Account::ENTITY_TYPE, [
+ 'collection' => [
+ 'textFilterFields' => [
+ Field::PHONE_NUMBER,
+ ],
+ ],
+ ]);
+ $metadata->save();
+
+ $factory = $this->getInjectableFactory()->create(SelectBuilderFactory::class);
+
+
+ $em = $this->getEntityManager();
+
+ $em->createEntity(Account::ENTITY_TYPE, [
+ Field::PHONE_NUMBER => '+15453535543'
+ ]);
+
+ //
+
+ $query = $factory->create()
+ ->from(Account::ENTITY_TYPE)
+ ->withTextFilter('545-353-5543')
+ ->build();
+
+ $this->assertEquals(
+ 1,
+ $em->getRDBRepositoryByClass(Account::class)
+ ->clone($query)
+ ->count()
+ );
+
+ //
+
+ $query = $factory->create()
+ ->from(Account::ENTITY_TYPE)
+ ->withTextFilter('+1545-353-5543')
+ ->build();
+
+ $this->assertEquals(
+ 1,
+ $em->getRDBRepositoryByClass(Account::class)
+ ->clone($query)
+ ->count()
+ );
+
+ //
+
+ $query = $factory->create()
+ ->from(Account::ENTITY_TYPE)
+ ->withTextFilter('+545-353-5543')
+ ->build();
+
+ $this->assertEquals(
+ 0,
+ $em->getRDBRepositoryByClass(Account::class)
+ ->clone($query)
+ ->count()
+ );
+ }
+
protected function createUserEmailAddress(Container $container) : string
{
$userId = $container->getByClass(User::class)->getId();