From 0674d062f8b0b3f07219ae674b138846f5fc39e5 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 15 Apr 2022 11:55:24 +0200 Subject: [PATCH 1/4] select on source fields import --- htdocs/imports/import.php | 583 +++++++++++++++++++++++++++++++- htdocs/langs/en_US/exports.lang | 1 + 2 files changed, 580 insertions(+), 4 deletions(-) diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index 01f152f1ea5..b3303402d22 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -35,6 +35,8 @@ 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'); @@ -136,7 +138,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 : ',')); @@ -763,7 +765,7 @@ if ($step == 3 && $datatoimport) { // STEP 4: Page to make matching between source file and database fields -if ($step == 4 && $datatoimport) { +if ($step == 4 && $datatoimport && !$selectimport) { $model = $format; $list = $objmodelimport->liste_modeles($db); @@ -1315,6 +1317,565 @@ if ($step == 4 && $datatoimport) { } } +if ($step == 4 && $datatoimport && $selectimport) { + $model = $format; + $list = $objmodelimport->liste_modeles($db); + + // Create classe to use for import + $dir = DOL_DOCUMENT_ROOT."/core/modules/import/"; + $file = "import_".$model.".modules.php"; + $classname = "Import".ucfirst($model); + require_once $dir.$file; + $obj = new $classname($db, $datatoimport); + if ($model == 'csv') { + $obj->separator = $separator_used; + $obj->enclosure = $enclosure; + } + if ($model == 'xlsx') { + if (!preg_match('/\.xlsx$/i', $filetoimport)) { + $langs->load("errors"); + $param = '&datatoimport='.$datatoimport.'&format='.$format; + setEventMessages($langs->trans("ErrorFileMustHaveFormat", $model), null, 'errors'); + header("Location: ".$_SERVER["PHP_SELF"].'?step=3'.$param.'&filetoimport='.urlencode($relativepath)); + exit; + } + } + + if (GETPOST('update')) { + $array_match_file_to_database = array(); + } + + // Load source fields in input file + $fieldssource = array(); + $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs); + if ($result >= 0) { + // Read first line + $arrayrecord = $obj->import_read_record(); + // Put into array fieldssource starting with 1. + $i = 1; + foreach ($arrayrecord as $key => $val) { + $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24); + $i++; + } + $obj->import_close_file(); + } + + // Load targets fields in database + $fieldstarget = $objimport->array_import_fields[0]; + $fieldstarget_tmp = array(); + + 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); + } + $fieldstarget_tmp[$key]["imported"] = true; + } + $fieldstarget = $fieldstarget_tmp; + $maxpos = max(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) + if (count($array_match_file_to_database) == 0) { + // This is first input in screen, we need to define + // $array_match_file_to_database + // $serialized_array_match_file_to_database + // $_SESSION["dol_array_match_file_to_database"] + $pos = 1; + $num = count($fieldssource); + while ($pos <= $num) { + if ($num >= 1 && $pos <= $num) { + $posbis = 1; + foreach ($fieldstarget as $key => $val) { + if ($posbis < $pos) { + $posbis++; + continue; + } + // We found the key of targets that is at position pos + $array_match_file_to_database[$pos] = $key; + if ($serialized_array_match_file_to_database) { + $serialized_array_match_file_to_database .= ','; + } + $serialized_array_match_file_to_database .= ($pos.'='.$key); + break; + } + } + $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; + } + $array_match_database_to_file = array_flip($array_match_file_to_database); + + //print $serialized_array_match_file_to_database; + //print $_SESSION["dol_array_match_file_to_database"]; + //var_dump($array_match_file_to_database);exit; + + // Now $array_match_file_to_database contains fieldnb(1,2,3...)=>fielddatabase(key in $array_match_file_to_database) + + $param = '&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport); + if ($excludefirstline) { + $param .= '&excludefirstline='.urlencode($excludefirstline); + } + if ($endatlinenb) { + $param .= '&endatlinenb='.urlencode($endatlinenb); + } + if ($separator) { + $param .= '&separator='.urlencode($separator); + } + if ($enclosure) { + $param .= '&enclosure='.urlencode($enclosure); + } + + llxHeader('', $langs->trans("NewImport"), $help_url); + + $head = import_prepare_head($param, 4); + + print dol_get_fiche_head($head, 'step4', '', -2); + + print '
'; + print '
'; + + print ''; + + // Module + print ''; + print ''; + + // Lot de donnees a importer + print ''; + print ''; + + print '
'.$langs->trans("Module").''; + $titleofmodule = $objimport->array_import_module[0]['module']->getName(); + // Special cas for import common to module/services + if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) { + $titleofmodule = $langs->trans("ProductOrService"); + } + print $titleofmodule; + print '
'.$langs->trans("DatasetToImport").''; + $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]); + $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity); + print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' '; + print $objimport->array_import_label[0]; + print '
'; + print '
'; + + print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export'); + + print '
'; + print '
'; + print ''; + + // Source file format + print ''; + print ''; + + // Separator and enclosure + if ($model == 'csv') { + print ''; + print ''; + } + + // File to import + print ''; + print ''; + + print '
'.$langs->trans("SourceFileFormat").''; + $text = $objmodelimport->getDriverDescForKey($format); + print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text); + print '
'.$langs->trans("CsvOptions").''; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print $langs->trans("Separator").' : '; + print ''; + print '    '.$langs->trans("Enclosure").' : '; + print ' '; + print ''; + print '
'; + print '
'.$langs->trans("FileToImport").''; + $modulepart = 'import'; + $relativepath = GETPOST('filetoimport'); + print ''; + print img_mime($file, '', 'pictofixedwidth'); + print $filetoimport; + print ''; + print '
'; + print '
'; + + print dol_get_fiche_end(); + + print '
'."\n"; + + + // List of source fields + print ''."\n"; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + print ''; + $s = $langs->trans("SelectImportFields", '{s1}'); + $s = str_replace('{s1}', img_picto('', 'grip_title', '', false, 0, 0, '', '', 0), $s); + print $s; + print ' '; + $htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1, $user->id); + print ''; + print '
'; + print '
'; + + // Title of array with fields + print '
'; // You can use div-table-responsive-no-min if you dont need reserved height for your table + print ''; + print ''; + print ''; + print ''; + print ''; + + //var_dump($array_match_file_to_database); + + print ''; + + // List of not imported fields + print ''; + + print ''; + print ''; + + print '
'.$langs->trans("FieldsInSourceFile").''.$langs->trans("FieldsInTargetDatabase").'
'; + + $fieldsplaced = array(); + $valforsourcefieldnb = array(); + $listofkeys = array(); + foreach ($array_match_file_to_database as $key => $val) { + $listofkeys[$key] = 1; + } + + print "\n\n"; + print '
'."\n"; + + // List of source fields + $var = true; + $lefti = 1; + foreach ($array_match_file_to_database as $key => $val) { + $var = !$var; + show_elem($fieldssource, $key, $val, $var); // key is field number in source file + //print '> '.$lefti.'-'.$key.'-'.$val; + $listofkeys[$key] = 1; + $fieldsplaced[$key] = 1; + $valforsourcefieldnb[$lefti] = $key; + $lefti++; + + if ($lefti > count($fieldstarget)) { + break; // Other fields are in the not imported area + } + } + //var_dump($valforsourcefieldnb); + + // Complete source fields from count($fieldssource)+1 to count($fieldstarget) + $more = 1; + $num = count($fieldssource); + while ($lefti <= $num) { + $var = !$var; + $newkey = getnewkey($fieldssource, $listofkeys); + show_elem($fieldssource, $newkey, '', $var); // key start after field number in source file + //print '> '.$lefti.'-'.$newkey; + $listofkeys[$key] = 1; + $lefti++; + $more++; + } + + print "
\n"; + print "\n"; + + + print '
'; + + // List of target fields + $height = '24px'; //needs px for css height attribute below + $i = 0; + $mandatoryfieldshavesource = true; + + print ''; + + foreach ($fieldstarget as $code => $line) { + print ''; + $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 = !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 '"; + print ''; + } + print '
=>'.img_object('', $entityicon).' '.$langs->trans($entitylang).''; + print ''; + //print ajax_combobox("select_".$newlabel); + print "'; + $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)) { + $htmltext .= $langs->trans("DataComeFromNoWhere").'
'; + } else { + if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion + $filecolumntoshow = $filecolumn; + $htmltext .= $langs->trans("DataComeFromFileFieldNb", $filecolumntoshow).'
'; + } else { + if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') { + $htmltext .= $langs->trans("DataComeFromIdFoundFromRef", $filecolumn, $langs->transnoentitiesnoconv($entitylang)).'
'; + } + if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') { + $htmltext .= $langs->trans("DataComeFromIdFoundFromCodeId", $filecolumn, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).'
'; + } + } + } + // Source required + $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) { + $htmltext .= $langs->trans("SourceExample").': '.$example.'
'; + } + } else { + if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') { + $htmltext .= $langs->trans("SourceExample").': '.$langs->transnoentitiesnoconv("ExampleAnyRefFoundIntoElement", $entitylang).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'
'; + } elseif ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') { + $htmltext .= $langs->trans("SourceExample").': '.$langs->trans("ExampleAnyCodeOrIdFoundIntoDictionary", $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'
'; + } elseif ($example) { + $htmltext .= $langs->trans("SourceExample").': '.$example.'
'; + } + } + // Format control rule + if (!empty($objimport->array_import_regex[0][$code])) { + $htmltext .= $langs->trans("FormatControlRule").': '.$objimport->array_import_regex[0][$code].'
'; + } + $htmltext .= '
'; + // Target field info + $htmltext .= ''.$langs->trans("FieldTarget").'
'; + if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion + $htmltext .= $langs->trans("DataIsInsertedInto").'
'; + } else { + if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') { + $htmltext .= $langs->trans("DataIDSourceIsInsertedInto").'
'; + } + if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') { + $htmltext .= $langs->trans("DataCodeIDSourceIsInsertedInto").'
'; + } + } + $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 '
'.$langs->trans("NotImportedFields").'
'; + + print "\n\n"; + print '\n"; + print "\n"; + + print ''; + $i = 0; + while ($i < $nbofnotimportedfields) { + // Print empty cells + show_elem('', '', 'none', $var, 'nostyle'); + $i++; + } + print '
'; + print '
'; + + + if ($conf->use_javascript_ajax) { + print ''."\n"; + } + + /* + * Action bar + */ + print '
'; + + if (count($array_match_file_to_database)) { + if ($mandatoryfieldshavesource) { + print ''.$langs->trans("NextStep").''; + } else { + print ''.$langs->trans("NextStep").''; + } + } + + print '
'; + + + // Area for profils import + if (count($array_match_file_to_database)) { + print '
'."\n"; + print ''."\n"; + print '
'.$langs->trans("SaveImportModel").'
'; + + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; // You can use div-table-responsive-no-min if you dont need reserved height for your table + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + + // List of existing import profils + $sql = "SELECT rowid, label, fk_user, entity"; + $sql .= " FROM ".MAIN_DB_PREFIX."import_model"; + $sql .= " WHERE type = '".$db->escape($datatoimport)."'"; + if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner. + $sql .= " AND fk_user IN (0, ".((int) $user->id).")"; + } + $sql .= " ORDER BY rowid"; + + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + + $tmpuser = new user($db); + + $i = 0; + while ($i < $num) { + $obj = $db->fetch_object($resql); + + print ''; + print ''; + print ''; + $i++; + } + } else { + dol_print_error($db); + } + + print '
'.$langs->trans("ImportModelName").''.$langs->trans("Visibility").'
'; + $arrayvisibility = array('private'=>$langs->trans("Private"), 'all'=>$langs->trans("Everybody")); + print $form->selectarray('visibility', $arrayvisibility, 'private'); + print ''; + print ''; + print '
'; + print $obj->label; + print ''; + if (empty($obj->fk_user)) { + print $langs->trans("Everybody"); + } else { + $tmpuser->fetch($obj->fk_user); + print $tmpuser->getNomUrl(1); + } + print ''; + print 'rowid.'&filetoimport='.urlencode($filetoimport).'">'; + print img_delete(); + print ''; + print '
'; + print '
'; + + print '
'; + } +} // STEP 5: Summary of choices and launch simulation if ($step == 5 && $datatoimport) { @@ -1471,6 +2032,20 @@ if ($step == 5 && $datatoimport) { if ($action == 'launchsimu') { print '   '.$langs->trans("Modify").''; } + if ($excludefirstline == 2) { + print $form->textwithpicto("", $langs->trans("WarningFirstImportedLine", $excludefirstline), 1, 'warning', "warningexcludefirstline"); + print ''; + } print ''; // Keys for data UPDATE (not INSERT of new data) @@ -2121,7 +2696,7 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') if ($pos && $pos > count($fieldssource)) { // No fields 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,7 +2716,7 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') print ''; 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 ''; print $langs->trans("Field").' '.$pos; diff --git a/htdocs/langs/en_US/exports.lang b/htdocs/langs/en_US/exports.lang index f2f2d2cf587..a20741472b7 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -135,3 +135,4 @@ 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 From 81e238351526e5494f6db123754b44bce3d1831e Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 6 May 2022 15:38:47 +0200 Subject: [PATCH 2/4] import with selectbox --- htdocs/imports/import.php | 724 +++++++------------------------------- 1 file changed, 124 insertions(+), 600 deletions(-) diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index b3303402d22..a8fa3013ea2 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -167,6 +167,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 @@ -356,7 +369,22 @@ if ($action == 'saveorder') { $_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 = ''; + 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 .= ','; + } + $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); +} @@ -765,7 +793,18 @@ 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 && $selectimport) { + $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); @@ -810,9 +849,7 @@ if ($step == 4 && $datatoimport && !$selectimport) { // 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) @@ -843,525 +880,10 @@ if ($step == 4 && $datatoimport && !$selectimport) { $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); - //print $serialized_array_match_file_to_database; - //print $_SESSION["dol_array_match_file_to_database"]; - //var_dump($array_match_file_to_database);exit; - - // Now $array_match_file_to_database contains fieldnb(1,2,3...)=>fielddatabase(key in $array_match_file_to_database) - - $param = '&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport); - if ($excludefirstline) { - $param .= '&excludefirstline='.urlencode($excludefirstline); - } - if ($endatlinenb) { - $param .= '&endatlinenb='.urlencode($endatlinenb); - } - if ($separator) { - $param .= '&separator='.urlencode($separator); - } - if ($enclosure) { - $param .= '&enclosure='.urlencode($enclosure); - } - - llxHeader('', $langs->trans("NewImport"), $help_url); - - $head = import_prepare_head($param, 4); - - print dol_get_fiche_head($head, 'step4', '', -2); - - print '
'; - print '
'; - - print ''; - - // Module - print ''; - print ''; - - // Lot de donnees a importer - print ''; - print ''; - - print '
'.$langs->trans("Module").''; - $titleofmodule = $objimport->array_import_module[0]['module']->getName(); - // Special cas for import common to module/services - if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) { - $titleofmodule = $langs->trans("ProductOrService"); - } - print $titleofmodule; - print '
'.$langs->trans("DatasetToImport").''; - $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]); - $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity); - print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' '; - print $objimport->array_import_label[0]; - print '
'; - print '
'; - - print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export'); - - print '
'; - print '
'; - print ''; - - // Source file format - print ''; - print ''; - - // Separator and enclosure - if ($model == 'csv') { - print ''; - print ''; - } - - // File to import - print ''; - print ''; - - print '
'.$langs->trans("SourceFileFormat").''; - $text = $objmodelimport->getDriverDescForKey($format); - print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text); - print '
'.$langs->trans("CsvOptions").''; - print '
'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print $langs->trans("Separator").' : '; - print ''; - print '    '.$langs->trans("Enclosure").' : '; - print ' '; - print ''; - print '
'; - print '
'.$langs->trans("FileToImport").''; - $modulepart = 'import'; - $relativepath = GETPOST('filetoimport'); - print ''; - print img_mime($file, '', 'pictofixedwidth'); - print $filetoimport; - print ''; - print '
'; - print '
'; - - print dol_get_fiche_end(); - - print '
'."\n"; - - - // List of source fields - print ''."\n"; - print '
'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print '
'; - print ''; - $s = $langs->trans("SelectImportFields", '{s1}'); - $s = str_replace('{s1}', img_picto('', 'grip_title', '', false, 0, 0, '', '', 0), $s); - print $s; - print ' '; - $htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1, $user->id); - print ''; - print '
'; - print '
'; - - // Title of array with fields - print '
'; // You can use div-table-responsive-no-min if you dont need reserved height for your table - print ''; - print ''; - print ''; - print ''; - print ''; - - //var_dump($array_match_file_to_database); - - print ''; - - // List of not imported fields - print ''; - - print ''; - print ''; - - print '
'.$langs->trans("FieldsInSourceFile").''.$langs->trans("FieldsInTargetDatabase").'
'; - - $fieldsplaced = array(); - $valforsourcefieldnb = array(); - $listofkeys = array(); - foreach ($array_match_file_to_database as $key => $val) { - $listofkeys[$key] = 1; - } - - print "\n\n"; - print '
'."\n"; - - // List of source fields - $var = true; - $lefti = 1; - foreach ($array_match_file_to_database as $key => $val) { - $var = !$var; - show_elem($fieldssource, $key, $val, $var); // key is field number in source file - //print '> '.$lefti.'-'.$key.'-'.$val; - $listofkeys[$key] = 1; - $fieldsplaced[$key] = 1; - $valforsourcefieldnb[$lefti] = $key; - $lefti++; - - if ($lefti > count($fieldstarget)) { - break; // Other fields are in the not imported area - } - } - //var_dump($valforsourcefieldnb); - - // Complete source fields from count($fieldssource)+1 to count($fieldstarget) - $more = 1; - $num = count($fieldssource); - while ($lefti <= $num) { - $var = !$var; - $newkey = getnewkey($fieldssource, $listofkeys); - show_elem($fieldssource, $newkey, '', $var); // key start after field number in source file - //print '> '.$lefti.'-'.$newkey; - $listofkeys[$key] = 1; - $lefti++; - $more++; - } - - print "
\n"; - print "\n"; - - - print '
'; - - // List of target fields - $height = '24px'; //needs px for css height attribute below - $i = 0; - $mandatoryfieldshavesource = true; - - print ''; - foreach ($fieldstarget as $code => $label) { - 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', ... - $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 ''; - } - print '
=>'.img_object('', $entityicon).' '.$langs->trans($entitylang).''; - $newlabel = preg_replace('/\*$/', '', $label); - $text = $langs->trans($newlabel); - $more = ''; - if (preg_match('/\*$/', $label)) { - $text = ''.$text.''; - $more = ((!empty($valforsourcefieldnb[$i]) && $valforsourcefieldnb[$i] <= count($fieldssource)) ? '' : img_warning($langs->trans("FieldNeedSource"))); - if ($mandatoryfieldshavesource) { - $mandatoryfieldshavesource = (!empty($valforsourcefieldnb[$i]) && ($valforsourcefieldnb[$i] <= count($fieldssource))); - } - //print 'xx'.($i).'-'.$valforsourcefieldnb[$i].'-'.$mandatoryfieldshavesource; - } - print $text; - print ''; - $filecolumn = $array_match_database_to_file[$code]; - // Source field info - $htmltext = ''.$langs->trans("FieldSource").'
'; - if ($filecolumn > count($fieldssource)) { - $htmltext .= $langs->trans("DataComeFromNoWhere").'
'; - } else { - if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion - $filecolumntoshow = $filecolumn; - $htmltext .= $langs->trans("DataComeFromFileFieldNb", $filecolumntoshow).'
'; - } else { - if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') { - $htmltext .= $langs->trans("DataComeFromIdFoundFromRef", $filecolumn, $langs->transnoentitiesnoconv($entitylang)).'
'; - } - if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') { - $htmltext .= $langs->trans("DataComeFromIdFoundFromCodeId", $filecolumn, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).'
'; - } - } - } - // Source required - $htmltext .= $langs->trans("SourceRequired").': '.yn(preg_match('/\*$/', $label)).'
'; - $example = $objimport->array_import_examplevalues[0][$code]; - // Example - if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion - if ($example) { - $htmltext .= $langs->trans("SourceExample").': '.$example.'
'; - } - } else { - if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') { - $htmltext .= $langs->trans("SourceExample").': '.$langs->transnoentitiesnoconv("ExampleAnyRefFoundIntoElement", $entitylang).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'
'; - } elseif ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') { - $htmltext .= $langs->trans("SourceExample").': '.$langs->trans("ExampleAnyCodeOrIdFoundIntoDictionary", $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'
'; - } elseif ($example) { - $htmltext .= $langs->trans("SourceExample").': '.$example.'
'; - } - } - // Format control rule - if (!empty($objimport->array_import_regex[0][$code])) { - $htmltext .= $langs->trans("FormatControlRule").': '.$objimport->array_import_regex[0][$code].'
'; - } - $htmltext .= '
'; - // Target field info - $htmltext .= ''.$langs->trans("FieldTarget").'
'; - if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion - $htmltext .= $langs->trans("DataIsInsertedInto").'
'; - } else { - if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') { - $htmltext .= $langs->trans("DataIDSourceIsInsertedInto").'
'; - } - if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') { - $htmltext .= $langs->trans("DataCodeIDSourceIsInsertedInto").'
'; - } - } - $htmltext .= $langs->trans("FieldTitle").": ".$langs->trans($newlabel)."
"; - $htmltext .= $langs->trans("Table")." -> ".$langs->trans("Field").': '.$tablename." -> ".preg_replace('/^.*\./', '', $code)."
"; - print $form->textwithpicto($more, $htmltext); - print '
'; - - print '
'.$langs->trans("NotImportedFields").'
'; - - print "\n\n"; - print '\n"; - print "\n"; - - print ''; - $i = 0; - while ($i < $nbofnotimportedfields) { - // Print empty cells - show_elem('', '', 'none', $var, 'nostyle'); - $i++; - } - print '
'; - print '
'; - - - if ($conf->use_javascript_ajax) { - print ''."\n"; - } - - /* - * Action bar - */ - print '
'; - - if (count($array_match_file_to_database)) { - if ($mandatoryfieldshavesource) { - print ''.$langs->trans("NextStep").''; - } else { - print ''.$langs->trans("NextStep").''; - } - } - - print '
'; - - - // Area for profils import - if (count($array_match_file_to_database)) { - print '
'."\n"; - print ''."\n"; - print '
'.$langs->trans("SaveImportModel").'
'; - - print '
'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print '
'; // You can use div-table-responsive-no-min if you dont need reserved height for your table - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print ''; - print ''; - print ''; - print ''; - - // List of existing import profils - $sql = "SELECT rowid, label, fk_user, entity"; - $sql .= " FROM ".MAIN_DB_PREFIX."import_model"; - $sql .= " WHERE type = '".$db->escape($datatoimport)."'"; - if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner. - $sql .= " AND fk_user IN (0, ".((int) $user->id).")"; - } - $sql .= " ORDER BY rowid"; - - $resql = $db->query($sql); - if ($resql) { - $num = $db->num_rows($resql); - - $tmpuser = new user($db); - - $i = 0; - while ($i < $num) { - $obj = $db->fetch_object($resql); - - print ''; - print ''; - print ''; - $i++; - } - } else { - dol_print_error($db); - } - - print '
'.$langs->trans("ImportModelName").''.$langs->trans("Visibility").'
'; - $arrayvisibility = array('private'=>$langs->trans("Private"), 'all'=>$langs->trans("Everybody")); - print $form->selectarray('visibility', $arrayvisibility, 'private'); - print ''; - print ''; - print '
'; - print $obj->label; - print ''; - if (empty($obj->fk_user)) { - print $langs->trans("Everybody"); - } else { - $tmpuser->fetch($obj->fk_user); - print $tmpuser->getNomUrl(1); - } - print ''; - print 'rowid.'&filetoimport='.urlencode($filetoimport).'">'; - print img_delete(); - print ''; - print '
'; - print '
'; - - print '
'; - } -} - -if ($step == 4 && $datatoimport && $selectimport) { - $model = $format; - $list = $objmodelimport->liste_modeles($db); - - // Create classe to use for import - $dir = DOL_DOCUMENT_ROOT."/core/modules/import/"; - $file = "import_".$model.".modules.php"; - $classname = "Import".ucfirst($model); - require_once $dir.$file; - $obj = new $classname($db, $datatoimport); - if ($model == 'csv') { - $obj->separator = $separator_used; - $obj->enclosure = $enclosure; - } - if ($model == 'xlsx') { - if (!preg_match('/\.xlsx$/i', $filetoimport)) { - $langs->load("errors"); - $param = '&datatoimport='.$datatoimport.'&format='.$format; - setEventMessages($langs->trans("ErrorFileMustHaveFormat", $model), null, 'errors'); - header("Location: ".$_SERVER["PHP_SELF"].'?step=3'.$param.'&filetoimport='.urlencode($relativepath)); - exit; - } - } - - if (GETPOST('update')) { - $array_match_file_to_database = array(); - } - - // Load source fields in input file - $fieldssource = array(); - $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs); - if ($result >= 0) { - // Read first line - $arrayrecord = $obj->import_read_record(); - // Put into array fieldssource starting with 1. - $i = 1; - foreach ($arrayrecord as $key => $val) { - $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24); - $i++; - } - $obj->import_close_file(); - } - - // Load targets fields in database - $fieldstarget = $objimport->array_import_fields[0]; $fieldstarget_tmp = array(); foreach ($fieldstarget as $key => $label) { @@ -1372,44 +894,13 @@ if ($step == 4 && $datatoimport && $selectimport) { } else { $fieldstarget_tmp[$key] = array("label"=>$label,"required"=>false); } - $fieldstarget_tmp[$key]["imported"] = true; + if (!empty($array_match_database_to_file[$key])) { + $fieldstarget_tmp[$key]["imported"] = true; + } else { + $fieldstarget_tmp[$key]["imported"] = false; + } } $fieldstarget = $fieldstarget_tmp; - $maxpos = max(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) - if (count($array_match_file_to_database) == 0) { - // This is first input in screen, we need to define - // $array_match_file_to_database - // $serialized_array_match_file_to_database - // $_SESSION["dol_array_match_file_to_database"] - $pos = 1; - $num = count($fieldssource); - while ($pos <= $num) { - if ($num >= 1 && $pos <= $num) { - $posbis = 1; - foreach ($fieldstarget as $key => $val) { - if ($posbis < $pos) { - $posbis++; - continue; - } - // We found the key of targets that is at position pos - $array_match_file_to_database[$pos] = $key; - if ($serialized_array_match_file_to_database) { - $serialized_array_match_file_to_database .= ','; - } - $serialized_array_match_file_to_database .= ($pos.'='.$key); - break; - } - } - $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; - } - $array_match_database_to_file = array_flip($array_match_file_to_database); //print $serialized_array_match_file_to_database; //print $_SESSION["dol_array_match_file_to_database"]; @@ -1568,7 +1059,7 @@ if ($step == 4 && $datatoimport && $selectimport) { // 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; @@ -1603,13 +1094,28 @@ if ($step == 4 && $datatoimport && $selectimport) { 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 => $line) { + if ($i == $minpos) { + break; + } print ''; $entity = (!empty($objimport->array_import_entities[0][$code]) ? $objimport->array_import_entities[0][$code] : $objimport->array_import_icon[0]); @@ -1621,9 +1127,14 @@ if ($step == 4 && $datatoimport && $selectimport) { print ''; print '"; @@ -1691,6 +1203,7 @@ if ($step == 4 && $datatoimport && $selectimport) { $htmltext .= $langs->trans("Table")." -> ".$langs->trans("Field").': '.$tablename." -> ".preg_replace('/^.*\./', '', $code)."
"; print $form->textwithpicto($more, $htmltext); print ''; + $i++; } print '
=>'.img_object('', $entityicon).' '.$langs->trans($entitylang).''; - print ''; + if ($line["imported"]) { + print ''; + print ''; + print ''; + print $optionsnotused; print ''; //print ajax_combobox("select_".$newlabel); print "
'; @@ -1705,11 +1218,11 @@ if ($step == 4 && $datatoimport && $selectimport) { print ''; @@ -2679,7 +2199,7 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') { global $langs, $bc; - $height = '24px'; + $height = '28px'; if ($key == 'none') { //stop multiple duplicate ids with no number @@ -2693,7 +2213,7 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') print ''."\n"; } - if ($pos && $pos > count($fieldssource)) { // No fields + if (($pos && $pos > count($fieldssource)) && (!isset($fieldssource[$pos]["imported"]))) { // No fields print ''; print ''; print '"; print ''; // List of not imported fields - print ''; + print ''; print ''; - print '
'; //print img_picto(($pos > 0 ? $langs->trans("MoveField", $pos) : ''), 'grip_title', 'class="boxhandle" style="cursor:move;"'); @@ -2720,7 +2240,11 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '') 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); From ed13280ea10d932419615a0bb64199354b078e28 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Tue, 10 May 2022 12:15:52 +0200 Subject: [PATCH 3/4] 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 "'; $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 '
'.$langs->trans("NotImportedFields").'
'.$langs->trans("NotUsedFields").'
'; @@ -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 ''; + 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 From 7e439a824848546a539229f3fd19de4e5fabcf1d Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Tue, 10 May 2022 15:50:01 +0200 Subject: [PATCH 4/4] add of selectimportfieldsource in lang --- htdocs/imports/import.php | 8 +++++--- htdocs/langs/en_US/exports.lang | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index d4b7ad4e4b6..15d83cfe8e7 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -791,8 +791,10 @@ 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(); } @@ -994,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 ' '; diff --git a/htdocs/langs/en_US/exports.lang b/htdocs/langs/en_US/exports.lang index 44cb98d3679..e7ac91e5722 100644 --- a/htdocs/langs/en_US/exports.lang +++ b/htdocs/langs/en_US/exports.lang @@ -136,4 +136,5 @@ 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 +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: