diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index 01f152f1ea5..15d83cfe8e7 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -136,7 +136,7 @@ $step = (GETPOST('step') ? GETPOST('step') : 1); $import_name = GETPOST('import_name'); $hexa = GETPOST('hexa'); $importmodelid = GETPOST('importmodelid'); -$excludefirstline = (GETPOST('excludefirstline') ? GETPOST('excludefirstline') : 1); +$excludefirstline = (GETPOST('excludefirstline') ? GETPOST('excludefirstline') : 2); $endatlinenb = (GETPOST('endatlinenb') ? GETPOST('endatlinenb') : ''); $updatekeys = (GETPOST('updatekeys', 'array') ? GETPOST('updatekeys', 'array') : array()); $separator = (GETPOST('separator', 'nohtml') ? GETPOST('separator', 'nohtml') : (!empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ? $conf->global->IMPORT_CSV_SEPARATOR_TO_USE : ',')); @@ -165,6 +165,19 @@ foreach ($fieldsarray as $elem) { } } +if (empty($array_match_file_to_database)) { + $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); + foreach ($fieldsarray as $elem) { + $tabelem = explode('=', $elem, 2); + $key = $tabelem[0]; + $val = (isset($tabelem[1]) ? $tabelem[1] : ''); + if ($key && $val) { + $array_match_file_to_database[$key] = $val; + } + } +} /* * Actions @@ -306,58 +319,25 @@ if ($step == 4 && $action == 'select_model') { $_SESSION["dol_array_match_file_to_database"] = $serialized_array_match_file_to_database; } } - -if ($action == 'saveorder') { +if ($action == 'saveselectorder') { // 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++; + dol_syslog("selectorder=".GETPOST('selectorder'), LOG_DEBUG); + $selectorder = explode(",", GETPOST('selectorder')); + $fieldtarget = $fieldstarget = $objimport->array_import_fields[0]; + foreach ($selectorder as $key => $code) { + $serialized_array_match_file_to_database .= $key.'='.$code; + $serialized_array_match_file_to_database .= ','; } - - // 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); + $serialized_array_match_file_to_database = substr($serialized_array_match_file_to_database, 0, -1); + dol_syslog('dol_array_match_file_to_database_select='.$serialized_array_match_file_to_database); + $_SESSION["dol_array_match_file_to_database_select"] = $serialized_array_match_file_to_database; + echo "{}"; + exit(0); } - /* * View */ @@ -764,6 +744,17 @@ if ($step == 3 && $datatoimport) { // STEP 4: Page to make matching between source file and database fields 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); + foreach ($fieldsarray as $elem) { + $tabelem = explode('=', $elem, 2); + $key = $tabelem[0]; + $val = (isset($tabelem[1]) ? $tabelem[1] : ''); + if ($key && $val) { + $array_match_file_to_database[$key] = $val; + } + } $model = $format; $list = $objmodelimport->liste_modeles($db); @@ -800,17 +791,17 @@ if ($step == 4 && $datatoimport) { // Put into array fieldssource starting with 1. $i = 1; foreach ($arrayrecord as $key => $val) { - $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24); - $i++; + if ($val["type"] != -1) { + $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24); + $i++; + } } $obj->import_close_file(); } // Load targets fields in database $fieldstarget = $objimport->array_import_fields[0]; - - $maxpos = max(count($fieldssource), count($fieldstarget)); - + $minpos = min(count($fieldssource), count($fieldstarget)); //var_dump($array_match_file_to_database); // Is it a first time in page (if yes, we must initialize array_match_file_to_database) @@ -841,10 +832,47 @@ if ($step == 4 && $datatoimport) { $pos++; } // Save the match array in session. We now will use the array in session. - $_SESSION["dol_array_match_file_to_database"] = $serialized_array_match_file_to_database; + $_SESSION["dol_array_match_file_to_database_select"] = $serialized_array_match_file_to_database; } $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)) { + $newlabel = substr($label, 0, -1); + $fieldstarget_tmp[$key] = array("label"=>$newlabel,"required"=>true); + } else { + $fieldstarget_tmp[$key] = array("label"=>$label,"required"=>false); + } + 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; + //print $serialized_array_match_file_to_database; //print $_SESSION["dol_array_match_file_to_database"]; //var_dump($array_match_file_to_database);exit; @@ -968,7 +996,7 @@ if ($step == 4 && $datatoimport) { print '
| =>'.img_object('', $entityicon).' '.$langs->trans($entitylang).' | '; print ''; - $newlabel = preg_replace('/\*$/', '', $label); - $text = $langs->trans($newlabel); + print ' | '; - // Info field + print ''; + print $optionsnotused; + print ''; + print ""; print '';
- $filecolumn = $array_match_database_to_file[$code];
+ $filecolumn = !empty($array_match_database_to_file[$code])?$array_match_database_to_file[$code]:0;
// Source field info
$htmltext = ''.$langs->trans("FieldSource").' '; if ($filecolumn > count($fieldssource)) { @@ -1090,8 +1136,8 @@ if ($step == 4 && $datatoimport) { } } // Source required - $htmltext .= $langs->trans("SourceRequired").': '.yn(preg_match('/\*$/', $label)).' '; - $example = $objimport->array_import_examplevalues[0][$code]; + $htmltext .= $langs->trans("SourceRequired").': '.yn($line["label"]).' '; + $example = !empty($objimport->array_import_examplevalues[0][$code])?$objimport->array_import_examplevalues[0][$code]:""; // Example if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion if ($example) { @@ -1123,19 +1169,18 @@ if ($step == 4 && $datatoimport) { $htmltext .= $langs->trans("DataCodeIDSourceIsInsertedInto").' '; } } - $htmltext .= $langs->trans("FieldTitle").": ".$langs->trans($newlabel)." "; + $htmltext .= $langs->trans("FieldTitle").": ".$langs->trans($line["label"])." "; $htmltext .= $langs->trans("Table")." -> ".$langs->trans("Field").': '.$tablename." -> ".preg_replace('/^.*\./', '', $code)." "; print $form->textwithpicto($more, $htmltext); - print ' | ';
-
print '
| '; - print img_picto(($pos > 0 ? $langs->trans("MoveField", $pos) : ''), 'grip_title', 'class="boxhandle" style="cursor:move;"'); + //print img_picto(($pos > 0 ? $langs->trans("MoveField", $pos) : ''), 'grip_title', 'class="boxhandle" style="cursor:move;"'); print ' | '; print ''; print $langs->trans("NoFields"); @@ -2141,11 +2212,19 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') print ' | ||
| '; // 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 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; - $example = $fieldssource[$pos]['example1']; + if (empty($fieldssource[$pos]['example1'])) { + $example = $fieldssource[$pos]['label']; + } else { + $example = $fieldssource[$pos]['example1']; + } if ($example) { if (!utf8_check($example)) { $example = utf8_encode($example); @@ -2190,3 +2269,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 f2f2d2cf587..e7ac91e5722 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -135,3 +135,6 @@ NbInsert=Number of inserted lines: %s 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 +SelectImportFieldsSource = Choose the source file fields you want to import and their target field in database by choosing the fields in each select boxes, or select a predefined import profile: |