-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.php
125 lines (110 loc) · 3.66 KB
/
converter.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
<?php
session_start();
$_SESSION['hiba'] = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
$tableName = $_POST['table_name'] ?? 'default_table_name';
if (isset($_POST['first_row_header'])) {
$firstRowHeader = true;
} else {
$firstRowHeader = false;
}
if (isset($_POST['first_row_header'])) {
$firstColumnPK = true;
} else {
$firstColumnPK = false;
}
// We hope the user didn't lied about pk
if (isset($_POST['generate_pk'])) {
$generatePK = true;
} else {
$generatePK = false;
}
if (isset($_POST['first_column_pk'])) {
$firstColumnKey = true;
} else {
$firstColumnKey = false;
}
if ($_POST['radio'] == 'tabulator') {
$delimiter = " ";
}
if ($_POST['radio'] == 'semicolon') {
$delimiter = ";";
}
if ($_POST['radio'] == 'comma') {
$delimiter = ",";
}
if ($_POST['radio'] == 'custom') {
$delimiter = isset($_POST['delimiter']) ? $_POST['delimiter'] : ',';
}
// $delimiter = isset($_POST['delimiter']) ? $_POST['delimiter'] : ',';
// echo $_POST['tabulator'];
$csvFilePath = $_FILES['csv_file']['tmp_name'];
$csvData = file_get_contents($csvFilePath);
$lines = explode(PHP_EOL, $csvData);
$pkColumnName = 'id';
if (isset($_POST['first_column_pk'])) {
$firstColumnKey = true;
} else {
$firstColumnKey = false;
}
$headers = [];
$firstColumnType = 'INTEGER';
if ($firstRowHeader) {
$headers = str_getcsv(array_shift($lines), $delimiter);
} else {
$firstLine = str_getcsv($lines[0], $delimiter);
$secondLine = str_getcsv($lines[1], $delimiter);
for ($i = 0; $i < count($firstLine); $i++) {
$headers[] = 'column' . ($i + 1);
}
if ($firstColumnPK = true && is_numeric(trim($secondLine[0]))) {
$generatePK = false;
// echo(trim($secondLine[0]));
} else {
$hiba = "Az első oszlop nem alkalmazható elsődleges kulcsként";
$_SESSION['hiba'] = $hiba;
}
}
$createTableSQL = "CREATE TABLE IF NOT EXISTS `$tableName`(\n";
if ($generatePK) {
$createTableSQL .= " `$pkColumnName` INTEGER AUTO_INCREMENT PRIMARY KEY,\n";
}
foreach ($headers as $index => $header) {
$columnType = 'VARCHAR(255)';
$columnExtra = ' NOT NULL';
if ($index == 0) {
// $columnType = $firstColumnType;
$secondLine = str_getcsv($lines[1], $delimiter);
if (isset($_POST['first_column_pk']) && is_numeric(trim($secondLine[0]))) {
$columnType = 'INTEGER';
}
if ($firstColumnKey) {
$columnExtra .= ' PRIMARY KEY';
}
}
if ($header === 'id' && $generatePK) {
$header = 'id2';
}
$createTableSQL .= " `$header` $columnType$columnExtra,\n";
}
$createTableSQL = rtrim($createTableSQL, ",\n") . "\n);";
$insertSQL = [];
foreach ($lines as $line) {
if (!empty($line)) {
$rowData = str_getcsv($line, $delimiter);
$rowData = array_map(function($value) { return addslashes($value); }, $rowData);
$insertSQL[] = "INSERT INTO `$tableName` (`" . implode("`, `", $headers) . "`) VALUES ('" . implode("', '", $rowData) . "');";
}
}
$sqlContent = $createTableSQL . "\n" . implode("\n", $insertSQL);
$_SESSION['sql_content'] = $sqlContent;
header('Location: gen/');
// echo($_POST['first_column_pk']);
// echo('<a href="gen/">gen/</a>');
exit;
} else {
// Redirect back to the form if no file was uploaded
$_SESSION['hiba'] = 'Fájl feltöltés hiba';
header('Location: index.php?error=NoFileUploaded');
exit;
}