From e3f7637b641f54631b581107b30f4e05709777ac Mon Sep 17 00:00:00 2001 From: solocla Date: Fri, 10 Jul 2026 09:10:06 +0200 Subject: [PATCH] changed search matrici strategy --- public/userarea/search_matrici.php | 32 ++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/public/userarea/search_matrici.php b/public/userarea/search_matrici.php index eb75d918..50833800 100644 --- a/public/userarea/search_matrici.php +++ b/public/userarea/search_matrici.php @@ -2,7 +2,11 @@ require_once dirname(__DIR__, 2) . '/vendor/autoload.php'; require_once __DIR__ . '/class/db-functions.php'; include dirname(__DIR__) . '/../extra/auth.php'; -if (!Auth::check()) { http_response_code(401); echo json_encode(['error' => 'Unauthorized']); exit; } +if (!Auth::check()) { + http_response_code(401); + echo json_encode(['error' => 'Unauthorized']); + exit; +} header('Content-Type: application/json'); ini_set('display_errors', '0'); @@ -59,15 +63,35 @@ if (isset($_GET['macro_list'])) { exit; } -// Search (with optional MacroMatrice filter) +// Search (with optional MacroMatrice filter) - multi-term AND, position-independent $results = []; + +// Spezza la query in termini separati (gestisce spazi multipli, rimuove vuoti) +$terms = array_values(array_filter( + preg_split('/\s+/u', $q, -1, PREG_SPLIT_NO_EMPTY) +)); + foreach ($matrici as $m) { $nome = $m['NomeMatrice'] ?? ''; if ($macro !== '' && ($m['MacroMatrice'] ?? '') !== $macro) continue; - if ($q === '' || mb_strpos(mb_strtolower($nome), $q) !== false) { + + if (empty($terms)) { + // Nessun termine digitato: mostra tutto fino al limite + $results[] = ['id' => $m['IdMatrice'], 'text' => $nome]; + } else { + $nomeLower = mb_strtolower($nome); + $matchAll = true; + foreach ($terms as $t) { + if (mb_strpos($nomeLower, $t) === false) { + $matchAll = false; + break; + } + } + if (!$matchAll) continue; $results[] = ['id' => $m['IdMatrice'], 'text' => $nome]; - if (count($results) >= $limit) break; } + + if (count($results) >= $limit) break; } echo json_encode(['results' => $results]);