Files
trf_certest/public/userarea/search_fixed_field_values.php
T
2026-09-23 15:50:03 +03:00

55 lines
1.6 KiB
PHP

<?php
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once __DIR__ . '/class/db-functions.php';
require_once __DIR__ . '/class/binding-functions.php';
include dirname(__DIR__) . '/../extra/auth.php';
header('Content-Type: application/json');
ini_set('display_errors', '0');
error_reporting(E_ALL);
if (!Auth::check()) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
exit;
}
$fieldKey = trim($_GET['field_key'] ?? '');
$templateId = (int) ($_GET['template_id'] ?? 0);
$q = binding_normalize_value($_GET['q'] ?? '');
$rawLimit = (int) ($_GET['limit'] ?? 50);
$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 ($q !== '') {
$terms = array_values(array_filter(preg_split('/\s+/u', $q, -1, PREG_SPLIT_NO_EMPTY)));
$values = array_values(array_filter($values, function ($v) use ($terms) {
$hay = binding_normalize_value($v['text'] ?? '');
foreach ($terms as $t) {
if (mb_strpos($hay, $t) === false) {
return false;
}
}
return true;
}));
}
if ($limit > 0) {
$values = array_slice($values, 0, $limit);
}
echo json_encode(['results' => $values]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage(), 'results' => []]);
}