-
Notifications
You must be signed in to change notification settings - Fork 0
/
view-users.php
290 lines (186 loc) · 9.22 KB
/
view-users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php include_once __DIR__ . "/assets/php/app.php" ?>
<?php
define("PARENT_DIR", __DIR__);
if ( !chechkIfLoggedIn() )
{
header("location: login.php?returnUrl=" . htmlspecialchars($_SERVER['PHP_SELF']) );
}
$userDetails = getUserDetailsUsingUsername($dbConn, $_SESSION["SSID-USERNAME"] );
if ( $userDetails['user_role'] != 'super-admin' && $userDetails['user_role'] != 'admin' )
{
load404Page();
exit;
}
?>
<?php
if( $_SERVER["REQUEST_METHOD"] === "GET" )
{
$products = getAllUsers($dbConn, 0);
if ( isset( $_GET["offset"] ) && isset( $_GET["SSID"] ) && !empty( $_GET["offset"] ) && !empty( $_GET["SSID"] ) )
{
if ( !hash_equals( $_SESSION["SSID"], $_GET["SSID"] ) )
{
load404Page();
exit;
}
$offset = intval( $_GET["offset"] );
$categories = getAllUsers($dbConn, $offset);
$result = array(
"status" => 200,
"response" => $categories
);
echo json_encode($result);
exit;
}
}
if( $_SERVER["REQUEST_METHOD"] === "POST" )
{
if ( !isset( $_POST["delete-id"] ) || empty( $_POST["delete-id"] ) )
{
load404Page();
exit;
}
$deleteId = $_POST["delete-id"];
$checkIfRecordExist = chechkIfValueExist($dbConn, 'table_users', "user_id", $deleteId);
if ( $checkIfRecordExist != 1 )
{
$dir = '../NFSFU-ECOMM/product-not-found.php';
loadPage($dir);
exit;
}
$userName = getUserDetailsUsingUsername($dbConn, $deleteId);
if ( $deleteId === $userName["user_id"] )
{
$path = __DIR__ . "/assets/users/" . $deleteId;
deleteAFolderAndItsContents( $path, true );
if ( deleteUserDetailInDB( $dbConn, "table_users", $deleteId ) )
{
header("Location:" . htmlspecialchars($_SERVER['PHP_SELF']) . "?respCode=200&msg=success");
}
else
{
header("Location:" . htmlspecialchars($_SERVER['PHP_SELF']) . "?respCode=400&msg=error");
}
}
else
{
header("Location:" . htmlspecialchars($_SERVER['PHP_SELF']) . "?respCode=400&msg=YOU-CANNOT-DELETE-YOURSELF");
}
}
?>
<?php require_once 'assets/pages/admin/head.php' ?>
<title> View Users - <?php echo $site_titile ?></title>
</head>
<body>
<div class="container-fluid">
<?php require_once 'assets/pages/admin/navigation.php' ?>
<div class="contents container-fluid">
<?php require_once 'assets/pages/admin/sidebar.php' ?>
<?php require_once 'assets/pages/view-user.php' ?>
</div>
</div>
</body>
<script>
var numberOfrecords = parseInt( document.getElementById("numberOfRecordsShown").innerHTML );
if ( numberOfrecords < 50 )
{
document.querySelector(".loadMoreButton").classList.add("display-none");
}
else
{
document.querySelector(".loadMoreButton").addEventListener("click", ()=>
{
const url = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>?offset=" + numberOfrecords + "&SSID=<?php echo $_SESSION["SSID"] ?>";
let requestOptions =
{
method: "GET",
mode: "same-origin",
credentials: "same-origin",
Accept: 'application/json, text/plain, */*',
headers: {
Accept: 'application/json, text/plain, */*',
},
cache: 'default',
}
fetch( url, requestOptions )
.then( (response) => response.json() )
.then( (data) => {
let status = parseInt(data.status);
console.log(data.status);
if ( status === 200 )
{
console.log(data.response);
let datas = data.response;
if ( datas.length === 0 )
{
document.querySelector(".loadMoreButton").innerHTML = "<h1>You Have Reached the end of all categories in the Database</h1>";
}
else
{
datas.forEach( ( data )=>
{
const dataRow = document.createElement("tr");
const table_categroy_id = document.createElement("td");
table_categroy_id.setAttribute("scope", "row");
table_categroy_id.innerHTML = numberOfrecords + 1;
const table_user_name = document.createElement("td");
table_user_name.innerHTML = data.user_name;
const table_user_id = document.createElement("td");
table_user_id.innerHTML = data.user_id;
const table_user_username = document.createElement("td");
table_user_username.innerHTML = data.user_username;
const table_user_email = document.createElement("td");
table_user_email.innerHTML = data.user_email;
const table_product_user_role = document.createElement("td");
table_product_user_role.innerHTML = data.user_role;
const table_number_of_orders = document.createElement("td");
table_number_of_orders.innerHTML = data.user_number_of_orders;
const table_actions = document.createElement("td");
const table_actions_action_container = document.createElement("div");
table_actions_action_container.setAttribute("class", "btn-group");
const edit_link = document.createElement("a");
edit_link.setAttribute("href", "profile-edit.php?product-id=" + data.product_id);
const edit_link_btn = document.createElement("button");
edit_link_btn.setAttribute("class", "btn btn-primary");
edit_link_btn.innerHTML = '<i class="fas fa-pen"></i>';
edit_link.appendChild(edit_link_btn);
const category_delete_form = document.createElement("form");
category_delete_form.setAttribute("action", "<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>");
category_delete_form.setAttribute("method", "POST");
const category_delete_form_input = document.createElement("input");
category_delete_form_input.setAttribute("type", "hidden");
category_delete_form_input.setAttribute("name", "delete-id");
category_delete_form_input.setAttribute("value", data.user_id);
const category_delete_form_button = document.createElement("button");
category_delete_form_button.setAttribute( "class", "btn btn-danger");
category_delete_form_button.innerHTML = '<i class="fa fa-delete-left"></i>';
category_delete_form.appendChild(category_delete_form_input);
category_delete_form.appendChild(category_delete_form_button);
table_actions_action_container.appendChild(edit_link);
table_actions_action_container.appendChild(category_delete_form);
table_actions.appendChild(table_actions_action_container);
dataRow.appendChild(table_user_name);
dataRow.appendChild(table_user_id);
dataRow.appendChild(table_user_username);
dataRow.appendChild(table_user_email);
dataRow.appendChild( table_product_user_role);
dataRow.appendChild(table_number_of_orders);
dataRow.appendChild(table_actions);
console.log(dataRow);
const table = document.querySelector(".table");
const tableBody = table.querySelector("tbody");
numberOfrecords += 1;
document.getElementById("numberOfRecordsShown").innerHTML = numberOfrecords;
tableBody.appendChild(dataRow);
} );
}
}
})
.catch( (error) =>
{
console.error("Error:", error);
Rstatus = false;
} );
});
}
</script>