38438-vm/api/search.php
2026-02-15 02:14:10 +00:00

46 lines
1.2 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 FROM projects WHERE name LIKE ? OR code LIKE ? LIMIT 5");
$stmt->execute(['%' . $q . '%', '%' . $q . '%']);
foreach ($stmt->fetchAll() as $row) {
$results[] = [
'type' => 'Project',
'id' => $row['id'],
'label' => $row['name'] . ' (' . $row['code'] . ')',
'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);