-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
222 lines (183 loc) · 5.55 KB
/
export.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
<?php
// Debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Import XLSX maker
require 'vendor/autoload.php';
// No timeouts!!!
ini_set('max_execution_time', 0);
// Creating the connection by specifying the connection details
$db = mysqli_connect("localhost", "root", "root", "export");
// Checking the connection
if (!$db) {
// Connection error return data
ReturnData(array(
"status" => "420",
"error" => "No DB connection - are you sure the service is running?",
"data" => ""
), $db);
}
// Importing CSV data for each table
SetFKAndUniqueCheck(0, $db);
$tables = array(
"employees" => "(
ID,
name
)",
"projects" => "(
@ID_str,
name
) SET
ID = CAST(@ID_str AS UNSIGNED INTEGER)",
"tasks" => "(
@ID_str,
name
) SET
ID = CAST(@ID_str AS UNSIGNED INTEGER)",
"registrations" => "(
@ID_str,
@taskID_str,
@projectID_str,
employeeID,
monthAndYear,
month,
@totalHoursAsNumber_str,
@employeeCost_str,
@generalCost_str,
overwriteProject,
overwriteTask
) SET
ID = CAST(@ID_str AS UNSIGNED INTEGER),
taskID = CAST(@taskID_str AS UNSIGNED INTEGER),
projectID = CAST(@projectID_str AS UNSIGNED INTEGER),
totalHoursAsNumber = CAST(@totalHoursAsNumber_str AS DOUBLE),
employeeCost = CAST(@employeeCost_str AS DOUBLE),
generalCost = CAST(@generalCost_str AS DOUBLE)"
);
array_walk($tables, "ImportData", $db);
SetFKAndUniqueCheck(1, $db);
// Get records for export
$registrations = $db->query(
"SELECT
COALESCE(
CASE WHEN r.overwriteProject IS NOT NULL AND TRIM(r.overwriteProject) <> '' THEN r.overwriteProject ELSE NULL END,
p.name
) AS Project,
e.name AS Employee,
COALESCE(
CASE WHEN r.overwriteTask IS NOT NULL AND TRIM(r.overwriteTask) <> '' THEN r.overwriteTask ELSE NULL END,
t.name
) AS Task,
r.monthAndYear AS Date,
r.month AS Month,
SUM(r.totalHoursAsNumber) AS SumHours,
SUM(r.employeeCost + r.generalCost) AS SumCost
FROM
registrations r
JOIN
projects p ON r.projectID = p.ID
JOIN
employees e ON r.employeeID = e.ID
JOIN
tasks t ON r.taskID = t.ID
GROUP BY
COALESCE(
CASE WHEN r.overwriteProject IS NOT NULL AND TRIM(r.overwriteProject) <> '' THEN r.overwriteProject ELSE NULL END,
p.name
),
e.name,
COALESCE(
CASE WHEN r.overwriteTask IS NOT NULL AND TRIM(r.overwriteTask) <> '' THEN r.overwriteTask ELSE NULL END,
t.name
),
r.monthAndYear,
r.month
ORDER BY
COALESCE(
CASE WHEN r.overwriteProject IS NOT NULL AND TRIM(r.overwriteProject) <> '' THEN r.overwriteProject ELSE NULL END,
p.name
),
e.name,
COALESCE(
CASE WHEN r.overwriteTask IS NOT NULL AND TRIM(r.overwriteTask) <> '' THEN r.overwriteTask ELSE NULL END,
t.name
),
r.monthAndYear,
r.month
"
);
// Check if registrations
if(!$registrations){
ReturnData(array(
"status" => "420",
"error" => "There are no registrations generated.",
"data" => ""
), $db);
}
// Create a new Spreadsheet
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Set column names as headers
$columns = array();
$i = 0;
while ($fieldInfo = $registrations->fetch_field()) {
$i++;
$sheet->setCellValueByColumnAndRow($i, 1, $fieldInfo->name);
$columns[] = $fieldInfo->name;
}
// start from the second row for row-data
$rowNumber = 2;
while ($row = $registrations->fetch_assoc()) {
$columnNumber = 0;
foreach ($columns as $columnName) {
$columnNumber++;
$sheet->setCellValueByColumnAndRow($columnNumber, $rowNumber, $row[$columnName]);
}
$rowNumber++;
}
// Generate xlsx from the Spreadsheet
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
ob_start();
$writer->save('php://output');
$excelOutput = ob_get_clean();
// Convert to base64
$base64Excel = base64_encode($excelOutput);
// Get data
ReturnData(array(
"status" => "200",
"error" => "",
"data" => $base64Excel
), $db);
// Sets FK and unique checks
function SetFKAndUniqueCheck(int $set, mysqli|false $db){
$db->query("SET foreign_key_checks = $set");
$db->query("SET foreign_key_checks = $set");
}
// Imports data from csv into DB
function ImportData(string $columns, string $table, mysqli|false $db){
// Importing x...
$db->query("DELETE FROM $table");
$query = "LOAD DATA INFILE '$table.csv' IGNORE INTO TABLE $table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n'";
$result = $db->query($query);
// Check if x import was success
if(!$result){
ReturnData(array(
"status" => "420",
"error" => "Could not import $table",
"data" => ""
), $db);
}
}
// Returns the data from the script as JSON
function ReturnData(array $data, mysqli|false $db){
// Close DB is open
if($db){
$db->close();
}
// Set header and print response
header("Content-Type: application/json");
echo json_encode($data);
// Exit PHP
exit();
}