This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.php
437 lines (393 loc) · 16.5 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/*******************************************************************************
MLInvoice: web-based invoicing application.
Copyright (C) 2010-2015 Ere Maijala
This program is free software. See attached LICENSE.
*******************************************************************************/
/*******************************************************************************
MLInvoice: web-pohjainen laskutusohjelma.
Copyright (C) 2010-2015 Ere Maijala
Tämä ohjelma on vapaa. Lue oheinen LICENSE.
*******************************************************************************/
require_once 'localize.php';
require_once 'miscfuncs.php';
require_once 'import.php';
class ExportData
{
protected $importer;
public function __construct()
{
$this->importer = new ImportFile();
}
public function launch()
{
$charset = getRequest('charset', 'UTF-8');
$table = getRequest('table', '');
$format = getRequest('format', '');
$fieldDelimiter = getRequest('field_delim', ',');
$enclosureChar = getRequest('enclosure_char', '"');
$rowDelimiter = getRequest('row_delim', "\n");
$columns = getRequest('column', '');
$childRows = getRequest('child_rows', '');
$deletedRecords = getRequest('deleted', false);
if ($table && $format && $columns) {
if (!table_valid($table))
die('Invalid table name');
$res = mysqli_query_check("show fields from {prefix}$table");
$field_count = mysqli_num_rows($res);
$field_defs = [];
while ($row = mysqli_fetch_assoc($res)) {
$field_defs[$row['Field']] = $row;
}
foreach ($columns as $key => $column) {
if (!$column)
unset($columns[$key]);
elseif (!isset($field_defs[$column]))
die('Invalid column name');
}
ob_clean();
$filename = isset($GLOBALS["locTable_$table"]) ? $GLOBALS["locTable_$table"] : $table;
switch ($format) {
case 'csv' :
$field_delims = $this->importer->get_field_delims();
$enclosure_chars = $this->importer->get_enclosure_chars();
$row_delims = $this->importer->get_row_delims();
if (!isset($field_delims[$fieldDelimiter]))
die('Invalid field delimiter');
$fieldDelimiter = $field_delims[$fieldDelimiter]['char'];
if (!isset($enclosure_chars[$enclosureChar]))
die('Invalid enclosure character');
$enclosureChar = $enclosure_chars[$enclosureChar]['char'];
if (!isset($row_delims[$rowDelimiter]))
die('Invalid field delimiter');
$rowDelimiter = $row_delims[$rowDelimiter]['char'];
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=\"$filename.csv\"");
if ($charset == 'UTF-16')
echo iconv($charset, 'UTF-16', ''); // output BOM
$this->output_str(
$this->str_putcsv($columns, $fieldDelimiter, $enclosureChar) .
$rowDelimiter, $charset);
break;
case 'xml' :
header('Content-type: text/xml');
header("Content-Disposition: attachment; filename=\"$filename.xml\"");
if ($charset == 'UTF-16')
echo iconv($charset, 'UTF-16', ''); // output BOM
$this->output_str("<?xml version=\"1.0\"?>\n<records>\n", $charset);
break;
case 'json' :
header('Content-type: application/json');
header(
"Content-Disposition: attachment; filename=\"$filename.json\"");
if ($charset == 'UTF-16')
echo iconv($charset, 'UTF-16', ''); // output BOM
echo "{\"$table\":[\n";
break;
}
$query = "select * from {prefix}$table";
if (!$deletedRecords) {
$query .= ' where deleted=0';
if ($table == 'company_contact')
$query .= ' and company_id not in (select id from {prefix}company where deleted=1)';
elseif ($table == 'invoice_row')
$query .= ' and invoice_id not in (select id from {prefix}invoice where deleted=1)';
}
$res = mysqli_query_check($query);
$first = true;
while ($row = mysqli_fetch_assoc($res)) {
$data = [];
foreach ($columns as $column) {
$value = $row[$column];
if (is_null($value))
$data[$column] = '';
if ($value &&
substr($field_defs[$column]['Type'], 0, 8) == 'longblob')
$data[$column] = '0x' . bin2hex($value);
else {
$data[$column] = $value;
}
}
switch ($format) {
case 'csv' :
$this->output_str(
$this->str_putcsv($data, $fieldDelimiter, $enclosureChar) .
$rowDelimiter, $charset);
break;
case 'xml' :
$str = " <$table>\n";
foreach ($columns as $column) {
$str .= " <$column>" . xml_encode($data[$column]) .
"</$column>\n";
}
if ($childRows && ($table == 'invoice' || $table == 'company')) {
if ($table == 'invoice') {
$cres = mysqli_param_query(
'select * from {prefix}invoice_row where invoice_id=?',
[
$row['id']
]);
$tag = 'invoice_row';
} else {
$cres = mysqli_param_query(
'select * from {prefix}company_contact where company_id=?',
[
$row['id']
]);
$tag = 'company_contact';
}
while ($crow = mysqli_fetch_assoc($cres)) {
$str .= " <$tag>\n";
foreach ($crow as $column => $value) {
$str .= " <$column>" . xml_encode($value) .
"</$column>\n";
}
$str .= " </$tag>\n";
}
}
$str .= " </$table>\n";
$this->output_str($str, $charset);
break;
case 'json' :
if ($childRows && ($table == 'invoice' || $table == 'company')) {
if ($table == 'invoice')
$childTable = 'invoice_row';
else
$childTable = 'company_contact';
$data[$childTable] = [];
if ($table == 'invoice')
$cres = mysqli_param_query(
'select * from {prefix}invoice_row where invoice_id=?',
[
$row['id']
]);
else
$cres = mysqli_param_query(
'select * from {prefix}company_contact where company_id=?',
[
$row['id']
]);
while ($crow = mysqli_fetch_assoc($cres)) {
$data[$childTable][] = $crow;
}
}
if ($first)
$first = false;
else
echo (",\n");
$this->output_str(json_encode($data), $charset);
break;
}
}
switch ($format) {
case 'xml' :
$this->output_str("</records>\n");
break;
case 'json' :
echo ("\n]}\n");
break;
}
exit();
}
?>
<script type="text/javascript">
$(document).ready(function() {
$('#imessage').ajaxStart(function() {
$('#spinner').css('visibility', 'visible');
});
$('#imessage').ajaxStop(function() {
$('#spinner').css('visibility', 'hidden');
});
$('#imessage').ajaxError(function(event, request, settings) {
alert('Server request failed: ' + request.status + ' - ' + request.statusText);
$('#spinner').css('visibility', 'hidden');
});
update_field_states();
reset_columns();
});
var g_column_id = 0;
function reset_columns()
{
$("#columns > select").remove();
g_column_id = 0;
add_column();
}
function add_column()
{
var table = document.getElementById("sel_table").value;
$.getJSON("json.php?func=get_table_columns&table=" + table, function(json) {
var index = ++g_column_id;
var columns = document.getElementById("columns");
var select = document.createElement("select");
select.id = "column" + index;
select.name = "column[]";
select.onchange = update_columns;
var option = document.createElement("option");
option.value = "";
option.text = "<?php echo $GLOBALS['locImportExportColumnNone']?>";
select.options.add(option);
for (var i = 0; i < json.columns.length; i++)
{
var option = document.createElement("option");
option.value = json.columns[i].name;
option.text = json.columns[i].name;
select.options.add(option);
}
columns.appendChild(document.createTextNode(' '));
columns.appendChild(select);
});
}
function update_columns()
{
if (this.value == "" && $("#columns > select").size() > 1)
$(this).remove();
else if (this.id == "column" + g_column_id)
add_column();
}
function update_field_states()
{
var type = document.getElementById('format').value;
document.getElementById('field_delim').disabled = type != 'csv';
document.getElementById('enclosure_char').disabled = type != 'csv';
document.getElementById('row_delim').disabled = type != 'csv';
document.getElementById('child_rows').disabled = type == 'csv';
}
function add_all_columns()
{
var options = document.getElementById("column" + g_column_id).options;
$("#columns > select").remove();
g_column_id = 0;
var columns = document.getElementById("columns");
for (var i = 1; i < options.length; i++)
{
var index = ++g_column_id;
var select = document.createElement("select");
select.id = "column" + index;
select.name = "column[]";
select.onchange = update_columns;
var option = document.createElement("option");
for (var opt = 0; opt < options.length; opt++)
select.options.add(options[opt].cloneNode(true));
select.selectedIndex = i;
columns.appendChild(document.createTextNode(' '));
columns.appendChild(select);
}
}
</script>
<div class="form_container">
<h1><?php echo $GLOBALS['locExport']?></h1>
<span id="imessage" style="display: none"></span> <span id="spinner"
style="visibility: hidden"><img src="images/spinner.gif" alt=""></span>
<form id="export_form" name="export_form" method="GET">
<input type="hidden" name="func" value="system"> <input type="hidden"
name="operation" value="export">
<div class="medium_label"><?php echo $GLOBALS['locImportExportCharacterSet']?></div>
<div class="field">
<select id="charset" name="charset">
<option value="UTF-8">UTF-8</option>
<option value="ISO-8859-1">ISO-8859-1</option>
<option value="ISO-8859-15">ISO-8859-15</option>
<option value="Windows-1251">Windows-1251</option>
<option value="UTF-16">UTF-16</option>
<option value="UTF-16LE">UTF-16 LE</option>
<option value="UTF-16BE">UTF-16 BE</option>
</select>
</div>
<div class="medium_label"><?php echo $GLOBALS['locImportExportTable']?></div>
<div class="field">
<select id="sel_table" name="table" onchange="reset_columns()">
<option value="company"><?php echo $GLOBALS['locImportExportTableCompanies']?></option>
<option value="company_contact"><?php echo $GLOBALS['locImportExportTableCompanyContacts']?></option>
<option value="base"><?php echo $GLOBALS['locImportExportTableBases']?></option>
<option value="invoice"><?php echo $GLOBALS['locImportExportTableInvoices']?></option>
<option value="invoice_row"><?php echo $GLOBALS['locImportExportTableInvoiceRows']?></option>
<option value="product"><?php echo $GLOBALS['locImportExportTableProducts']?></option>
<option value="row_type"><?php echo $GLOBALS['locImportExportTableRowTypes']?></option>
<option value="invoice_state"><?php echo $GLOBALS['locImportExportTableInvoiceStates']?></option>
</select>
</div>
<div class="medium_label"><?php echo $GLOBALS['locImportExportFormat']?></div>
<div class="field">
<select id="format" name="format" onchange="update_field_states()">
<option value="csv">CSV</option>
<option value="xml">XML</option>
<option value="json">JSON</option>
</select>
</div>
<div class="medium_label"><?php echo $GLOBALS['locImportExportFieldDelimiter']?></div>
<div class="field">
<select id="field_delim" name="field_delim">
<?php
$field_delims = $this->importer->get_field_delims();
foreach ($field_delims as $key => $delim) {
echo "<option value=\"$key\">" . $delim['name'] . "</option>\n";
}
?>
</select>
</div>
<div class="medium_label"><?php echo $GLOBALS['locImportExportEnclosureCharacter']?></div>
<div class="field">
<select id="enclosure_char" name="enclosure_char">
<?php
$enclosure_chars = $this->importer->get_enclosure_chars();
foreach ($enclosure_chars as $key => $delim) {
echo "<option value=\"$key\">" . $delim['name'] . "</option>\n";
}
?>
</select>
</div>
<div class="medium_label"><?php echo $GLOBALS['locImportExportRowDelimiter']?></div>
<div class="field">
<select id="row_delim" name="row_delim">
<?php
$row_delims = $this->importer->get_row_delims();
foreach ($row_delims as $key => $delim) {
echo "<option value=\"$key\">" . $delim['name'] . "</option>\n";
}
?>
</select>
</div>
<div class="medium_label"><?php echo $GLOBALS['locExportIncludeChildRows']?></div>
<div class="field">
<input id="child_rows" name="child_rows" type="checkbox"
checked="checked">
</div>
<div class="medium_label"><?php echo $GLOBALS['locExportIncludeDeletedRecords']?></div>
<div class="field">
<input id="deleted" name="deleted" type="checkbox">
</div>
<div class="medium_label"><?php echo $GLOBALS['locExportColumns']?> <input
type="button"
value="<?php echo $GLOBALS['locExportAddAllColumns']?>"
onclick="add_all_columns()">
</div>
<div id="columns" class="field"></div>
<div class="form_buttons" style="clear: both">
<input type="submit" value="<?php echo $GLOBALS['locExportDo']?>">
</div>
</form>
</div>
<?php
}
protected function str_putcsv($data, $delimiter = ',', $enclosure = '"')
{
$fp = fopen('php://temp', 'r+');
fputcsv($fp, $data, $delimiter, $enclosure);
rewind($fp);
$data = '';
while (!feof($fp))
$data .= fread($fp, 1024);
fclose($fp);
return rtrim($data, "\n");
}
protected function output_str($str, $charset)
{
if ($charset && $charset != _CHARSET_) {
$str = iconv(_CHARSET_, $charset, $str);
// No need for BOM here, this is just a simple string
if (substr($str, 0, 2) == "\xFE\xFF" || substr($str, 0, 2) == "\xFF\xFE")
$str = substr($str, 2);
}
echo $str;
}
}