diff --git a/application/Espo/Controllers/EmailAddress.php b/application/Espo/Controllers/EmailAddress.php index 4d4386f52b..927253b270 100644 --- a/application/Espo/Controllers/EmailAddress.php +++ b/application/Espo/Controllers/EmailAddress.php @@ -29,52 +29,7 @@ namespace Espo\Controllers; -use Espo\Core\Exceptions\Forbidden; -use Espo\Core\Exceptions\BadRequest; - -use Espo\Tools\Email\AddressService as Service; - -use Espo\Core\Api\Request; use Espo\Core\Controllers\RecordBase; class EmailAddress extends RecordBase -{ - private const ADDRESS_MAX_SIZE = 50; - - /** - * @return array> - * @throws Forbidden - * @throws BadRequest - */ - public function actionSearchInAddressBook(Request $request): array - { - if (!$this->acl->checkScope('Email')) { - throw new Forbidden(); - } - - if (!$this->acl->checkScope('Email', 'create')) { - throw new Forbidden(); - } - - $q = $request->getQueryParam('q'); - - if ($q === null) { - throw new BadRequest("No `q` parameter."); - } - - $maxSize = intval($request->getQueryParam('maxSize')); - - if (!$maxSize || $maxSize > self::ADDRESS_MAX_SIZE) { - $maxSize = (int) $this->config->get('recordsPerPage'); - } - - $onlyActual = $request->getQueryParam('onlyActual') === 'true'; - - return $this->getEmailAddressService()->searchInAddressBook($q, $maxSize, $onlyActual); - } - - private function getEmailAddressService(): Service - { - return $this->injectableFactory->create(Service::class); - } -} +{} diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index fd851bd5d4..a1f762cb8a 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -82,7 +82,7 @@ } }, { - "route": "/GlobalSearch/:query", + "route": "/GlobalSearch", "method": "get", "actionClassName": "Espo\\Tools\\GlobalSearch\\Api\\Get" }, @@ -357,6 +357,16 @@ "method": "get", "actionClassName": "Espo\\Tools\\Email\\Api\\GetInsertFieldData" }, + { + "route": "/EmailAddress/search", + "method": "get", + "actionClassName": "Espo\\Tools\\EmailAddress\\Api\\GetSearch" + }, + { + "route": "/Email/insertFieldData", + "method": "get", + "actionClassName": "Espo\\Tools\\Email\\Api\\GetInsertFieldData" + }, { "route": "/Oidc/authorizationData", "method": "get", diff --git a/application/Espo/Tools/EmailAddress/Api/GetSearch.php b/application/Espo/Tools/EmailAddress/Api/GetSearch.php new file mode 100644 index 0000000000..d47949731f --- /dev/null +++ b/application/Espo/Tools/EmailAddress/Api/GetSearch.php @@ -0,0 +1,88 @@ +acl->checkScope(Email::ENTITY_TYPE)) { + throw new Forbidden(); + } + + if (!$this->acl->checkScope(Email::ENTITY_TYPE, Acl\Table::ACTION_CREATE)) { + throw new Forbidden(); + } + + $q = $request->getQueryParam('q'); + + if (is_string($q)) { + $q = trim($q); + } + + if (!$q) { + throw new BadRequest("No `q` parameter."); + } + + $maxSize = intval($request->getQueryParam('maxSize')); + + if (!$maxSize || $maxSize > self::ADDRESS_MAX_SIZE) { + $maxSize = (int) $this->config->get('recordsPerPage'); + } + + $onlyActual = $request->getQueryParam('onlyActual') === 'true'; + + $result = $this->service->searchInAddressBook($q, $maxSize, $onlyActual); + + return ResponseComposer::json($result); + } +} diff --git a/application/Espo/Tools/GlobalSearch/Api/Get.php b/application/Espo/Tools/GlobalSearch/Api/Get.php index 7f728feacb..574c2e062c 100644 --- a/application/Espo/Tools/GlobalSearch/Api/Get.php +++ b/application/Espo/Tools/GlobalSearch/Api/Get.php @@ -46,10 +46,10 @@ class Get implements Action public function process(Request $request): Response { - $query = $request->getRouteParam('query'); + $query = $request->getQueryParam('q'); if ($query === null || $query === '') { - throw new BadRequest(); + throw new BadRequest("No `q` parameter."); } $offset = intval($request->getQueryParam('offset')); diff --git a/client/src/views/email/fields/email-address-varchar.js b/client/src/views/email/fields/email-address-varchar.js index 14799eef9b..d1131d045c 100644 --- a/client/src/views/email/fields/email-address-varchar.js +++ b/client/src/views/email/fields/email-address-varchar.js @@ -206,9 +206,10 @@ function (Dep, From, EmailAddress) { }); this.$input.autocomplete({ - serviceUrl: (q) => { - return 'EmailAddress/action/searchInAddressBook?onlyActual=true&maxSize=' + - this.getAutocompleteMaxCount(); + serviceUrl: () => { + return `EmailAddress/search` + + `?maxSize=${this.getAutocompleteMaxCount()}` + + `&onlyActual=true`; }, paramName: 'q', minChars: 1, diff --git a/client/src/views/email/fields/email-address.js b/client/src/views/email/fields/email-address.js index 28e576dba4..835ac829d2 100644 --- a/client/src/views/email/fields/email-address.js +++ b/client/src/views/email/fields/email-address.js @@ -59,8 +59,8 @@ define('views/email/fields/email-address', ['views/fields/base'], function (Dep) this.$input.autocomplete({ serviceUrl: () => { - return 'EmailAddress/action/searchInAddressBook?maxSize=' + - this.getAutocompleteMaxCount(); + return `EmailAddress/search` + + `?maxSize=${this.getAutocompleteMaxCount()}` }, paramName: 'q', minChars: 1, diff --git a/client/src/views/fields/email.js b/client/src/views/fields/email.js index 1c8699d56d..74276a75cb 100644 --- a/client/src/views/fields/email.js +++ b/client/src/views/fields/email.js @@ -341,9 +341,9 @@ define('views/fields/email', ['views/fields/varchar'], function (Dep) { if (this.mode === this.MODE_SEARCH && this.getAcl().check('Email', 'create')) { this.$element.autocomplete({ - serviceUrl: (q) => { - return 'EmailAddress/action/searchInAddressBook?maxSize=' + - this.getAutocompleteMaxCount(); + serviceUrl: () => { + return `EmailAddress/search` + + `?maxSize=${this.getAutocompleteMaxCount()}` }, paramName: 'q', minChars: 1, diff --git a/client/src/views/global-search/global-search.js b/client/src/views/global-search/global-search.js index fd9f10adaf..eff243812e 100644 --- a/client/src/views/global-search/global-search.js +++ b/client/src/views/global-search/global-search.js @@ -78,7 +78,7 @@ define('views/global-search/global-search', ['view'], function (Dep) { }, search: function (text) { - this.collection.url = this.collection.urlRoot = 'GlobalSearch/' + encodeURIComponent(text); + this.collection.url = this.collection.urlRoot = 'GlobalSearch?q=' + encodeURIComponent(text); this.showPanel(); },