changed search matrici strategy
This commit is contained in:
@@ -2,7 +2,11 @@
|
|||||||
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
||||||
require_once __DIR__ . '/class/db-functions.php';
|
require_once __DIR__ . '/class/db-functions.php';
|
||||||
include dirname(__DIR__) . '/../extra/auth.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');
|
header('Content-Type: application/json');
|
||||||
ini_set('display_errors', '0');
|
ini_set('display_errors', '0');
|
||||||
@@ -59,15 +63,35 @@ if (isset($_GET['macro_list'])) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search (with optional MacroMatrice filter)
|
// Search (with optional MacroMatrice filter) - multi-term AND, position-independent
|
||||||
$results = [];
|
$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) {
|
foreach ($matrici as $m) {
|
||||||
$nome = $m['NomeMatrice'] ?? '';
|
$nome = $m['NomeMatrice'] ?? '';
|
||||||
if ($macro !== '' && ($m['MacroMatrice'] ?? '') !== $macro) continue;
|
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];
|
$results[] = ['id' => $m['IdMatrice'], 'text' => $nome];
|
||||||
if (count($results) >= $limit) break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (count($results) >= $limit) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode(['results' => $results]);
|
echo json_encode(['results' => $results]);
|
||||||
|
|||||||
Reference in New Issue
Block a user