Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,212 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
use Espo\Core\Select\Text\FullTextSearch\Data as FullTextSearchData;
use Espo\Core\Select\Text\FullTextSearch\DataComposerFactory as FullTextSearchDataComposerFactory;
use Espo\Core\Select\Text\FullTextSearch\DataComposer\Params as FullTextSearchDataComposerParams;
use Espo\Core\Select\Text\Filter\Data as FilterData;
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
use Espo\ORM\Query\Part\Order as OrderExpr;
use Espo\ORM\Query\Part\Expression as Expr;
use Espo\ORM\Query\Part\WhereItem;
use Espo\Entities\User;
class Applier
{
/** @todo Move to metadata. */
private ?int $fullTextRelevanceThreshold = null; /** @phpstan-ignore-line */
/** @todo Move to metadata. */
private int $fullTextOrderRelevanceDivider = 5; /** @phpstan-ignore-line */
private const DEFAULT_FT_ORDER = self::FT_ORDER_COMBINED;
private const DEFAULT_ATTRIBUTE_LIST = ['name'];
private const FT_ORDER_COMBINED = 0;
private const FT_ORDER_RELEVANCE = 1;
private const FT_ORDER_ORIGINAL = 3;
public function __construct(
private string $entityType,
private User $user,
private MetadataProvider $metadataProvider,
private FullTextSearchDataComposerFactory $fullTextSearchDataComposerFactory,
private FilterFactory $filterFactory,
private ConfigProvider $config
) {}
/** @noinspection PhpUnusedParameterInspection */
public function apply(QueryBuilder $queryBuilder, string $filter, FilterParams $params): void
{
$forceFullText = false;
$skipFullText = false;
if (mb_strpos($filter, 'ft:') === 0) {
$filter = mb_substr($filter, 3);
$forceFullText = true;
}
$fullTextData = $this->composeFullTextSearchData($filter);
if ($fullTextData && !$forceFullText && $this->toSkipFullText($filter)) {
$skipFullText = true;
}
$fullTextWhere = $fullTextData && !$skipFullText ?
$this->processFullTextSearch($queryBuilder, $fullTextData) : null;
$fieldList = $this->getFieldList($forceFullText, $fullTextData);
$filterData = $this->prepareFilterData($filter, $fieldList, $forceFullText, $fullTextWhere);
$this->applyFilter($queryBuilder, $filterData);
}
private function composeFullTextSearchData(string $filter): ?FullTextSearchData
{
$composer = $this->fullTextSearchDataComposerFactory->create($this->entityType);
$params = FullTextSearchDataComposerParams::create();
return $composer->compose($filter, $params);
}
private function processFullTextSearch(QueryBuilder $queryBuilder, FullTextSearchData $data): WhereItem
{
$expression = $data->getExpression();
$orderType = self::DEFAULT_FT_ORDER;
$orderTypeMap = [
'combined' => self::FT_ORDER_COMBINED,
'relevance' => self::FT_ORDER_RELEVANCE,
'original' => self::FT_ORDER_ORIGINAL,
];
$mOrderType = $this->metadataProvider->getFullTextSearchOrderType($this->entityType);
if ($mOrderType) {
$orderType = $orderTypeMap[$mOrderType];
}
$previousOrderBy = $queryBuilder->build()->getOrder();
$hasOrderBy = !empty($previousOrderBy);
if (!$hasOrderBy || $orderType === self::FT_ORDER_RELEVANCE) {
$queryBuilder->order([
OrderExpr::create($expression)->withDesc()
]);
} else if ($orderType === self::FT_ORDER_COMBINED) {
$orderExpression =
Expr::round(
Expr::divide($expression, $this->fullTextOrderRelevanceDivider)
);
$newOrderBy = array_merge(
[OrderExpr::create($orderExpression)->withDesc()],
$previousOrderBy
);
$queryBuilder->order($newOrderBy);
}
if ($this->fullTextRelevanceThreshold) {
return Expr::greaterOrEqual(
$expression,
$this->fullTextRelevanceThreshold
);
}
return Expr::notEqual($expression, 0);
}
private function toSkipFullText(string $filter): bool
{
$min = $this->config->getFullTextSearchMinLength();
if ($min === null || strlen($filter) >= $min) {
return false;
}
return
!str_contains($filter, '*') &&
!str_contains($filter, '"') &&
!str_contains($filter, '+') &&
!str_contains($filter, '-');
}
/**
* @return string[]
*/
private function getFieldList(bool $forceFullTextSearch, ?FullTextSearchData $fullTextData): array
{
if ($forceFullTextSearch) {
return [];
}
$fullTextFieldList = $fullTextData ? $fullTextData->getFieldList() : [];
return array_filter(
$this->metadataProvider->getTextFilterAttributeList($this->entityType) ?? self::DEFAULT_ATTRIBUTE_LIST,
fn ($field) => !in_array($field, $fullTextFieldList)
);
}
/**
* @param string[] $fieldList
* @param ?WhereItem $fullTextWhere
*/
private function prepareFilterData(
string $filter,
array $fieldList,
bool $forceFullTextSearch,
?WhereItem $fullTextWhere
): FilterData {
$skipWildcards = false;
if (mb_strpos($filter, '*') !== false) {
$skipWildcards = true;
$filter = str_replace('*', '%', $filter);
}
return FilterData::create($filter, $fieldList)
->withSkipWildcards($skipWildcards)
->withForceFullTextSearch($forceFullTextSearch)
->withFullTextSearchWhereItem($fullTextWhere);
}
private function applyFilter(QueryBuilder $queryBuilder, FilterData $filterData): void
{
$filterObj = $this->filterFactory->create($this->entityType, $this->user);
$filterObj->apply($queryBuilder, $filterData);
}
}

