'Unauthorized']); exit; } $fieldKey = trim($_GET['field_key'] ?? ''); $templateId = intval($_GET['template_id'] ?? 0); $q = mb_strtolower(trim($_GET['q'] ?? '')); $id = isset($_GET['id']) ? intval($_GET['id']) : null; $rawLimit = intval($_GET['limit'] ?? 30); $limit = $rawLimit <= 0 ? 0 : max(1, min(500, $rawLimit)); if ($fieldKey === '' || !binding_fixed_is_list($fieldKey)) { echo json_encode(['results' => []]); exit; } try { $pdo = DBHandlerSelect::getInstance()->getConnection(); $values = binding_get_fixed_values($pdo, $fieldKey, $templateId); if ($id !== null) { foreach ($values as $v) { if ((int) $v['id'] === $id) { echo json_encode(['results' => [['id' => $v['id'], 'text' => $v['text']]]]); exit; } } echo json_encode(['results' => []]); exit; } $results = []; foreach ($values as $v) { $text = (string) $v['text']; if ($q === '' || mb_strpos(mb_strtolower($text), $q) !== false) { $results[] = ['id' => $v['id'], 'text' => $text]; if ($limit > 0 && count($results) >= $limit) { break; } } } usort($results, fn($a, $b) => strcasecmp($a['text'], $b['text'])); echo json_encode(['results' => $results]); } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); }