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]);