flexday/allusers.php

119 lines
4.2 KiB
PHP

<?php
// allusers.php - inline editing table for all users
include_once("peter_db.php");
$peter_db = new peter_db();
$c = $peter_db->getConnection();
mysqli_set_charset($c, 'utf8');
// Fetch departments
$dept_opts = [];
$dept_q = "SELECT id, name FROM gavi_departments ORDER BY name";
$dept_r = mysqli_query($c, $dept_q);
while ($row = mysqli_fetch_assoc($dept_r)) { $dept_opts[] = $row; }
$users = [];
$q = "SELECT cu.id, cu.name, cu.email, cu.goo, cud.department_id
FROM conf_users cu
LEFT JOIN conf_user_departments cud ON cud.user_id = cu.id
ORDER BY cu.name";
$r = mysqli_query($c, $q);
while ($row = mysqli_fetch_assoc($r)) { $users[] = $row; }
// Handle AJAX updates
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$uid = isset($_POST['id']) ? intval($_POST['id']) : 0;
$field = isset($_POST['field']) ? $_POST['field'] : '';
$value = isset($_POST['value']) ? trim($_POST['value']) : '';
if ($uid > 0 && in_array($field, ['name','email','goo'])) {
$stmt = mysqli_prepare($c, "UPDATE conf_users SET $field = ? WHERE id = ?");
mysqli_stmt_bind_param($stmt, "si", $value, $uid);
mysqli_stmt_execute($stmt);
echo json_encode(['ok' => true]);
exit;
}
if ($uid > 0 && $field === 'department_id') {
$dept = intval($value);
if ($dept > 0) {
$stmt = 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($stmt, "ii", $uid, $dept);
mysqli_stmt_execute($stmt);
} else {
$stmt = mysqli_prepare($c, "DELETE FROM conf_user_departments WHERE user_id = ?");
mysqli_stmt_bind_param($stmt, "i", $uid);
mysqli_stmt_execute($stmt);
}
echo json_encode(['ok' => true]);
exit;
}
echo json_encode(['ok' => false, 'err' => 'invalid']);
exit;
}
$MY_TITLE = "All Users";
$MY_CRUMB = "All Users";
ob_start();
?>
<div class="bg-white p-4 rounded shadow">
<table class="min-w-full text-sm border-collapse">
<thead>
<tr class="border-b">
<th class="text-left py-2 pr-3">Name</th>
<th class="text-left py-2 pr-3">Email</th>
<th class="text-left py-2 pr-3">GOO</th>
<th class="text-left py-2 pr-3">Department</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $u) { ?>
<tr class="border-b" data-id="<?= $u['id'] ?>">
<td contenteditable="true" data-field="name" class="py-1 pr-3"><?= htmlspecialchars($u['name']) ?></td>
<td contenteditable="true" data-field="email" class="py-1 pr-3"><?= htmlspecialchars($u['email']) ?></td>
<td contenteditable="true" data-field="goo" class="py-1 pr-3"><?= htmlspecialchars($u['goo']) ?></td>
<td class="py-1 pr-3">
<select data-field="department_id" class="border rounded px-2 py-1">
<option value="0">-- None --</option>
<?php foreach ($dept_opts as $d) { ?>
<option value="<?= $d['id'] ?>" <?= ($u['department_id'] == $d['id']) ? 'selected' : '' ?>><?= htmlspecialchars($d['name']) ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script>
(function(){
const rows = document.querySelectorAll('tbody tr');
function postUpdate(id, field, value) {
const form = new FormData();
form.append('id', id);
form.append('field', field);
form.append('value', value);
fetch(window.location.href, { method: 'POST', body: form })
.then(r => r.json())
.then(j => { if(!j.ok){ alert('Save failed'); } })
.catch(() => alert('Save failed'));
}
rows.forEach(row => {
const id = row.getAttribute('data-id');
row.querySelectorAll('[contenteditable]').forEach(cell => {
cell.addEventListener('blur', () => {
postUpdate(id, cell.dataset.field, cell.innerText.trim());
});
});
row.querySelectorAll('select[data-field]').forEach(sel => {
sel.addEventListener('change', () => {
postUpdate(id, sel.dataset.field, sel.value);
});
});
});
})();
</script>
<?php
$CONTENT = ob_get_clean();
include 'layout.php';