View File

@@ -0,0 +1,67 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
use Espo\Core\Utils\Config;
class ConfigProvider
{
private const MIN_LENGTH_FOR_CONTENT_SEARCH = 4;
public function __construct(private Config $config)
{}
/**
* Full-text search min indexed word length.
*
* @internal Do not use.
* @since 8.3.0
*/
public function getFullTextSearchMinLength(): ?int
{
return $this->config->get('fullTextSearchMinLength');
}
public function getMinLengthForContentSearch(): int
{
return $this->config->get('textFilterContainsMinLength') ??
self::MIN_LENGTH_FOR_CONTENT_SEARCH;
}
public function useContainsForVarchar(): bool
{
return $this->config->get('textFilterUseContainsForVarchar') ?? false;
}
public function usePhoneNumberNumericSearch(): bool
{
return $this->config->get('phoneNumberNumericSearch') ?? false;
}
}

View File

@@ -0,0 +1,211 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Select\Text\Filter\Data;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
use Espo\ORM\Query\Part\Where\OrGroup;
use Espo\ORM\Query\Part\Where\OrGroupBuilder;
use Espo\ORM\Query\Part\Where\Comparison as Cmp;
use Espo\ORM\Query\Part\Expression as Expr;
use Espo\ORM\Entity;
use RuntimeException;
class DefaultFilter implements Filter
{
public function __construct(
private string $entityType,
private MetadataProvider $metadataProvider,
private ConfigProvider $config
) {}
public function apply(QueryBuilder $queryBuilder, Data $data): void
{
$orGroupBuilder = OrGroup::createBuilder();
foreach ($data->getAttributeList() as $attribute) {
$this->applyAttribute($queryBuilder, $orGroupBuilder, $attribute, $data);
}
if ($data->getFullTextSearchWhereItem()) {
$orGroupBuilder->add(
$data->getFullTextSearchWhereItem()
);
}
$orGroup = $orGroupBuilder->build();
if ($orGroup->getItemCount() === 0) {
$queryBuilder->where([Attribute::ID => null]);
return;
}
$queryBuilder->where($orGroup);
}
/**
* @todo AttributeFilterFactory.
*/
private function applyAttribute(
QueryBuilder $queryBuilder,
OrGroupBuilder $orGroupBuilder,
string $attribute,
Data $data
): void {
$filter = $data->getFilter();
$skipWildcards = $data->skipWildcards();
$attributeType = $this->getAttributeTypeAndApplyJoin($queryBuilder, $attribute);
if ($attributeType === Entity::INT) {
if (is_numeric($filter)) {
$orGroupBuilder->add(
Cmp::equal(
Expr::column($attribute),
intval($filter)
)
);
}
return;
}
if (
!str_contains($attribute, '.') &&
$this->metadataProvider->getFieldType($this->entityType, $attribute) === FieldType::EMAIL &&
str_contains($filter, ' ')
) {
return;
}
if (
!str_contains($attribute, '.') &&
$this->metadataProvider->getFieldType($this->entityType, $attribute) === FieldType::PHONE
) {
if (!preg_match("#[0-9()\-+% ]+$#", $filter)) {
return;
}
if ($this->config->usePhoneNumberNumericSearch()) {
$attribute = $attribute . 'Numeric';
$filter = preg_replace('/[^0-9%]/', '', $filter);
}
if (!$filter) {
return;
}
}
$expression = $filter;
if (!$skipWildcards) {
$expression = $this->checkWhetherToUseContains($attribute, $filter, $attributeType) ?
'%' . $filter . '%' :
$filter . '%';
}
$expression = addslashes($expression);
$orGroupBuilder->add(
Cmp::like(
Expr::column($attribute),
$expression
)
);
}
private function getAttributeTypeAndApplyJoin(QueryBuilder $queryBuilder, string $attribute): string
{
if (str_contains($attribute, '.')) {
[$link, $foreignField] = explode('.', $attribute);
$foreignEntityType = $this->metadataProvider->getRelationEntityType($this->entityType, $link);
if (!$foreignEntityType) {
throw new RuntimeException("Bad relation in text filter field '$attribute'.");
}
if ($this->metadataProvider->getRelationType($this->entityType, $link) === Entity::HAS_MANY) {
$queryBuilder->distinct();
}
$queryBuilder->leftJoin($link);
return $this->metadataProvider->getAttributeType($foreignEntityType, $foreignField);
}
$attributeType = $this->metadataProvider->getAttributeType($this->entityType, $attribute);
if ($attributeType === Entity::FOREIGN) {
$link = $this->metadataProvider->getAttributeRelationParam($this->entityType, $attribute);
if ($link) {
$queryBuilder->leftJoin($link);
}
}
return $attributeType;
}
private function checkWhetherToUseContains(string $attribute, string $filter, string $attributeType): bool
{
if (mb_strlen($filter) < $this->config->getMinLengthForContentSearch()) {
return false;
}
if ($attributeType === Entity::TEXT) {
return true;
}
if (
in_array(
$attribute,
$this->metadataProvider->getUseContainsAttributeList($this->entityType)
)
) {
return true;
}
if (
$attributeType === Entity::VARCHAR &&
$this->config->useContainsForVarchar()
) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
use Espo\ORM\Query\SelectBuilder;
use Espo\Core\Select\Text\Filter\Data;
interface Filter
{
public function apply(SelectBuilder $queryBuilder, Data $data): void;
}

View File

@@ -0,0 +1,133 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\Filter;
use Espo\ORM\Query\Part\WhereItem;
/**
* Immutable.
*/
class Data
{
private string $filter;
/** @var string[] */
private array $attributeList;
private bool $skipWildcards = false;
private ?WhereItem $fullTextSearchWhereItem = null;
private bool $forceFullTextSearch = false;
/**
* @param string[] $attributeList
*/
public function __construct(string $filter, array $attributeList)
{
$this->filter = $filter;
$this->attributeList = $attributeList;
}
/**
* @param string[] $attributeList
*/
public static function create(string $filter, array $attributeList): self
{
return new self($filter, $attributeList);
}
public function withFilter(string $filter): self
{
$obj = clone $this;
$obj->filter = $filter;
return $obj;
}
/**
* @param string[] $attributeList
*/
public function withAttributeList(array $attributeList): self
{
$obj = clone $this;
$obj->attributeList = $attributeList;
return $obj;
}
public function withSkipWildcards(bool $skipWildcards = true): self
{
$obj = clone $this;
$obj->skipWildcards = $skipWildcards;
return $obj;
}
public function withForceFullTextSearch(bool $forceFullTextSearch = true): self
{
$obj = clone $this;
$obj->forceFullTextSearch = $forceFullTextSearch;
return $obj;
}
public function withFullTextSearchWhereItem(?WhereItem $fullTextSearchWhereItem): self
{
$obj = clone $this;
$obj->fullTextSearchWhereItem = $fullTextSearchWhereItem;
return $obj;
}
public function getFilter(): string
{
return $this->filter;
}
/**
* @return string[]
*/
public function getAttributeList(): array
{
return $this->attributeList;
}
public function skipWildcards(): bool
{
return $this->skipWildcards;
}
public function forceFullTextSearch(): bool
{
return $this->forceFullTextSearch;
}
public function getFullTextSearchWhereItem(): ?WhereItem
{
return $this->fullTextSearchWhereItem;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
use Espo\Core\Utils\Metadata;
use Espo\Core\InjectableFactory;
use Espo\Entities\User;
use Espo\Core\Binding\BindingContainerBuilder;
use Espo\Core\Binding\ContextualBinder;
class FilterFactory
{
public function __construct(
private Metadata $metadata,
private InjectableFactory $injectableFactory
) {}
public function create(string $entityType, User $user): Filter
{
/** @var class-string<Filter> $className */
$className = $this->metadata->get(['selectDefs', $entityType, 'textFilterClassName']) ??
DefaultFilter::class;
$bindingContainer = BindingContainerBuilder::create()
->bindInstance(User::class, $user)
->inContext(
$className,
function (ContextualBinder $binder) use ($entityType) {
$binder->bindValue('$entityType', $entityType);
}
)
->inContext(
DefaultFilter::class,
function (ContextualBinder $binder) use ($entityType) {
$binder->bindValue('$entityType', $entityType);
}
)
->build();
return $this->injectableFactory->createWithBinding($className, $bindingContainer);
}
}

View File

@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
/**
* Immutable.
*/
class FilterParams
{
private bool $noFullTextSearch = false;
private function __construct() {}
public static function create(): self
{
return new self();
}
public function withNoFullTextSearch(bool $noFullTextSearch = true): self
{
$obj = clone $this;
$obj->noFullTextSearch = $noFullTextSearch;
return $obj;
}
public function noFullTextSearch(): bool
{
return $this->noFullTextSearch;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch;
use Espo\ORM\Query\Part\Expression;
use InvalidArgumentException;
/**
* Immutable.
*/
class Data
{
/** @var string[] */
private array $fieldList;
/** @var string[] */
private array $columnList;
/** @var Mode::* $mode */
private string $mode;
/**
* @param string[] $fieldList
* @param string[] $columnList
* @param Mode::* $mode
*/
public function __construct(private Expression $expression, array $fieldList, array $columnList, string $mode)
{
$this->fieldList = $fieldList;
$this->columnList = $columnList;
$this->mode = $mode;
if (!in_array($mode, [Mode::NATURAL_LANGUAGE, Mode::BOOLEAN])) {
throw new InvalidArgumentException("Bad mode.");
}
}
public function getExpression(): Expression
{
return $this->expression;
}
/**
* @return string[]
*/
public function getFieldList(): array
{
return $this->fieldList;
}
/**
* @return string[]
*/
public function getColumnList(): array
{
return $this->columnList;
}
/**
* @return Mode::*
*/
public function getMode(): string
{
return $this->mode;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch;
use Espo\Core\Select\Text\FullTextSearch\DataComposer\Params;
interface DataComposer
{
public function compose(string $filter, Params $params): ?Data;
}

View File

@@ -0,0 +1,43 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch\DataComposer;
/**
* Immutable.
*/
class Params
{
private function __construct() {}
public static function create(): self
{
return new self();
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Metadata;
class DataComposerFactory
{
public function __construct(
private InjectableFactory $injectableFactory,
private Metadata $metadata
) {}
public function create(string $entityType): DataComposer
{
$className = $this->getClassName($entityType);
return $this->injectableFactory->createWith($className, [
'entityType' => $entityType,
]);
}
/**
* @return class-string<DataComposer>
*/
private function getClassName(string $entityType): string
{
return
$this->metadata->get([
'selectDefs', $entityType, 'fullTextSearchDataComposerClassName'
]) ??
DefaultDataComposer::class;
}
}

View File

@@ -0,0 +1,168 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch;
use Espo\Core\Utils\Config;
use Espo\Core\Select\Text\MetadataProvider;
use Espo\Core\Select\Text\FullTextSearch\DataComposer\Params;
use Espo\ORM\Query\Part\Expression\Util as ExpressionUtil;
use Espo\ORM\Query\Part\Expression;
class DefaultDataComposer implements DataComposer
{
/** @var array<Mode::*, string>*/
private array $functionMap = [
Mode::BOOLEAN => 'MATCH_BOOLEAN',
Mode::NATURAL_LANGUAGE => 'MATCH_NATURAL_LANGUAGE',
];
public function __construct(
private string $entityType,
private Config $config,
private MetadataProvider $metadataProvider
) {}
public function compose(string $filter, Params $params): ?Data
{
if ($this->config->get('fullTextSearchDisabled')) {
return null;
}
$columnList = $this->metadataProvider->getFullTextSearchColumnList($this->entityType) ?? [];
if (!count($columnList)) {
return null;
}
$fieldList = [];
foreach ($this->getTextFilterFieldList() as $field) {
if (str_contains($field, '.')) {
continue;
}
if ($this->metadataProvider->isFieldNotStorable($this->entityType, $field)) {
continue;
}
if (!$this->metadataProvider->isFullTextSearchSupportedForField($this->entityType, $field)) {
continue;
}
$fieldList[] = $field;
}
if (!count($fieldList)) {
return null;
}
$preparedFilter = $this->prepareFilter($filter, $params);
$mode = Mode::BOOLEAN;
if (
mb_strpos($preparedFilter, ' ') === false &&
mb_strpos($preparedFilter, '+') === false &&
mb_strpos($preparedFilter, ' -') === false &&
mb_strpos($preparedFilter, '-') !== 0 &&
mb_strpos($preparedFilter, '*') === false
) {
$mode = Mode::NATURAL_LANGUAGE;
}
if ($mode === Mode::BOOLEAN) {
$preparedFilter = str_replace('@', '*', $preparedFilter);
}
$argumentList = array_merge(
array_map(fn ($item) => Expression::column($item), $columnList),
[$preparedFilter]
);
$function = $this->functionMap[$mode];
$expression = ExpressionUtil::composeFunction($function, ...$argumentList);
return new Data(
$expression,
$fieldList,
$columnList,
$mode
);
}
private function prepareFilter(string $filter, Params $params): string
{
$filter = str_replace('%', '*', $filter);
$filter = str_replace(['(', ')'], '', $filter);
$filter = str_replace('"*', '"', $filter);
$filter = str_replace('*"', '"', $filter);
while (str_contains($filter, '**')) {
$filter = trim(
str_replace('**', '*', $filter)
);
}
while (mb_substr($filter, -2) === ' *') {
$filter = trim(
mb_substr($filter, 0, mb_strlen($filter) - 2)
);
}
$filter = str_replace(['+-', '--', '-+', '++', '+*', '-*'], '', $filter);
while (str_contains($filter, '+ ')) {
$filter = str_replace('+ ', '', $filter);
}
while (str_contains($filter, '- ')) {
$filter = str_replace('- ', '', $filter);
}
while (in_array(substr($filter, -1), ['-', '+'])) {
$filter = substr($filter, 0, -1);
}
if ($filter === '*') {
$filter = '';
}
return $filter;
}
/**
* @return string[]
*/
private function getTextFilterFieldList(): array
{
return $this->metadataProvider->getTextFilterAttributeList($this->entityType) ?? ['name'];
}
}

View File

@@ -0,0 +1,36 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch;
class Mode
{
public const NATURAL_LANGUAGE = 'NATURAL_LANGUAGE';
public const BOOLEAN = 'BOOLEAN';
}

View File

@@ -0,0 +1,156 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Defs\Params\FieldParam;
class MetadataProvider
{
private Defs $ormDefs;
public function __construct(private Metadata $metadata, Defs $ormDefs)
{
$this->ormDefs = $ormDefs;
}
public function getFullTextSearchOrderType(string $entityType): ?string
{
return $this->metadata->get([
'entityDefs', $entityType, 'collection', 'fullTextSearchOrderType'
]);
}
/**
* @return string[]|null
*/
public function getTextFilterAttributeList(string $entityType): ?array
{
return $this->metadata->get([
'entityDefs', $entityType, 'collection', 'textFilterFields'
]);
}
public function isFieldNotStorable(string $entityType, string $field): bool
{
return (bool) $this->metadata->get([
'entityDefs', $entityType, 'fields', $field, Defs\Params\FieldParam::NOT_STORABLE
]);
}
public function isFullTextSearchSupportedForField(string $entityType, string $field): bool
{
$fieldType = $this->metadata->get([
'entityDefs', $entityType, 'fields', $field, FieldParam::TYPE
]);
return (bool) $this->metadata->get([
'fields', $fieldType, 'fullTextSearch'
]);
}
public function hasFullTextSearch(string $entityType): bool
{
return (bool) $this->metadata->get([
'entityDefs', $entityType, 'collection', 'fullTextSearch'
]);
}
/**
* @return string[]
*/
public function getUseContainsAttributeList(string $entityType): array
{
return $this->metadata->get([
'selectDefs', $entityType, 'textFilterUseContainsAttributeList'
]) ?? [];
}
/**
* @return string[]|null
*/
public function getFullTextSearchColumnList(string $entityType): ?array
{
return $this->ormDefs
->getEntity($entityType)
->getParam('fullTextSearchColumnList');
}
public function getRelationType(string $entityType, string $link): string
{
return $this->ormDefs
->getEntity($entityType)
->getRelation($link)
->getType();
}
public function getAttributeType(string $entityType, string $attribute): string
{
return $this->ormDefs
->getEntity($entityType)
->getAttribute($attribute)
->getType();
}
public function getFieldType(string $entityType, string $field): ?string
{
$entityDefs = $this->ormDefs->getEntity($entityType);
if (!$entityDefs->hasField($field)) {
return null;
}
return $entityDefs->getField($field)->getType();
}
public function getRelationEntityType(string $entityType, string $link): ?string
{
$relationDefs = $this->ormDefs
->getEntity($entityType)
->getRelation($link);
if (!$relationDefs->hasForeignEntityType()) {
return null;
}
return $relationDefs->getForeignEntityType();
}
public function getAttributeRelationParam(string $entityType, string $attribute): ?string
{
return $this->ormDefs
->getEntity($entityType)
->getAttribute($attribute)
->getParam(AttributeParam::RELATION);
}
}