-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.php
More file actions
313 lines (282 loc) · 14.5 KB
/
users.php
File metadata and controls
313 lines (282 loc) · 14.5 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
// Initialize the session
session_start();
if (!$_SESSION['is_admin']){
header("location: index.php");
}
//active nav menu
$_SESSION['nav'] = 'users';
//title of page
global $title;
$title = "Users";
include "includes/checkLoggedIn.php";
//defining error messages
$nameErrorMessage = $emailErrorMessage = $passwordErrorMessage = $confirmPasswordErrorMessage = "";
$errorMessage = $successMessage = "";
//if request is post method
if($_SERVER["REQUEST_METHOD"] == "POST"){
$now = date('Y-m-d H:i:s');
if ($_POST['type'] == "addUser"){
// Check if name is empty
if(empty(trim($_POST["name"]))){
$nameErrorMessage = "Please enter name.";
} else{
$name = trim($_POST["name"]);
}
// Check if email is empty
if(empty(trim($_POST["email"]))){
$emailErrorMessage = "Please enter email.";
} else{
$email = trim($_POST["email"]);
$emailUniqueValidation = "SELECT id FROM users WHERE email = '$email'";
$emailUniqueValidationResult = $dbConnection->query($emailUniqueValidation);
if ($emailUniqueValidationResult->num_rows > 0) {
$emailErrorMessage = "Email is already taken.";
}
}
if (empty(trim($_POST["password"]))) {
$passwordErrorMessage = "Please enter password.";
} else {
$password = trim($_POST["password"]);
}
// Check if confirm password is empty
if (empty(trim($_POST["confirmPassword"]))) {
$confirmPasswordErrorMessage = "Please enter confirm password.";
} else {
$confirmPassword = trim($_POST["confirmPassword"]);
}
if ($password !== $confirmPassword) {
$passwordErrorMessage = $confirmPasswordErrorMessage = "Password confirmation does not match";
}
if (empty($emailErrorMessage) && empty($passwordErrorMessage) && empty($confirmPasswordErrorMessage) && empty($passwordErrorMessage)){
$password = password_hash($password, PASSWORD_BCRYPT);
$queryCreateNewUser = "INSERT INTO users (name, email, password) VALUES ('$name', '$email', '$password')";
if ($dbConnection->query($queryCreateNewUser)) {
$successMessage = "New user is successfully created.";
} else {
$errorMessage = "Something went wrong: " . "<br>" . $dbConnection->error;
}
}
else{
$errorMessage = "New user could not added. Please, open modal and look for errors.";
}
}
elseif ($_POST['type'] == "deleteUser"){
// Check if id is empty
if(empty(trim($_POST["id"]))){
$errorMessage = "Something went wrong.";
} else{
$id = trim($_POST["id"]);
}
if (empty($errorMessage)){
$queryDeleteUser = "DELETE FROM users WHERE id=$id";
if ($dbConnection->query($queryDeleteUser)) {
$successMessage = "User is successfully deleted.";
} else {
$errorMessage = "Something went wrong: " . "<br>" . $dbConnection->error;
}
}
}
elseif ($_POST['type'] == "updateUser"){
// Check if id is empty
if(empty(trim($_POST["id"]))){
$errorMessage = "Something went wrong.";
} else{
$id = trim($_POST["id"]);
}
// Check if name is empty
if(empty(trim($_POST["name"]))){
$nameErrorMessage = "Please enter name.";
} else{
$name = trim($_POST["name"]);
}
// Check if email is empty
if(empty(trim($_POST["email"]))){
$emailErrorMessage = "Please enter email.";
} else{
$email = trim($_POST["email"]);
$emailUniqueValidation = "SELECT id FROM users WHERE email = '$email' and id !=$id";
$emailUniqueValidationResult = $dbConnection->query($emailUniqueValidation);
if ($emailUniqueValidationResult->num_rows > 0) {
$emailErrorMessage = "Email is already taken.";
}
}
$password = "";
if (!empty(trim($_POST["password"])) && !empty(trim($_POST["confirmPassword"]))) {
$password = trim($_POST["password"]);
$confirmPassword = trim($_POST["confirmPassword"]);
if ($password !== $confirmPassword) {
$passwordErrorMessage = $confirmPasswordErrorMessage = "Password confirmation does not match";
}
$password = password_hash($password, PASSWORD_BCRYPT);
}
if (empty($emailErrorMessage) && empty($passwordErrorMessage) && empty($confirmPasswordErrorMessage) && empty($passwordErrorMessage)){
if (empty($password)){
$queryUpdateUser = "UPDATE users set name='$name', email='$email', updated_at = '$now' where id=$id";
}
else{
$queryUpdateUser = "UPDATE users set name='$name', email='$email', password='$password', updated_at = '$now' where id=$id";
}
if ($dbConnection->query($queryUpdateUser)) {
$successMessage = "User is successfully updated.";
} else {
$errorMessage = "Something went wrong: " . "<br>" . $dbConnection->error;
}
}
else{
$errorMessage = "User could not updated. Please, open modal and look for errors.";
}
}
}
?>
<?php include "partials/header.php"; ?>
<div class="content">
<?php if (!empty($errorMessage) || !empty($successMessage)){ ?>
<div class="alert">
<p class="text-<?php echo empty($errorMessage) ? "success" : "danger"; ?>">
<?php echo empty($errorMessage) ? $successMessage : $errorMessage; ?>
</p>
</div>
<?php } ?>
<div class="header">
<h2>Users</h2>
<button class="btn btn-modal" data-target="createNewItemModal" style="width: auto;"> Add new user </button>
</div>
<div class="content-data">
<?php
$queryUsers = "SELECT * FROM users WHERE is_admin = 0 order by id desc";
$userResult = $dbConnection->query($queryUsers);
if ($userResult->num_rows > 0) {
while ($user = $userResult->fetch_assoc()) {
?>
<div class="content-item">
<span>
ID:
<strong> <?php echo $user['id']; ?> </strong>
</span>
<span>
Name:
<strong> <?php echo $user['name']; ?> </strong>
</span>
<span>
Email:
<strong> <?php echo $user['email']; ?> </strong>
</span>
<span>
Created at:
<strong> <?php echo date('d-m-y H:i', strtotime($user['created_at'])); ?> </strong>
</span>
<span>
Updated at:
<strong> <?php echo date('d-m-y H:i', strtotime($user['updated_at'])); ?> </strong>
</span>
<span>
<a href="index.php?id=<?php echo $user['id']; ?>" class="btn btn-success mr-5">Tasks</a>
<button class="btn mr-5 btn-modal" data-target="editUser<?php echo $user['id']; ?>">Edit</button>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
<input type="hidden" name="type" value="deleteUser">
<a class="btn btn-danger mr-5"
onclick="if (confirm('Do you want to delete this user?')) {this.parentElement.submit();}"
>
Delete
</a>
</form>
</span>
</div>
<div id="editUser<?php echo $user['id']; ?>" class="modal">
<div class="modal-content">
<h4>Update user</h4>
<form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>' method="POST">
<input type="hidden" name="type" value="updateUser">
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
<div class="form-group <?php echo (!empty($nameErrorMessage)) ? 'has-error' : ''; ?>">
<label>Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter your name" required value="<?php echo $user["name"]; ?>">
<span class='form-text text-danger'>
<?php echo $nameErrorMessage; ?>
</span>
</div>
<div class="form-group <?php echo (!empty($emailErrorMessage)) ? 'has-error' : ''; ?>">
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Enter your email" required value="<?php echo $user["email"]; ?>">
<span class='form-text text-danger'>
<?php echo $emailErrorMessage; ?>
</span>
</div>
<div class="form-group <?php echo (!empty($passwordErrorMessage)) ? 'has-error' : ''; ?>">
<label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
<span class='form-text text-danger'>
<?php echo $passwordErrorMessage; ?>
</span>
</div>
<div class="form-group <?php echo (!empty($confirmPasswordErrorMessage)) ? 'has-error' : ''; ?>">
<label>Confirm password</label>
<input type="password" class="form-control" name="confirmPassword" placeholder="Confirm password">
<span class='form-text text-danger'>
<?php echo $confirmPasswordErrorMessage; ?>
</span>
</div>
<div class="form-group">
<button type="submit" class="btn">Update</button>
</div>
<div class="form-group">
<button class="btn cancel" data-target="editUser<?php echo $user['id']; ?>"> Cancel </button>
</div>
</form>
</div>
</div>
<?php
}
}
else{
echo "<p>No users</p>";
}
?>
</div>
</div>
<div id="createNewItemModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<h4>Create new user</h4>
<form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>' method="POST">
<input type="hidden" name="type" value="addUser">
<div class="form-group <?php echo (!empty($nameErrorMessage)) ? 'has-error' : ''; ?>">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Enter your name" required value="<?php echo isset($_POST["name"]) ? $_POST["name"] : "" ?>">
<span class='form-text text-danger'>
<?php echo $nameErrorMessage; ?>
</span>
</div>
<div class="form-group <?php echo (!empty($emailErrorMessage)) ? 'has-error' : ''; ?>">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter your email" required value="<?php echo isset($_POST["name"]) ? $_POST["email"] : "" ?>">
<span class='form-text text-danger'>
<?php echo $emailErrorMessage; ?>
</span>
</div>
<div class="form-group <?php echo (!empty($passwordErrorMessage)) ? 'has-error' : ''; ?>">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Password" required>
<span class='form-text text-danger'>
<?php echo $passwordErrorMessage; ?>
</span>
</div>
<div class="form-group <?php echo (!empty($confirmPasswordErrorMessage)) ? 'has-error' : ''; ?>">
<label for="confirmPassword">Confirm password</label>
<input type="password" class="form-control" name="confirmPassword" id="confirmPassword" placeholder="Confirm password" required>
<span class='form-text text-danger'>
<?php echo $confirmPasswordErrorMessage; ?>
</span>
</div>
<div class="form-group">
<button type="submit" class="btn">Add</button>
</div>
<div class="form-group">
<button class="btn cancel" data-target="createNewItemModal"> Cancel </button>
</div>
</form>
</div>
</div>
<?php include "partials/footer.php"; ?>