flexday/user.php

116 lines
4.2 KiB
PHP

<?php
// user.php - edit a single user (conf_users) and their department mapping (conf_user_departments)
include_once("peter_db.php");
$peter_db = new peter_db();
$c = $peter_db->getConnection();
mysqli_set_charset($c, 'utf8');
$user_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$message = '';
// Fetch department options
$dept_opts = [];
$dept_q = "SELECT id, parent, name FROM gavi_departments ORDER BY name";
$dept_r = mysqli_query($c, $dept_q);
while ($row = mysqli_fetch_assoc($dept_r)) { $dept_opts[] = $row; }
// Helper: fetch user record
function fetch_user($c, $uid) {
$sql = "SELECT id, goo, email, name FROM conf_users WHERE id = ?";
$stmt = mysqli_prepare($c, $sql);
mysqli_stmt_bind_param($stmt, "i", $uid);
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt);
return mysqli_fetch_assoc($res);
}
// Helper: fetch mapping
function fetch_dept($c, $uid) {
$sql = "SELECT department_id FROM conf_user_departments WHERE user_id = ?";
$stmt = mysqli_prepare($c, $sql);
mysqli_stmt_bind_param($stmt, "i", $uid);
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($res);
return $row ? intval($row['department_id']) : null;
}
// Process save
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $user_id > 0) {
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$goo = isset($_POST['goo']) ? trim($_POST['goo']) : '';
$dept = isset($_POST['department_id']) ? intval($_POST['department_id']) : 0;
// Update conf_users
$upd = mysqli_prepare($c, "UPDATE conf_users SET name = ?, email = ?, goo = ? WHERE id = ?");
mysqli_stmt_bind_param($upd, "sssi", $name, $email, $goo, $user_id);
mysqli_stmt_execute($upd);
// Upsert department mapping
if ($dept > 0) {
$ins = mysqli_prepare($c, "INSERT INTO conf_user_departments (user_id, department_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE department_id = VALUES(department_id)");
mysqli_stmt_bind_param($ins, "ii", $user_id, $dept);
mysqli_stmt_execute($ins);
} else {
$del = mysqli_prepare($c, "DELETE FROM conf_user_departments WHERE user_id = ?");
mysqli_stmt_bind_param($del, "i", $user_id);
mysqli_stmt_execute($del);
}
$message = "Saved changes.";
}
$user = $user_id ? fetch_user($c, $user_id) : null;
$user_dept = $user_id ? fetch_dept($c, $user_id) : null;
$MY_TITLE = "Edit User";
$MY_CRUMB = "Edit User";
if (!$user) {
$CONTENT = "<p>No user found. Provide ?id=USER_ID in the query string.</p>";
include 'layout.php';
exit();
}
ob_start();
?>
<?php if ($message) { ?>
<div class="mb-4 p-3 bg-green-100 text-green-800 rounded border border-green-200"><?= htmlspecialchars($message) ?></div>
<?php } ?>
<form method="post" class="space-y-4 bg-white p-4 rounded shadow">
<div>
<label class="block text-sm font-medium text-gray-700">Name</label>
<input name="name" value="<?= htmlspecialchars($user['name']) ?>" class="w-full border rounded px-3 py-2" />
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Email</label>
<input name="email" value="<?= htmlspecialchars($user['email']) ?>" class="w-full border rounded px-3 py-2" />
</div>
<div>
<label class="block text-sm font-medium text-gray-700">GOO</label>
<input name="goo" value="<?= htmlspecialchars($user['goo'] ?? '') ?>" class="w-full border rounded px-3 py-2" />
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Department</label>
<select name="department_id" class="w-full border rounded px-3 py-2">
<option value="0">-- None --</option>
<?php foreach ($dept_opts as $d) {
$dept_name = trim($d['name'] ?? '');
if ($dept_name === '') { $dept_name = '(Unnamed Department)'; }
?>
<option value="<?= intval($d['id']) ?>" <?= ($user_dept === intval($d['id'])) ? 'selected' : '' ?>>
<?= htmlspecialchars($dept_name) ?> (<?= intval($d['id']) ?>)
</option>
<?php } ?>
</select>
</div>
<div class="pt-2">
<button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Save</button>
</div>
</form>
<?php
$CONTENT = ob_get_clean();
include 'layout.php';