57 lines
2.0 KiB
PHP
57 lines
2.0 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
ini_set('display_errors', '0');
|
|
|
|
$q = mb_strtolower(trim($_GET['q'] ?? ''));
|
|
$id = isset($_GET['id']) ? intval($_GET['id']) : null;
|
|
$limit = max(1, min(50, intval($_GET['limit'] ?? 20)));
|
|
$macro = trim($_GET['macro'] ?? '');
|
|
|
|
$cacheFile = __DIR__ . '/cache/matrici.json';
|
|
|
|
if (!file_exists($cacheFile)) {
|
|
// Trigger cache creation
|
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
|
$api = VisualLimsApiClient::getInstance();
|
|
$data = $api->get('Matrice');
|
|
$matrici = [];
|
|
foreach (($data['value'] ?? []) as $item) {
|
|
$nome = $item['NomeMatrice'] ?? '';
|
|
if ($nome !== '' && substr($nome, 0, 1) !== '*') {
|
|
$matrici[] = ['IdMatrice' => $item['IdMatrice'], 'NomeMatrice' => $nome, 'MacroMatrice' => $item['MacroMatrice'] ?? null];
|
|
}
|
|
}
|
|
usort($matrici, fn($a, $b) => strcasecmp($a['NomeMatrice'], $b['NomeMatrice']));
|
|
if (!is_dir(__DIR__ . '/cache')) mkdir(__DIR__ . '/cache', 0777, true);
|
|
file_put_contents($cacheFile, json_encode(['success' => true, 'value' => $matrici]));
|
|
} else {
|
|
$cached = json_decode(file_get_contents($cacheFile), true);
|
|
$matrici = $cached['value'] ?? [];
|
|
}
|
|
|
|
// Lookup by ID
|
|
if ($id !== null) {
|
|
foreach ($matrici as $m) {
|
|
if ((int)$m['IdMatrice'] === $id) {
|
|
echo json_encode(['results' => [['id' => $m['IdMatrice'], 'text' => $m['NomeMatrice']]]]);
|
|
exit;
|
|
}
|
|
}
|
|
echo json_encode(['results' => []]);
|
|
exit;
|
|
}
|
|
|
|
// Search (with optional MacroMatrice filter)
|
|
$results = [];
|
|
foreach ($matrici as $m) {
|
|
$nome = $m['NomeMatrice'] ?? '';
|
|
if ($macro !== '' && ($m['MacroMatrice'] ?? '') !== $macro) continue;
|
|
if ($q === '' || mb_strpos(mb_strtolower($nome), $q) !== false) {
|
|
$results[] = ['id' => $m['IdMatrice'], 'text' => $nome];
|
|
if (count($results) >= $limit) break;
|
|
}
|
|
}
|
|
|
|
echo json_encode(['results' => $results]);
|