prepare("
INSERT IGNORE INTO consultant_clients (consultant_id, client_id, assigned_by)
VALUES (?, ?, ?)
")->execute([$consultantId, $clientId, getCurrentUserId()]);
$flash = 'Client assigned successfully.';
} catch (PDOException $e) {
$flash = 'Assignment failed: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
$flashType = 'danger';
}
} else {
$flash = 'Please select both a consultant and a client.';
$flashType = 'warning';
}
} elseif ($action === 'remove') {
$assignmentId = (int) ($_POST['assignment_id'] ?? 0);
if ($assignmentId) {
$pdo->prepare("DELETE FROM consultant_clients WHERE id = ?")->execute([$assignmentId]);
$flash = 'Assignment removed.';
}
} elseif ($action === 'set_type') {
$targetUserId = (int) ($_POST['user_id'] ?? 0);
$userType = $_POST['user_type'] ?? '';
if ($targetUserId && in_array($userType, ['client', 'consultant', 'admin'], true)) {
$pdo->prepare("UPDATE users SET user_type = ? WHERE id = ?")
->execute([$userType, $targetUserId]);
$flash = 'User type updated.';
} else {
$flash = 'Invalid user type selection.';
$flashType = 'warning';
}
}
}
}
// ── Load data ─────────────────────────────────────────────────────────────────
// All users with their type
$allUsers = $pdo->query("
SELECT id, fullname, email, user_type
FROM users
WHERE active = 1
ORDER BY user_type, fullname
")->fetchAll();
// Consultants with their assigned clients
$consultants = $pdo->query("
SELECT u.id, u.fullname, u.email
FROM users u
WHERE u.user_type IN ('consultant','admin') AND u.active = 1
ORDER BY u.fullname
")->fetchAll();
// All client records (for the assign dropdown)
$allClients = $pdo->query("
SELECT id, client, company
FROM client_records
ORDER BY client ASC
")->fetchAll();
// Assignments: consultant_id → [{assignment_id, client_id, client_name, company, assigned_at}]
$assignmentRows = $pdo->query("
SELECT cc.id AS assignment_id, cc.consultant_id, cc.assigned_at,
cr.id AS client_id, cr.client, cr.company
FROM consultant_clients cc
JOIN client_records cr ON cr.id = cc.client_id
ORDER BY cc.consultant_id, cr.client
")->fetchAll();
$assignmentsByConsultant = [];
foreach ($assignmentRows as $row) {
$assignmentsByConsultant[$row['consultant_id']][] = $row;
}
// ── Page render ───────────────────────────────────────────────────────────────
$pageTitle = 'Manage Consultants';
$siteName = 'Crop Monitor';
include __DIR__ . '/../../layouts/header.php';
include __DIR__ . '/../../layouts/navbar.php';
?>
= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?>
- Consultant
- Manage Consultants
= htmlspecialchars($flash, ENT_QUOTES, 'UTF-8') ?>
| Name |
Email |
Role |
|
|
= htmlspecialchars($u['fullname'], ENT_QUOTES, 'UTF-8') ?>
|
= htmlspecialchars($u['email'], ENT_QUOTES, 'UTF-8') ?>
|
'danger',
'consultant' => 'info',
default => 'secondary',
};
?>
= htmlspecialchars(ucfirst($u['user_type']), ENT_QUOTES, 'UTF-8') ?>
|
|
No consultant accounts yet. Set a user's role to "Consultant" using the panel on the left.
No clients assigned yet.
-
= htmlspecialchars($a['client'], ENT_QUOTES, 'UTF-8') ?>
— = htmlspecialchars($a['company'], ENT_QUOTES, 'UTF-8') ?>
Assigned = date('j M Y', strtotime($a['assigned_at'])) ?>