/home/techb158/workloadmatch.com/api/routes
Edit: /home/techb158/workloadmatch.com/api/routes/managers.php (9612B)
prepare($sql);
if ($params) $stmt->bind_param($types, ...$params);
$stmt->execute();
$stmt->bind_result($total);
$stmt->fetch();
$stmt->close();
$sql = "SELECT m.* FROM manager_profile m $whereClause ORDER BY $sort $dir LIMIT ? OFFSET ?";
$stmt = $mysqli->prepare($sql);
$bindParams = array_merge($params, [$perPage, $offset]);
$bindTypes = $types . 'ii';
if ($bindParams) $stmt->bind_param($bindTypes, ...$bindParams);
$stmt->execute();
$result = $stmt->get_result();
$items = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
foreach ($items as &$item) {
unset($item['Password'], $item['salt']);
// Get teacher count for each manager
$s = $mysqli->prepare("SELECT COUNT(*) FROM teacher_profile WHERE Manager_ID = ?");
$s->bind_param('i', $item['Manager_ID']);
$s->execute();
$s->bind_result($item['teacher_count']);
$s->fetch();
$s->close();
}
Response::paginated($items, $total, $page, $perPage);
}
function getManager(string $id, mysqli $mysqli): void
{
Auth::requireLogin();
$stmt = $mysqli->prepare("SELECT * FROM manager_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
if (!$item) Response::notFound('Manager not found');
unset($item['Password'], $item['salt']);
// Get teacher count
$s = $mysqli->prepare("SELECT COUNT(*) FROM teacher_profile WHERE Manager_ID = ?");
$s->bind_param('i', $id);
$s->execute();
$s->bind_result($item['teacher_count']);
$s->fetch();
$s->close();
// Get group count
$s = $mysqli->prepare("SELECT COUNT(*) FROM manager_group_name WHERE Manager_ID = ?");
$s->bind_param('i', $id);
$s->execute();
$s->bind_result($item['group_count']);
$s->fetch();
$s->close();
Response::success($item);
}
function createManager(?array $body, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
$firstName = trim($body['First_Name'] ?? '');
$lastName = trim($body['Last_Name'] ?? '');
$email = trim($body['Email'] ?? '');
$username = trim($body['username'] ?? $body['User_Name'] ?? '');
$password = $body['password'] ?? $body['p'] ?? '';
if (!$firstName || !$lastName || !$email || !$password) {
Response::validationError(['First_Name', 'Last_Name', 'Email', 'password' => 'Required']);
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
Response::validationError(['Email' => 'Invalid email format']);
}
$passwordHash = password_hash($password, PASSWORD_BCRYPT);
// Check uniqueness
foreach (['manager_profile' => 'User_Name', 'manager_profile' => 'Email'] as $table => $col) {
$stmt = $mysqli->prepare("SELECT 1 FROM $table WHERE $col = ? LIMIT 1");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) { $stmt->close(); Response::error("$col already exists", 409); }
$stmt->close();
}
$userId = Auth::getUserId();
$userType = Auth::getUserType();
if ($userType === 'admin_profile') {
$masterId = $body['Master_ID'] ?? 0;
$adminId = $userId;
} else {
$q = $mysqli->prepare("SELECT Admin_ID, Master_ID FROM master_profile WHERE Master_ID = ?");
$q->bind_param('s', $userId);
$q->execute();
$r = $q->get_result()->fetch_assoc();
$q->close();
$adminId = $r['Admin_ID'];
$masterId = $r['Master_ID'];
}
$dateTime = date('Y/m/d H:i:s');
$stmt = $mysqli->prepare("INSERT INTO manager_profile (Admin_ID, Master_ID, First_Name, Last_Name, Email, User_Name, Password, Reg_Date, Login_Date, User_Access) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
$stmt->bind_param('sssssssss', $adminId, $masterId, $firstName, $lastName, $email, $username, $passwordHash, $dateTime, $dateTime);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
log_activity($mysqli, 'create', 'manager', $newId, "$firstName $lastName", 'Manager created via API');
$stmt = $mysqli->prepare("SELECT * FROM manager_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $newId);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
unset($item['Password'], $item['salt']);
Response::created($item);
}
function updateManager(?string $id, ?array $body, mysqli $mysqli): void
{
Auth::requireAnyRole(['admin_profile', 'master_profile']);
if (!$id) Response::error('Manager ID required');
$stmt = $mysqli->prepare("SELECT * FROM manager_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$existing = $result->fetch_assoc();
$stmt->close();
if (!$existing) Response::notFound('Manager not found');
$firstName = trim($body['First_Name'] ?? $existing['First_Name']);
$lastName = trim($body['Last_Name'] ?? $existing['Last_Name']);
$email = trim($body['Email'] ?? $existing['Email']);
$userAccess = (int)($body['User_Access'] ?? $existing['User_Access']);
$stmt = $mysqli->prepare("UPDATE manager_profile SET First_Name = ?, Last_Name = ?, Email = ?, User_Access = ? WHERE Manager_ID = ?");
$stmt->bind_param('sssii', $firstName, $lastName, $email, $userAccess, $id);
$stmt->execute();
$stmt->close();
// Update password if provided
if (!empty($body['password'])) {
$newHash = password_hash($body['password'], PASSWORD_BCRYPT);
$stmt = $mysqli->prepare("UPDATE manager_profile SET Password = ?, salt = '' WHERE Manager_ID = ?");
$stmt->bind_param('si', $newHash, $id);
$stmt->execute();
$stmt->close();
}
log_activity($mysqli, 'update', 'manager', $id, "$firstName $lastName", 'Manager updated via API');
$stmt = $mysqli->prepare("SELECT * FROM manager_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$item = $result->fetch_assoc();
$stmt->close();
unset($item['Password'], $item['salt']);
Response::success($item, 'Manager updated');
}
function deleteManager(?string $id, mysqli $mysqli): void
{
Auth::requireRole('admin_profile');
if (!$id) Response::error('Manager ID required');
$stmt = $mysqli->prepare("SELECT First_Name, Last_Name FROM manager_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($fn, $ln);
$stmt->fetch();
$stmt->close();
if (!$fn) Response::notFound('Manager not found');
// Check for teachers
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM teacher_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($tCount);
$stmt->fetch();
$stmt->close();
if ($tCount > 0) {
Response::error("Cannot delete manager: $tCount teacher(s) are assigned", 409);
}
// Check for groups
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM manager_group_name WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($gCount);
$stmt->fetch();
$stmt->close();
if ($gCount > 0) {
Response::error("Cannot delete manager: $gCount group(s) are assigned", 409);
}
$stmt = $mysqli->prepare("DELETE FROM manager_profile WHERE Manager_ID = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
log_activity($mysqli, 'delete', 'manager', $id, "$fn $ln", 'Manager deleted via API');
Response::success(null, 'Manager deleted');
}