-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_Customer.php
More file actions
250 lines (223 loc) · 8.58 KB
/
controller_Customer.php
File metadata and controls
250 lines (223 loc) · 8.58 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
<?php
include("model_Customer.php");
include("model_BookLoan.php");
session_start();
// cek dulu kalau misal ngga ada session customerList
if (!isset($_SESSION['customerList'])) {
// buat deklarasi session customerList sebagai array untuk menampung objek customer
$_SESSION['customerList'] = array();
}
function create_Customer()
{
$customer = new Customer();
$customer->name = $_POST['name'];
$customer->address = $_POST['address'];
$customer->email = $_POST['email'];
date_default_timezone_set("Asia/Bangkok");
$customerBirthdate = date("Y-m-d", strtotime($_POST['birthdate']));
$currentDate = date("Y-m-d");
if ($customerBirthdate >= $currentDate) {
$_SESSION['birthdate_error'] = "Please input your birthdate correctly!";
$birthdateOk = 0;
} else {
$customer->birthdate = $customerBirthdate;
$birthdateOk = 1;
}
$customer->fileName = $_FILES['customerImage']['name'];
$fileType = strtolower(pathinfo(($customer->fileName), PATHINFO_EXTENSION));
$fileTmp_Name = $_FILES['customerImage']['tmp_name'];
$fileSize = $_FILES['customerImage']['size'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($customer->fileName);
$check = getimagesize($fileTmp_Name);
if ($check !== false) {
$_SESSION['fileIsImg'] = "File is an image - " . $check["mime"] . "." . "<br>";
$uploadOk = 1;
} else {
$_SESSION['fileNotImg_error'] = "File is not an image." . "<br>";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
$_SESSION['fileExist_error'] = "File already exists." . "<br>";
$uploadOk = 0;
}
// Check file size
// 1.000.000 byte = 1 mb
if ($fileSize > 500000) {
$_SESSION['fileSize_error'] = "Sorry, your file is too large." . "<br>";
$uploadOk = 0;
}
// Allow certain file formats
if ($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif") {
$_SESSION['fileType_error'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed." . "<br>";
$uploadOk = 0;
}
// Check if $uploadOk and $birthdateOk is set to 0 by an error
if ($uploadOk == 0 || $birthdateOk == 0) {
$referer = $_SERVER['HTTP_REFERER'];
echo "
<script language='javascript'>
alert('Sorry, $customer->name hasn\\'t been created.');
window.location.href = '$referer';
</script>
";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($fileTmp_Name, $target_file)) {
array_push($_SESSION['customerList'], $customer);
echo "
<script language='javascript'>
alert('$customer->name has been successfully created.');
window.location.href = 'view_indexCustomer.php';
</script>
";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
function update_Customer($customerID)
{
$customer = $_SESSION['customerList'][$customerID]; // ini ambil customer dengan id tertentu
$customer->name = $_POST['name'];
$customer->address = $_POST['address'];
$customer->email = $_POST['email'];
date_default_timezone_set("Asia/Bangkok");
$customerBirthdate = date("Y-m-d", strtotime($_POST['birthdate']));
$currentDate = date("Y-m-d");
$birthdateOk = 1;
if ($customerBirthdate >= $currentDate) {
$_SESSION['birthdate_error'] = "Please input your birthdate correctly!";
$birthdateOk = 0;
} else {
$customer->birthdate = $customerBirthdate;
}
$customerImage = $_FILES['customerImage']['name'];
$fileType = strtolower(pathinfo(($customerImage), PATHINFO_EXTENSION));
$fileTmp_Name = $_FILES['customerImage']['tmp_name'];
$fileSize = $_FILES['customerImage']['size'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($customerImage);
$check = getimagesize($fileTmp_Name);
if ($check !== false) {
$_SESSION['fileIsImg'] = "File is an image - " . $check["mime"] . "." . "<br>";
$uploadOk = 1;
} else {
$_SESSION['fileNotImg_error'] = "File is not an image." . "<br>";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
$_SESSION['fileExist_error'] = "File already exists." . "<br>";
$uploadOk = 0;
}
// Check file size
// 1.000.000 byte = 1 mb
if ($fileSize > 500000) {
$_SESSION['fileSize_error'] = "Sorry, your file is too large." . "<br>";
$uploadOk = 0;
}
// Allow certain file formats
if ($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif") {
$_SESSION['fileType_error'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed." . "<br>";
$uploadOk = 0;
}
// Check if $uploadOk and $birthdateOk is set to 0 by an error
if ($uploadOk == 0 || $birthdateOk == 0) {
echo "
<script language='javascript'>
alert('Sorry, $customer->name hasn\\'t been updated.');
window.location.href = 'view_updateCustomer.php?updateCustomerID=" . $_POST['customer_ID'] . "';
</script>
";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($fileTmp_Name, $target_file)) {
$customer->fileName = $customerImage;
echo "
<script language='javascript'>
alert('$customer->name has been successfully updated.');
window.location.href = 'view_indexCustomer.php';
</script>
";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
// ini sudah ngga perlu lagi push ke dalam array nanti malah double
// jadi tujuannya hanya update
}
function read_Customer()
{
return $_SESSION['customerList'];
}
function get_CustomerWithID($customerID)
{
return $_SESSION['customerList'][$customerID];
}
function delete_Customer($customerID)
{
unset($_SESSION['customerList'][$customerID]);
}
// cek jika button ADD ditekan
if (isset($_POST['addCustomer'])) {
$_SESSION['input_data'] = $_POST;
create_Customer();
// redirect/kembali ke halaman index untuk melihat semua data yang sudah ditambahkan
}
// cek jika button UPDATE ditekan
if (isset($_POST['updateCustomer'])) {
if ($_FILES['customerImage']['error'] == 4) {
$customer = $_SESSION['customerList'][$_POST['customer_ID']];
$customer->name = $_POST['name'];
$customer->address = $_POST['address'];
$customer->email = $_POST['email'];
date_default_timezone_set("Asia/Bangkok");
$customerBirthdate = date("Y-m-d", strtotime($_POST['birthdate']));
$currentDate = date("Y-m-d");
$birthdateOk = 1;
if ($customerBirthdate >= $currentDate) {
$_SESSION['birthdate_error'] = "Please input your birthdate correctly!";
$birthdateOk = 0;
} else {
$customer->birthdate = $customerBirthdate;
}
$customer->fileName = $_POST['customerImageOld'];
if ($birthdateOk == 0) {
echo "
<script language='javascript'>
alert('Sorry, $customer->name hasn\\'t been updated.');
window.location.href = 'view_updateCustomer.php?updateCustomerID=" . $_POST['customer_ID'] . "';
</script>
";
} else {
echo "
<script language='javascript'>
alert('$customer->name has been successfully updated.');
window.location.href = 'view_indexCustomer.php';
</script>
";
}
} else {
update_Customer($_POST['customer_ID']);
// redirect/kembali ke halaman index untuk melihat semua data yang sudah ditambahkan
}
}
// cek jika button DELETE ditekan
if (isset($_GET['deleteCustomerID'])) {
delete_Customer($_GET['deleteCustomerID']);
if (isset($_SESSION['bookLoanList'])) {
$bookLoanList = $_SESSION['bookLoanList'];
foreach ($bookLoanList as $index => $loan) {
if ($loan->customer_ID == $_GET['deleteCustomerID']) {
unset($bookLoanList[$index]);
}
}
// jika sudah dihapus objek bookLoanList nya maka harus disimpan kembali ke dalam session
// untuk ditampilkan ke dalam halaman index BookLoanList
$_SESSION['bookLoanList'] = $bookLoanList;
}
header("location:view_indexCustomer.php");
}
?>