29require_once
'../main.inc.php';
37require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formfile.class.php';
38require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
39require_once DOL_DOCUMENT_ROOT.
'/imports/class/import.class.php';
40require_once DOL_DOCUMENT_ROOT.
'/core/modules/import/modules_import.class.php';
41require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/core/lib/import.lib.php';
46$langs->loadLangs(array(
'exports',
'compta',
'projects',
'admin',
'products',
'margins'));
49$hookmanager->initHooks(array(
'imports'));
57 'invoice_line' =>
'bill',
59 'order_line' =>
'order',
61 'propal_line' =>
'propal',
62 'intervention' =>
'intervention',
63 'inter_line' =>
'intervention',
65 'member_type' =>
'group',
66 'subscription' =>
'payment',
67 'payment' =>
'payment',
69 'tax_type' =>
'generic',
71 'account' =>
'account',
72 'product' =>
'product',
73 'productattribute' =>
'product',
74 'productattributevalue' =>
'product',
75 'productcombination' =>
'product',
76 'virtualproduct' =>
'product',
77 'subproduct' =>
'product',
78 'product_supplier_ref' =>
'product',
80 'warehouse' =>
'stock',
82 'stockbatch' =>
'stock',
83 'category' =>
'category',
84 'shipment' =>
'sending',
85 'shipment_line' =>
'sending',
86 'project' =>
'project',
88 'reception' =>
'sending',
89 'reception_line' =>
'sending',
90 'expensereport' =>
'trip',
91 'expensereport_line' =>
'trip',
92 'holiday' =>
'holiday',
93 'contract_line' =>
'contract',
94 'translation' =>
'generic',
100$entitytolang = array(
102 'company' =>
'Company',
103 'contact' =>
'Contact',
105 'invoice_line' =>
'InvoiceLine',
107 'order_line' =>
'OrderLine',
108 'propal' =>
'Proposal',
109 'propal_line' =>
'ProposalLine',
110 'intervention' =>
'Intervention',
111 'inter_line' =>
'InterLine',
112 'member' =>
'Member',
113 'member_type' =>
'MemberType',
114 'subscription' =>
'Subscription',
115 'tax' =>
'SocialContribution',
116 'tax_type' =>
'DictionarySocialContributions',
117 'account' =>
'BankTransactions',
118 'payment' =>
'Payment',
119 'product' =>
'Product',
120 'productattribute' =>
'ProductAttribute',
121 'productattributevalue' =>
'ProductAttributeValue',
122 'productcombination' =>
'ProductCombination',
123 'virtualproduct' =>
'AssociatedProducts',
124 'subproduct' =>
'SubProduct',
125 'product_supplier_ref' =>
'SupplierPrices',
126 'service' =>
'Service',
128 'movement' =>
'StockMovement',
130 'stockbatch' =>
'StockDetailPerBatch',
131 'warehouse' =>
'Warehouse',
132 'category' =>
'Category',
134 'trip' =>
'TripsAndExpenses',
135 'shipment' =>
'Shipments',
136 'shipment_line' =>
'ShipmentLine',
137 'project' =>
'Projects',
138 'projecttask' =>
'Tasks',
139 'task_time' =>
'TaskTimeSpent',
141 'expensereport' =>
'ExpenseReport',
142 'expensereport_line' =>
'ExpenseReportLine',
143 'holiday' =>
'TitreRequestCP',
144 'contract' =>
'Contract',
145 'contract_line' =>
'ContractLine',
146 'translation' =>
'Translation',
148 'bomline' =>
'BOMLine'
151$datatoimport =
GETPOST(
'datatoimport');
153$filetoimport =
GETPOST(
'filetoimport');
154$action =
GETPOST(
'action',
'alpha');
155$confirm =
GETPOST(
'confirm',
'alpha');
157$import_name =
GETPOST(
'import_name');
160$excludefirstline = (
GETPOST(
'excludefirstline',
'alphanohtml', 3) ?
GETPOST(
'excludefirstline',
'alphanohtml', 3) : 2);
161$endatlinenb = (
GETPOST(
'endatlinenb',
'alphanohtml', 3) ?
GETPOST(
'endatlinenb',
'alphanohtml', 3) :
'');
162$updatekeys = (
GETPOST(
'updatekeys',
'array', 3) ?
GETPOST(
'updatekeys',
'array', 3) : array());
163$importtriggermode =
GETPOST(
'importtriggermode',
'alpha', 3);
164$separator = (
GETPOST(
'separator',
'nohtml') ?
GETPOST(
'separator',
'nohtml', 3) :
'');
165$enclosure = (
GETPOST(
'enclosure',
'nohtml') ?
GETPOST(
'enclosure',
'nohtml') :
'"');
166$charset =
GETPOST(
'charset',
'aZ09');
167$separator_used = str_replace(
'\t',
"\t", $separator);
170if (empty($importtriggermode)) {
171 $importtriggermode =
getDolGlobalString(
'IMPORT_TRIGGER_MODE_DEFAULT',
'strict_line');
173if (!in_array($importtriggermode, array(
'strict_line',
'fast_bulk'),
true)) {
174 $importtriggermode =
'strict_line';
177$objimport =
new Import($db);
178$objimport->load_arrays($user, ($step == 1 ?
'' : $datatoimport));
180if (empty($updatekeys) && !empty($objimport->array_import_preselected_updatekeys[0])) {
181 $updatekeys = $objimport->array_import_preselected_updatekeys[0];
186$form =
new Form($db);
190if (empty($array_match_file_to_database)) {
191 $serialized_array_match_file_to_database = isset($_SESSION[
"dol_array_match_file_to_database_select"]) ? $_SESSION[
"dol_array_match_file_to_database_select"] :
'';
192 $array_match_file_to_database = array();
193 $fieldsarray = explode(
',', $serialized_array_match_file_to_database);
194 foreach ($fieldsarray as $elem) {
195 $tabelem = explode(
'=', $elem, 2);
197 $val = (isset($tabelem[1]) ? $tabelem[1] :
'');
199 $array_match_file_to_database[$key] = $val;
209if ($action ==
'deleteprof' && $user->hasRight(
'import',
'run')) {
212 $result = $objimport->delete($user);
214 setEventMessages($objimport->error, $objimport->errors,
'errors');
220if ($action ==
'add_import_model' && $user->hasRight(
'import',
'run')) {
224 foreach ($array_match_file_to_database as $key => $val) {
228 $hexa .= $key.
'='.$val;
231 $objimport->model_name = $import_name;
232 $objimport->datatoimport = $datatoimport;
233 $objimport->hexa = $hexa;
234 $objimport->fk_user = (
GETPOST(
'visibility',
'aZ09') ==
'all' ? 0 : $user->id);
236 $result = $objimport->create($user);
238 setEventMessages($langs->trans(
"ImportModelSaved", $objimport->model_name),
null,
'mesgs');
241 $langs->load(
"errors");
242 if ($objimport->errno ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
243 setEventMessages($langs->trans(
"ErrorImportDuplicateProfil"),
null,
'errors');
245 setEventMessages($objimport->error,
null,
'errors');
249 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentities(
"ImportModelName")),
null,
'errors');
253if ($step == 2 && $datatoimport) {
261 if (is_numeric($resultupload) && $resultupload > 0) {
262 dol_syslog(
"File ".$fullpath.
" was added for import");
264 $langs->load(
"errors");
265 setEventMessages($langs->trans(
"ErrorFailedToSaveFile"),
null,
'errors');
270 if ($action ==
'confirm_deletefile' && $confirm ==
'yes' && $user->hasRight(
'import',
'run')) {
271 $langs->load(
"other");
273 $param =
'&datatoimport='.urlencode($datatoimport).
'&format='.urlencode($format);
274 if ($excludefirstline) {
275 $param .=
'&excludefirstline='.urlencode($excludefirstline);
278 $param .=
'&endatlinenb='.urlencode($endatlinenb);
284 setEventMessages($langs->trans(
"FileWasRemoved",
GETPOST(
'urlfile')),
null,
'mesgs');
286 $langs->load(
'errors');
287 setEventMessages($langs->trans(
"ErrorFailToDeleteFile",
GETPOST(
'urlfile')),
null,
'errors');
289 header(
'Location: '.$_SERVER[
"PHP_SELF"].
'?step='.$step.$param);
294if ($step == 4 && $action ==
'select_model' && $user->hasRight(
'import',
'run')) {
296 $_SESSION[
"dol_array_match_file_to_database"] =
'';
297 $serialized_array_match_file_to_database =
'';
298 $array_match_file_to_database = array();
302 $result = $objimport->fetch($importmodelid);
304 $serialized_array_match_file_to_database = $objimport->hexa;
305 $fieldsarray = explode(
',', $serialized_array_match_file_to_database);
306 foreach ($fieldsarray as $elem) {
307 $tabelem = explode(
'=', $elem);
311 $array_match_file_to_database[$key] = $val;
314 $_SESSION[
"dol_array_match_file_to_database"] = $serialized_array_match_file_to_database;
315 $_SESSION[
'dol_array_match_file_to_database_select'] = $_SESSION[
"dol_array_match_file_to_database"];
318if ($action ==
'saveselectorder' && $user->hasRight(
'import',
'run')) {
320 $serialized_array_match_file_to_database =
'';
322 $selectorder = explode(
",",
GETPOST(
'selectorder'));
323 $fieldtarget = $fieldstarget = $objimport->array_import_fields[0];
324 foreach ($selectorder as $key => $code) {
325 $serialized_array_match_file_to_database .= $key.
'='.$code;
326 $serialized_array_match_file_to_database .=
',';
328 $serialized_array_match_file_to_database = substr($serialized_array_match_file_to_database, 0, -1);
329 dol_syslog(
'dol_array_match_file_to_database_select='.$serialized_array_match_file_to_database);
330 $_SESSION[
"dol_array_match_file_to_database_select"] = $serialized_array_match_file_to_database;
340$help_url =
'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones';
343if ($step == 1 || !$datatoimport) {
345 $serialized_array_match_file_to_database =
'';
346 $array_match_file_to_database = array();
347 $_SESSION[
"dol_array_match_file_to_database"] =
'';
348 $_SESSION[
"dol_array_match_file_to_database_select"] =
'';
351 if ($excludefirstline) {
352 $param .=
'&excludefirstline='.urlencode($excludefirstline);
355 $param .=
'&endatlinenb='.urlencode($endatlinenb);
358 $param .=
'&separator='.urlencode($separator);
361 $param .=
'&enclosure='.urlencode($enclosure);
364 llxHeader(
'', $langs->trans(
"NewImport"), $help_url);
370 print
'<div class="opacitymedium">'.$langs->trans(
"SelectImportDataSet").
'</div>';
374 $nbmodulesnotautoenabled = count(
$conf->modules);
375 $listofmodulesautoenabled = array(
'user',
'agenda',
'fckeditor',
'export',
'import');
376 foreach ($listofmodulesautoenabled as $moduleautoenable) {
377 if (in_array($moduleautoenable,
$conf->modules)) {
378 $nbmodulesnotautoenabled--;
382 if ($user->admin && $nbmodulesnotautoenabled <
getDolGlobalInt(
'MAIN_MIN_NB_ENABLED_MODULE_FOR_WARNING', 1)) {
383 print
info_admin($langs->trans(
"WarningOnlyProfilesOfActivatedModules").
' '.$langs->trans(
"YouCanEnableModulesFrom"));
389 print
'<div class="div-table-responsive-no-min">';
390 print
'<table class="noborder centpercent nomarginbottom">';
391 print
'<tr class="liste_titre">';
392 print
'<td>'.$langs->trans(
"Module").
'</td>';
393 print
'<td>'.$langs->trans(
"ImportableDatas").
'</td>';
394 print
'<td> </td>';
397 if (count($objimport->array_import_module)) {
398 $sortedarrayofmodules =
dol_sort_array($objimport->array_import_module,
'position_of_profile',
'asc', 0, 0, 1);
399 foreach ($sortedarrayofmodules as $key => $value) {
401 $titleofmodule = $objimport->array_import_module[$key][
'module']->getName();
402 print
'<tr class="oddeven"><td class="tdoverflowmax200" title="'.dolPrintHTML($titleofmodule).
'">';
404 if (in_array($objimport->array_import_code[$key], array(
'produit_supplierprices',
'produit_multiprice',
'produit_languages'))) {
405 $titleofmodule = $langs->trans(
"ProductOrService");
407 print dolPrintHTML($titleofmodule);
409 $entity = preg_replace(
'/:.*$/',
'', $objimport->array_import_icon[$key]);
410 $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
411 $label = $objimport->array_import_label[$key];
412 print
'<div class="twolinesmax-normallineheight minwidth200onall">';
413 print img_object($objimport->array_import_module[$key][
'module']->getName(), $entityicon,
'class="pictofixedwidth"');
414 print dolPrintHTML($label);
416 print
'</td><td style="text-align: right">';
417 if ($objimport->array_import_perms[$key]) {
418 print
'<a href="'.DOL_URL_ROOT.
'/imports/import.php?step=2&datatoimport='.$objimport->array_import_code[$key].$param.
'">'.img_picto($langs->trans(
"NewImport"),
'next',
'class="fa-15"').
'</a>';
420 print $langs->trans(
"NotEnoughPermissions");
425 print
'<tr><td class="oddeven" colspan="3">'.$langs->trans(
"NoImportableData").
'</td></tr>';
435if ($step == 2 && $datatoimport) {
436 $param =
'&datatoimport='.urlencode($datatoimport);
437 if ($excludefirstline) {
438 $param .=
'&excludefirstline='.urlencode($excludefirstline);
441 $param .=
'&endatlinenb='.urlencode($endatlinenb);
444 $param .=
'&separator='.urlencode($separator);
447 $param .=
'&enclosure='.urlencode($enclosure);
450 $list = $objmodelimport->listOfAvailableImportFormat($db);
452 llxHeader(
'', $langs->trans(
"NewImport"), $help_url);
461 if ($action ==
'delete') {
462 print $form->formconfirm($_SERVER[
"PHP_SELF"].
'?urlfile='.urlencode(
GETPOST(
'urlfile')).
'&step=2'.$param, $langs->trans(
'DeleteFile'), $langs->trans(
'ConfirmDeleteFile'),
'confirm_deletefile',
'', 0, 1);
465 print
'<div class="underbanner clearboth"></div>';
466 print
'<div class="fichecenter">';
468 print
'<table class="border tableforfield centpercent">';
471 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"Module").
'</td>';
473 $titleofmodule = $objimport->array_import_module[0][
'module']->getName();
475 if (in_array($objimport->array_import_code[0], array(
'produit_supplierprices',
'produit_multiprice',
'produit_languages'))) {
476 $titleofmodule = $langs->trans(
"ProductOrService");
478 print $titleofmodule;
482 print
'<tr><td>'.$langs->trans(
"DatasetToImport").
'</td>';
484 $entity = preg_replace(
'/:.*$/',
'', $objimport->array_import_icon[0]);
485 $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
486 print img_object($objimport->array_import_module[0][
'module']->getName(), $entityicon).
' ';
487 print $objimport->array_import_label[0];
517 if ($format ==
'xlsx' && !class_exists(
'XMLWriter')) {
518 $langs->loadLangs(array(
"install",
"errors"));
519 print
info_admin($langs->trans(
"ErrorPHPDoesNotSupport",
'php-xml'), 0, 0,
'1',
'error');
525 print
'<form name="userfile" action="'.$_SERVER[
"PHP_SELF"].
'" enctype="multipart/form-data" method="POST">';
526 print
'<input type="hidden" name="token" value="'.newToken().
'">';
527 print
'<input type="hidden" value="'.$step.
'" name="step">';
528 print
'<input type="hidden" value="'.dol_escape_htmltag($format).
'" name="format">';
529 print
'<input type="hidden" value="'.$excludefirstline.
'" name="excludefirstline">';
530 print
'<input type="hidden" value="'.$endatlinenb.
'" name="endatlinenb">';
531 print
'<input type="hidden" value="'.dol_escape_htmltag($separator).
'" name="separator">';
532 print
'<input type="hidden" value="'.dol_escape_htmltag($enclosure).
'" name="enclosure">';
533 print
'<input type="hidden" value="'.dol_escape_htmltag($datatoimport).
'" name="datatoimport">';
535 print
'<span class="opacitymedium">';
536 $s = $langs->trans(
"ChooseFileToImport",
'{s1}');
537 $s = str_replace(
'{s1}', img_picto(
'',
'next'), $s);
539 print
'</span><br><br>';
544 print
'<table class="noborder centpercent" cellpadding="4">';
546 print
'<tr class="liste_titre"><td colspan="4">';
547 print $langs->trans(
"FileMustHaveOneOfFollowingFormat").
'...';
549 $list = $objmodelimport->listOfAvailableImportFormat($db);
550 foreach ($list as $key) {
551 print
'<tr class="oddeven">';
552 print
'<td width="16">'.img_picto_common($key, $objmodelimport->getPictoForKey($key)).
'</td>';
553 $htmltext = $objmodelimport->getDriverDescForKey($key);
554 print
'<td>'.$form->textwithpicto($objmodelimport->getDriverLabelForKey($key), $htmltext).
'</td>';
555 print
'<td style="text-align:center">';
556 if (empty($objmodelimport->drivererror[$key])) {
557 $filename = $langs->transnoentitiesnoconv(
"ExampleOfImportFile").
'_'.$datatoimport.
'.'.$key;
558 print
'<a href="'.DOL_URL_ROOT.
'/imports/emptyexample.php?format='.$key.$param.
'&output=file&file='.urlencode($filename).
'" target="_blank" rel="noopener noreferrer">';
559 print img_picto(
'',
'download',
'class="paddingright opacitymedium"');
560 print $langs->trans(
"DownloadEmptyExampleShort");
562 print $form->textwithpicto(
'', $langs->trans(
"DownloadEmptyExample").
'.<br>'.$langs->trans(
"StarAreMandatory"));
564 print dolPrintHTML($objmodelimport->drivererror[$key]);
576 print
'<div class="marginbottomonly">';
578 $maxmin = $maxfilesizearray[
'maxmin'];
580 print
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
582 print
'<input type="file" name="userfile" size="20" maxlength="80"> ';
584 print
'<input type="submit" class="button small" value="'.$langs->trans(
"AddFile").
'"'.$out.
' name="sendit">';
588 $maxphp = @ini_get(
'upload_max_filesize');
589 if (preg_match(
'/k$/i', $maxphp)) {
590 $maxphp = (int) substr($maxphp, 0, -1);
592 if (preg_match(
'/m$/i', $maxphp)) {
593 $maxphp = (int) substr($maxphp, 0, -1) * 1024;
595 if (preg_match(
'/g$/i', $maxphp)) {
596 $maxphp = (int) substr($maxphp, 0, -1) * 1024 * 1024;
598 if (preg_match(
'/t$/i', $maxphp)) {
599 $maxphp = (int) substr($maxphp, 0, -1) * 1024 * 1024 * 1024;
601 $maxphp2 = @ini_get(
'post_max_size');
602 if (preg_match(
'/k$/i', $maxphp2)) {
603 $maxphp2 = (int) substr($maxphp2, 0, -1);
605 if (preg_match(
'/m$/i', $maxphp2)) {
606 $maxphp2 = (int) substr($maxphp2, 0, -1) * 1024;
608 if (preg_match(
'/g$/i', $maxphp2)) {
609 $maxphp2 = (int) substr($maxphp2, 0, -1) * 1024 * 1024;
611 if (preg_match(
'/t$/i', $maxphp2)) {
612 $maxphp2 = (int) substr($maxphp2, 0, -1) * 1024 * 1024 * 1024;
616 $maxphptoshow = $maxphptoshowparam =
'';
618 $maxmin = min($max, $maxphp);
619 $maxphptoshow = $maxphp;
620 $maxphptoshowparam =
'upload_max_filesize';
623 $maxmin = min($max, $maxphp2);
624 if ($maxphp2 < $maxphp) {
625 $maxphptoshow = $maxphp2;
626 $maxphptoshowparam =
'post_max_size';
630 $langs->load(
'other');
632 $out .=
info_admin($langs->trans(
"ThisLimitIsDefinedInSetup", $max, $maxphptoshow), 1);
634 $out .=
' ('.$langs->trans(
"UploadDisabled").
')';
641 $filearray =
dol_dir_list(
$conf->import->dir_temp,
'files', 0,
'',
'',
'name', SORT_DESC);
642 if (count($filearray) > 0) {
643 print
'<div class="div-table-responsive-no-min">';
644 print
'<table class="noborder centpercent" width="100%" cellpadding="4">';
646 $dir =
$conf->import->dir_temp;
650 foreach ($filearray as $key => $val) {
651 $file = $val[
'name'];
655 $file = mb_convert_encoding($file,
'UTF-8',
'ISO-8859-1');
658 if (preg_match(
'/^\./', $file)) {
662 $modulepart =
'import';
663 $urlsource = $_SERVER[
"PHP_SELF"].
'?step='.$step.$param.
'&filetoimport='.urlencode($filetoimport);
664 $relativepath = $file;
666 print
'<tr class="oddeven">';
668 print
img_mime($file,
'',
'pictofixedwidth');
669 print
'<a data-ajax="false" href="'.DOL_URL_ROOT.
'/document.php?modulepart='.$modulepart.
'&file='.urlencode($relativepath).
'&step=2'.$param.
'" target="_blank" rel="noopener noreferrer">';
674 print
'<td style="text-align:right">'.dol_print_size(
dol_filesize($dir.
'/'.$file)).
'</td>';
676 print
'<td style="text-align:right">'.dol_print_date(
dol_filemtime($dir.
'/'.$file),
'dayhour').
'</td>';
678 print
'<td style="text-align:right"><a href="'.$_SERVER[
'PHP_SELF'].
'?action=delete&token='.
newToken().
'&step=2'.$param.
'&urlfile='.urlencode($relativepath);
679 print
'">'.img_delete().
'</a></td>';
681 print
'<td style="text-align:right">';
682 print
'<a href="'.$_SERVER[
'PHP_SELF'].
'?step=3'.$param.
'&filetoimport='.urlencode($relativepath).
'">'.img_picto($langs->trans(
"ImportThisFile"),
'next',
'class="fa-15"').
'</a>';
696if ($step == 3 && $datatoimport) {
698 $serialized_array_match_file_to_database = isset($_SESSION[
"dol_array_match_file_to_database_select"]) ? $_SESSION[
"dol_array_match_file_to_database_select"] :
'';
699 $fieldsarray = explode(
',', $serialized_array_match_file_to_database);
700 $array_match_file_to_database = array();
701 foreach ($fieldsarray as $elem) {
702 $tabelem = explode(
'=', $elem, 2);
704 $val = (isset($tabelem[1]) ? $tabelem[1] :
'');
706 $array_match_file_to_database[$key] = $val;
713 if (empty($format) && !empty($filetoimport)) {
714 $format = pathinfo($filetoimport, PATHINFO_EXTENSION);
720 if ($model ===
'txt') {
724 $list = $objmodelimport->listOfAvailableImportFormat($db);
726 if (empty($separator)) {
727 $separator = (!
getDolGlobalString(
'IMPORT_CSV_SEPARATOR_TO_USE') ?
',' :
$conf->global->IMPORT_CSV_SEPARATOR_TO_USE);
731 if ($model ==
'csv' && strlen($separator) == 1 && !
GETPOSTISSET(
'separator')) {
732 '@phan-var-force ImportCsv $obj';
734 $fh = fopen(
$conf->import->dir_temp.
'/'.$filetoimport,
'r');
736 $sline = fgets($fh, 1000000);
738 $nboccurence = substr_count($sline, $separator);
739 $nboccurencea = substr_count($sline,
',');
740 $nboccurenceb = substr_count($sline,
';');
742 if ($nboccurence == 0) {
743 if ($nboccurencea > 2) {
745 } elseif ($nboccurenceb > 2) {
753 $separator_used = str_replace(
'\t',
"\t", $separator);
756 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/import/";
757 $file =
"import_".$model.
".modules.php";
758 $classname =
"Import".ucfirst($model);
759 require_once $dir.$file;
760 $obj =
new $classname($db, $datatoimport);
761 '@phan-var-force ModeleImports $obj';
763 if (!empty($obj->error)) {
764 $langs->load(
"errors");
765 $param =
'&datatoimport='.$datatoimport.
'&format='.$format;
766 setEventMessages($obj->error,
null,
'errors');
767 header(
"Location: ".$_SERVER[
"PHP_SELF"].
'?step=2'.$param.
'&filetoimport='.urlencode($relativepath));
771 if ($model ==
'csv') {
772 '@phan-var-force ImportCsv $obj';
773 $obj->separator = $separator_used;
774 $obj->enclosure = $enclosure;
777 if ($model ==
'xlsx') {
778 '@phan-var-force ImportXlsx $obj';
779 if (!preg_match(
'/\.xlsx$/i', $filetoimport)) {
780 $langs->load(
"errors");
781 $param =
'&datatoimport='.$datatoimport.
'&format='.$format;
782 setEventMessages($langs->trans(
"ErrorFileMustHaveFormat", $model),
null,
'errors');
783 header(
"Location: ".$_SERVER[
"PHP_SELF"].
'?step=2'.$param.
'&filetoimport='.urlencode($relativepath));
789 $array_match_file_to_database = array();
793 $fieldssource = array();
795 $result = $obj->import_open_file(
$conf->import->dir_temp.
'/'.$filetoimport);
798 $arrayrecord = $obj->import_read_record();
802 foreach ($arrayrecord as $key => $val) {
803 if ($val[
"type"] != -1) {
804 $fieldssource[$i][
'example1'] =
dol_trunc($val[
'val'], 128);
806 $fieldssource[$i][
'example1'] = $langs->trans(
'Empty');
808 $fieldssource[$i][
'imported'] =
false;
811 $obj->import_close_file();
815 $fieldstarget = $objimport->array_import_fields[0];
816 $minpos = min(count($fieldssource), count($fieldstarget));
820 $initialloadofstep4 =
false;
821 if (empty($_SESSION[
'dol_array_match_file_to_database_select'])) {
822 $initialloadofstep4 =
true;
826 if (count($array_match_file_to_database) == 0) {
832 $num = count($fieldssource);
833 while ($pos <= $num) {
834 if ($num >= 1 && $pos <= $num) {
836 foreach ($fieldstarget as $key => $val) {
837 if ($posbis < $pos) {
842 $array_match_file_to_database[$pos] = $key;
849 $array_match_database_to_file = array_flip($array_match_file_to_database);
853 $fieldstarget_tmp = array();
854 $arraykeysfieldtarget = array_keys($fieldstarget);
856 foreach ($fieldstarget as $key => $label) {
857 $isrequired = preg_match(
'/\*$/', $label);
858 if (!empty($isrequired)) {
859 $newlabel = substr($label, 0, -1);
860 $fieldstarget_tmp[$key] = array(
"label" => (
string) $newlabel,
"required" =>
true);
862 $fieldstarget_tmp[$key] = array(
"label" => (
string) $label,
"required" =>
false);
864 if (!empty($array_match_database_to_file[$key])) {
865 $fieldstarget_tmp[$key][
"imported"] =
true;
866 $fieldstarget_tmp[$key][
"position"] = (int) $array_match_database_to_file[$key] - 1;
868 while (!empty($array_match_database_to_file[$keytoswap])) {
869 if ($position + 1 > $array_match_database_to_file[$keytoswap]) {
870 $keytoswapwith = $array_match_database_to_file[$keytoswap] - 1;
871 $tmp = [$keytoswap => $fieldstarget_tmp[$keytoswap]];
872 unset($fieldstarget_tmp[$keytoswap]);
873 $fieldstarget_tmp =
arrayInsert($fieldstarget_tmp, $keytoswapwith, $tmp);
874 $keytoswapwith = $arraykeysfieldtarget[$array_match_database_to_file[$keytoswap] - 1];
875 $tmp = $fieldstarget_tmp[$keytoswapwith];
876 unset($fieldstarget_tmp[$keytoswapwith]);
877 $fieldstarget_tmp[$keytoswapwith] = $tmp;
878 $keytoswap = $keytoswapwith;
884 $fieldstarget_tmp[$key][
"imported"] =
false;
888 $fieldstarget = $fieldstarget_tmp;
897 $param =
'&format='.$format.
'&datatoimport='.urlencode($datatoimport).
'&filetoimport='.urlencode($filetoimport);
898 if ($excludefirstline) {
899 $param .=
'&excludefirstline='.urlencode($excludefirstline);
902 $param .=
'&endatlinenb='.urlencode($endatlinenb);
905 $param .=
'&separator='.urlencode($separator);
908 $param .=
'&enclosure='.urlencode($enclosure);
911 llxHeader(
'', $langs->trans(
"NewImport"), $help_url);
915 $titleofmodule = $objimport->array_import_module[0][
'module']->getName();
919 print
'<div class="underbanner clearboth"></div>';
920 print
'<div class="fichecenter">';
922 print
'<table class="centpercent border tableforfield">';
925 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"Module").
'</td>';
928 if (in_array($objimport->array_import_code[0], array(
'produit_supplierprices',
'produit_multiprice',
'produit_languages'))) {
929 $titleofmodule = $langs->trans(
"ProductOrService");
931 print dolPrintHTML($titleofmodule);
935 print
'<tr><td>'.$langs->trans(
"DatasetToImport").
'</td>';
937 $entity = preg_replace(
'/:.*$/',
'', $objimport->array_import_icon[0]);
938 $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
939 print img_object($objimport->array_import_module[0][
'module']->getName(), $entityicon).
' ';
940 print $objimport->array_import_label[0];
950 print
load_fiche_titre($langs->trans(
"InformationOnSourceFile"),
'',
'file-export');
952 print
'<div class="underbanner clearboth"></div>';
953 print
'<div class="fichecenter">';
954 print
'<table width="100%" class="border tableforfield">';
957 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"SourceFileFormat").
'</td>';
958 print
'<td class="nowraponall">';
959 $text = $objmodelimport->getDriverDescForKey($format);
961 print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
965 if ($model ==
'csv') {
966 '@phan-var-force ImportCsv $obj';
967 print
'<tr><td>'.$langs->trans(
"CsvOptions").
'</td>';
969 print
'<form method="POST">';
970 print
'<input type="hidden" name="token" value="'.newToken().
'">';
971 print
'<input type="hidden" value="'.$step.
'" name="step">';
972 print
'<input type="hidden" value="'.$format.
'" name="format">';
973 print
'<input type="hidden" value="'.$excludefirstline.
'" name="excludefirstline">';
974 print
'<input type="hidden" value="'.$endatlinenb.
'" name="endatlinenb">';
975 print
'<input type="hidden" value="'.$datatoimport.
'" name="datatoimport">';
976 print
'<input type="hidden" value="'.$filetoimport.
'" name="filetoimport">';
977 print $langs->trans(
"Separator").
' : ';
978 print
'<input type="text" class="width25 center" name="separator" value="'.dol_escape_htmltag($separator).
'"/>';
979 print
' '.$langs->trans(
"Enclosure").
' : ';
980 print
'<input type="text" class="width25 center" name="enclosure" value="'.dol_escape_htmltag($enclosure).
'"/> ';
981 print
'<input name="update" type="submit" value="'.$langs->trans(
'Update').
'" class="button smallpaddingimp" />';
987 print
'<tr><td>'.$langs->trans(
"FileToImport").
'</td>';
989 $modulepart =
'import';
990 $relativepath =
GETPOST(
'filetoimport');
991 print
'<a data-ajax="false" href="'.DOL_URL_ROOT.
'/document.php?modulepart='.$modulepart.
'&file='.urlencode($relativepath).
'&step=3'.$param.
'" target="_blank" rel="noopener noreferrer">';
992 print
img_mime($file,
'',
'pictofixedwidth');
994 print img_picto($langs->trans(
"Download"),
'download',
'class="paddingleft opacitymedium"');
1007 print
'<!-- List of source fields -->'.
"\n";
1008 print
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
1009 print
'<input type="hidden" name="token" value="'.newToken().
'">';
1010 print
'<input type="hidden" name="action" value="select_model">';
1011 print
'<input type="hidden" name="step" value="3">';
1012 print
'<input type="hidden" name="format" value="'.$format.
'">';
1013 print
'<input type="hidden" name="datatoimport" value="'.$datatoimport.
'">';
1014 print
'<input type="hidden" name="filetoimport" value="'.$filetoimport.
'">';
1015 print
'<input type="hidden" name="excludefirstline" value="'.$excludefirstline.
'">';
1016 print
'<input type="hidden" name="endatlinenb" value="'.$endatlinenb.
'">';
1017 print
'<input type="hidden" name="separator" value="'.dol_escape_htmltag($separator).
'">';
1018 print
'<input type="hidden" name="enclosure" value="'.dol_escape_htmltag($enclosure).
'">';
1021 print
'<div class="marginbottomonly">';
1022 print
'<span class="opacitymedium">';
1023 $s = $langs->trans(
"SelectImportFieldsSource",
'{s1}');
1024 $s = str_replace(
'{s1}', img_picto(
'',
'grip_title',
'', 0, 0, 0,
'',
'', 0), $s);
1027 $htmlother->select_import_model((
string) $importmodelid,
'importmodelid', $datatoimport, 1, $user->id);
1028 print
'<input type="submit" class="button smallpaddingimp reposition nomargintop nomarginbottom" id="applyprofile" name="applyprofile" value="'.$langs->trans(
"Apply").
'">';
1030 if (!empty(
$conf->use_javascript_ajax)) {
1032 $(document).ready(function() {
1034 $("#applyprofile").hide();
1036 // follow change on list
1037 $("#importmodelid").change(function() {
1038 console.log("We select a new profile");
1039 if ($(this).val() && $(this).val() != "-1") {
1040 $("#applyprofile").show();
1042 $("#applyprofile").hide();
1056 print
'<div class="div-table-responsive-no-min">';
1057 print
'<table class="noborder centpercent">';
1058 print
'<tr class="liste_titre">';
1059 print
'<td>'.$langs->trans(
"FieldsInSourceFile").
'</td>';
1060 print
'<td>'.$langs->trans(
"FieldsInTargetDatabase").
'</td>';
1065 print
'<tr valign="top"><td width="50%" class="nopaddingleftimp">';
1067 $fieldsplaced = array();
1068 $valforsourcefieldnb = array();
1069 $listofkeys = array();
1070 foreach ($array_match_file_to_database as $key => $val) {
1071 $listofkeys[$key] = 1;
1074 print
"\n<!-- Box left container -->\n";
1075 print
'<div id="left" class="connectedSortable">'.
"\n";
1080 foreach ($fieldssource as $key => $val) {
1081 show_elem($fieldssource, $key, (
string) $key);
1082 $listofkeys[$key] = 1;
1083 $fieldsplaced[$key] = 1;
1084 $valforsourcefieldnb[$lefti] = $key;
1094 print
"<!-- End box left container -->\n";
1097 print
'</td><td width="50%" class="nopaddingrightimp">';
1101 $optionsall = array();
1102 foreach ($fieldstarget as $code => $line) {
1103 $tmparray = explode(
'|', $line[
"label"]);
1105 foreach ($tmparray as $tmpkey => $tmpval) {
1106 $labeltoshow .= ($labeltoshow ?
' '.$langs->trans(
'or').
' ' :
'').$langs->transnoentities($tmpval);
1111 $optionsall[$code] = array(
1112 'labelkey' => $line[
'label'],
1113 'labelkeyarray' => $tmparray,
1114 'label' => $labeltoshow,
1115 'required' => (empty($line[
"required"]) ? 0 : 1),
1116 'position' => (!empty($line[
'position']) ? $line[
'position'] : 0),
1124 $mandatoryfieldshavesource =
true;
1137 $modetoautofillmapping =
'session';
1138 if ($initialloadofstep4) {
1139 $modetoautofillmapping =
'guess';
1143 print
'<table class="nobordernopadding centpercent tableimport">';
1144 foreach ($fieldssource as $code => $line) {
1149 print
'<tr style="height:'.$height.
'" class="trimport oddevenimport">';
1151 $entity = (!empty($objimport->array_import_entities[0][$code]) ? $objimport->array_import_entities[0][$code] : $objimport->array_import_icon[0]);
1153 $entityicon = !empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity;
1154 $entitylang = !empty($entitytolang[$entity]) ? $entitytolang[$entity] : $objimport->array_import_label[0];
1156 print
'<td class="nowraponall hideonsmartphone" style="font-weight: normal">=> </td>';
1157 print
'<td class="nowraponall" style="font-weight: normal">';
1159 $selectforline =
'';
1160 $selectforline .=
'<select id="selectorderimport_'.($i + 1).
'" class="targetselectchange minwidth300" name="select_'.($i + 1).
'">';
1161 $selectforline .=
'<option value="-1"> </option>';
1164 $codeselectedarray = array();
1165 foreach ($optionsall as $tmpcode => $tmpval) {
1167 if (!empty($tmpval[
'picto'])) {
1168 $label .= img_picto(
'', $tmpval[
'picto'],
'class="pictofixedwidth"');
1170 $label .= $tmpval[
'required'] ?
'<strong>' :
'';
1171 $label .= $tmpval[
'label'];
1172 $label .= $tmpval[
'required'] ?
'*</strong>' :
'';
1174 $tablealias = preg_replace(
'/(\..*)$/i',
'', $tmpcode);
1175 $tablename = !empty($objimport->array_import_tables[0][$tablealias]) ? $objimport->array_import_tables[0][$tablealias] :
"";
1179 $filecolumn = ($i + 1);
1181 if (empty($objimport->array_import_convertvalue[0][$tmpcode])) {
1184 if ($objimport->array_import_convertvalue[0][$tmpcode][
'rule'] ==
'fetchidfromref') {
1185 $htmltext .= $langs->trans(
"DataComeFromIdFoundFromRef", $langs->transnoentitiesnoconv($entitylang)).
'<br>';
1187 if ($objimport->array_import_convertvalue[0][$tmpcode][
'rule'] ==
'fetchidfromcodeid') {
1188 $dictlabel = $objimport->array_import_convertvalue[0][$tmpcode][
'dict'] ??
'';
1189 $htmltext .= $langs->trans(
"DataComeFromIdFoundFromCodeId", $langs->transnoentitiesnoconv($dictlabel)).
'<br>';
1193 $example = !empty($objimport->array_import_examplevalues[0][$tmpcode]) ? $objimport->array_import_examplevalues[0][$tmpcode] :
"";
1195 if (empty($objimport->array_import_convertvalue[0][$tmpcode])) {
1197 $htmltext .= $langs->trans(
"SourceExample").
': <b>'.str_replace(
'"',
'', $example).
'</b><br>';
1200 if ($objimport->array_import_convertvalue[0][$tmpcode][
'rule'] ==
'fetchidfromref') {
1201 $htmltext .= $langs->trans(
"SourceExample").
': <b>'.$langs->transnoentitiesnoconv(
"ExampleAnyRefFoundIntoElement", $entitylang).($example ?
' ('.$langs->transnoentitiesnoconv(
"Example").
': '.str_replace(
'"',
'', $example).
')' :
'').
'</b><br>';
1202 } elseif ($objimport->array_import_convertvalue[0][$tmpcode][
'rule'] ==
'fetchidfromcodeid') {
1203 $dictlabel = $objimport->array_import_convertvalue[0][$tmpcode][
'dict'] ??
'';
1204 $htmltext .= $langs->trans(
"SourceExample").
': <b>'.$langs->trans(
"ExampleAnyCodeOrIdFoundIntoDictionary", $langs->transnoentitiesnoconv($dictlabel)).($example ?
' ('.$langs->transnoentitiesnoconv(
"Example").
': '.str_replace(
'"',
'', $example).
')' :
'').
'</b><br>';
1205 } elseif ($example) {
1206 $htmltext .= $langs->trans(
"SourceExample").
': <b>'.str_replace(
'"',
'', $example).
'</b><br>';
1210 if (!empty($objimport->array_import_regex[0][$tmpcode])) {
1211 $htmltext .= $langs->trans(
"FormatControlRule").
': <b>'.str_replace(
'"',
'', $objimport->array_import_regex[0][$tmpcode]).
'</b><br>';
1215 $htmltext .= $langs->trans(
"InformationOnTargetTables").
': <b>'.$tablename.
"->".preg_replace(
'/^.*\./',
'', $tmpcode).
"</b>";
1217 $labelhtml = $label.
' '.$form->textwithpicto(
'', $htmltext, 1,
'help',
'', 1);
1220 $selectforline .=
'<option value="'.$tmpcode.
'"';
1221 if ($modetoautofillmapping ==
'orderoftargets') {
1224 $selectforline .=
' selected';
1226 } elseif ($modetoautofillmapping ==
'guess') {
1230 if (preg_match(
'/^(.+)\((.+\..+)\)$/', $line[
'example1'], $regs)) {
1231 $tmpstring1 = $regs[1];
1232 $tmpstring2 = $regs[2];
1234 $tmpstring1 = $line[
'example1'];
1242 foreach ($tmpval[
'labelkeyarray'] as $tmpval2) {
1243 $labeltarget = $langs->transnoentities($tmpval2);
1245 if ($tmpstring1 && ($tmpstring1 == $tmpcode || $tmpstring1 == strtolower($labeltarget)
1246 || $tmpstring1 == strtolower(
dol_string_unaccent($labeltarget)) || $tmpstring1 == strtolower($tmpval2))) {
1247 if (empty($codeselectedarray[$code])) {
1248 $selectforline .=
' selected';
1249 $codeselectedarray[$code] = 1;
1252 } elseif ($tmpstring2 && ($tmpstring2 == $tmpcode || $tmpstring2 == strtolower($labeltarget)
1253 || $tmpstring2 == strtolower(
dol_string_unaccent($labeltarget)) || $tmpstring2 == strtolower($tmpval2))) {
1254 if (empty($codeselectedarray[$code])) {
1255 $selectforline .=
' selected';
1256 $codeselectedarray[$code] = 1;
1261 } elseif ($modetoautofillmapping ==
'session' && !empty($_SESSION[
'dol_array_match_file_to_database_select'])) {
1262 $tmpselectioninsession =
dolExplodeIntoArray($_SESSION[
'dol_array_match_file_to_database_select'],
',',
'=');
1264 if (!empty($tmpselectioninsession[(
string) ($i + 1)]) && $tmpselectioninsession[(
string) ($i + 1)] == $tmpcode) {
1265 $selectforline .=
' selected';
1267 $selectforline .=
' data-debug="'.$tmpcode.
'-'.$code.
'-'.$j.
'-'.(!empty($tmpselectioninsession[(
string) ($i + 1)]) ? $tmpselectioninsession[(string) ($i + 1)] :
"").
'"';
1269 $selectforline .=
' data-html="'.dol_escape_htmltag($labelhtml).
'"';
1270 $selectforline .=
'>';
1271 $selectforline .= $label;
1272 $selectforline .=
'</options>';
1275 $selectforline .=
'</select>';
1276 $selectforline .=
ajax_combobox(
'selectorderimport_'.($i + 1));
1278 print $selectforline;
1283 print
'<td class="nowraponall" style="font-weight:normal; text-align:right">';
1286 $htmltext =
'<b><u>'.$langs->trans(
"FieldSource").
'</u></b><br>';
1288 $htmltext .= $langs->trans(
"DataComeFromFileFieldNb", $filecolumntoshow).
'<br>';
1290 print $form->textwithpicto(
'', $htmltext);
1301 print
'<tr class="liste_titre"><td colspan="2">'.$langs->trans(
"Note").
'</td></tr>';
1302 print
'<tr><td colspan="2"><div id="div-mandatory-target-fields-not-mapped"></div></td></tr>';
1308 if (!empty(
$conf->use_javascript_ajax)) {
1309 print
'<script type="text/javascript">'.
"\n";
1310 print
'var previousselectedvalueimport = "0";'.
"\n";
1311 print
'var previousselectedlabelimport = "0";'.
"\n";
1312 print
'var arrayofselectedvalues = [];'.
"\n";
1313 print
'var arrayoftargetfields = [];'.
"\n";
1314 print
'var arrayoftargetmandatoryfields = [];'.
"\n";
1318 foreach ($fieldstarget as $key => $val) {
1319 print
"arrayoftargetfields[".$tmpi.
"] = '".
dol_escape_js($langs->trans($val[
'label'])).
"'; ";
1320 if ($val[
'required']) {
1321 print
"arrayoftargetmandatoryfields[".$tmpi.
"] = '".
dol_escape_js($key).
"'; ";
1327 print
'$(document).ready(function () {'.
"\n";
1329 print
'setOptionsToDisabled();'.
"\n";
1330 print
'saveSelection();'.
"\n";
1332 print
'$(".targetselectchange").focus(function(){'.
"\n";
1333 print
' previousselectedvalueimport = $(this).val();'.
"\n";
1334 print
' previousselectedlabelimport = $(this).children("option:selected").text();'.
"\n";
1335 print
' console.log("previousselectedvalueimport="+previousselectedvalueimport)'.
"\n";
1342 print
'function setOptionsToDisabled() {'.
"\n";
1343 print
' console.log("Remove the disabled flag everywhere");'.
"\n";
1344 print
' $("select.targetselectchange").not($( this )).find(\'option\').prop("disabled", false);'.
"\n";
1345 print
' arrayofselectedvalues = [];'.
"\n";
1347 print
' $("select.targetselectchange").each(function(){'.
"\n";
1348 print
' id = $(this).attr(\'id\')'.
"\n";
1349 print
' value = $(this).val()'.
"\n";
1350 print
' console.log("a selected value has been found for component "+id+" = "+value);'.
"\n";
1351 print
' arrayofselectedvalues.push(value);'.
"\n";
1354 print
' console.log("List of all selected values arrayofselectedvalues");'.
"\n";
1355 print
' console.log(arrayofselectedvalues);'.
"\n";
1356 print
' console.log("Set the option to disabled for every entry that is currently selected somewhere else (so into arrayofselectedvalues)");'.
"\n";
1358 print
' $.each(arrayofselectedvalues, function(key, value) {'.
"\n";
1359 print
' if (value != -1) {'.
"\n";
1360 print
' console.log("Process key="+key+" value="+value+" to disable.");'.
"\n";
1361 print
' $("select.targetselectchange").find(\'option[value="\'+value+\'"]:not(:selected)\').prop("disabled", true);'.
"\n";
1367 print
'function saveSelection() {'.
"\n";
1369 print
' arrayselectedfields = [];'.
"\n";
1370 print
' arrayselectedfields.push(0);'.
"\n";
1372 print
' $.each( arrayofselectedvalues, function( key, value ) {'.
"\n";
1373 print
' if (value != -1) {'.
"\n";
1374 print
' arrayselectedfields.push(value);'.
"\n";
1375 print
' } else {'.
"\n";
1376 print
' arrayselectedfields.push(0);'.
"\n";
1380 print
" $.ajax({\n";
1381 print
" type: 'POST',\n";
1382 print
" dataType: 'json',\n";
1383 print
" url: '".dol_escape_js($_SERVER[
"PHP_SELF"]).
"?action=saveselectorder&token=".
newToken().
"',\n";
1384 print
" data: 'selectorder='+arrayselectedfields.toString(),\n";
1385 print
" success: function(){\n";
1386 print
" console.log('The selected fields have been saved into '+arrayselectedfields.toString());\n";
1391 print
' console.log("arrayselectedfields");';
1392 print
' console.log(arrayselectedfields);';
1393 print
' console.log("arrayoftargetmandatoryfields");';
1394 print
' console.log(arrayoftargetmandatoryfields);';
1395 print
" listtoshow = '';";
1396 print
" nbelement = arrayoftargetmandatoryfields.length
1397 for (let i = 0; i < nbelement; i++) {
1398 if (arrayoftargetmandatoryfields[i] && ! arrayselectedfields.includes(arrayoftargetmandatoryfields[i])) {
1399 console.log(arrayoftargetmandatoryfields[i]+' not mapped');
1400 listtoshow = listtoshow + (listtoshow ? ', ' : '') + '<b>' + arrayoftargetfields[i] + '*</b>';
1403 console.log(listtoshow);
1405 listtoshow = '".dol_escape_js(img_warning($langs->trans(
"MandatoryTargetFieldsNotMapped")).
' '.$langs->trans(
"MandatoryTargetFieldsNotMapped")).
": ' + listtoshow;
1406 $('#div-mandatory-target-fields-not-mapped').html(listtoshow);
1408 $('#div-mandatory-target-fields-not-mapped').html('<span class=\"opacitymedium\">".
dol_escape_js($langs->trans(
"AllTargetMandatoryFieldsAreMapped")).
"</span>');
1415 print
'$(".targetselectchange").change(function(){'.
"\n";
1416 print
' setOptionsToDisabled();'.
"\n";
1418 print
' if(previousselectedlabelimport != "" && previousselectedvalueimport != -1) {'.
"\n";
1419 print
' let valuetochange = $(this).val(); '.
"\n";
1420 print
' $(".boxtdunused").each(function(){'.
"\n";
1421 print
' if ($(this).text().includes(valuetochange)){'.
"\n";
1422 print
' arraychild = $(this)[0].childNodes'.
"\n";
1423 print
' arraytexttomodify = arraychild[0].textContent.split(" ")'.
"\n";
1424 print
' arraytexttomodify[1] = previousselectedvalueimport '.
"\n";
1425 print
' textmodified = arraytexttomodify.join(" ") '.
"\n";
1426 print
' arraychild[0].textContent = textmodified'.
"\n";
1427 print
' arraychild[1].innerHTML = previousselectedlabelimport'.
"\n";
1431 print
' $(this).blur()'.
"\n";
1433 print
' saveSelection()'.
"\n";
1437 print
'</script>'.
"\n";
1443 print
'<div class="tabsAction">';
1445 if (count($array_match_file_to_database)) {
1446 if ($mandatoryfieldshavesource) {
1447 print
'<a class="butAction saveorderselect" href="import.php?step=4'.$param.
'&filetoimport='.urlencode($filetoimport).
'">'.$langs->trans(
"NextStep").
'</a>';
1449 print
'<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv(
"SomeMandatoryFieldHaveNoSource")).
'">'.$langs->trans(
"NextStep").
'</a>';
1457 if (count($array_match_file_to_database)) {
1459 print
'<!-- Area to add new import profile -->'.
"\n";
1460 print
'<div class="marginbottomonly"><span class="opacitymedium">'.$langs->trans(
"SaveImportModel").
'</span></div>';
1462 print
'<form class="nocellnopadd" action="'.$_SERVER[
"PHP_SELF"].
'" method="post">';
1463 print
'<input type="hidden" name="token" value="'.newToken().
'">';
1464 print
'<input type="hidden" name="action" value="add_import_model">';
1465 print
'<input type="hidden" name="step" value="'.$step.
'">';
1466 print
'<input type="hidden" name="format" value="'.$format.
'">';
1467 print
'<input type="hidden" name="datatoimport" value="'.$datatoimport.
'">';
1468 print
'<input type="hidden" name="filetoimport" value="'.$filetoimport.
'">';
1469 print
'<input type="hidden" name="hexa" value="'.$hexa.
'">';
1470 print
'<input type="hidden" name="excludefirstline" value="'.$excludefirstline.
'">';
1471 print
'<input type="hidden" name="endatlinenb" value="'.$endatlinenb.
'">';
1472 print
'<input type="hidden" name="page_y" value="">';
1473 print
'<input type="hidden" value="'.dol_escape_htmltag($separator).
'" name="separator">';
1474 print
'<input type="hidden" value="'.dol_escape_htmltag($enclosure).
'" name="enclosure">';
1476 print
'<div class="div-table-responsive-no-min">';
1477 print
'<table summary="selectofimportprofil" class="noborder centpercent">';
1478 print
'<tr class="liste_titre">';
1479 print
'<td>'.$langs->trans(
"ImportModelName").
'</td>';
1480 print
'<td>'.$langs->trans(
"Visibility").
'</td>';
1484 $nameofimportprofile = str_replace(
' ',
'-', $langs->trans(
"ImportProfile").
' '.$titleofmodule.
' '.
dol_print_date(
dol_now(
'gmt'),
'dayxcard'));
1486 $nameofimportprofile = $import_name;
1489 print
'<tr class="oddeven">';
1490 print
'<td><input name="import_name" class="minwidth300" value="'.$nameofimportprofile.
'"></td>';
1492 $arrayvisibility = array(
'private' => $langs->trans(
"Private"),
'all' => $langs->trans(
"Everybody"));
1493 print $form->selectarray(
'visibility', $arrayvisibility,
'private');
1495 print
'<td class="right">';
1496 print
'<input type="submit" class="button smallpaddingimp reposition" value="'.$langs->trans(
"SaveImportProfile").
'">';
1500 $sql =
"SELECT rowid, label, fk_user, entity";
1501 $sql .=
" FROM ".MAIN_DB_PREFIX.
"import_model";
1502 $sql .=
" WHERE type = '".$db->escape($datatoimport).
"'";
1504 $sql .=
" AND fk_user IN (0, ".((int) $user->id).
")";
1506 $sql .=
" ORDER BY rowid";
1508 $resql = $db->query($sql);
1510 $num = $db->num_rows($resql);
1512 $tmpuser =
new User($db);
1516 $obj = $db->fetch_object($resql);
1518 print
'<tr class="oddeven"><td>';
1521 print
'<td class="tdoverflowmax150">';
1522 if (empty($obj->fk_user)) {
1523 print $langs->trans(
"Everybody");
1525 $tmpuser->fetch($obj->fk_user);
1526 print $tmpuser->getNomUrl(-1);
1529 print
'<td class="right">';
1530 print
'<a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?step='.$step.$param.
'&action=deleteprof&token='.
newToken().
'&id='.$obj->rowid.
'&filetoimport='.urlencode($filetoimport).
'">';
1537 dol_print_error($db);
1548if ($step == 4 && $datatoimport) {
1549 $max_execution_time_for_importexport =
getDolGlobalInt(
'IMPORT_MAX_EXECUTION_TIME', 300);
1550 $max_time = @ini_get(
"max_execution_time");
1551 if ($max_time && $max_time < $max_execution_time_for_importexport) {
1552 dol_syslog(
"max_execution_time=".$max_time.
" is lower than max_execution_time_for_importexport=".$max_execution_time_for_importexport.
". We try to increase it dynamically.");
1553 @ini_set(
"max_execution_time", $max_execution_time_for_importexport);
1559 if ($model ===
'txt') {
1563 $list = $objmodelimport->listOfAvailableImportFormat($db);
1566 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/import/";
1567 $file =
"import_".$model.
".modules.php";
1568 $classname =
"Import".ucfirst($model);
1569 require_once $dir.$file;
1570 $obj =
new $classname($db, $datatoimport);
1571 '@phan-var-force ModeleImports $obj';
1572 $obj->importtriggermode = $importtriggermode;
1573 $obj->importissimulation = 1;
1574 if ($model ==
'csv') {
1575 '@phan-var-force ImportCsv $obj';
1576 $obj->separator = $separator_used;
1577 $obj->enclosure = $enclosure;
1581 $fieldssource = array();
1582 $result = $obj->import_open_file(
$conf->import->dir_temp.
'/'.$filetoimport);
1586 $arrayrecord = $obj->import_read_record();
1589 foreach ($arrayrecord as $key => $val) {
1590 $fieldssource[$i][
'example1'] =
dol_trunc($val[
'val'], 24);
1593 $obj->import_close_file();
1596 $nboflines = $obj->import_get_nb_of_lines(
$conf->import->dir_temp.
'/'.$filetoimport);
1598 $param =
'&leftmenu=import&format='.urlencode($format).
'&datatoimport='.urlencode($datatoimport).
'&filetoimport='.urlencode($filetoimport).
'&nboflines='.((int) $nboflines).
'&separator='.urlencode($separator).
'&enclosure='.urlencode($enclosure).
'&importtriggermode='.urlencode($importtriggermode);
1600 if ($excludefirstline) {
1601 $param .=
'&excludefirstline='.urlencode($excludefirstline);
1604 $param .=
'&endatlinenb='.urlencode($endatlinenb);
1606 if (!empty($updatekeys)) {
1607 $param .=
'&updatekeys[]='.implode(
'&updatekeys[]=', $updatekeys);
1610 llxHeader(
'', $langs->trans(
"NewImport"), $help_url);
1615 print
'<form action="'.$_SERVER[
"PHP_SELF"].
'?'.$param2.
'" method="POST">';
1616 print
'<input type="hidden" name="token" value="'.newToken().
'">';
1617 print
'<input type="hidden" name="step" value="4">';
1618 print
'<input type="hidden" name="action" value="launchsimu">';
1619 print
'<input type="hidden" name="importtriggermode" value="'.dol_escape_htmltag($importtriggermode).
'">';
1623 print
'<div class="underbanner clearboth"></div>';
1624 print
'<div class="fichecenter">';
1626 print
'<table width="100%" class="border tableforfield">';
1629 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"Module").
'</td>';
1631 $titleofmodule = $objimport->array_import_module[0][
'module']->getName();
1633 if (in_array($objimport->array_import_code[0], array(
'produit_supplierprices',
'produit_multiprice',
'produit_languages'))) {
1634 $titleofmodule = $langs->trans(
"ProductOrService");
1636 print $titleofmodule;
1640 print
'<tr><td>'.$langs->trans(
"DatasetToImport").
'</td>';
1642 $entity = preg_replace(
'/:.*$/',
'', $objimport->array_import_icon[0]);
1643 $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
1644 print img_object($objimport->array_import_module[0][
'module']->getName(), $entityicon).
' ';
1645 print $objimport->array_import_label[0];
1653 print
load_fiche_titre($langs->trans(
"InformationOnSourceFile"),
'',
'file-export');
1655 print
'<div class="underbanner clearboth"></div>';
1656 print
'<div class="fichecenter">';
1657 print
'<table width="100%" class="border tableforfield">';
1660 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"SourceFileFormat").
'</td>';
1661 print
'<td class="nowraponall">';
1662 $text = $objmodelimport->getDriverDescForKey($format);
1664 print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
1668 if ($model ==
'csv') {
1669 '@phan-var-force ImportCsv $obj';
1670 print
'<tr><td>'.$langs->trans(
"CsvOptions").
'</td>';
1673 print
' '.$langs->trans(
"Enclosure").
' : '.
dol_escape_htmltag($enclosure);
1678 print
'<tr><td>'.$langs->trans(
"FileToImport").
'</td>';
1680 $modulepart =
'import';
1681 $relativepath =
GETPOST(
'filetoimport');
1682 print
'<a data-ajax="false" href="'.DOL_URL_ROOT.
'/document.php?modulepart='.urlencode($modulepart).
'&file='.urlencode($relativepath).
'&step=4'.$param.
'" target="_blank" rel="noopener noreferrer">';
1683 print
img_mime($file,
'',
'pictofixedwidth');
1684 print $filetoimport;
1685 print img_picto($langs->trans(
"Download"),
'download',
'class="paddingleft opacitymedium"');
1691 print $langs->trans(
"NbOfSourceLines");
1698 print $langs->trans(
"ImportFromToLine");
1700 if ($action ==
'launchsimu') {
1701 print
'<input type="number" min="1" class="maxwidth50 right valignmiddle" name="excludefirstlinebis" disabled="disabled" value="'.$excludefirstline.
'">';
1702 print
'<input type="hidden" name="excludefirstline" value="'.$excludefirstline.
'">';
1704 print
'<input type="number" min="1" class="maxwidth50 right valignmiddle" name="excludefirstline" value="'.$excludefirstline.
'">';
1705 print $form->textwithpicto(
"", $langs->trans(
"SetThisValueTo2ToExcludeFirstLine"));
1708 if ($action ==
'launchsimu') {
1709 print
'<input type="text" class="maxwidth50 valignmiddle" name="endatlinenbbis" disabled="disabled" value="'.$endatlinenb.
'">';
1710 print
'<input type="hidden" name="endatlinenb" value="'.$endatlinenb.
'">';
1712 print
'<input type="text" class="maxwidth50 valignmiddle" name="endatlinenb" value="'.$endatlinenb.
'">';
1713 print $form->textwithpicto(
"", $langs->trans(
"KeepEmptyToGoToEndOfFile"));
1715 if ($action ==
'launchsimu') {
1716 print
' <a href="'.$_SERVER[
"PHP_SELF"].
'?step=4'.$param.
'" class="editfielda">';
1718 print img_picto($langs->trans(
"Modify"),
'edit');
1721 if ($excludefirstline == 2) {
1722 print $form->textwithpicto(
"", $langs->trans(
"WarningFirstImportedLine", $excludefirstline), 1,
'warning',
"warningexcludefirstline valignmiddle");
1724 $( document ).ready(function() {
1725 $("input[name=\'excludefirstline\']").on("change",function(){
1726 if($(this).val() <= 1){
1727 $(".warningexcludefirstline").hide();
1729 $(".warningexcludefirstline").show();
1739 print $form->textwithpicto($langs->trans(
"KeysToUseForUpdates"), $langs->trans(
"SelectPrimaryColumnsForUpdateAttempt"));
1741 if ($action ==
'launchsimu') {
1742 if (count($updatekeys)) {
1743 print $form->multiselectarray(
'updatekeysbis', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0,
'', 1,
'80%',
'disabled');
1745 print
'<span class="opacitymedium">'.$langs->trans(
"NoUpdateAttempt").
'</span>';
1747 foreach ($updatekeys as $val) {
1748 print
'<input type="hidden" name="updatekeys[]" value="'.$val.
'">';
1750 print
' <a href="'.$_SERVER[
"PHP_SELF"].
'?step=4'.$param.
'" class="editfielda">';
1752 print img_picto($langs->trans(
"Modify"),
'edit');
1755 if (is_array($objimport->array_import_updatekeys[0]) && count($objimport->array_import_updatekeys[0])) {
1756 print $form->multiselectarray(
'updatekeys', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0,
'', 1,
'80%');
1759 print
'<span class="opacitymedium">'.$langs->trans(
"UpdateNotYetSupportedForThisImport").
'</span>';
1772 print
load_fiche_titre($langs->trans(
"InformationOnTargetTables"),
'',
'file-import');
1774 print
'<div class="underbanner clearboth"></div>';
1775 print
'<div class="fichecenter">';
1777 print
'<table class="centpercent border tableforfield">';
1780 print
'<tr><td class="titlefieldcreate">';
1781 print $langs->trans(
"TablesTarget");
1783 $listtables = array();
1784 $sort_array_match_file_to_database = $array_match_file_to_database;
1785 foreach ($array_match_file_to_database as $code => $label) {
1787 if ($code > count($fieldssource)) {
1791 $alias = preg_replace(
'/(\..*)$/i',
'', $label);
1792 $listtables[$alias] = $objimport->array_import_tables[0][$alias];
1794 if (count($listtables)) {
1797 foreach ($listtables as $val) {
1816 print $langs->trans(
"Error");
1822 print $langs->trans(
"FieldsTarget").
'</td><td class="small">';
1823 $listfields = array();
1826 $sort_array_match_file_to_database = $array_match_file_to_database;
1827 ksort($sort_array_match_file_to_database);
1829 foreach ($sort_array_match_file_to_database as $code => $label) {
1832 if ($code > count($fieldssource)) {
1836 $alias = preg_replace(
'/(\..*)$/i',
'', $label);
1837 $listfields[$i] =
'<span class="nowrap">'.$langs->trans(
"Column").
' '.
num2Alpha((
int) $code - 1).
' -> '.$label.
'</span>';
1839 print count($listfields) ? (implode(
', ', $listfields)) : $langs->trans(
"Error");
1844 print $form->textwithpicto($langs->trans(
"ImportTriggerModeLabel"), $langs->trans(
"ImportTriggerModeHint"));
1846 $triggerModeChoices = array(
1847 'strict_line' => $langs->trans(
"ImportTriggerModeStrictLine"),
1848 'fast_bulk' => $langs->trans(
"ImportTriggerModeFastBulk"),
1850 if ($action ==
'launchsimu') {
1851 print
dol_escape_htmltag($triggerModeChoices[$importtriggermode] ?? $importtriggermode);
1852 print
'<input type="hidden" name="importtriggermode" value="'.dol_escape_htmltag($importtriggermode).
'">';
1853 print
' <a href="'.$_SERVER[
"PHP_SELF"].
'?step=4'.$param.
'" class="editfielda">';
1855 print img_picto($langs->trans(
"Modify"),
'edit');
1858 print $form->selectarray(
'importtriggermode', $triggerModeChoices, $importtriggermode, 0);
1860 if ($importtriggermode ===
'fast_bulk') {
1861 print
'<br><span class="warning">';
1862 print $langs->trans(
"ImportTriggerModeFastBulkWarning");
1873 if ($action !=
'launchsimu') {
1876 print
'<div class="neutral">';
1877 print $langs->trans(
"NowClickToTestTheImport", $langs->transnoentitiesnoconv(
"RunSimulateImportFile")).
'<br>';
1881 print
'<div class="center">';
1882 if ($user->hasRight(
'import',
'run')) {
1883 print
'<input type="submit" class="butAction" value="'.$langs->trans(
"RunSimulateImportFile").
'">';
1885 print
'<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv(
"NotEnoughPermissions")).
'">'.$langs->trans(
"RunSimulateImportFile").
'</a>';
1890 print
'<div class="warning">';
1891 print
'<b>'.$langs->trans(
"ImportTriggerModeSimulationWarningTitle").
'</b><br>';
1892 print $langs->trans(
"ImportTriggerModeSimulationWarning");
1896 $arrayoferrors = array();
1897 $arrayofwarnings = array();
1911 $pathfile =
$conf->import->dir_temp.
'/'.$filetoimport;
1912 $result = $obj->import_open_file($pathfile);
1914 global $tablewithentity_cache;
1915 $tablewithentity_cache = array();
1920 while (($sourcelinenb < $nboflines) && !$endoffile) {
1924 $arrayrecord = $obj->import_read_record();
1925 if ($arrayrecord ===
false) {
1926 $arrayofwarnings[$sourcelinenb][0] = array(
'lib' => $langs->trans(
'ErrorFileLinesReachEOF', $nboflines, $sourcelinenb),
'type' =>
'EOF_RECORD_ON_SEVERAL_LINES');
1930 if ($excludefirstline && ($sourcelinenb < $excludefirstline)) {
1933 if ($endatlinenb && ($sourcelinenb > $endatlinenb)) {
1937 $parameters = array(
1939 'datatoimport' => $datatoimport,
1941 'arrayrecord' => $arrayrecord,
1942 'array_match_file_to_database' => $array_match_file_to_database,
1943 'objimport' => $objimport,
1944 'fieldssource' => $fieldssource,
1945 'importid' => $importid,
1946 'updatekeys' => $updatekeys,
1947 'importtriggermode' => $importtriggermode,
1948 'arrayoferrors' => &$arrayoferrors,
1949 'arrayofwarnings' => &$arrayofwarnings,
1953 $reshook = $hookmanager->executeHooks(
'ImportInsert', $parameters);
1955 $arrayoferrors[$sourcelinenb][] = [
1956 'lib' => implode(
"<br>", array_merge([$hookmanager->error], $hookmanager->errors))
1960 if (empty($reshook)) {
1962 $result = $obj->import_insert($arrayrecord, $array_match_file_to_database, $objimport, count($fieldssource), $importid, $updatekeys);
1964 if (count($obj->errors)) {
1965 $arrayoferrors[$sourcelinenb] = $obj->errors;
1967 if (count($obj->warnings)) {
1968 $arrayofwarnings[$sourcelinenb] = $obj->warnings;
1970 if (!count($obj->errors) && !count($obj->warnings)) {
1975 $reshook = $hookmanager->executeHooks(
'AfterImportInsert', $parameters);
1977 $arrayoferrors[$sourcelinenb][] = [
1978 'lib' => implode(
"<br>", array_merge([$hookmanager->error], $hookmanager->errors))
1983 $obj->import_close_file();
1985 print $langs->trans(
"ErrorFailedToOpenFile", $pathfile);
1992 if (!empty($objimport->array_import_run_sql_after[0]) && is_array($objimport->array_import_run_sql_after[0])) {
1994 foreach ($objimport->array_import_run_sql_after[0] as $sqlafterimport) {
1996 $resqlafterimport = $db->query($sqlafterimport);
1997 if (!$resqlafterimport) {
1998 $arrayoferrors[
'none'][] = array(
'lib' => $langs->trans(
"Error running final request: ".$sqlafterimport));
2007 if (!count($arrayoferrors) && !count($arrayofwarnings)) {
2009 print
'<div class="info">';
2010 print
'<div class=""><b>'.$langs->trans(
"ResultOfSimulationNoError").
'</b></div>';
2011 print $langs->trans(
"NbInsertSim", empty($obj->nbinsert) ? 0 : $obj->nbinsert).
'<br>';
2012 print $langs->trans(
"NbUpdateSim", empty($obj->nbupdate) ? 0 : $obj->nbupdate).
'<br>';
2017 print
'<div class="warning">';
2018 print $langs->trans(
"NbOfLinesOK", $nbok).
'...<br>';
2025 if (count($arrayoferrors)) {
2026 $langs->load(
'errors');
2027 print img_error().
' <b>'.$langs->trans(
"ErrorsOnXLines", count($arrayoferrors)).
'</b><br>';
2028 print
'<table width="100%" class="border"><tr><td>';
2029 foreach ($arrayoferrors as $key => $val) {
2031 if ($nboferrors > $maxnboferrors) {
2032 print $langs->trans(
"TooMuchErrors", (count($arrayoferrors) - $nboferrors)).
"<br>";
2036 foreach ($val as $i => $err) {
2037 print
' > '.dol_escape_htmltag($err[
'lib']).
'<br>';
2040 print
'</td></tr></table>';
2046 if (count($arrayofwarnings)) {
2047 $langs->load(
'errors');
2048 print img_warning().
' <b>'.$langs->trans(
"WarningsOnXLines", count($arrayofwarnings)).
'</b><br>';
2049 print
'<table width="100%" class="border"><tr><td>';
2050 foreach ($arrayofwarnings as $key => $val) {
2052 if ($nbofwarnings > $maxnbofwarnings) {
2053 print $langs->trans(
"TooMuchWarnings", (count($arrayofwarnings) - $nbofwarnings)).
"<br>";
2057 foreach ($val as $i => $err) {
2058 print
' > '.dol_escape_htmltag($err[
'lib']).
'<br>';
2061 print
'</td></tr></table>';
2068 print
'<div class="center">';
2069 print
'<span class="opacitymedium">'.$langs->trans(
"NowClickToRunTheImport", $langs->transnoentitiesnoconv(
"RunImportFile")).
'</span><br>';
2078 print
'<div class="center">';
2079 if ($user->hasRight(
'import',
'run')) {
2080 if (empty($nboferrors)) {
2081 print
'<a class="butAction" href="'.DOL_URL_ROOT.
'/imports/import.php?leftmenu=import&step=5&importid='.$importid.$param.
'">'.$langs->trans(
"RunImportFile").
'</a>';
2085 print
'<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv(
"CorrectErrorBeforeRunningImport")).
'">'.$langs->trans(
"RunImportFile").
'</a>';
2088 print
'<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv(
"NotEnoughPermissions")).
'">'.$langs->trans(
"RunSimulateImportFile").
'</a>';
2090 print
'<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv(
"NotEnoughPermissions")).
'">'.$langs->trans(
"RunImportFile").
'</a>';
2100if ($step == 5 && $datatoimport) {
2101 $max_execution_time_for_importexport =
getDolGlobalInt(
'IMPORT_MAX_EXECUTION_TIME', 300);
2102 $max_time = @ini_get(
"max_execution_time");
2103 if ($max_time && $max_time < $max_execution_time_for_importexport) {
2104 dol_syslog(
"max_execution_time=".$max_time.
" is lower than max_execution_time_for_importexport=".$max_execution_time_for_importexport.
". We try to increase it dynamically.");
2105 @ini_set(
"max_execution_time", $max_execution_time_for_importexport);
2111 if ($model ===
'txt') {
2115 $list = $objmodelimport->listOfAvailableImportFormat($db);
2116 $importid =
GETPOST(
"importid",
'alphanohtml');
2120 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/import/";
2121 $file =
"import_".$model.
".modules.php";
2122 $classname =
"Import".ucfirst($model);
2123 require_once $dir.$file;
2124 $obj =
new $classname($db, $datatoimport);
2125 '@phan-var-force ModeleImports $obj';
2126 $obj->importtriggermode = $importtriggermode;
2127 $obj->importissimulation = 0;
2128 if ($model ==
'csv') {
2129 '@phan-var-force ImportCsv $obj';
2130 $obj->separator = $separator_used;
2131 $obj->enclosure = $enclosure;
2135 $fieldssource = array();
2136 $result = $obj->import_open_file(
$conf->import->dir_temp.
'/'.$filetoimport);
2139 $arrayrecord = $obj->import_read_record();
2142 foreach ($arrayrecord as $key => $val) {
2143 $fieldssource[$i][
'example1'] =
dol_trunc($val[
'val'], 24);
2146 $obj->import_close_file();
2151 $param =
'&format='.$format.
'&datatoimport='.urlencode($datatoimport).
'&filetoimport='.urlencode($filetoimport).
'&nboflines='.((int) $nboflines).
'&importtriggermode='.urlencode($importtriggermode);
2152 if ($excludefirstline) {
2153 $param .=
'&excludefirstline='.urlencode($excludefirstline);
2156 $param .=
'&endatlinenb='.urlencode($endatlinenb);
2159 $param .=
'&separator='.urlencode($separator);
2162 $param .=
'&enclosure='.urlencode($enclosure);
2165 llxHeader(
'', $langs->trans(
"NewImport"), $help_url);
2171 print
'<div class="underbanner clearboth"></div>';
2172 print
'<div class="fichecenter">';
2174 print
'<table width="100%" class="border">';
2177 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"Module").
'</td>';
2179 $titleofmodule = $objimport->array_import_module[0][
'module']->getName();
2181 if (in_array($objimport->array_import_code[0], array(
'produit_supplierprices',
'produit_multiprice',
'produit_languages'))) {
2182 $titleofmodule = $langs->trans(
"ProductOrService");
2184 print $titleofmodule;
2188 print
'<tr><td>'.$langs->trans(
"DatasetToImport").
'</td>';
2190 $entity = preg_replace(
'/:.*$/',
'', $objimport->array_import_icon[0]);
2191 $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
2192 print img_object($objimport->array_import_module[0][
'module']->getName(), $entityicon).
' ';
2193 print $objimport->array_import_label[0];
2201 print
load_fiche_titre($langs->trans(
"InformationOnSourceFile"),
'',
'file-export');
2203 print
'<div class="underbanner clearboth"></div>';
2204 print
'<div class="fichecenter">';
2205 print
'<table width="100%" class="border tableforfield">';
2208 print
'<tr><td class="titlefieldcreate">'.$langs->trans(
"SourceFileFormat").
'</td>';
2209 print
'<td class="nowraponall">';
2210 $text = $objmodelimport->getDriverDescForKey($format);
2212 print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
2216 if ($model ==
'csv') {
2217 '@phan-var-force ImportCsv $obj';
2218 print
'<tr><td>'.$langs->trans(
"CsvOptions").
'</td>';
2220 print $langs->trans(
"Separator").
' : ';
2221 print htmlentities($separator);
2222 print
' '.$langs->trans(
"Enclosure").
' : ';
2223 print htmlentities($enclosure);
2228 print
'<tr><td>'.$langs->trans(
"FileToImport").
'</td>';
2230 $modulepart =
'import';
2231 $relativepath =
GETPOST(
'filetoimport');
2232 print
'<a data-ajax="false" href="'.DOL_URL_ROOT.
'/document.php?modulepart='.$modulepart.
'&file='.urlencode($relativepath).
'&step=4'.$param.
'" target="_blank" rel="noopener noreferrer">';
2233 print
img_mime($file,
'',
'pictofixedwidth');
2234 print $filetoimport;
2240 print $langs->trans(
"NbOfSourceLines");
2247 print $langs->trans(
"ImportFromLine");
2249 print
'<input type="text" size="4" name="excludefirstline" disabled="disabled" value="'.$excludefirstline.
'">';
2254 print $langs->trans(
"EndAtLineNb");
2256 print
'<input type="text" size="4" name="endatlinenb" disabled="disabled" value="'.$endatlinenb.
'">';
2261 print $langs->trans(
"ImportTriggerModeLabel");
2263 if ($importtriggermode ===
'fast_bulk') {
2264 print $langs->trans(
"ImportTriggerModeSummaryFast");
2266 print $langs->trans(
"ImportTriggerModeSummaryStrict");
2268 print
'<br><span class="opacitymedium">';
2269 print $langs->trans(
"ImportTriggerModeSummaryHint");
2278 print
'<b>'.$langs->trans(
"InformationOnTargetTables").
'</b>';
2279 print
'<div class="underbanner clearboth"></div>';
2280 print
'<div class="fichecenter">';
2281 print
'<table class="border centpercent">';
2284 print
'<tr><td width="25%">';
2285 print $langs->trans(
"TablesTarget");
2287 $listtables = array();
2288 foreach ($array_match_file_to_database as $code => $label) {
2290 if ($code > count($fieldssource)) {
2294 $alias = preg_replace(
'/(\..*)$/i',
'', $label);
2295 $listtables[$alias] = $objimport->array_import_tables[0][$alias];
2297 if (count($listtables)) {
2299 foreach ($listtables as $val) {
2318 print $langs->trans(
"Error");
2324 print $langs->trans(
"FieldsTarget").
'</td><td>';
2325 $listfields = array();
2327 $sort_array_match_file_to_database = $array_match_file_to_database;
2328 ksort($sort_array_match_file_to_database);
2330 foreach ($sort_array_match_file_to_database as $code => $label) {
2333 if ($code > count($fieldssource)) {
2337 $alias = preg_replace(
'/(\..*)$/i',
'', $label);
2338 $listfields[$i] = $langs->trans(
"Field").
' '.$code.
'->'.$label;
2340 print count($listfields) ? (implode(
', ', $listfields)) : $langs->trans(
"Error");
2347 $arrayoferrors = array();
2348 $arrayofwarnings = array();
2349 $maxnboferrors = !
getDolGlobalString(
'IMPORT_MAX_NB_OF_ERRORS') ? 50 :
$conf->global->IMPORT_MAX_NB_OF_ERRORS;
2350 $maxnbofwarnings = !
getDolGlobalString(
'IMPORT_MAX_NB_OF_WARNINGS') ? 50 :
$conf->global->IMPORT_MAX_NB_OF_WARNINGS;
2362 $pathfile =
$conf->import->dir_temp.
'/'.$filetoimport;
2363 $result = $obj->import_open_file($pathfile);
2365 global $tablewithentity_cache;
2366 $tablewithentity_cache = array();
2370 while ($sourcelinenb < $nboflines && !$endoffile) {
2372 $arrayrecord = $obj->import_read_record();
2373 if ($arrayrecord ===
false) {
2374 $arrayofwarnings[$sourcelinenb][0] = array(
'lib' => $langs->trans(
'ErrorFileLinesReachEOF', $nboflines, $sourcelinenb),
'type' =>
'EOF_RECORD_ON_SEVERAL_LINES');
2378 if ($excludefirstline && ($sourcelinenb < $excludefirstline)) {
2381 if ($endatlinenb && ($sourcelinenb > $endatlinenb)) {
2385 $parameters = array(
2387 'datatoimport' => $datatoimport,
2389 'arrayrecord' => $arrayrecord,
2390 'array_match_file_to_database' => $array_match_file_to_database,
2391 'objimport' => $objimport,
2392 'fieldssource' => $fieldssource,
2393 'importid' => $importid,
2394 'updatekeys' => $updatekeys,
2395 'importtriggermode' => $importtriggermode,
2396 'arrayoferrors' => &$arrayoferrors,
2397 'arrayofwarnings' => &$arrayofwarnings,
2401 $reshook = $hookmanager->executeHooks(
'ImportInsert', $parameters);
2403 $arrayoferrors[$sourcelinenb][] = [
2404 'lib' => implode(
"<br>", array_merge([$hookmanager->error], $hookmanager->errors))
2408 if (empty($reshook)) {
2410 $result = $obj->import_insert($arrayrecord, $array_match_file_to_database, $objimport, count($fieldssource), $importid, $updatekeys);
2412 if (count($obj->errors)) {
2413 $arrayoferrors[$sourcelinenb] = $obj->errors;
2415 if (count($obj->warnings)) {
2416 $arrayofwarnings[$sourcelinenb] = $obj->warnings;
2419 if (!count($obj->errors) && !count($obj->warnings)) {
2424 $reshook = $hookmanager->executeHooks(
'AfterImportInsert', $parameters);
2426 $arrayoferrors[$sourcelinenb][] = [
2427 'lib' => implode(
"<br>", array_merge([$hookmanager->error], $hookmanager->errors))
2432 $obj->import_close_file();
2434 print $langs->trans(
"ErrorFailedToOpenFile", $pathfile);
2437 if (count($arrayoferrors) > 0) {
2444 if (!empty($objimport->array_import_run_sql_after[0]) && is_array($objimport->array_import_run_sql_after[0])) {
2446 foreach ($objimport->array_import_run_sql_after[0] as $sqlafterimport) {
2448 $resqlafterimport = $db->query($sqlafterimport);
2449 if (!$resqlafterimport) {
2450 $arrayoferrors[
'none'][] = array(
'lib' => $langs->trans(
"Error running final request: ".$sqlafterimport));
2456 if (!$error && $importtriggermode ===
'fast_bulk') {
2457 $resbulktrigger = $obj->runImportBulkTrigger($importid, $user, $langs,
$conf);
2458 if ($resbulktrigger < 0) {
2459 $arrayoferrors[
'none'][] = array(
'lib' => $langs->trans(
"ErrorFailedTriggerCall"),
'type' =>
'TRIGGER');
2460 if (!empty($obj->errors) && is_array($obj->errors)) {
2461 foreach ($obj->errors as $tmperror) {
2462 $lib = (is_array($tmperror) ? ($tmperror[
'lib'] ??
'') : $tmperror);
2464 $arrayoferrors[
'none'][] = array(
'lib' => $lib,
'type' =>
'TRIGGER');
2481 if ($importtriggermode ===
'fast_bulk') {
2482 print
'<div class="warning">';
2483 print
'<b>'.$langs->trans(
"ImportTriggerModeFastBulkFinalWarningTitle").
'</b><br>';
2484 print $langs->trans(
"ImportTriggerModeFastBulkFinalWarning");
2491 print
'<div class="info">';
2492 print $langs->trans(
"NbOfLinesImported", $nbok).
'</b><br>';
2493 print $langs->trans(
"NbInsert", empty($obj->nbinsert) ? 0 : $obj->nbinsert).
'<br>';
2494 print $langs->trans(
"NbUpdate", empty($obj->nbupdate) ? 0 : $obj->nbupdate).
'<br>';
2496 print
'<div class="center">';
2497 print $langs->trans(
"FileWasImported", $importid).
'<br>';
2498 print
'<span class="opacitymedium">'.$langs->trans(
"YouCanUseImportIdToFindRecord", $importid).
'</span><br>';
2521 global
$conf, $langs;
2525 if ($key ==
'none') {
2527 print
"\n\n<!-- Box_no-key start-->\n";
2528 print
'<div class="box boximport" style="padding:0;">'.
"\n";
2529 print
'<table summary="boxtable_no-key" class="centpercent nobordernopadding">'.
"\n";
2531 print
"\n\n<!-- Box ".$pos.
" start -->\n";
2532 print
'<div class="box boximport" style="padding: 0;" id="boxto_'.$pos.
'">'.
"\n";
2534 print
'<table summary="boxtable'.$pos.
'" class="nobordernopadding centpercent tableimport">'.
"\n";
2537 if (($pos && $pos > count($fieldssource)) && (!isset($fieldssource[$pos][
"imported"]))) {
2547 } elseif ($key ==
'none') {
2548 print
'<tr style="height:'.$height.
'" class="trimport oddevenimport">';
2549 print
'<td class="nocellnopadd" width="16" style="font-weight: normal">';
2552 print
'<td style="font-weight: normal">';
2558 print
'<tr style="height:'.$height.
'" class="trimport oddevenimport">';
2559 print
'<td class="nocellnopadd" width="16" style="font-weight: normal">';
2562 print img_picto($langs->trans(
"Column").
' '.
num2Alpha($pos - 1),
'file',
'class="pictofixedwidth marginleftonly"');
2564 if (isset($fieldssource[$pos][
'imported']) && $fieldssource[$pos][
'imported'] ==
false) {
2565 print
'<td class="nowraponall boxtdunused" style="font-weight: normal">';
2567 print
'<td class="nowraponall tdoverflowmax500" style="font-weight: normal">';
2569 print $langs->trans(
"Column").
' '.
num2Alpha($pos - 1).
' (#'.$pos.
')';
2570 if (empty($fieldssource[$pos][
'example1'])) {
2571 $example = $fieldssource[$pos][
'label'];
2573 $example = $fieldssource[$pos][
'example1'];
2577 $example = mb_convert_encoding($example,
'UTF-8',
'ISO-8859-1');
2581 print
'<i class="opacitymedium">'.dol_escape_htmltag($example).
'</i>';
2590 print
"<!-- Box end -->\n\n";
2603 $i = count($fieldssource) + 1;
2606 foreach ($listofkey as $key => $val) {
2607 $maxkey = max($maxkey, $key);
2610 while ($i <= $maxkey) {
2611 if (empty($listofkey[$i])) {
2633 if ($position == count($array)) {
2634 $ret = $array + $insertArray;
2637 foreach ($array as $key => $value) {
2638 if ($position == $i++) {
2639 $ret += $insertArray;
2642 $ret[$key] = $value;
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='', $placeholder='')
Convert a html select field into an ajax combobox.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Parent class for import file readers.
Class to manage Dolibarr users.
dol_filemtime($pathoffile)
Return time of a file.
dol_filesize($pathoffile)
Return size of a file.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_count_nb_of_line($file)
Count number of lines in a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $keyforsourcefile='addedfile', $upload_dir='', $mode=0)
Check validity of a file upload from an GUI page, and move it to its final destination.
dol_now($mode='gmt')
Return date for now.
dolExplodeIntoArray($string, $delimiter=';', $kv='=')
Split a string with 2 keys into key array.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into JavaScript code.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by the value of a given key, which produces ascending (default) or descending out...
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
num2Alpha($n)
Return a numeric value into an Excel like column number.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
img_mime($file, $titlealt='', $morecss='')
Show MIME img of a file.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='', $cssfordropdown='info_admin')
Show information in HTML for admin users or standard users.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
import_prepare_head($param, $maxstep=0)
Function to return list of tabs for import pages.
show_elem($fieldssource, $pos, $key)
Function to put the movable box of a source field.
getnewkey(&$fieldssource, &$listofkey)
Return not used field number.
arrayInsert($array, $position, $insertArray)
Return array with element inserted in it at position $position.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.
getMaxFileSizeArray()
Return the max allowed for file upload.