-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
228 lines (194 loc) · 7.65 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload</title>
<link rel="stylesheet" href="./Assets/css/app.css">
</head>
<body>
<!-- Modal -->
<div id="add-new-image-modal" class="modal">
<div class="modal-content">
<span class="close" id="close">×</span>
<div class="modal-header">
<h3 class="modal-title">Add New Image</h3>
</div>
<div class="modal-body">
<div>
<input type="text" class="input title" id="title" placeholder="Enter Title" />
<span class="error title"></span>
</div>
<div class="image-upload" style="margin-top: 20px;">
<span class="image-upload-text">Drop file here or Click to Upload</span>
<input type="file" accept="image/png,image/jpeg" id="image" class="image-upload-input" hidden />
</div>
<div>
<span class="error image"></span>
</div>
</div>
<div class="container" style="margin-top: 50px;">
<div class="justify-end">
<button class="add-new-image-btn btn btn-red close-btn">Cancel</button>
<button class="add-new-image-btn btn" id="save-iamge-btn">Save</button>
</div>
</div>
</div>
</div>
<div class="container justify-content-center">
<div class="table-container shadow">
<div class="table-header">
<h3>Images</h3>
<button type="button" class="add-new-image-btn justify-end btn" id="add-new-image-btn">Add New</button>
</div>
<table>
<thead>
<th>Title</th>
<th>Thumbnail</th>
<th>Filename</th>
<th>Date Added</th>
<th>Action</th>
</thead>
<tbody id="image-table-body">
</tbody>
</table>
</div>
</div>
<script src="./Assets/js/jquery.js"></script>
<script src="./Assets/js/app.js"></script>
<script>
$(document).ready(function() {
loadDatas();
modal("add-new-image-modal", "add-new-image-btn");
$('#save-iamge-btn').on('click', function(e) {
let formData = new FormData();
formData.append('file', $('#image')[0].files[0]);
formData.append('title', $('#title').val());
formData.append('method', 'insertorUpdateImage');
if ($(this).data('id')) {
formData.append('id', $(this).data('id'));
}
$.ajax({
method: 'POST',
url: window.location.origin + "/api/image.php",
data: formData,
dataType: 'json',
contentType: false,
processData: false,
beforeSend: function() {
$('#save-iamge-btn').attr('disabled', true).html('Saving...');
},
complete: function() {
$('#save-iamge-btn').attr('disabled', false).html('Save');
},
success: function(response) {
if (typeof response.errors == 'undefined') {
loadDatas();
closeModal('add-new-image-modal');
} else {
$('.error.title').html(response.errors.title);
$('.error.image').html(response.errors.file);
}
}
});
});
});
function loadDatas() {
$.ajax({
method: 'POST',
url: window.location.origin + "/api/image.php",
data: {
method: 'getAllDatas'
},
dataType: 'json',
success: function(response) {
populateTable(response);
}
});
}
function populateTable(datas = []) {
let table = $('#image-table-body');
table.empty();
if (datas.length > 0) {
$.each(datas, function(i, v) {
let title = "<td>" + v.title + "</td>";
let filename = "<td>" + v.filename + "</td>";
let date_created = "<td>" + new Date(v.created_at).toLocaleDateString() + "</td>";
let image = $('<img />', {
src: window.location.origin + v.image,
style: 'width:200px;'
});
let imageLink = $('<a/>', {
href: window.location.origin + v.image,
target: '_blank'
})
.append(image);
let thumbnail = $("<td />", {
style: 'text-align:center;'
}).append(imageLink);
let deleteBtn = $('<button/>', {
text: 'Delete',
class: 'delete btn btn-red',
id: 'delete' + i,
click: function() {
deleteData(v.id);
}
});
let editBtn = $('<button/>', {
text: 'Edit',
class: 'edit btn',
id: 'edit-btn' + i,
style: 'margin-left:5px',
click: function() {
edit(v.id, this);
}
});
let actions = $('<td/>').append(deleteBtn).append(editBtn);
let tr = $('<tr>')
.append(title)
.append(thumbnail)
.append(filename)
.append(date_created)
.append(actions);
table.append(tr);
modal("add-new-image-modal", "edit-btn" + i);
});
} else {
table.append("<tr><td colspan='5' style='text-align:center;'>Empty Table</td></tr>");
}
}
function edit(id, elem) {
$('#add-new-image-modal .modal-title').html("Edit Image");
$('#save-iamge-btn').data('id', id);
$.ajax({
method: 'POST',
url: window.location.origin + "/api/image.php",
data: {
id: id,
method: 'getData'
},
dataType: 'json',
success: function(response) {
$('#title').val(response.title);
$('.image-upload-text').html("Drop file or Click to Replace Image");
}
});
}
function deleteData(id) {
$.ajax({
method: 'POST',
url: window.location.origin + "/api/image.php",
data: {
id: id,
method: 'deleteData'
},
dataType: 'json',
success: function(response) {
loadDatas();
}
});
}
</script>
</body>
</html>