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 '
'; print ''; - $s = $langs->trans("SelectImportFields", '{s1}'); + $s = $langs->trans("SelectImportFieldsSource", '{s1}'); $s = str_replace('{s1}', img_picto('', 'grip_title', '', false, 0, 0, '', '', 0), $s); print $s; print ' '; @@ -1002,7 +1030,7 @@ if ($step == 4 && $datatoimport) { // List of source fields $var = true; $lefti = 1; - foreach ($array_match_file_to_database as $key => $val) { + foreach ($fieldssource as $key => $val) { $var = !$var; show_elem($fieldssource, $key, $val, $var); // key is field number in source file //print '> '.$lefti.'-'.$key.'-'.$val; @@ -1037,41 +1065,59 @@ if ($step == 4 && $datatoimport) { print ''; // List of target fields + $optionsnotused = ""; + foreach ($fieldstarget as $code => $line) { + if (!$line["imported"]) { + $text = ''; + $optionsnotused .= $text; + } + } + $height = '24px'; //needs px for css height attribute below $i = 0; $mandatoryfieldshavesource = true; print ''; - foreach ($fieldstarget as $code => $label) { + foreach ($fieldstarget as $code => $line) { + if ($i == $minpos) { + break; + } print ''; - - $i++; $entity = (!empty($objimport->array_import_entities[0][$code]) ? $objimport->array_import_entities[0][$code] : $objimport->array_import_icon[0]); $tablealias = preg_replace('/(\..*)$/i', '', $code); $tablename = $objimport->array_import_tables[0][$tablealias]; - $entityicon = $entitytoicon[$entity] ? $entitytoicon[$entity] : $entity; // $entityicon must string name of picto of the field like 'project', 'company', 'contact', 'modulename', ... + $entityicon = !empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity; // $entityicon must string name of picto of the field like 'project', 'company', 'contact', 'modulename', ... $entitylang = $entitytolang[$entity] ? $entitytolang[$entity] : $objimport->array_import_label[0]; // $entitylang must be a translation key to describe object the field is related to, like 'Company', 'Contact', 'MyModyle', ... print ''; print ''; - // Info field + print ''; + print $optionsnotused; + print ''; + print ""; print ''; - print ''; + $i++; } print '
=>'.img_object('', $entityicon).' '.$langs->trans($entitylang).''; - $newlabel = preg_replace('/\*$/', '', $label); - $text = $langs->trans($newlabel); + 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 ''; // List of not imported fields - print ''.$langs->trans("NotImportedFields").''; + print ''.$langs->trans("NotUsedFields").''; print ''; @@ -1143,11 +1188,11 @@ if ($step == 4 && $datatoimport) { print '