-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_exporter.php
More file actions
166 lines (144 loc) · 5.83 KB
/
file_exporter.php
File metadata and controls
166 lines (144 loc) · 5.83 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
<?php
/*
* Copyright (c) 2025 Bloxtor (http://bloxtor.com) and Joao Pinto (http://jplpinto.com)
*
* Multi-licensed: BSD 3-Clause | Apache 2.0 | GNU LGPL v3 | HLNC License (http://bloxtor.com/LICENSE_HLNC.md)
* Choose one license that best fits your needs.
*
* Original PHP DB Lib Repo: https://github.com/a19836/php-db-lib/
* Original Bloxtor Repo: https://github.com/a19836/bloxtor
*
* YOU ARE NOT AUTHORIZED TO MODIFY OR REMOVE ANY PART OF THIS NOTICE!
*/
include_once __DIR__ . "/config.php";
include_once get_lib("db.DBFileExporter");
$table_name = "a_product_x";
$export_type = "txt";
$output_type = "save"; //If output_type is "return" then return the created output. If output_type is "save" then save the output to the doc file pass as the send argument in the exportFile function. If output_type is empty or "print", then the browser will download the doc automatically and stop script right away.
//connect to DB
try {
if ($password) {
$DBDriver->connect();
}
else
throw new Exception("Please edit config.php file and define your DB credentials first!");
}
catch (Throwable $e) {
$error = $e->getMessage() . (!empty($e->problem) ? "<br/>" . $e->problem : "");
}
if (!empty($error))
$html = '<div class="error">' . $error . '</div>';
else if (!empty($_POST)) {
//create temp table if not exists yet
try {
$tables = $DBDriver->listTables();
//Create product table
$tn = DB::getStaticTableInNamesList($tables, $table_name);
if (!$tn) {
$drop_table = true;
$sql = $DBDriver->getCreateTableStatement(array(
"name" => $table_name,
"attributes" => array(
array("name" => "product_id", "type" => "bigint", "length" => 20, "primary_key" => true, "auto_increment" => true),
array("name" => "client_id", "type" => "bigint", "length" => 20, "unsigned" => true, "null" => true),
array("name" => "type_id", "type" => "bigint", "length" => 20, "unsigned" => true, "null" => true),
array("name" => "name", "type" => "varchar", "length" => 200, "default" => "", "null" => false),
array("name" => "description", "type" => "blob", "default" => "", "null" => false)
)
));
$status = $DBDriver->setSQL($sql);
//echo "sql: $sql<br/>status: $status<br/>";die();
//insert some dummy records to export
$attributes = array(
"client_id" => 10,
"type_id" => 2,
"name" => "onion",
"description" => "simple onion",
);
$status = $DBDriver->insertObject($table_name, $attributes);
//echo "sql: $sql<br/>status: $status<br/>";die();
$attributes["name"] = $attributes["description"] = "carrot";
$status = $DBDriver->insertObject($table_name, $attributes);
//echo "sql: $sql<br/>status: $status<br/>";die();
$attributes["name"] = $attributes["description"] = "potato";
$status = $DBDriver->insertObject($table_name, $attributes);
//echo "sql: $sql<br/>status: $status<br/>";die();
}
}
catch (Throwable $e) {
$error = $e->getMessage() . (!empty($e->problem) ? "<br/>" . $e->problem : "");
}
if (!empty($error))
$html = '<div class="error">' . $error . '</div>';
else {
$export_type = !empty($_POST["export_type"]) ? $_POST["export_type"] : "txt";
$DBFileExporter = new DBFileExporter($DBDriver);
$DBFileExporter->setOptions(array( //all the following options are optional:
"export_type" => $export_type, //default is txt
));
$file_path = __DIR__ . "/products_from_export.xls";
$sql = $DBDriver->buildTableFindSQL($table_name);
$status = true;
try {
$status = $DBFileExporter->exportFile($sql, $file_path, $output_type);
}
catch (Exception $e) {
$status = false;
}
if ($status)
$status = "File dumped successfully from DB!";
else {
$errors = $DBFileExporter->getErrors();
$status = 'Error: File not exported!';
if ($errors) {
$func = function($v) { return nl2br($v, false); };
$status .= "\n\nErrors:\n- " . implode('\n- ', array_map($func, $errors));
}
}
$file_contents = file_exists($file_path) ? file_get_contents($file_path) : 'File Does NOT exists!';
$html = "<h5>Export '" . basename($file_path) . "' file from '$table_name' table</h5>";
$html .= "<div class=\"code sql one-line\"><textarea readonly>$sql</textarea></div>
<div class=\"code output\"><textarea readonly>$status\n\nFile contents:\n$file_contents</textarea></div>";
//drop created temp table
if (!empty($drop_table)) {
$sql = $DBDriver->getDropTableStatement($table_name);
$status = $DBDriver->setSQL($sql);
//echo "sql: $sql<br/>status: $status<br/>";die();
}
}
}
echo $style;
echo '<style>
.code.sql:before {content:"sql used to export";}
</style>';
echo "<h1>DB File Exporter</h1>
<p>Export data from DB</p>";
echo '<form method="POST">
<h4>Choose a DB driver:
<select onChange="this.form.setAttribute(\'action\', \'?type=\' + this.value);">';
$types = DB::getAllDriverLabelsByType();
foreach ($types as $id => $label)
echo "<option value='$id'" . ($id == $type ? " selected" : "") . ">$label</option>";
echo '</select>
</h4>
<div class="note">
<span>
The system will create a temp table, fill it with some dummy data and then export that data into the "products_from_export.xls" file.<br/>
At the end will drop/remove the temp table from your DB.<br/>
<br/>
The exported file can be a tab delimiter or csv file according with your selection below.<br/>
If you wish to download the exported file automatically, change the $output_type var to "print".
</span>
</div>
<div style="text-align:center;">
<select name="export_type">
<option value="txt"' . ($export_type == "txt" ? " selected" : "") . '>Tab Delimiter</option>
<option value="csv"' . ($export_type == "csv" ? " selected" : "") . '>CSV</option>
<option value="xls"' . ($export_type == "xls" ? " selected" : "") . '>XLS</option>
</select>
<input type="submit" name="export" value="Export to File">
</div>
</form>';
if (!empty($html))
echo $html;
?>