Merge pull request #20818 from Hystepik/develop#3

NEW : Import with select boxes V2
This commit is contained in:
Laurent Destailleur 2022-05-12 02:04:52 +02:00 committed by GitHub
commit 3be2993dc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 227 additions and 118 deletions

View File

@ -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 '<div class="marginbottomonly">';
print '<span class="opacitymedium">';
$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 '</span> ';
@ -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 '</td><td width="50%">';
// List of target fields
$optionsnotused = "";
foreach ($fieldstarget as $code => $line) {
if (!$line["imported"]) {
$text = '<option value="'.$code.'">';
$text .= $langs->trans($line["label"]);
if ($line["required"]) {
$text .= "*";
}
$text .= '</option>';
$optionsnotused .= $text;
}
}
$height = '24px'; //needs px for css height attribute below
$i = 0;
$mandatoryfieldshavesource = true;
print '<table width="100%" class="nobordernopadding">';
foreach ($fieldstarget as $code => $label) {
foreach ($fieldstarget as $code => $line) {
if ($i == $minpos) {
break;
}
print '<tr class="oddeven" style="height:'.$height.'">';
$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 '<td class="nowraponall" style="font-weight: normal">=>'.img_object('', $entityicon).' '.$langs->trans($entitylang).'</td>';
print '<td class="nowraponall" style="font-weight: normal">';
$newlabel = preg_replace('/\*$/', '', $label);
$text = $langs->trans($newlabel);
print '<select id="selectorderimport_'.($i+1).'" class="targetselectchange minwidth300" name="select_'.$line["label"].'">';
if ($line["imported"]) {
print '<option value="-1">&nbsp;</option>';
print '<option selected="" value="'.$code.'">';
} else {
print '<option selected="" value="-1">&nbsp;</option>';
print '<option value="'.$code.'">';
}
$text = $langs->trans($line["label"]);
$more = '';
if (preg_match('/\*$/', $label)) {
$text = '<span class="fieldrequired">'.$text.'</span>';
$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;
if ($line["required"]) {
$text .= "*";
}
print $text;
print '</td>';
// Info field
print '</option>';
print $optionsnotused;
print '</select>';
print "</td>";
print '<td class="nowraponall" style="font-weight:normal; text-align:right">';
$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 = '<b><u>'.$langs->trans("FieldSource").'</u></b><br>';
if ($filecolumn > count($fieldssource)) {
@ -1090,8 +1136,8 @@ if ($step == 4 && $datatoimport) {
}
}
// Source required
$htmltext .= $langs->trans("SourceRequired").': <b>'.yn(preg_match('/\*$/', $label)).'</b><br>';
$example = $objimport->array_import_examplevalues[0][$code];
$htmltext .= $langs->trans("SourceRequired").': <b>'.yn($line["label"]).'</b><br>';
$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").'<br>';
}
}
$htmltext .= $langs->trans("FieldTitle").": <b>".$langs->trans($newlabel)."</b><br>";
$htmltext .= $langs->trans("FieldTitle").": <b>".$langs->trans($line["label"])."</b><br>";
$htmltext .= $langs->trans("Table")." -> ".$langs->trans("Field").': <b>'.$tablename." -> ".preg_replace('/^.*\./', '', $code)."</b><br>";
print $form->textwithpicto($more, $htmltext);
print '</td>';
print '</tr>';
$i++;
}
print '</table>';
print '</td></tr>';
// List of not imported fields
print '<tr class="liste_titre"><td colspan="2">'.$langs->trans("NotImportedFields").'</td></tr>';
print '<tr class="liste_titre"><td colspan="2">'.$langs->trans("NotUsedFields").'</td></tr>';
print '<tr valign="top"><td width="50%">';
@ -1143,11 +1188,11 @@ if ($step == 4 && $datatoimport) {
print '<div id="right" class="connectedSortable">'."\n";
$nbofnotimportedfields = 0;
foreach ($fieldssource as $key => $val) {
if (empty($fieldsplaced[$key])) {
foreach ($fieldstarget as $key => $val) {
if (!$fieldstarget[$key]['imported']) {
//
$nbofnotimportedfields++;
show_elem($fieldssource, $key, '', $var, 'nostyle');
show_elem($fieldstarget, $key, '', $var, 'nostyle');
//print '> '.$lefti.'-'.$key;
$listofkeys[$key] = 1;
$lefti++;
@ -1179,38 +1224,51 @@ if ($step == 4 && $datatoimport) {
if ($conf->use_javascript_ajax) {
print '<script type="text/javascript">';
print 'jQuery(function() {
jQuery("#left, #right").sortable({
/* placeholder: \'ui-state-highlight\', */
handle: \'.boxhandle\',
revert: \'invalid\',
items: \'.box\',
containment: \'.fiche\',
connectWith: \'.connectedSortable\',
stop: function(event, ui) {
updateOrder();
}
});
});
';
print "\n";
print 'function updateOrder(){'."\n";
print 'var left_list = cleanSerialize(jQuery("#left").sortable("serialize" ));'."\n";
//print 'var right_list = cleanSerialize(jQuery("#right").sortable("serialize" ));'."\n";
print 'var boxorder = \'A:\' + left_list;'."\n";
//print 'var boxorder = \'A:\' + left_list + \'-B:\' + right_list;'."\n";
//print 'alert(\'boxorder=\' + boxorder);';
//print 'var userid = \''.$user->id.'\';'."\n";
//print 'var datatoimport = "'.$datatoimport.'";'."\n";
// print 'jQuery.ajax({ url: "ajaximport.php?step=4&boxorder=" + boxorder + "&userid=" + userid + "&datatoimport=" + datatoimport,
// async: false
// });'."\n";
// Now reload page
print 'var newlocation= \''.$_SERVER["PHP_SELF"].'?step=4'.$param.'&action=saveorder&token='.newToken().'&boxorder=\' + boxorder;'."\n";
//print 'alert(newlocation);';
print 'window.location.href=newlocation;'."\n";
print '<script type="text/javascript">'."\n";
print 'var previousselectedvalueimport = "0";'."\n";
print 'var previousselectedlabelimport = "0";'."\n";
print '$(document).ready(function () {'."\n";
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 '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 '}'."\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 '</script>'."\n";
}
@ -1221,7 +1279,7 @@ if ($step == 4 && $datatoimport) {
if (count($array_match_file_to_database)) {
if ($mandatoryfieldshavesource) {
print '<a class="butAction" href="import.php?step=5'.$param.'&filetoimport='.urlencode($filetoimport).'">'.$langs->trans("NextStep").'</a>';
print '<a class="butAction saveorderselect" href="import.php?step=5'.$param.'&filetoimport='.urlencode($filetoimport).'">'.$langs->trans("NextStep").'</a>';
} else {
print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("SomeMandatoryFieldHaveNoSource")).'">'.$langs->trans("NextStep").'</a>';
}
@ -1315,7 +1373,6 @@ if ($step == 4 && $datatoimport) {
}
}
// STEP 5: Summary of choices and launch simulation
if ($step == 5 && $datatoimport) {
$max_execution_time_for_importexport = (empty($conf->global->IMPORT_MAX_EXECUTION_TIME) ? 300 : $conf->global->IMPORT_MAX_EXECUTION_TIME); // 5mn if not defined
@ -1471,6 +1528,20 @@ if ($step == 5 && $datatoimport) {
if ($action == 'launchsimu') {
print ' &nbsp; <a href="'.$_SERVER["PHP_SELF"].'?step=5'.$param.'">'.$langs->trans("Modify").'</a>';
}
if ($excludefirstline == 2) {
print $form->textwithpicto("", $langs->trans("WarningFirstImportedLine", $excludefirstline), 1, 'warning', "warningexcludefirstline");
print '<script>
$( document ).ready(function() {
$("input[name=\'excludefirstline\']").on("change",function(){
if($(this).val() <= 1){
$(".warningexcludefirstline").hide();
}else{
$(".warningexcludefirstline").show();
}
})
});
</script>';
}
print '</td></tr>';
// Keys for data UPDATE (not INSERT of new data)
@ -2104,7 +2175,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
@ -2118,10 +2189,10 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '')
print '<table summary="boxtable'.$pos.'" width="100%" class="nobordernopadding">'."\n";
}
if ($pos && $pos > count($fieldssource)) { // No fields
if (($pos && $pos > count($fieldssource)) && (!isset($fieldssource[$pos]["imported"]))) { // No fields
print '<tr'.($nostyle ? '' : ' '.$bc[$var]).' style="height:'.$height.'">';
print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
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 '</td>';
print '<td style="font-weight: normal">';
print $langs->trans("NoFields");
@ -2141,11 +2212,19 @@ function show_elem($fieldssource, $pos, $key, $var, $nostyle = '')
print '<tr'.($nostyle ? '' : ' '.$bc[$var]).' style="height:'.$height.'">';
print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
// 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 '</td>';
print '<td class="nowraponall" style="font-weight: normal">';
if (isset($fieldssource[$pos]['imported']) && $fieldssource[$pos]['imported'] == false) {
print '<td class="nowraponall boxtdunused" style="font-weight: normal">';
} else {
print '<td class="nowraponall" style="font-weight: normal">';
}
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;
}

View File

@ -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: