From ed13280ea10d932419615a0bb64199354b078e28 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Tue, 10 May 2022 12:15:52 +0200 Subject: [PATCH] fix bug in select boxes --- htdocs/imports/import.php | 163 ++++++++++++++++---------------- htdocs/langs/en_US/exports.lang | 1 + 2 files changed, 85 insertions(+), 79 deletions(-) diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index a8fa3013ea2..d4b7ad4e4b6 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -35,8 +35,6 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/import.lib.php'; // Load translation files required by the page $langs->loadLangs(array('exports', 'compta', 'errors')); -$selectimport = true; - // Security check $result = restrictedArea($user, 'import'); @@ -321,54 +319,6 @@ if ($step == 4 && $action == 'select_model') { $_SESSION["dol_array_match_file_to_database"] = $serialized_array_match_file_to_database; } } - -if ($action == 'saveorder') { - // Enregistrement de la position des champs - dol_syslog("boxorder=".GETPOST('boxorder')." datatoimport=".GETPOST("datatoimport"), LOG_DEBUG); - $part = explode(':', GETPOST('boxorder')); - $colonne = $part[0]; - $list = $part[1]; - dol_syslog('column='.$colonne.' list='.$list); - - // Init targets fields array - $fieldstarget = $objimport->array_import_fields[0]; - - // Reinit match arrays. We redefine array_match_file_to_database - $serialized_array_match_file_to_database = ''; - $array_match_file_to_database = array(); - $fieldsarray = explode(',', $list); - $pos = 0; - foreach ($fieldsarray as $fieldnb) { // For each elem in list. fieldnb start from 1 to ... - // Get name of database fields at position $pos and put it into $namefield - $posbis = 0; $namefield = ''; - foreach ($fieldstarget as $key => $val) { // key: val: - //dol_syslog('AjaxImport key='.$key.' val='.$val); - if ($posbis < $pos) { - $posbis++; - continue; - } - // We found the key of targets that is at position pos - $namefield = $key; - //dol_syslog('AjaxImport Field name found for file field nb '.$fieldnb.'='.$namefield); - - break; - } - - if ($fieldnb && $namefield) { - $array_match_file_to_database[$fieldnb] = $namefield; - if ($serialized_array_match_file_to_database) { - $serialized_array_match_file_to_database .= ','; - } - $serialized_array_match_file_to_database .= ($fieldnb.'='.$namefield); - } - - $pos++; - } - - // We save new matching in session - $_SESSION["dol_array_match_file_to_database"] = $serialized_array_match_file_to_database; - dol_syslog('dol_array_match_file_to_database='.$serialized_array_match_file_to_database); -} if ($action == 'saveselectorder') { // Enregistrement de la position des champs $serialized_array_match_file_to_database = ''; @@ -793,7 +743,7 @@ if ($step == 3 && $datatoimport) { // STEP 4: Page to make matching between source file and database fields -if ($step == 4 && $datatoimport && $selectimport) { +if ($step == 4 && $datatoimport) { $serialized_array_match_file_to_database = isset($_SESSION["dol_array_match_file_to_database_select"]) ? $_SESSION["dol_array_match_file_to_database_select"] : ''; $array_match_file_to_database = array(); $fieldsarray = explode(',', $serialized_array_match_file_to_database); @@ -885,7 +835,8 @@ if ($step == 4 && $datatoimport && $selectimport) { $array_match_database_to_file = array_flip($array_match_file_to_database); $fieldstarget_tmp = array(); - + $arraykeysfieldtarget = array_keys($fieldstarget); + $position = 0; foreach ($fieldstarget as $key => $label) { $isrequired = preg_match('/\*$/', $label); if (!empty($isrequired)) { @@ -896,9 +847,27 @@ if ($step == 4 && $datatoimport && $selectimport) { } if (!empty($array_match_database_to_file[$key])) { $fieldstarget_tmp[$key]["imported"] = true; + $fieldstarget_tmp[$key]["position"] = $array_match_database_to_file[$key]-1; + $keytoswap = $key; + while (!empty($array_match_database_to_file[$keytoswap])) { + if ($position+1 > $array_match_database_to_file[$keytoswap]) { + $keytoswapwith = $array_match_database_to_file[$keytoswap]-1; + $tmp = [$keytoswap=>$fieldstarget_tmp[$keytoswap]]; + unset($fieldstarget_tmp[$keytoswap]); + $fieldstarget_tmp = arrayInsert($fieldstarget_tmp, $keytoswapwith, $tmp); + $keytoswapwith = $arraykeysfieldtarget[$array_match_database_to_file[$keytoswap]-1]; + $tmp = $fieldstarget_tmp[$keytoswapwith]; + unset($fieldstarget_tmp[$keytoswapwith]); + $fieldstarget_tmp[$keytoswapwith] = $tmp; + $keytoswap = $keytoswapwith; + } else { + break; + } + } } else { $fieldstarget_tmp[$key]["imported"] = false; } + $position++; } $fieldstarget = $fieldstarget_tmp; @@ -1144,7 +1113,6 @@ if ($step == 4 && $datatoimport && $selectimport) { print ''; print $optionsnotused; print ''; - //print ajax_combobox("select_".$newlabel); print ""; print ''; $filecolumn = !empty($array_match_database_to_file[$code])?$array_match_database_to_file[$code]:0; @@ -1210,7 +1178,7 @@ if ($step == 4 && $datatoimport && $selectimport) { print ''; // List of not imported fields - print ''.$langs->trans("NotImportedFields").''; + print ''.$langs->trans("NotUsedFields").''; print ''; @@ -1261,15 +1229,42 @@ if ($step == 4 && $datatoimport && $selectimport) { print '$(".targetselectchange").focus(function(){'."\n"; print 'previousselectedvalueimport = $(this).val();'."\n"; print 'previousselectedlabelimport = $(this).children("option:selected").text();'."\n"; + print 'console.log(previousselectedvalueimport)'."\n"; print '})'."\n"; print '$(".targetselectchange").change(function(){'."\n"; print 'if(previousselectedlabelimport != "" && previousselectedvalueimport != -1){'."\n"; - print '$(".targetselectchange").not($( this )).append(new Option(previousselectedlabelimport,previousselectedvalueimport));'."\n"; + print '$(".targetselectchange").not($(this)).append(new Option(previousselectedlabelimport,previousselectedvalueimport));'."\n"; + print 'let valuetochange = $(this).val(); '."\n"; + print '$(".boxtdunused").each(function(){'."\n"; + print 'if ($(this).text().includes(valuetochange)){'."\n"; + print 'arraychild = $(this)[0].childNodes'."\n"; + print 'arraytexttomodify = arraychild[0].textContent.split(" ")'."\n"; + print 'arraytexttomodify[1] = previousselectedvalueimport '."\n"; + print 'textmodified = arraytexttomodify.join(" ") '."\n"; + print 'arraychild[0].textContent = textmodified'."\n"; + print 'arraychild[1].innerHTML = previousselectedlabelimport'."\n"; + print '}'."\n"; + print '})'."\n"; print '}'."\n"; print 'if($( this ).val() != -1){'."\n"; print '$(".targetselectchange").not($( this )).find(\'option[value="\'+$( this ).val()+\'"]\').remove();'."\n"; - print '$(".targetselectchange").not($( this )).find(\'option[value="\'+$( this ).val()+\'"]\').remove();'."\n"; print '}'."\n"; + print '$(this).blur()'."\n"; + print 'arrayselectedfields = [];'."\n"; + print 'arrayselectedfields.push("0");'."\n"; + print '$(".targetselectchange").each(function(){'."\n"; + print 'value = $(this).val()'."\n"; + print 'arrayselectedfields.push(value);'."\n"; + print '});'."\n"; + print '$.ajax({'."\n"; + print 'type: "POST",'."\n"; + print 'dataType: "json",'."\n"; + print 'url: "'.$_SERVER["PHP_SELF"].'?action=saveselectorder",'."\n"; + print 'data: "selectorder="+arrayselectedfields.toString(),'."\n"; + print 'success: function(){'."\n"; + print 'console.log("Select order saved");'."\n"; + print '},'."\n"; + print '});'."\n"; print '});'."\n"; print '})'."\n"; print ''."\n"; @@ -1282,31 +1277,10 @@ if ($step == 4 && $datatoimport && $selectimport) { if (count($array_match_file_to_database)) { if ($mandatoryfieldshavesource) { - print ''.$langs->trans("NextStep").''; + print ''.$langs->trans("NextStep").''; } else { print ''.$langs->trans("NextStep").''; } - if ($conf->use_javascript_ajax) { - print ''."\n"; - } } print ''; @@ -2238,7 +2212,11 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') // The image must have the class 'boxhandle' beause it's value used in DOM draggable objects to define the area used to catch the full object //print img_picto($langs->trans("MoveField", $pos), 'grip_title', 'class="boxhandle" style="cursor:move;"'); print ''; - print ''; + if (isset($fieldssource[$pos]['imported']) && $fieldssource[$pos]['imported'] == false) { + print ''; + } else { + print ''; + } print $langs->trans("Field").' '.$pos; if (empty($fieldssource[$pos]['example1'])) { $example = $fieldssource[$pos]['label']; @@ -2289,3 +2267,30 @@ function getnewkey(&$fieldssource, &$listofkey) $listofkey[$i] = 1; return $i; } +/** + * Return array with element inserted in it at position $position + * + * @param array $array Array of field source + * @param mixed $position key of postion to insert to + * @param array $insertArray Array to insert + * @return array + */ +function arrayInsert($array, $position, $insertArray) +{ + $ret = []; + + if ($position == count($array)) { + $ret = $array + $insertArray; + } else { + $i = 0; + foreach ($array as $key => $value) { + if ($position == $i++) { + $ret += $insertArray; + } + + $ret[$key] = $value; + } + } + + return $ret; +} diff --git a/htdocs/langs/en_US/exports.lang b/htdocs/langs/en_US/exports.lang index a20741472b7..44cb98d3679 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -136,3 +136,4 @@ NbUpdate=Number of updated lines: %s MultipleRecordFoundWithTheseFilters=Multiple records have been found with these filters: %s StocksWithBatch=Stocks and location (warehouse) of products with batch/serial number WarningFirstImportedLine=The first line(s) will not be imported with the current selection +NotUsedFields=Fields of database not used \ No newline at end of file