changed search matrici strategy

This commit is contained in:
2026-07-10 09:10:06 +02:00
parent f4ea06fb93
commit e3f7637b64
+28 -4
View File
@@ -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]);