1 |
15493e3d
|
Anděl Ondřej
|
<?php
|
2 |
|
|
if (isset($_SESSION['role']) && $_SESSION['role'] == "admin") {
|
3 |
|
|
echo "<script>
|
4 |
|
|
//what to do when page renders
|
5 |
|
|
window.onload = function() {
|
6 |
|
|
fetchUsers();
|
7 |
|
|
};
|
8 |
|
|
</script>
|
9 |
|
|
|
10 |
|
|
<div class=\"search-results my-4\">
|
11 |
|
|
<table class=\"table table-striped\">
|
12 |
|
|
<thead>
|
13 |
|
|
<tr>
|
14 |
|
|
<th scope=\"col\">Uživatel</th>
|
15 |
|
|
<th scope=\"col\">Práva</th>
|
16 |
|
|
<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>
|
17 |
|
|
</tr>
|
18 |
|
|
</thead>
|
19 |
|
|
<tbody id=\"search-table\">
|
20 |
|
|
<script>
|
21 |
|
|
let users;
|
22 |
|
|
function fetchUsers () {
|
23 |
|
|
// (A1) GET SEARCH TERM
|
24 |
|
|
const formData = new FormData();
|
25 |
|
|
|
26 |
|
|
// (A2) AJAX - USE HTTP:// NOT FILE:/
|
27 |
|
|
let xhr = new XMLHttpRequest();
|
28 |
|
|
xhr.open(\"POST\", \"./controller/UsersListController.php\");
|
29 |
|
|
xhr.onload = function(){
|
30 |
|
|
let search = this.response;
|
31 |
|
|
let parsedJSON = JSON.parse(search);
|
32 |
|
|
users = parsedJSON;
|
33 |
|
|
renderUsers(parsedJSON);
|
34 |
|
|
};
|
35 |
|
|
xhr.send(formData);
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
function renderUsers(users) {
|
40 |
|
|
let result = \"\";
|
41 |
|
|
users.forEach((item,id) => {
|
42 |
|
|
result += \"<tr>\";
|
43 |
|
|
result += \"<td>\" + item.username + \"</td>\";
|
44 |
|
|
result += \"<td>\" + item.role + \"</td>\";
|
45 |
|
|
(item.role !== \"admin\") ? (result += \"<td class='action-td'>\" +
|
46 |
|
|
\"<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>\" +
|
47 |
|
|
\"<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>\" +
|
48 |
|
|
\"</td>\") : result += \"<td class='action-td'/>\";
|
49 |
|
|
result += \"</tr>\";
|
50 |
|
|
});
|
51 |
|
|
document.getElementById(\"search-table\").innerHTML = result;
|
52 |
|
|
}
|
53 |
|
|
</script>
|
54 |
|
|
</tbody>
|
55 |
|
|
</table>
|
56 |
|
|
</div>";
|
57 |
|
|
} else {
|
58 |
|
|
echo "<h3 class=\"mx-auto text-center\">Pro navštívení stránky nemáte dostatečná oprávnění</h3>";
|
59 |
|
|
}
|
60 |
|
|
|