/
delete_multiple.php
446 lines (388 loc) · 18.7 KB
/
delete_multiple.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
438
439
440
441
442
443
444
445
446
<?php
/*
open enventory is a registered trademark of Felix Rudolphi and Lukas Goossen. Usage of the name "open enventory" or the logo requires prior written permission of the trademark holders.
This file is part of open enventory.
open enventory 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.
open enventory 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 open enventory. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Khoi: added function to be able to delete multiple chemical containers
*/
// import neu
require_once "lib_global_funcs.php";
require_once "lib_simple_forms.php";
require_once "lib_formatting.php";
require_once "lib_db_manip.php";
pageHeader();
require_once "lib_import.php";
include "SimpleXLSX.php"; // Khoi: for handling Excel file.
include "SimpleXLS.php"; // Khoi: for handling Excel file.
// admins only
if ($permissions & _admin) {
echo stylesheet.
style.
getFixedStyleBlock().
_style.
loadJS(array("client_cache.js","controls.js","forms.js","searchpk.js","subitemlist_helper.js","subitemlist_get.js","subitemlist.js","edit.js"),"lib/").
script."
readOnly=false;
editMode=false;
activateSearch(false);
"._script."
</head>
<body>";
showCommFrame();
$cols_chemical_storage=array(
"chemical_storage_barcode",
);
echo getHelperTop().
"<div id=\"browsenav\">".
getAlignTable(
array(" <table class=\"noborder\">
<tbody><tr><td>
<a href=\"Javascript:void submitForm("main");\" onClick=\"return confirmDelete();\" class=\"imgButtonSm\">
<img src=\"lib/save_sm.png\" border=\"0\"".getTooltip("save_changes").">
</a>
</td></tr></tbody>
</table>"),
array("<h1>".s("delete_multiple")."</h1>")
).
"</div>
<div id=\"browsemain\">
<form name=\"main\" id=\"main\" method=\"POST\" enctype=\"multipart/form-data\" ;><span id=\"temp\" style=\"display:none\"></span>".
getHiddenSubmit();
if (empty($_REQUEST["table"])) {
$_REQUEST["table"]="chemical_storage";
}
$for_chemical_storage=($_REQUEST["table"]=="chemical_storage");
$trimchars=" \t\n\r\0\x0B\"";
switch ($_REQUEST["desired_action"]) {
case "delete_multiple":
// Get the uploaded file:
$importedFile = $_REQUEST["import_file_upload"];
// Get the uploaded file extension:
$extension = $_REQUEST['file_extension'];
// echo("Uploaded file extension is: $extension <br>");
// https://stackoverflow.com/a/53962466/6596203
if ($extension == 'xlsx') {
$uploadedFile = SimpleXLSX::parse($importedFile);
} elseif ($extension == 'xls') {
$uploadedFile = SimpleXLS::parse($importedFile);
} else {
$handle=fopen($importedFile,"r");
}
// Khoi explain: result array
$zeilen=array();
// read file
if ($uploadedFile) {
foreach ($uploadedFile->rows() as $line => $cells) {
if ($line >= $_REQUEST["skip_lines"]) {
$zeilen[]=$cells;
}
}
}
elseif ($handle) {
// number_lines_preview (simple html table)
// Khoi: get file delimiters based on content of the file
$delimiter = getFileDelimiter($file=$importedFile, $chechkLines=10, $startLine=$_REQUEST["skip_lines"]);
// print_r("Import file delimiter is: '$delimiter'"); echo "<br>";
$line = -1;
while (!feof($handle)) {
$buffer = fgets($handle, 16384);
$line++;
if ($line >= $_REQUEST["skip_lines"]) {
// Khoi: using str_getcsv() because it is superior to explode()
// Ref: https://stackoverflow.com/questions/15444358/what-is-the-advantage-of-using-str-getcsv
// if ($delimiter) { //Not needed anymore because it has been checked in 'case "load_file"'
$zeilen[]=str_getcsv($buffer, $delimiter);
// }
}
}
fclose($handle);
}
// echo '<pre>'; var_dump($zeilen); echo '</pre>'; die();
// Check if result ($zeilen) exists and import line by line from result
if ($zeilen) {
$start = \microtime(true);
$a = 1;
foreach ($zeilen as $row) {
$chemical_storage=array();
$cells=$row;
for ($b=0;$b<count($cells);$b++) {
$cells[$b]=trim(autodecode($cells[$b]),$trimchars);
}
if ($for_chemical_storage) {
$input["chemical_storage_barcode"] = rtrim(getValue("chemical_storage_barcode",$cells)); // Khoi: fixed so that if this column is the last column in the text file, it will not add whitespace or \n character
}
echo "<br>".ucfirst(s("line"))." ".($_REQUEST["skip_lines"]+$a).": ".$input["chemical_storage_barcode"]."<br>";
flush();
ob_flush();
// Find chemical_storage_id
$chemical_storage["chemical_storage_id"] = getChemicalStorageFromOwnDB($input["chemical_storage_barcode"]);
if (!$chemical_storage["chemical_storage_id"]) { // Khoi: check if the chemical_storage does not exist by chemical_storage_barcode
echo "Warning: ".$input["chemical_storage_barcode"]." does NOT exist in this database.<br>";
$a++;
continue;
}
// Set up variables and delete the chemical_storage
if ($chemical_storage["chemical_storage_id"]) {
global $db;
$_REQUEST["db_id"] = -1; // only in your own database
$db_id = intval($_REQUEST["db_id"]);
$baseTable = $_REQUEST["table"];
$_REQUEST["pk"] = $chemical_storage["chemical_storage_id"];
// open db connection if necessary
if ($db_id>0) {
$dbObj=getForeignDbObjFromDBid($db_id);
if (!$dbObj) {
return array(FAILURE,s("error_no_access"));
}
}
else {
$dbObj=$db;
}
list($success,$message,$pks_added)=performDel($baseTable,$db_id,$dbObj);
echo $message;
}
echo "<br>";
$a++;
};
print '<br>Finished deleting.<br> Took ' . (\microtime(true) - $start) . ' seconds.' . \PHP_EOL;
// clean up
@unlink($_REQUEST["import_file_upload"]);
} else {
echo s("file_not_found");
}
break;
// Khoi explain: after clicking on the green checkmark to Upload File
case "load_file":
// file there?
if (count($_FILES["import_file_upload"]) && $_FILES["import_file_upload"]["error"]==0) {
$tmpdir=oe_get_temp_dir();
$tmpname=oe_tempnam($tmpdir,"oe");
@unlink($tmpname);
rename($_FILES["import_file_upload"]["tmp_name"],$tmpname);
@chmod($tmpname,0755);
// Khoi: Get the uploaded file extension:
$extension = pathinfo($_FILES['import_file_upload']['name'], PATHINFO_EXTENSION);
echo("Uploaded file extension is: $extension <br>");
// https://stackoverflow.com/a/53962466/6596203
if ($extension == 'xlsx') {
$uploadedFile = SimpleXLSX::parse($tmpname);
} elseif ($extension == 'xls') {
$uploadedFile = SimpleXLS::parse($tmpname);
} else {
$handle=fopen($tmpname,"r");
}
if ($uploadedFile || $handle) {
$line=-1;
$preview=array();
$line_sizes=array();
$max_cells=0;
if ($uploadedFile) {
/* Ref for excel file handling: https://github.com/shuchkin/simplexlsx */
// echo $uploadedFile->toHTML(); // for debugging
foreach ($uploadedFile->rows() as $line => $cells) {
$size=count($cells);
$max_cells=max($max_cells,$size);
$line_sizes[]=$size;
if ($line>=$_REQUEST["skip_lines"] && count($preview) < $_REQUEST["number_lines_preview"]) {
for ($b=0;$b<count($cells);$b++) {
$cells[$b]=trim(autodecode($cells[$b]),$trimchars);
}
$preview[]=$cells;
// break;
}
// go to the end to check for mismatching line sizes
}
}
elseif ($handle){
// Khoi: get file info (such as extension) to parse info correct (e.g. csv vs tsv))
$delimiter = getFileDelimiter($file=$tmpname, $chechkLines=10, $startLine=$_REQUEST["skip_lines"]);
// echo "Import file delimiter is: ".stripslashes($delimiter)."<br>";
while (!feof($handle)) {
$buffer=fgets($handle,16384);
$line++;
// Khoi: using str_getcsv() because it is superior to explode()
// Ref: https://stackoverflow.com/questions/15444358/what-is-the-advantage-of-using-str-getcsv
if ($delimiter) {
$cells=str_getcsv($buffer, $delimiter);
}
$size=count($cells);
$max_cells=max($max_cells,$size);
$line_sizes[]=$size;
if ($line>=$_REQUEST["skip_lines"] && count($preview)<$_REQUEST["number_lines_preview"]) {
for ($b=0;$b<count($cells);$b++) {
$cells[$b]=trim(autodecode($cells[$b]),$trimchars);
}
$preview[]=$cells;
}
// go to the end to check for mismatching line sizes
}
fclose ($handle);
// ~ var_dump($preview);die();
}
else {
echo 'xlsx error: '.$uploadedFile->error();
}
if ($max_cells==0) {
// die(s("must_be_tab_sep"));
die(s("must_be_txt"));
}
$error_lines=array();
for ($a=0;$a<count($line_sizes);$a++) { // leave heading alone
if ($line_sizes[$a]!=$max_cells) {
$error_lines[]=array($a+1,$line_sizes[$a]);
}
}
if (count($error_lines)) {
echo s("error_line_size1").getTable($error_lines,array(s("line"),s("number_columns"))).s("error_line_size2").$max_cells.s("error_line_size3");
}
// prepare select prototype
$cell_texts=array();
for ($a=0;$a<$max_cells;$a++) {
$cell_texts[]=s("column")." ".numToLett($a+1);
}
$select_proto=array(
"item" => "select",
"allowNone" => true,
"int_names" => range(0,$max_cells-1),
"texts" => $cell_texts,
);
$fieldsArray=array(
array("item" => "hidden", "int_name" => "table", "value" => $_REQUEST["table"], ),
array("item" => "hidden", "int_name" => "desired_action", "value" => "delete_multiple", ),
array("item" => "hidden", "int_name" => "import_file_upload", "value" => $tmpname, ),
array("item" => "hidden", "int_name" => "file_extension", "value" => $extension, ), // Khoi: pass on the extension variable
array("item" => "text", "text" => "<table><tbody><tr><td>", ),
"tableStart",
// headings
array("item" => "input", "int_name" => "skip_lines", "size" => 10, "maxlength" => 6, "value" => $_REQUEST["skip_lines"], ),
);
if ($for_chemical_storage) {
$cols = $cols_chemical_storage;
}
$idx=0;
foreach ($cols as $col) {
if ($idx%10==0) {
if ($idx>0) {
$fieldsArray[]="tableEnd";
$fieldsArray[]=array("item" => "text", "text" => "</td><td>", );
$fieldsArray[]="tableStart";
}
$fieldsArray[]=array("item" => "text", "text" => "<tr><td><b>".s("property")."</b></td><td><b>".s("column")."</b></td></tr>", );
}
$select_proto["text"]=s($col);
$select_proto["int_name"]="col_".$col;
$select_proto["value"]=$guessed_cols[$col];
$fieldsArray[]=$select_proto;
$idx++;
}
$fieldsArray[]="tableEnd";
$fieldsArray[]=array("item" => "text", "text" => "</td></tr></tbody></table>", );
echo getFormElements(
array(
READONLY => false,
"noFieldSet" => true,
),
$fieldsArray
);
// build table of sample data
//~ var_dump($preview);die();
echo s("number_lines").": ".count($line_sizes)."<br>".getTable($preview,$cell_texts);
}
}
// Asking user to confirm before import for deletion
echo "
<script>
function confirmDelete() {
if (confirm('Do you really want to delete uploaded containers?')) {
return true;
} else {
// if user picks no, return to delete_multiple.php page
self.location = 'delete_multiple.php';
return false;
}
}
</script>
";
break;
default:
echo getFormElements(
array(
READONLY => false,
"noFieldSet" => true,
),
array(
array("item" => "hidden", "int_name" => "desired_action", "value" => "load_file", ),
"tableStart",
// Khoi: adding function to upload storage location and its barcode
array("item" => "select", "int_name" => "table", "int_names" => array("chemical_storage", ), ),
array("item" => "input", "int_name" => "import_file_upload", "type" => "file", ),
array("item" => "input", "int_name" => "number_lines_preview", "size" => 10, "maxlength" => 6, "value" => 10, ),
array("item" => "input", "int_name" => "skip_lines", "size" => 10, "maxlength" => 6, "value" => 1, ),
"tableEnd",
));
echo <<<EOL
<br>
<h1 style='color:red'>WARNING: This function is used to delete multiple chemical containers, only use it if you are absolutely sure!</h1>
<h2>Instructions:</h2>
<ul>
<li>
To delete chemical container, please import a text file (.txt) with one column containing
chemical container barcodes on each row.
</li>
<br>
<li>
Example:
</li>
<p>
<table id="to-be-deleted">
<tr>
<th>Container barcode to be deleted/disposed</th>
</tr>
<tr>
<td>12345678</td>
</tr>
<tr>
<td>24681357</td>
</tr>
<tr>
<td>...</td>
</tr>
</table>
</p>
<br>
</ul>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* For example table */
table#to-be-deleted, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
text-align: left;
}
table#to-be-deleted th {
background-color: #dddddd;
}
</style>
</head>
EOL;
}
echo "</form></div>";
echo getHelperBottom();
echo " </body>
</html>";
completeDoc();
}
?>