-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction.php
300 lines (258 loc) · 8.08 KB
/
function.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
291
292
293
294
295
296
297
298
299
300
<?php
$servername = "localhost"; //host mysql
$username = "root"; //user mysql
$password = ""; //password mysql
$db_name = "test"; //database name mysql
// Create connection
$conn = mysql_connect($servername, $username, $password); //koneksi ke mysql
// Check connection
if (!$conn) { //cek koneksi ke mysql
die('Gagal Koneksi: ' . mysql_error());
}
mysql_select_db("test",$conn) or die("Couldn't select database."); //koneksi ke database
// ****kumpulan fungsi sistem
function getLastSKU($conn, $category) { //mendapatkan sku terakhir dari kategori yang dipilih
$sql_sku_cat = 'SELECT SKU from mytable WHERE Categories = "'.$category.'" ORDER by ID DESC LIMIT 1' ;
$ambildata = mysql_query($sql_sku_cat, $conn) or die("Cannot select db.");
if(!$ambildata ) {
die('Gagal ambil data: ' . mysql_error());
}
$check = mysql_fetch_array($ambildata);
return $check['SKU'];
}
function product_checker($conn, $product_name){
$sql_name = 'SELECT * FROM `mytable` WHERE Name = "'.$product_name.'"' ;
$ambildata = mysql_query($sql_name, $conn) or die("Cannot select db.");
if(!$ambildata ) {
die('Gagal ambil data: ' . mysql_error());
}
$check = mysql_fetch_array($ambildata);
if ($check != NULL) {
header("Location: index.php?message=produk_ada");
}
}
function insertProduct($conn, $insert) { //insert product baru ke database csv
list($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r) = $insert;
$insert_product = "INSERT INTO mytable (`ID`,`TYPE`,`SKU`,`Name`,`Published`,`Is_featured`,`Visibility_in_catalog`,`Short_description`,`Tax_status`,`In_stock`,`Backorders_allowed`,`Sold_individually`,`Allow_customer_reviews`,`Regular_price`,`Categories`,`Position`,`Attribute_1_name`,`Attribute_1_visible`) VALUES ('$a','$b','$c','$d','$e','$f','$g','$h','$i','$j','$k','$l','$m','$n','$o','$p','$q','$r')";
$query_input = mysql_query($insert_product, $conn);
// return $query_input;
header("Location: index.php?sku=".$c."&progress=".$query_input);
}
function newID($conn) { //menambahkan ID baru pada database csv (database csv tidak berformat auto increment
$sql_last_id = 'SELECT ID from mytable ORDER by ID DESC LIMIT 1 ';
$ambildata = mysql_query($sql_last_id, $conn) or die("Cannot select db.");
if(!$ambildata ) {
die('Gagal ambil data: ' . mysql_error());
}
$check = mysql_fetch_array($ambildata);
$new_id = $check['ID'] + 1;
return $new_id;
}
function nextSKU($conn, $category) { //menambah SKU baru sesuai urutan sku yang sudah ada
$last_sku = getLastSKU($conn, $category);
$split = explode('-', $last_sku);
if (isset($split[1])) {
$sku = $split[1];
} else {
$sku = $split[0];
}
$new_sku_num = $sku + 1;
$digit = count(str_split($new_sku_num));
if ($digit == 1) {
$new_sku_num = '000'.$new_sku_num;
} elseif ($digit == 2) {
$new_sku_num = '00'.$new_sku_num;
} elseif ($digit == 3) {
$new_sku_num = '0'.$new_sku_num;
} else {
$new_sku_num = $new_sku_num;
}
$sku_letter = skuLetter($category); //menggunakan convert kategori ke SKU
$new_sku = $sku_letter.'-'.$new_sku_num;
return $new_sku;
}
function skuLetter($category){ //redirect kategori ke SKU
switch ($category){
case "accessories":
return "ACC";
break;
case "android tv box":
return "ATB";
break;
case "notebook":
return "NTB";
break;
case "charger kabel data":
return "CKD";
break;
case "casing":
return "CS";
break;
case "earphone":
return "EP";
break;
case "flashdisk":
return "FDD";
break;
case "headset":
return "HS";
break;
case "printer canon":
return "PCN";
break;
case "printer brother":
return "PBR";
break;
case "printer hp":
return "PHP";
break;
case "printer epson":
return "PEP";
break;
case "scanner":
return "SCN";
break;
case "tinta epson original":
return "TEO";
break;
case "tinta canon original":
return "TCO";
break;
case "tinta hp original":
return "THO";
break;
case "tinta brother original":
return "TBO";
break;
case "networking brand asus":
return "NBA";
break;
case "networking brand dlink":
return "NBD";
break;
case "networking brand tp-link":
return "NBT";
break;
case "networking brand tenda":
return "NBTD";
break;
case "networking brand mikrotik":
return "NBMK";
break;
case "external hdd 2.5":
return "EHF";
break;
case "external hdd 3.5":
return "EHN";
break;
case "internal hdd 2.5":
return "IHF";
break;
case "internal hdd 3.5":
return "IHN";
break;
case "modem":
return "MDM";
break;
case "ssd":
return "SSD";
break;
case "memory card":
return "MMC";
break;
case "ram":
return "RAM";
break;
case "wireless receiver":
return "WRC";
break;
case "kabel":
return "KBL";
break;
case "ip camera":
return "IPCM";
break;
case "monitor":
return "MNTR";
break;
case "power bank":
return "PWBK";
break;
case "toner":
return "TNR";
break;
case "printer thermal & bluetooth":
return "PTB";
break;
case "projector":
return "PJT";
break;
case "tablet gambar":
return "TBGR";
break;
case "layar projector":
return "LYPJ";
break;
case "speaker":
return "SPKR";
break;
case "gaming peripheral":
return "GMPL";
break;
case "psu, ups & stabilizer":
return "USTB";
break;
case "fingerprint":
return "FGPR";
break;
case "kertas foto":
return "KSFT";
break;
case "tinta refill":
return "TRFL";
break;
case "keyboard & mouse":
return "KYMS";
break;
case "pointer":
return "PNTR";
break;
case "thermal paste":
return "TMPS";
break;
default:
return "NULL";
}
}
function directory_check($category) { //membuat folder baru berdasarkan kategori jika belum ada
$dirname = $category;
$filename = "/images/" . $dirname . "/";
if (!file_exists($filename)) {
@mkdir("images/" . $dirname, 0777);
}
}
function image_name_cleaner($name) { //menghilangkan garis miring di nama foto
$matches = preg_replace('/\//', '', $name);
return $matches;
}
function downloadFile($url, $category, $product_name) { //download image dari tokopedia dan menyimpan di folder images/$category/$filename
$product_name = image_name_cleaner($product_name); //menggunakan image_name_cleaner()
directory_check($category); //menggunakan directory_check()
$path = 'images/'.$category.'/'.$product_name.'.jpg'; //merangkai path foto di folder
$newfname = $path;
$file = fopen($url, 'rb');
if ($file) {
$newf = fopen($newfname, 'wb');
if ($newf) {
while (!feof($file)) {
fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
}
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}