select table rows and edit user

This commit is contained in:
2022-06-10 14:10:34 +02:00
parent 3c613cca33
commit 43c33b55d6
11 changed files with 200 additions and 6 deletions

View File

@@ -81,3 +81,11 @@ div.headline h2{
padding: 10px 0px; padding: 10px 0px;
font-weight: 400; font-weight: 400;
} }
tr.highlight {
background: #eef;
}
tr.clicked {
background: #ccd;
}

72
app/public/edit_user.php Normal file
View File

@@ -0,0 +1,72 @@
<?php
session_start();
include("../scripts/connection.php");
include("../scripts/functions.php");
$user_data = check_admin($con);
if($_SERVER['REQUEST_METHOD'] == "GET") {
$row = get_user($con, $_GET['id'])->fetch();
include("header_footer/header.php");
}
if($_SERVER['REQUEST_METHOD'] == "POST") {
$user = get_user($con, $_POST['id'])->fetch();
if($user['user_name'] != $_POST['user_name']) {
change_user_name($con, $_POST['id'], $_POST['user_name']);
}
if($user['user_group'] != $_POST['user_group']) {
if($_POST['user_group'] == "station") {
change_user_group($con, $_POST['id'], $_POST['user_group'], $_POST['bind_station']);
} else {
change_user_group($con, $_POST['id'], $_POST['user_group'], NULL);
}
}
if(!empty($_POST['password'])) {
$phash = generate_password_hash($_POST['password'], $user['salt']);
change_password($con, $_POST['id'], $phash);
}
header("Location: manage_user.php");
die;
}
?>
<body>
<div class="headline">
<h2>Benutzer bearbeiten</h2>
</div>
<div>
<form method="post">
<label for="user_name">Benutzername:</label>
<input name="user_name" type="text" value=<?php echo "\"" . $row['user_name'] . "\"";?>/><br>
<label for="password">Neues Passwort:</label>
<input type="password" name="password"/><br>
<label for="user_group">Benutzergruppe:</label>
<select name="user_group" id="user_group">
<option value="station" <?php if($row['user_group'] == "station"){echo " selected";}?>>Station</option>
<option value="statistics" <?php if($row['user_group'] == "statistics") {echo " selected";}?>>Statistik</option>
<option value="admin" <?php if($row['user_group'] == "admin") {echo " selected";}?>>Admin</option>
</select><br>
<label for="bind_station">Gebunden an Station:</label>
<select name="bind_station" id="bind_station">
<?php
if($row['s_id'] == null) {
load_options_stations($con, "", false);
} else {
load_options_stations($con, $row['s_id'], false);
}
?>
</select>
<label for="bind_station">(Nur für Benutzergruppe Station)</label><br>
<input type="hidden" value=<?php echo "\"" . $row['id'] . "\""?> name="id"/>
<input type="submit" value="ändern">
</form>
</div>
</body>
<?php
$con = null;
echo file_get_contents("header_footer/footer.html");
?>

View File

@@ -6,9 +6,11 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/styles.css"> <link rel="stylesheet" href="css/styles.css">
<script src="js/select_table.js"></script>
</head> </head>
<header> <header>
<script src="js/http_req.js" type="module"></script>
<section> <section>
<div id="logo"> <div id="logo">
Punktesystem-KSP Punktesystem-KSP

View File

@@ -0,0 +1,17 @@
//wait for html site to be ready before executing init()
if (document.readyState == "complete") {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}
function init() {
var button = document.getElementById("edit_user");
button.onclick = function() {
var row = document.getElementsByClassName("selected")[0];
document.getElementById('user_id_val').value = row.id;
this.form.submit();
}
}

View File

@@ -0,0 +1,43 @@
//wait for html site to be ready before executing init()
if (document.readyState == "complete") {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}
function init() {
if(document.getElementById('table') != null) {
highlight_row();
}
}
var selected = null;
function highlight_row() {
var table = document.getElementById('table');
var cells = table.getElementsByTagName('td');
for( var i = 0; i < cells.length; i++) {
var cell = cells[i];
cell.onclick = function() {
var rowId = this.parentNode.rowIndex;
var rowsNotSelected = table.getElementsByTagName('tr');
for ( var row = 0; row < rowsNotSelected.length; row++) {
rowsNotSelected[row].style.backgroundColor = "";
rowsNotSelected[row].classList.remove('selected');
}
var rowSelected = table.getElementsByTagName('tr')[rowId];
if (selected != null && selected == rowId) {
selected = null;
rowSelected.style.backgroundColor = "";
rowSelected.classList.remove('selected');
document.getElementsByClassName("edit")[0].disabled = true;
} else {
selected = rowId;
rowSelected.style.backgroundColor = "yellow";
rowSelected.className += " selected";
document.getElementsByClassName("edit")[0].disabled = false;
}
}
}
}

