38438-vm/api/search.php
2026-02-15 15:19:05 +00:00

50 lines
1.4 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$q = $_GET['q'] ?? '';
if (strlen($q) < 2) {
echo json_encode([]);
exit;
}
$results = [];
try {
$db = db();
// Search Projects
$stmt = $db->prepare("SELECT id, name, code, is_archived FROM projects WHERE name LIKE ? OR code LIKE ? LIMIT 5");
$stmt->execute(['%' . $q . '%', '%' . $q . '%']);
foreach ($stmt->fetchAll() as $row) {
$label = $row['name'] . ' (' . $row['code'] . ')';
if ($row['is_archived']) {
$label .= ' [ARCHIVED]';
}
$results[] = [
'type' => 'Project',
'id' => $row['id'],
'label' => $label,
'url' => 'project_detail.php?id=' . $row['id']
];
}
// Search Employees
$stmt = $db->prepare("SELECT id, name, position FROM employees WHERE name LIKE ? OR first_name LIKE ? OR last_name LIKE ? LIMIT 5");
$stmt->execute(['%' . $q . '%', '%' . $q . '%', '%' . $q . '%']);
foreach ($stmt->fetchAll() as $row) {
$results[] = [
'type' => 'Employee',
'id' => $row['id'],
'label' => $row['name'] . ' - ' . ($row['position'] ?? 'Staff'),
'url' => 'employee_detail.php?id=' . $row['id']
];
}
} catch (Exception $e) {
// Silence error for now
}
echo json_encode($results);