diff --git a/web/modules/custom/materio_sapi/src/Controller/Base.php b/web/modules/custom/materio_sapi/src/Controller/Base.php index a5df220c..89e547dc 100644 --- a/web/modules/custom/materio_sapi/src/Controller/Base.php +++ b/web/modules/custom/materio_sapi/src/Controller/Base.php @@ -6,7 +6,6 @@ namespace Drupal\materio_sapi\Controller; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; -use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\Unicode; use Drupal\search_api\Entity\Index; @@ -62,6 +61,20 @@ class Base extends ControllerBase { private $limit = 15; private $offset = 0; + /** + * Longest phrase kept, in characters (H1, 150 decided on 17/11). The longest + * test phrase (S5) has 89 characters; beyond the limit the phrase is cut, + * not refused. + */ + public const MAX_PHRASE_LENGTH = 150; + + /** + * The phrase typed by the user, whole and case kept, for the model (H5). + * + * @var string + */ + protected $phrase = ''; + private function sapiQuery(){ // https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code // https://www.hashbangcode.com/article/drupal-8-date-search-boosting-search-api-and-solr-search @@ -379,22 +392,19 @@ class Base extends ControllerBase { * get params from request */ private function parseRequest(Request $request){ - // Get the typed string from the URL, if it exists. - $this->keys = $request->query->get('keys'); - if($this->keys){ - $this->keys = mb_strtolower($this->keys); - $this->keys = Tags::explode($this->keys); - // \Drupal::logger('materio_sapi')->notice($this->keys); - } + // One phrase, commas included: no more splitting into tags (H1). + // keys stays an array of one lowercased element for the keyword queries. + $this->phrase = self::cleanPhrase($request->query->get('keys')); + $this->keys = $this->phrase !== '' ? [mb_strtolower($this->phrase)] : NULL; // get the exacte term id in case of autocomplete // $this->terms = $request->query->get('terms'); $t = $request->query->get('terms'); // $this->terms = strlen($t) ? explode(',', $t) : null; $this->terms = $t && strlen($t) ? json_decode($t) : null; - // get the filters of advanced search - $f = $request->query->get('filters'); - $this->filters = $f && strlen($f) ? explode(',', $f) : null; + // Filters of the assisted search are gone (H1, lot F): the parameter of + // old URLs is ignored. + $this->filters = NULL; // $this->allparams = $request->query->all(); // $request->attributes->get('_raw_variables')->get('filters') // @@ -402,6 +412,17 @@ class Base extends ControllerBase { $this->limit = $request->query->get('limit') ?? $this->limit; } + /** + * Normalizes the typed phrase: spaces collapsed, length limited. + */ + public static function cleanPhrase($keys): string { + if (!is_string($keys)) { + return ''; + } + $phrase = trim(preg_replace('/[\s\p{Cc}]+/u', ' ', $keys) ?? ''); + return mb_substr($phrase, 0, self::MAX_PHRASE_LENGTH); + } + /** * Handler for ajax search. */ @@ -415,14 +436,13 @@ class Base extends ControllerBase { ), ]; - if ($this->keys || $this->filters) { + if ($this->keys) { $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); $this->sapiQuery(); $resp['keys'] = json_encode($this->keys); $resp['terms'] = json_encode($this->terms); - $resp['filters'] = $this->filters; // $resp['count'] = $this->results->getResultCount(); $resp['count'] = count($this->results['nids']); $resp['exactematch_count'] = $this->exactematch_count; @@ -430,28 +450,9 @@ class Base extends ControllerBase { "@exactmatchcount" => $resp['exactematch_count'], "@count" => $resp['count'] )); - if ($this->keys) { - $resp['infos'] .= t(' keywords @keys', array( - "@keys" => implode(', ', $this->keys) - )); - } - if ($this->keys && $this->filters) { - $resp['infos'] .= " and"; - } - if ($this->filters) { - // get the terms names from tid - $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); - $filters_names = []; - foreach ($this->filters as $tid) { - /** @var \Drupal\Core\Entity\EntityInterface $entity */ - $entity = $storage->load($tid); - $entity_trans = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $lang); - $filters_names[] = $entity_trans->getName(); - } - $resp['infos'] .= t(' filters @filters', array( - "@filters" => implode(', ', $filters_names) - )); - } + $resp['infos'] .= t(' keywords @keys', array( + "@keys" => $this->phrase + )); // $resp['options'] = $this->query->getOptions(); // $uuids = []; @@ -508,7 +509,7 @@ class Base extends ControllerBase { ]; if ($this->keys) { - $resp['#title'] = implode(', ', $this->keys); + $resp['#title'] = $this->phrase; // $this->sapiQuery();