-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_edit_note.php
225 lines (195 loc) · 8.42 KB
/
form_edit_note.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
<?php
session_start();
include("database/config.php");
$usernameFolder = "";
$username = "";
$image = "";
if(!isset($_SESSION['f32bee31be074f58db022158fb7f400bfa8b321f0012c9e50346e8bc230d5cb2']) && !isset($_SESSION['ee49dcf234054d9329748e09f2cd9d29e20f7a000a533812653f9e552ce67dd7']) ){
echo "<script>
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
if(isset($_SESSION['f32bee31be074f58db022158fb7f400bfa8b321f0012c9e50346e8bc230d5cb2']) && isset($_SESSION['ee49dcf234054d9329748e09f2cd9d29e20f7a000a533812653f9e552ce67dd7'])){
$userSession = $_SESSION['f32bee31be074f58db022158fb7f400bfa8b321f0012c9e50346e8bc230d5cb2'];
$sql = "SELECT * FROM user";
$query = mysqli_query($db,$sql);
while($data = mysqli_fetch_array($query)){
$hashUsernameDatabase = hash("sha256",$data['username']);
if($userSession === $hashUsernameDatabase){
$username = $data['username'];
}
}
}
if(isset($_POST['editNote'])){
$id = $_POST['id'];
$titleNotes = htmlspecialchars($_POST['titleNotes']);
$descriptionNotes = htmlspecialchars($_POST['descriptionNotes']);
$publish = htmlspecialchars($_POST['publish']);
$idFolder = $_POST['idFolder'];
$usernameFolder = htmlspecialchars($_POST['usernameFolder']);
$NameFolder = htmlspecialchars($_POST['NameFolder']);
$image = htmlspecialchars($_POST['image']);
$createdAt = htmlspecialchars($_POST['created_at']);
}
if(isset($_POST['editNotes'])){
$id = $_POST['id'];
$titleNotes = htmlspecialchars($_POST['titleNotes']);
$descriptionNotes = $_POST['descriptionNotes'];
$saveString = nl2br(str_replace(" ", " ",$descriptionNotes));
$idFolder = htmlspecialchars($_POST['idFolder']);
$NameFolder = htmlspecialchars($_POST['NameFolder']);
$username = htmlspecialchars($_POST['username']);
$publish = htmlspecialchars($_POST['publish']);
$gambarSebelumnya = htmlspecialchars($_POST['image']);
$nameFile = $_FILES['background_notes']['name'];
$sizeFile = $_FILES['background_notes']['size'];
$errorFile = $_FILES['background_notes']['error'];
$tmpNameFile = $_FILES['background_notes']['tmp_name'];
$viewDefault = 0;
if(file_exists($tmpNameFile) || is_uploaded_file($tmpNameFile)){
$ektensiGambarValid = ['jpg','jpeg','png'];
$ektensiGambar = explode('.',$nameFile);
$ektensiGambar = strtolower(end($ektensiGambar));
if(!in_array($ektensiGambar,$ektensiGambarValid)){
echo "<script>
alert('Silahkan upload file yang memiliki ekstensi jpeg,jpg dan png');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
if($sizeFile > 1000000){
echo "<script>
alert('Ukuran gambar telalu besar');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
$newNameFile = uniqid();
$newNameFile .= '.';
$newNameFile .= $ektensiGambar;
if($publish == "Private"){
$sql = "UPDATE notes SET titleNotes='$titleNotes',descriptionNotes='$saveString',publish='$publish',view='$viewDefault',image='$newNameFile' WHERE id='$id'";
$query = mysqli_query($db,$sql);
if($query){
unlink('img/' . $gambarSebelumnya);
move_uploaded_file($tmpNameFile, 'img/' . $newNameFile);
echo "<script>
alert('Data berhasil diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}else{
echo "<script>
alert('Data gagal diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
}else{
$sql = "UPDATE notes SET titleNotes='$titleNotes',descriptionNotes='$saveString',publish='$publish',view='$viewDefault',image='$newNameFile' WHERE id='$id'";
$query = mysqli_query($db,$sql);
if($query){
unlink('img/' . $gambarSebelumnya);
move_uploaded_file($tmpNameFile, 'img/' . $newNameFile);
echo "<script>
alert('Data berhasil diupdate dan akan menghapus file');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}else{
echo "<script>
alert('Data gagal diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
}
}else{
if($publish == "Private"){
$sql = "UPDATE notes SET titleNotes='$titleNotes',descriptionNotes='$saveString',publish='$publish',view='$viewDefault' WHERE id='$id'";
$query = mysqli_query($db,$sql);
if($query){
echo "<script>
alert('Data berhasil diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}else{
echo "<script>
alert('Data gagal diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
}else{
$sql = "UPDATE notes SET titleNotes='$titleNotes',descriptionNotes='$saveString',publish='$publish' WHERE id='$id'";
$query = mysqli_query($db,$sql);
if($query){
echo "<script>
alert('Data berhasil diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}else{
echo "<script>
alert('Data gagal diupdate');
window.location.href = 'dashboard_user.php';
</script>";
exit;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My Notes || Form edit notes</title>
<link rel="stylesheet" href="src/css/form_add_notes.css" type="text/css" media="all" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="img/asset/favicon_io/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="img/asset/favicon_io/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="img/asset/favicon_io/favicon-16x16.png">
<link rel="manifest" href="img/asset/favicon_io/site.webmanifest">
</head>
<body>
<form class="form_add_notes" method="POST" enctype="multipart/form-data">
<?php if($usernameFolder == $username): ?>
<h1 class="title">Formulir edit catatan</h1>
<input type="text" class="hidden" name="id" value="<?= $id; ?>" readonly required>
<input type="text" name="titleNotes" value="<?= $titleNotes; ?>" required>
<textarea class="descriptionNotesAreas" name="descriptionNotes" value="<?= $descriptoSaveString; ?>" required><?= $descriptionNotes; ?></textarea>
<!-- <input type="text" name="descriptionNotes" value="" required> -->
<input type="text" class="hidden" name="idFolder" value="<?= $idFolder; ?>" readonly required>
<input type="text" name="NameFolder" class="hidden" value="<?= $NameFolder; ?>" readonly required>
<input type="text" name="username" value="<?= $usernameFolder; ?>" readonly required>
<input type="text" name="created_at" class="hidden" value="<?= $createdAt; ?>" required readonly>
<input type="text" name="image" class="hidden" value="<?= $image; ?>" required readonly>
<input type="image" src="img/<?= $image; ?>" alt="Background Notes">
<input type="file" name="background_notes" placeholder="Silahkan masukan gambar yang kamu sukai" required readonly>
<select name="publish" required>
<option value="">Silahkan pilih publish atau private</option>
<option value="Private"
<?php
if($publish == "Private"){
echo "selected";
}
?>>Private</option>
<option value="Publis" <?php
if($publish == "Publis"){
echo "selected";
}
?>>Publis</option>
</select>
<a href="detail_folder.php">Kembali</a>
<button type="submit" name="editNotes">Edit</button>
<?php endif; ?>
</form>
</body>
</html>