commit 15493e3d7987f073cfa27b132dc5c87818dc00f3
Author: Anděl Ondřej <ondrej.andel@marbes.cz>
Date:   Sun Apr 18 18:08:38 2021 +0200

    Feature #8646: Správa uživatelů

diff --git a/application/controller/LoginController.php b/application/controller/LoginController.php
index bced67c..4cc6c66 100644
--- a/application/controller/LoginController.php
+++ b/application/controller/LoginController.php
@@ -2,7 +2,6 @@
 require "../model/DB.php";
 session_start();
 $DB = new DB();
-var_dump($_POST);
 $result = $DB->getUser($_POST['username']);
 
 if (count($result) == 1) {
diff --git a/application/controller/UpdateUserRoleController.php b/application/controller/UpdateUserRoleController.php
index 8467999..9ea3d5f 100644
--- a/application/controller/UpdateUserRoleController.php
+++ b/application/controller/UpdateUserRoleController.php
@@ -1,7 +1,7 @@
 <?php
 require "../model/DB.php";
-
-if ($_SESSION['role'] == "ADMIN") {
+session_start();
+if ($_SESSION['role'] == "admin") {
     $DB = new DB();
 
     $results = $DB->updateUserRole($_POST["userId"], $_POST["role"]);
diff --git a/application/controller/UsersListController.php b/application/controller/UsersListController.php
index 881ca18..1f2feac 100644
--- a/application/controller/UsersListController.php
+++ b/application/controller/UsersListController.php
@@ -1,7 +1,8 @@
 <?php
 require "../model/DB.php";
+session_start();
 
-if ($_SESSION['role'] == "ADMIN") {
+if ($_SESSION['role'] == "admin") {
     $DB = new DB();
 
     $results = $DB->select("SELECT id, username, role from USERS", array());
diff --git a/application/users.php b/application/users.php
new file mode 100644
index 0000000..62076b4
--- /dev/null
+++ b/application/users.php
@@ -0,0 +1,36 @@
+<!doctype html>
+
+<html >
+<head>
+    <meta charset="utf-8">
+    <title>Dalimilova kronika</title>
+
+    <!-- css -->
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
+    <link rel="stylesheet" href="view/style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>">
+    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
+
+    <!-- jQuery library -->
+    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
+
+    <!-- JavaScript -->
+    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
+</head>
+<body>
+<div class="container-fluid">
+    <?php include "./view/header.html" ?>
+    <div class="row app-content">
+        <?php include "./view/menu.php" ?>
+
+        <div class="col-xs-12 col-sm-10 p-4 content d-flex">
+            <?php include "./view/userContent.php" ?>
+        </div>
+    </div>
+    <?php include "./view/footer.html" ?>
+</div>
+<?php include "./view/modals/settingsModal.html" ?>?>
+<?php include "./view/modals/editUserModal.html" ?>
+<?php include "./view/modals/removeUserModal.html" ?>
+</body>
+</html>
diff --git a/application/view/menu.php b/application/view/menu.php
index 606556f..3866977 100644
--- a/application/view/menu.php
+++ b/application/view/menu.php
@@ -6,6 +6,13 @@
         <a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'], '/index.php') ? 'active' : '');?>" href="index.php">Úvodní info</a>
         <a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'],'/search.php') ? 'active' : '');?>" href="search.php">Vyhledávání</a>
         <a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'],'/login.php') ? 'active' : '');?>" href="#" data-toggle="modal" data-target="#login-modal">Přihlášení</a>
+        <?php
+            if (isset($_SESSION['role']) && $_SESSION['role'] == "admin") {
+                echo "<a class=\"px-4 py-1";
+                echo (strstr($_SERVER['PHP_SELF'],'/users.php') ? 'active' : '');
+                echo "href=\"users.php\">Správa uživatelů</a>";
+            }
+        ?>
         <a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'],'/settings.php') ? 'active' : '');?>" href="#" data-toggle="modal" data-target="#settings-modal" id="modalSettingsHref">Nastavení</a>
     </div>
 </div>
\ No newline at end of file
diff --git a/application/view/modals/editUserModal.html b/application/view/modals/editUserModal.html
new file mode 100644
index 0000000..af8c422
--- /dev/null
+++ b/application/view/modals/editUserModal.html
@@ -0,0 +1,129 @@
+<div id="edit-modal" class="modal fade" role="dialog">
+    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h4 class="modal-title"></h4>
+                <button type="button" class="close" data-dismiss="modal">&times;</button>
+            </div>
+            <div class="modal-body">
+                <div class="container">
+                    <div class="row my-1" id="username-wrapper">
+                        <div class="col-xs-12 col-sm-4">
+                            <label for="username">Uživatelské jméno:</label>
+                        </div>
+                        <div class="col-xs-12 col-sm-8">
+                            <input class="min-wdth" id="username"/>
+                        </div>
+                    </div>
+                    <div class="row my-1" id="password-wrapper">
+                        <div class="col-xs-12 col-sm-4">
+                            <label for="password">Heslo:</label>
+                        </div>
+                        <div class="col-xs-12 col-sm-8">
+                            <input type="password" class="min-wdth" id="password"/>
+                        </div>
+                    </div>
+                    <div class="row my-1">
+                        <div class="col-xs-12 col-sm-4">
+                            <label for="email">Email:</label>
+                        </div>
+                        <div class="col-xs-12 col-sm-8">
+                            <input class="min-wdth" id="email"/>
+                        </div>
+                    </div>
+                    <div class="row my-1">
+                        <div class="col-xs-12 col-sm-4">
+                            <label for="rights">Práva:</label>
+                        </div>
+                        <div class="col-xs-12 col-sm-8">
+                            <select class="min-wdth" id="rights">
+                                <option value = "editor">Editor</option>
+                                <option value = "admin">Admin</option>
+                            </select>
+                        </div>
+                    </div>
+
+                </div>
+            </div>
+            <div class="modal-footer">
+                <button type="button" id="submit-but" class="btn btn-default" data-dismiss="modal"></button>
+                <button type="button" class="btn btn-default" data-dismiss="modal">Zavřít</button>
+            </div>
+        </div>
+    </div>
+</div>
+
+<script>
+    $('#edit-modal').on('show.bs.modal', function (event) {
+        const button = $(event.relatedTarget);
+        const pseudo_id = button.data('pseudo-id');// Extract info from data-* attributes
+        const title = button.data('title');// Extract info from data-* attributes
+
+        let modal = $(this);
+        modal.find('.modal-title').text(title);
+
+        if(title === "Nový uživatel"){
+            prefill(modal);
+            modal.find('#submit-but').text("Založit");
+            modal.find("#username-wrapper").show();
+            modal.find("#password-wrapper").show();
+            modal.find('#submit-but').click(() => createData(modal));
+        } else {
+            const data_obj = users[pseudo_id];
+            modal.find('#submit-but').text("Upravit");
+            modal.find("#username-wrapper").hide();
+            modal.find("#password-wrapper").hide();
+            modal.find('#submit-but').click(() => editData(data_obj, modal));
+            prefill(modal, data_obj);
+        }
+    });
+
+
+    function prefill(modal, data = null){
+        modal.find("#username").val("");
+        modal.find("#password").val("");
+        modal.find("#email").val("");
+        modal.find("#rights").val("");
+
+        if(data !== null){
+            modal.find("#email").val(data.email);
+            modal.find("#rights").val(data.role);
+        }
+    }
+
+    function editData(data, modal){
+        const formData = prepareFormData(modal);
+        formData.append("userId", data.id);
+
+        var xhr = new XMLHttpRequest();
+        xhr.open("POST", "./controller/UpdateUserRoleController.php");
+        xhr.send(formData);
+        setTimeout(function(){
+         fetchUsers(); //reload data after item delete
+        }, 500);
+    }
+
+    function prepareFormData(modal) {
+        const formData = new FormData();
+        formData.append("role", modal.find("#rights")[0].value);
+        formData.append("email", modal.find("#email")[0].value);
+
+        return formData;
+    }
+
+    function createData(modal){
+        const formData = prepareFormData(modal);
+        const username = modal.find("#username")[0].value;
+        if(username !== undefined && username !== null){
+            formData.append("username", username);
+            formData.append("password", modal.find("#password")[0].value);
+
+            var xhr = new XMLHttpRequest();
+            xhr.open("POST", "./controller/RegistrationController.php");
+            xhr.send(formData);
+            setTimeout(function(){
+                fetchUsers(); //reload data after item delete
+            }, 500);
+        }
+    }
+</script>
\ No newline at end of file
diff --git a/application/view/modals/removeUserModal.html b/application/view/modals/removeUserModal.html
new file mode 100644
index 0000000..7595064
--- /dev/null
+++ b/application/view/modals/removeUserModal.html
@@ -0,0 +1,47 @@
+<div id="remove-modal" class="modal fade" role="dialog">
+	<div class="modal-dialog modal-dialog-centered" role="document">
+		<div class="modal-content">
+			 <div class="modal-header">
+					<h4 class="modal-title">Potvrdit odstranění</h4>
+					<button type="button" class="close" data-dismiss="modal">&times;</button>
+			 </div>
+			 <div class="modal-body">
+				<div class="container">
+					<div class="row">
+                        <p id="mesBody"></p>
+                    </div>
+				</div>
+			 </div>
+			 <div class="modal-footer">
+                <button type="button" class="btn btn-default" data-dismiss="modal" onclick="removeUser(document.getElementById('idToRemoveInput').value)">Ano</button>
+                <button type="button" class="btn btn-default" data-dismiss="modal">Ne</button>
+				<input type="hidden" id="idToRemoveInput" />
+			</div>
+		</div>
+	</div>
+</div>
+
+<script>
+    $('#remove-modal').on('show.bs.modal', function (event){ //wait for modal to show 
+        const button = $(event.relatedTarget);
+        const pseudo_id = button.data('pseudo-id');//Extract info from data-* attributes
+
+        const data_obj = users[pseudo_id];
+        let modal = $(this);
+
+		modal.find('#mesBody').text("Opravdu chcete odstranit uživatele  \"" + data_obj.username + "\"?");
+		modal.find('#idToRemoveInput').val(data_obj.id); //assign id to delete to hidden input
+    });
+
+	function removeUser(id){
+		const data = new FormData(); //to pass ids, which we want to delete from DB
+		data.append("userId", ""+id); //id to delete from table dd_wordform
+
+		let xhr = new XMLHttpRequest();
+		xhr.open("POST", "./controller/DeleteUserController.php");
+		xhr.send(data);
+		setTimeout(function(){
+			fetchUsers(); //reload data after item delete
+		}, 500);
+	}
+</script>
\ No newline at end of file
diff --git a/application/view/userContent.php b/application/view/userContent.php
new file mode 100644
index 0000000..272b2b5
--- /dev/null
+++ b/application/view/userContent.php
@@ -0,0 +1,65 @@
+<?php
+
+session_start();
+if (isset($_SESSION['role']) && $_SESSION['role'] == "admin") {
+    echo "<script>
+        //what to do when page renders
+        window.onload = function() {
+            fetchUsers();
+        };
+    </script>
+
+    <div class=\"search-results my-4\">
+        <table class=\"table table-striped\">
+            <thead>
+            <tr>
+                <th scope=\"col\">Uživatel</th>
+                <th scope=\"col\">Email</th>
+                <th scope=\"col\">Práva</th>
+                <th scope=\"col\"><button class=\"btn ml-1\" title=\"Přidat\"  data-toggle=\"modal\" data-target=\"#edit-modal\"  data-title=\"Nový uživatel\"><i class=\"fa fa-plus\"></i></button></th>
+            </tr>
+            </thead>
+            <tbody id=\"search-table\">
+                <script>
+                    let users;
+                    function fetchUsers () {
+                        // (A1) GET SEARCH TERM
+                        const formData = new FormData();
+
+                        // (A2) AJAX - USE HTTP:// NOT FILE:/
+                        let xhr = new XMLHttpRequest();
+                        xhr.open(\"POST\", \"./controller/UsersListController.php\");
+                        xhr.onload = function(){
+                            let search = this.response;
+                            let parsedJSON = JSON.parse(search);
+                            users = parsedJSON;
+                            renderUsers(parsedJSON);
+                        };
+                        xhr.send(formData);
+                    }
+
+
+                    function renderUsers(users) {
+                        let result = \"\";
+                        users.forEach((item,id) => {
+                            result += \"<tr>\";
+                            result += \"<td>\" + item.username + \"</td>\";
+                            result += \"<td>\" + ((item.email !== undefined && item.email !== null) ? item.email : \"-\") + \"</td>\";
+                            result += \"<td>\" + item.role + \"</td>\";
+                            (item.role !== \"admin\") ? (result += \"<td class='action-td'>\" +
+                                \"<button class='btn ml-1' title='Upravit'  data-toggle='modal' data-target='#edit-modal' data-pseudo-id='\" + id + \"' data-title='Upravit uživatele'><i class='fa fa-pencil'></i></button>\" +
+                                \"<button class='btn ml-1' title='Odstranit'  data-toggle='modal' data-target='#remove-modal' data-pseudo-id='\" + id + \"'><i class='fa fa-trash'></i></button>\" +
+                                \"</td>\") : result += \"<td class='action-td'/>\";
+                            result += \"</tr>\";
+                        });
+                        document.getElementById(\"search-table\").innerHTML = result;
+                    }
+                </script>
+            </tbody>
+        </table>
+    </div>";
+} else {
+    echo "<h3 class=\"mx-auto text-center\">Pro navštívení stránky nemáte dostatečná oprávnění</h3>";
+}
+
+