View File

@@ -16,8 +16,15 @@
<input type="submit" value="Benutzer hinzufügen"/> <input type="submit" value="Benutzer hinzufügen"/>
</form> </form>
</div> </div>
<div>
<form action="edit_user.php" method="get">
<input id="edit_user" type="button" value="Eintrag bearbeiten" class="edit" disabled="true"/>
<input id="user_id_val" type="hidden" name="id"/>
</form>
</div>
<script src="js/edit_user.js"></script>
<div class="table-div"> <div class="table-div">
<table> <table id="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Name</th> <th scope="col">Name</th>

View File

@@ -18,7 +18,7 @@
</form> </form>
</div> </div>
<div class="table-div"> <div class="table-div">
<table> <table id="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Name</th> <th scope="col">Name</th>

View File

@@ -18,7 +18,7 @@
</form> </form>
</div> </div>
<div class="table-div"> <div class="table-div">
<table> <table id="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Name</th> <th scope="col">Name</th>

View File

@@ -47,7 +47,7 @@
?> ?>
</div> </div>
<div class="table-div"> <div class="table-div">
<table> <table id="table">
<?php <?php
if($session == "total-score") { if($session == "total-score") {
load_total_score($con); load_total_score($con);

View File

@@ -191,7 +191,7 @@ function write_team($con, $team_name, $fire_department) {
function get_users($con) { function get_users($con) {
try { try {
$stmt = $con->prepare("SELECT user_name, user_group, s_id FROM users"); $stmt = $con->prepare("SELECT id, user_name, user_group, s_id FROM users");
$stmt->execute(); $stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt; return $stmt;
@@ -200,6 +200,17 @@ function get_users($con) {
} }
} }
function get_user($con, $id) {
try {
$stmt = $con->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute(['id' => $id]);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt;
} catch(PDOException $e) {
handle_pdo_exception($e);
}
}
function write_user($con, $user_name, $user_id, $phash, $salt, $user_group, $s_id) { function write_user($con, $user_name, $user_id, $phash, $salt, $user_group, $s_id) {
try { try {
$stmt = $con->prepare("INSERT INTO users (user_id, password, user_name, salt, user_group, s_id) VALUES (?, ?, ?, ?, ?, ?)"); $stmt = $con->prepare("INSERT INTO users (user_id, password, user_name, salt, user_group, s_id) VALUES (?, ?, ?, ?, ?, ?)");
@@ -213,4 +224,38 @@ function write_user($con, $user_name, $user_id, $phash, $salt, $user_group, $s_i
} catch(PDOException $e) { } catch(PDOException $e) {
handle_pdo_exception($e); handle_pdo_exception($e);
} }
}
function change_user_name($con, $id, $user_name) {
try {
$stmt = $con->prepare("UPDATE users SET user_name = ? WHERE id= ?");
$stmt->bindParam(1, $user_name, PDO::PARAM_STR);
$stmt->bindParam(2, $id, PDO::PARAM_STR);
$stmt->execute();
} catch(PDOExeption $e) {
handle_pdo_exception($e);
}
}
function change_user_group($con, $id, $user_group, $s_id) {
try {
$stmt = $con->prepare("UPDATE users SET user_group = ?, s_id = ? WHERE id= ?");
$stmt->bindParam(1, $user_group, PDO::PARAM_STR);
$stmt->bindParam(2, $s_id, PDO::PARAM_INT);
$stmt->bindParam(3, $id, PDO::PARAM_INT);
$stmt->execute();
} catch(PDOExeption $e) {
handle_pdo_exception($e);
}
}
function change_password($con, $id, $password) {
try {
$stmt = $con->prepare("UPDATE users SET password = ? WHERE id= ?");
$stmt->bindParam(1, $password, PDO::PARAM_STR);
$stmt->bindParam(2, $id, PDO::PARAM_STR);
$stmt->execute();
} catch(PDOExeption $e) {
handle_pdo_exception($e);
}
} }

View File

@@ -143,7 +143,7 @@ function check_admin($con) {
function load_users($con) { function load_users($con) {
$stmt = get_users($con); $stmt = get_users($con);
foreach($stmt->fetchAll() as $row) { foreach($stmt->fetchAll() as $row) {
echo "<tr>\n"; echo "<tr id=\"" . $row['id'] . "\" >\n";
echo "<td>" . $row['user_name'] . "</td>\n"; echo "<td>" . $row['user_name'] . "</td>\n";
echo "<td>" . $row['user_group'] . "</td>\n"; echo "<td>" . $row['user_group'] . "</td>\n";
echo "<td>" . $row['s_id'] . "</td>\n"; echo "<td>" . $row['s_id'] . "</td>\n";