-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importexport.php
283 lines (246 loc) · 9.81 KB
/
importexport.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
<?php
/* Copyright (C) 2001-2005 Rodolphe Quiedeville <[email protected]>
* Copyright (C) 2004-2015 Laurent Destailleur <[email protected]>
* Copyright (C) 2005-2012 Regis Houssin <[email protected]>
* Copyright (C) 2015 Jean-François Ferry <[email protected]>
* Copyright (C) 2021-2022 John Livingston <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file pickup/pickupindex.php
* \ingroup pickup
* \brief Home page of pickup top menu
*/
// Load Dolibarr environment
$res=0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res=@include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
$tmp=empty($_SERVER['SCRIPT_FILENAME'])?'':$_SERVER['SCRIPT_FILENAME'];$tmp2=realpath(__FILE__); $i=strlen($tmp)-1; $j=strlen($tmp2)-1;
while($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i]==$tmp2[$j]) { $i--; $j--; }
if (! $res && $i > 0 && file_exists(substr($tmp, 0, ($i+1))."/main.inc.php")) $res=@include substr($tmp, 0, ($i+1))."/main.inc.php";
if (! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php")) $res=@include dirname(substr($tmp, 0, ($i+1)))."/main.inc.php";
// Try main.inc.php using relative path
if (! $res && file_exists("../main.inc.php")) $res=@include "../main.inc.php";
if (! $res && file_exists("../../main.inc.php")) $res=@include "../../main.inc.php";
if (! $res && file_exists("../../../main.inc.php")) $res=@include "../../../main.inc.php";
if (! $res) die("Include of main fails");
// Load translation files required by the page
$langs->loadLangs(array('pickup@pickup', 'products', 'categories', 'stocks'));
dol_include_once('/core/lib/files.lib.php');
dol_include_once('/pickup/lib/export.lib.php');
dol_include_once('/pickup/lib/import.lib.php');
// Security check
if ($user->socid) {
$socid = $user->socid;
}
if (empty($conf->pickup->enabled)) {
accessforbidden('Module not enabled');
}
if ($user->societe_id > 0) { // Protection if external user
accessforbidden();
}
if (!$user->rights->pickup->importexport) {
accessforbidden();
}
$socid=GETPOST('socid', 'int');
if (isset($user->societe_id) && $user->societe_id > 0)
{
$socid = $user->societe_id;
}
$conf_importexport_what = [
'cat' => [
'label' => $langs->transnoentities('ProductsCategoryShort'),
'default_checked' => true
]
];
if (!empty($conf->global->PICKUP_IMPORTEXPORT_ALL)) {
$conf_importexport_what = array_merge($conf_importexport_what,
[
'pickup_conf' => [
'label' => $langs->transnoentities('PickupSetup')
],
'entrepot' => [
'label' => $langs->transnoentities('Warehouses')
],
'societe' => [
'label' => $langs->transnoentities('Clients')
],
'product' => [
'label' => $langs->transnoentities('Products')
],
'pickup' => [
'label' => $langs->transnoentities('Pickups'),
'export' => false
]
]
);
}
$action = GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : '';
$importexport_what = [];
$param_importexport_what = GETPOST('importexport_what', 'array');
foreach ($conf_importexport_what as $what => $wc) {
if ($action === 'export') {
if (array_key_exists('export', $wc) && !$wc['export']) { continue; }
} elseif ($action === 'import' || $action === 'doimport') {
if (array_key_exists('import', $wc) && !$wc['import']) { continue; }
}
if (in_array($what, $param_importexport_what)) {
$importexport_what[$what] = true;
}
}
unset($param_importexport_what);
if ($action === 'do_import' && empty($_SESSION['pickup_import_data'])) {
$action = '';
}
/*
* Actions
*/
if ($action === 'export') {
print_pickup_export($importexport_what);
exit;
}
$import_result = null;
if ($action === 'import') {
if (!empty($_FILES) && !empty($_FILES['userfile']) && !empty($_FILES['userfile']['tmp_name'])) {
$json = file_get_contents($_FILES['userfile']['tmp_name']);
dol_delete_file($_FILES['userfile']['tmp_name']);
$_SESSION['pickup_import_data'] = [
'json' => $json,
'what' => $importexport_what
];
$import_result = pickup_import($json, true, $importexport_what);
} else {
$action = '';
}
} else if ($action === 'do_import') {
$import_result = pickup_import(
$_SESSION['pickup_import_data']['json'],
false,
$_SESSION['pickup_import_data']['what']
);
unset($_SESSION['pickup_import_data']);
$_SESSION['pickup_import_result'] = $import_result;
header('Location: '.$_SERVER["PHP_SELF"]);
exit;
} else {
unset($_SESSION['pickup_import_data']);
}
if ($action == '' && empty($import_result) && !empty($_SESSION['pickup_import_result'])) {
$import_result = $_SESSION['pickup_import_result'];
unset($_SESSION['pickup_import_result']);
}
/*
* View
*/
llxHeader("", $langs->trans("PickupImportExportTitle"));
print load_fiche_titre($langs->trans("PickupImportExportTitle"), '', 'pickup.png@pickup');
print '<div class="info">' . $langs->trans('PickupImportExportHelp') . '</div>';
if (!empty($import_result)) {
print '<table class="valid centpercent">';
print ' <tr class="valid">';
print ' <th class="valid">OBJECT TYPE</th>';
print ' <th class="valid">OBJECT</th>';
print ' <th class="valid">ACTION</th>';
print ' <th class="valid">MESSAGE</th>';
print ' </tr>';
foreach ($import_result['actions'] as $ac) {
print ' <tr class="valid">';
foreach (['object_type', 'object', 'action', 'message'] as $field) {
print ' <td class="valid">';
if (!empty($ac[$field])) { print htmlspecialchars($ac[$field]); }
print ' </td>';
}
print ' </tr>';
}
print ' <tr class="valid">';
print ' <td class="valid" colspan="4">';
if ($import_result['status'] === 'ok') {
print "OK";
} else {
print "ERROR: ".($import_result['error'] ?? ' Unknown error');
}
print ' </td>';
print ' </tr>';
if ($action === 'import') {
print '<tr class="valid">';
print '<td class="valid center" colspan="4">';
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">'."\n";
print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
print '<input type="hidden" name="action" value="">'."\n";
print '<a class="butActionDelete" ';
print ' onclick="$(this).closest(\'form\').find(\'[name=action]\').val(\'\'); $(this).closest(\'form\').submit();" ';
print '>'.$langs->trans('Cancel').'</a>';
// Note: type="button" to prevent this button to be trigger on enter in a field from the form under it.
print '<input type="button" class="button" value="'.$langs->trans("Validate").'" ';
print ' onclick="$(this).closest(\'form\').find(\'input[name=action]\').val(\'do_import\'); $(this).closest(\'form\').submit();" ';
print '>';
print '</form>';
print '</td>';
print '</tr>';
}
print '</table>';
}
if ($action !== 'import' && $action !== 'doimport') {
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">'."\n";
print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
print '<input type="hidden" name="action" value="export">'."\n";
print ' <table class="border tableforfield centpercent">'."\n";
print ' <tr>';
print ' <td width="110">'.$langs->trans('PickupExport').'</td>';
print ' <td>';
foreach ($conf_importexport_what as $what => $wc) {
if (array_key_exists('export', $wc) && !$wc['export']) { continue; }
print ' <label>';
print ' <input type="checkbox" '.($wc['default_checked'] ? 'checked="checked"' : '').' name="importexport_what[]" value="'.htmlspecialchars($what).'">';
print ' ' . htmlspecialchars($wc['label']);
print ' </label>';
print ' </br>';
}
print ' </td>';
print ' </tr>'."\n";
print ' </table/>';
print ' <div class="center"><input type="submit" class="button" name="submit" value="'.$langs->trans("PickupExport").'"></div>';
print '</form>';
print '<form method="POST" enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"].'">'."\n";
print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
print '<input type="hidden" name="action" value="import">'."\n";
print ' <table class="border tableforfield centpercent">'."\n";
print ' <tr>';
print ' <td width="110">'.$langs->trans('PickupImport').'</td>';
print ' <td>';
foreach ($conf_importexport_what as $what => $wc) {
if (array_key_exists('import', $wc) && !$wc['import']) { continue; }
print ' <label>';
print ' <input type="checkbox" '.($wc['default_checked'] ? 'checked="checked"' : '').' name="importexport_what[]" value="'.htmlspecialchars($what).'">';
print ' ' . htmlspecialchars($wc['label']);
print ' </label>';
print ' </br>';
}
print ' </td>';
print ' </tr>'."\n";
print ' <tr>';
print ' <td></td>';
print ' <td>';
print ' <input type="file" name="userfile" accept="application/json">';
print ' </td>';
print ' </tr>'."\n";
print ' </table/>';
print ' <div class="center"><input type="submit" class="button" name="submit" value="'.$langs->trans("PickupImport").'"></div>';
print '</form>';
}
// End of page
llxFooter();
$db->close();