Skip to content

Commit

Permalink
added merge/overwrite function to csv upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoBoer authored and torinfo committed Apr 28, 2023
1 parent 01c0101 commit 96ec088
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
4 changes: 4 additions & 0 deletions editor/css/complex.css
Expand Up @@ -697,6 +697,10 @@ select, input, textarea
flex-grow: 1;
}

#mainPanel input[name=merge]{
float: right;
margin-bottom: 20px;
}
#mainPanel select.deprecated {
width: 95%;
}
Expand Down
19 changes: 16 additions & 3 deletions editor/js/toolbox.js
Expand Up @@ -4660,16 +4660,29 @@ var EDITOR = (function ($, parent) {
excel_form.append('<input type="hidden" name="colNum" value=' + options.columns + '>');
excel_form.append('<input type="hidden" name="type" value=' + name + '>');
excel_form.append('<input type="hidden" name="gridId" value=' + id + '>');
html.append(excel_form);


html.append(excel_form);
var checkbox_id = "csv_merge_" + name;
html.append(language.UploadCSV.mergeOld.$label + '<input type="checkbox" name="merge" value="Merge" id =' + checkbox_id + '>');
//called if user has uploaded a file to populate a grid
html.find('#excel_upload_' + name).submit(function (e){
e.preventDefault();
upload_file(new FormData(this));
var grid_id = '#' + id + '_jqgrid';
var current_grid_data = JSON.stringify($(grid_id).jqGrid("getRowData"))
var form_data = new FormData(this);
if ($('#csv_merge_categoryInfo').is(":checked")) {
form_data.append("merge", "Merge");
}
form_data.append('old_data', current_grid_data)
upload_file(form_data);
})

function upload_file(form_data){
if(confirm(language.UploadCSV.Info.$label)) {
var conf = false;
$('#csv_merge_categoryInfo').is(":checked") ? conf = confirm(language.UploadCSV.Info2.$label) : conf = confirm(language.UploadCSV.Info.$label);

if(conf) {
$.ajax({
type: 'POST',
dataType: 'text',
Expand Down
46 changes: 32 additions & 14 deletions editor/upload_file_to_jqgrid_template.php
Expand Up @@ -19,32 +19,50 @@
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
$merge = isset($_POST['merge']);
$file = $_FILES['fileToUpload'];
$file_content = file_get_contents($file['tmp_name']);
$csv = array_map('str_getcsv', file($file['tmp_name']));
$csv_parsed = '';
$result = '';
$nr_columns = $_POST['colNum'];

//if $merge is set then we want to keep the old grid
if ($merge){
$old_grid = json_decode($_POST['old_data'], true);
//drop row indicator
foreach ($old_grid as $key => $row){
array_shift($old_grid[$key]);
}
$result = parse_data($result, $old_grid, $nr_columns);

}
$result = parse_data($result, $csv, $nr_columns);
$result = substr($result, 0 , -2);
}
echo json_encode(array('type' => $_POST["type"], 'csv' => $result, 'gridId' => $_POST["gridId"] ));

function parse_data($csv_parsed, $input, $nr_columns)
{
//check if supplied file has correct size, if not drop or add cells and add to csv_parced
foreach ($csv as $key => $row) {
if(count($row) < $nr_columns){
foreach ($input as $key => $row) {
if (count($row) < $nr_columns) {
for ($i = 0; $i < $nr_columns - count($row); $i++) {
$csv[$key][] = " ";
$input[$key][] = " ";
}
$row = $csv[$key];
}
elseif(count($row) > $nr_columns) {
$row = $input[$key];
} elseif (count($row) > $nr_columns) {
for ($i = 0; $i < count($row) - $nr_columns; $i++) {
array_pop($csv[$key]);
array_pop($input[$key]);
}
$row = $csv[$key];
$row = $input[$key];
}
foreach ($row as $value){
if ($value === ""){$value = " ";}
$csv_parsed .= $value. "|";
foreach ($row as $value) {
if ($value === "") {
$value = " ";
}
$csv_parsed .= $value . "|";
}
$csv_parsed .= "|";
}
$csv_parsed = substr($csv_parsed, 0 , -2);
return $csv_parsed;
}
echo json_encode(array('type' => $_POST["type"], 'csv' => $csv_parsed, 'gridId' => $_POST["gridId"] ));
2 changes: 2 additions & 0 deletions languages/wizard_en-GB.xml
Expand Up @@ -241,5 +241,7 @@
<UploadCSV>
<UploadCSVBtn label = "Upload csv"/>
<Info label = "Warning: This will overwrite the current table, are you sure?"> </Info>
<Info2 label = "Warning: This will add the new table to the current table, are you sure? Please note that there might be duplicate entries after merge."> </Info2>
<mergeOld label = "Keep the old table when importing a csv file:"/>
</UploadCSV>>
</language>

0 comments on commit 96ec088

Please sign in to comment.