123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343 |
- <?php
- /* Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
- * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- /**
- * \file htdocs/imports/import.php
- * \ingroup import
- * \brief Pages of import Wizard
- */
- require_once '../main.inc.php';
- require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
- require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
- require_once DOL_DOCUMENT_ROOT.'/imports/class/import.class.php';
- require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.php';
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
- require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
- require_once DOL_DOCUMENT_ROOT.'/core/lib/import.lib.php';
- // Load translation files required by the page
- $langs->loadLangs(array('exports', 'compta', 'errors'));
- // Security check
- $result = restrictedArea($user, 'import');
- // Map icons, array duplicated in export.php, was not synchronized, TODO put it somewhere only once
- $entitytoicon = array(
- 'invoice' => 'bill',
- 'invoice_line' => 'bill',
- 'order' => 'order',
- 'order_line' => 'order',
- 'propal' => 'propal',
- 'propal_line' => 'propal',
- 'intervention' => 'intervention',
- 'inter_line' => 'intervention',
- 'member' => 'user',
- 'member_type' => 'group',
- 'subscription' => 'payment',
- 'payment' => 'payment',
- 'tax' => 'bill',
- 'tax_type' => 'generic',
- 'other' => 'generic',
- 'account' => 'account',
- 'product' => 'product',
- 'virtualproduct'=>'product',
- 'subproduct' => 'product',
- 'product_supplier_ref' => 'product',
- 'stock' => 'stock',
- 'warehouse' => 'stock',
- 'batch' => 'stock',
- 'stockbatch' => 'stock',
- 'category' => 'category',
- 'shipment' => 'sending',
- 'shipment_line'=> 'sending',
- 'reception'=> 'sending',
- 'reception_line'=> 'sending',
- 'expensereport'=> 'trip',
- 'expensereport_line'=> 'trip',
- 'holiday' => 'holiday',
- 'contract_line' => 'contract',
- 'translation' => 'generic',
- 'bomm' => 'bom',
- 'bomline' => 'bom'
- );
- // Translation code, array duplicated in export.php, was not synchronized, TODO put it somewhere only once
- $entitytolang = array(
- 'user' => 'User',
- 'company' => 'Company',
- 'contact' => 'Contact',
- 'invoice' => 'Bill',
- 'invoice_line' => 'InvoiceLine',
- 'order' => 'Order',
- 'order_line' => 'OrderLine',
- 'propal' => 'Proposal',
- 'propal_line' => 'ProposalLine',
- 'intervention' => 'Intervention',
- 'inter_line' => 'InterLine',
- 'member' => 'Member',
- 'member_type' => 'MemberType',
- 'subscription' => 'Subscription',
- 'tax' => 'SocialContribution',
- 'tax_type' => 'DictionarySocialContributions',
- 'account' => 'BankTransactions',
- 'payment' => 'Payment',
- 'product' => 'Product',
- 'virtualproduct' => 'AssociatedProducts',
- 'subproduct' => 'SubProduct',
- 'product_supplier_ref' => 'SupplierPrices',
- 'service' => 'Service',
- 'stock' => 'Stock',
- 'movement' => 'StockMovement',
- 'batch' => 'Batch',
- 'stockbatch' => 'StockDetailPerBatch',
- 'warehouse' => 'Warehouse',
- 'category' => 'Category',
- 'other' => 'Other',
- 'trip' => 'TripsAndExpenses',
- 'shipment' => 'Shipments',
- 'shipment_line'=> 'ShipmentLine',
- 'project' => 'Projects',
- 'projecttask' => 'Tasks',
- 'task_time' => 'TaskTimeSpent',
- 'action' => 'Event',
- 'expensereport'=> 'ExpenseReport',
- 'expensereport_line'=> 'ExpenseReportLine',
- 'holiday' => 'TitreRequestCP',
- 'contract' => 'Contract',
- 'contract_line'=> 'ContractLine',
- 'translation' => 'Translation',
- 'bom' => 'BOM',
- 'bomline' => 'BOMLine'
- );
- $datatoimport = GETPOST('datatoimport');
- $format = GETPOST('format');
- $filetoimport = GETPOST('filetoimport');
- $action = GETPOST('action', 'alpha');
- $confirm = GETPOST('confirm', 'alpha');
- $step = (GETPOST('step') ? GETPOST('step') : 1);
- $import_name = GETPOST('import_name');
- $hexa = GETPOST('hexa');
- $importmodelid = GETPOST('importmodelid');
- $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 : ','));
- $enclosure = (GETPOST('enclosure', 'nohtml') ? GETPOST('enclosure', 'nohtml') : '"');
- $separator_used = str_replace('\t', "\t", $separator);
- $objimport = new Import($db);
- $objimport->load_arrays($user, ($step == 1 ? '' : $datatoimport));
- $objmodelimport = new ModeleImports();
- $form = new Form($db);
- $htmlother = new FormOther($db);
- $formfile = new FormFile($db);
- // Init $array_match_file_to_database from _SESSION
- $serialized_array_match_file_to_database = isset($_SESSION["dol_array_match_file_to_database"]) ? $_SESSION["dol_array_match_file_to_database"] : '';
- $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;
- }
- }
- 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
- */
- /*
- if ($action=='downfield' || $action=='upfield')
- {
- $pos=$array_match_file_to_database[$_GET["field"]];
- if ($action=='downfield') $newpos=$pos+1;
- if ($action=='upfield') $newpos=$pos-1;
- // Recherche code avec qui switcher
- $newcode="";
- foreach($array_match_file_to_database as $code=>$value)
- {
- if ($value == $newpos)
- {
- $newcode=$code;
- break;
- }
- }
- //print("Switch pos=$pos (code=".$_GET["field"].") and newpos=$newpos (code=$newcode)");
- if ($newcode) // Si newcode trouve (protection contre resoumission de page)
- {
- $array_match_file_to_database[$_GET["field"]]=$newpos;
- $array_match_file_to_database[$newcode]=$pos;
- $_SESSION["dol_array_match_file_to_database"]=$serialized_array_match_file_to_database;
- }
- }
- */
- if ($action == 'builddoc') {
- // Build import file
- $result = $objimport->build_file($user, GETPOST('model', 'alpha'), $datatoimport, $array_match_file_to_database);
- if ($result < 0) {
- setEventMessages($objimport->error, $objimport->errors, 'errors');
- } else {
- setEventMessages($langs->trans("FileSuccessfullyBuilt"), null, 'mesgs');
- }
- }
- if ($action == 'deleteprof') {
- if (GETPOST("id", 'int')) {
- $objimport->fetch(GETPOST("id", 'int'));
- $result = $objimport->delete($user);
- }
- }
- // Save import config to database
- if ($action == 'add_import_model') {
- if ($import_name) {
- // Set save string
- $hexa = '';
- foreach ($array_match_file_to_database as $key => $val) {
- if ($hexa) {
- $hexa .= ',';
- }
- $hexa .= $key.'='.$val;
- }
- $objimport->model_name = $import_name;
- $objimport->datatoimport = $datatoimport;
- $objimport->hexa = $hexa;
- $objimport->fk_user = (GETPOST('visibility', 'aZ09') == 'all' ? 0 : $user->id);
- $result = $objimport->create($user);
- if ($result >= 0) {
- setEventMessages($langs->trans("ImportModelSaved", $objimport->model_name), null, 'mesgs');
- } else {
- $langs->load("errors");
- if ($objimport->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
- setEventMessages($langs->trans("ErrorImportDuplicateProfil"), null, 'errors');
- } else {
- setEventMessages($objimport->error, null, 'errors');
- }
- }
- } else {
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("ImportModelName")), null, 'errors');
- }
- }
- if ($step == 3 && $datatoimport) {
- if (GETPOST('sendit') && !empty($conf->global->MAIN_UPLOAD_DOC)) {
- dol_mkdir($conf->import->dir_temp);
- $nowyearmonth = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
- $fullpath = $conf->import->dir_temp."/".$nowyearmonth.'-'.$_FILES['userfile']['name'];
- if (dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $fullpath, 1) > 0) {
- dol_syslog("File ".$fullpath." was added for import");
- } else {
- $langs->load("errors");
- setEventMessages($langs->trans("ErrorFailedToSaveFile"), null, 'errors');
- }
- }
- // Delete file
- if ($action == 'confirm_deletefile' && $confirm == 'yes') {
- $langs->load("other");
- $param = '&datatoimport='.urlencode($datatoimport).'&format='.urlencode($format);
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- $file = $conf->import->dir_temp.'/'.GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
- $ret = dol_delete_file($file);
- if ($ret) {
- setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
- }
- Header('Location: '.$_SERVER["PHP_SELF"].'?step='.$step.$param);
- exit;
- }
- }
- if ($step == 4 && $action == 'select_model') {
- // Reinit match arrays
- $_SESSION["dol_array_match_file_to_database"] = '';
- $serialized_array_match_file_to_database = '';
- $array_match_file_to_database = array();
- // Load model from $importmodelid and set $array_match_file_to_database
- // and $_SESSION["dol_array_match_file_to_database"]
- $result = $objimport->fetch($importmodelid);
- if ($result > 0) {
- $serialized_array_match_file_to_database = $objimport->hexa;
- $fieldsarray = explode(',', $serialized_array_match_file_to_database);
- foreach ($fieldsarray as $elem) {
- $tabelem = explode('=', $elem);
- $key = $tabelem[0];
- $val = $tabelem[1];
- if ($key && $val) {
- $array_match_file_to_database[$key] = $val;
- }
- }
- $_SESSION["dol_array_match_file_to_database"] = $serialized_array_match_file_to_database;
- }
- }
- if ($action == 'saveselectorder') {
- // Enregistrement de la position des champs
- $serialized_array_match_file_to_database = '';
- dol_syslog("selectorder=".GETPOST('selectorder'), LOG_DEBUG);
- $selectorder = explode(",", GETPOST('selectorder'));
- $fieldtarget = $fieldstarget = $objimport->array_import_fields[0];
- foreach ($selectorder as $key => $code) {
- $serialized_array_match_file_to_database .= $key.'='.$code;
- $serialized_array_match_file_to_database .= ',';
- }
- $serialized_array_match_file_to_database = substr($serialized_array_match_file_to_database, 0, -1);
- dol_syslog('dol_array_match_file_to_database_select='.$serialized_array_match_file_to_database);
- $_SESSION["dol_array_match_file_to_database_select"] = $serialized_array_match_file_to_database;
- echo "{}";
- exit(0);
- }
- /*
- * View
- */
- $help_url = 'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones';
- // STEP 1: Page to select dataset to import
- if ($step == 1 || !$datatoimport) {
- // Clean saved file-database matching
- $serialized_array_match_file_to_database = '';
- $array_match_file_to_database = array();
- $_SESSION["dol_array_match_file_to_database"] = '';
- $_SESSION["dol_array_match_file_to_database_select"] = '';
- $param = '';
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- if ($separator) {
- $param .= '&separator='.urlencode($separator);
- }
- if ($enclosure) {
- $param .= '&enclosure='.urlencode($enclosure);
- }
- llxHeader('', $langs->trans("NewImport"), $help_url);
- $head = import_prepare_head($param, 1);
- print dol_get_fiche_head($head, 'step1', '', -1);
- print '<div class="opacitymedium">'.$langs->trans("SelectImportDataSet").'</div><br>';
- // Affiche les modules d'imports
- print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
- print '<table class="noborder centpercent">';
- print '<tr class="liste_titre">';
- print '<td>'.$langs->trans("Module").'</td>';
- print '<td>'.$langs->trans("ImportableDatas").'</td>';
- print '<td> </td>';
- print '</tr>';
- if (count($objimport->array_import_module)) {
- $sortedarrayofmodules = dol_sort_array($objimport->array_import_module, 'position_of_profile', 'asc', 0, 0, 1);
- foreach ($sortedarrayofmodules as $key => $value) {
- //var_dump($key.' '.$value['position_of_profile'].' '.$value['import_code'].' '.$objimport->array_import_module[$key]['module']->getName().' '.$objimport->array_import_code[$key]);
- print '<tr class="oddeven"><td>';
- $titleofmodule = $objimport->array_import_module[$key]['module']->getName();
- // Special cas for import common to module/services
- if (in_array($objimport->array_import_code[$key], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
- $titleofmodule = $langs->trans("ProductOrService");
- }
- print $titleofmodule;
- print '</td><td>';
- $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[$key]);
- $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
- print img_object($objimport->array_import_module[$key]['module']->getName(), $entityicon).' ';
- print $objimport->array_import_label[$key];
- print '</td><td style="text-align: right">';
- if ($objimport->array_import_perms[$key]) {
- 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-15x"').'</a>';
- } else {
- print $langs->trans("NotEnoughPermissions");
- }
- print '</td></tr>';
- }
- } else {
- print '<tr><td class="oddeven" colspan="3">'.$langs->trans("NoImportableData").'</td></tr>';
- }
- print '</table>';
- print '</div>';
- print dol_get_fiche_end();
- }
- // STEP 2: Page to select input format file
- if ($step == 2 && $datatoimport) {
- $param = '&datatoimport='.urlencode($datatoimport);
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- if ($separator) {
- $param .= '&separator='.urlencode($separator);
- }
- if ($enclosure) {
- $param .= '&enclosure='.urlencode($enclosure);
- }
- llxHeader('', $langs->trans("NewImport"), $help_url);
- $head = import_prepare_head($param, 2);
- print dol_get_fiche_head($head, 'step2', '', -2);
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table class="border tableforfield centpercent">';
- // Module
- print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
- print '<td>';
- $titleofmodule = $objimport->array_import_module[0]['module']->getName();
- // Special cas for import common to module/services
- if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
- $titleofmodule = $langs->trans("ProductOrService");
- }
- print $titleofmodule;
- print '</td></tr>';
- // Dataset to import
- print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
- print '<td>';
- $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
- $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
- print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
- print $objimport->array_import_label[0];
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print dol_get_fiche_end();
- print '<form name="userfile" action="'.$_SERVER["PHP_SELF"].'" enctype="multipart/form-data" METHOD="POST">';
- print '<input type="hidden" name="token" value="'.newToken().'">';
- print '<input type="hidden" name="max_file_size" value="'.$conf->maxfilesize.'">';
- print '<br>';
- print '<span class="opacitymedium">';
- $s = $langs->trans("ChooseFormatOfFileToImport", '{s1}');
- $s = str_replace('{s1}', img_picto('', 'next'), $s);
- print $s;
- print '</span><br><br>';
- print '<br>';
- print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
- print '<table class="noborder centpercent" cellpadding="4">';
- $filetoimport = '';
- // Add format informations and link to download example
- print '<tr class="liste_titre"><td colspan="6">';
- print $langs->trans("FileMustHaveOneOfFollowingFormat");
- print '</td></tr>';
- $list = $objmodelimport->liste_modeles($db);
- foreach ($list as $key) {
- print '<tr class="oddeven">';
- print '<td width="16">'.img_picto_common($key, $objmodelimport->getPictoForKey($key)).'</td>';
- $text = $objmodelimport->getDriverDescForKey($key);
- print '<td>'.$form->textwithpicto($objmodelimport->getDriverLabelForKey($key), $text).'</td>';
- print '<td style="text-align:center">';
- print img_picto('', 'download', 'class="paddingright opacitymedium"').'<a href="'.DOL_URL_ROOT.'/imports/emptyexample.php?format='.$key.$param.'" target="_blank" rel="noopener noreferrer">'.$langs->trans("DownloadEmptyExample");
- print '</a>';
- print ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("StarAreMandatory").')</span>';
- print '</td>';
- // Action button
- print '<td style="text-align:right">';
- print '<a href="'.DOL_URL_ROOT.'/imports/import.php?step=3&format='.$key.$param.'">'.img_picto($langs->trans("SelectFormat"), 'next', 'class="fa-15x"').'</a>';
- print '</td>';
- print '</tr>';
- }
- print '</table>';
- print '</div>';
- print '</form>';
- }
- // STEP 3: Page to select file
- if ($step == 3 && $datatoimport) {
- $param = '&datatoimport='.urlencode($datatoimport).'&format='.urlencode($format);
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- if ($separator) {
- $param .= '&separator='.urlencode($separator);
- }
- if ($enclosure) {
- $param .= '&enclosure='.urlencode($enclosure);
- }
- $list = $objmodelimport->liste_modeles($db);
- llxHeader('', $langs->trans("NewImport"), $help_url);
- $head = import_prepare_head($param, 3);
- print dol_get_fiche_head($head, 'step3', '', -2);
- /*
- * Confirm delete file
- */
- if ($action == 'delete') {
- print $form->formconfirm($_SERVER["PHP_SELF"].'?urlfile='.urlencode(GETPOST('urlfile')).'&step=3'.$param, $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1);
- }
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table class="border tableforfield centpercent">';
- // Module
- print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
- print '<td>';
- $titleofmodule = $objimport->array_import_module[0]['module']->getName();
- // Special cas for import common to module/services
- if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
- $titleofmodule = $langs->trans("ProductOrService");
- }
- print $titleofmodule;
- print '</td></tr>';
- // Lot de donnees a importer
- print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
- print '<td>';
- $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
- $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
- print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
- print $objimport->array_import_label[0];
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border tableforfield">';
- // Source file format
- print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
- print '<td class="nowraponall">';
- $text = $objmodelimport->getDriverDescForKey($format);
- print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
- print '</td><td style="text-align:right" class="nowrap">';
- print img_picto('', 'download', 'class="paddingright opacitymedium"').'<a href="'.DOL_URL_ROOT.'/imports/emptyexample.php?format='.$format.$param.'" target="_blank" rel="noopener noreferrer">'.$langs->trans("DownloadEmptyExample");
- print '</a>';
- print ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("StarAreMandatory").')</span>';
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print dol_get_fiche_end();
- if ($format == 'xlsx' && !class_exists('XMLWriter')) {
- $langs->load("install");
- print info_admin($langs->trans("ErrorPHPDoesNotSupport", 'php-xml'), 0, 0, 1, 'error');
- }
- print '<br>';
- print '<form name="userfile" action="'.$_SERVER["PHP_SELF"].'" enctype="multipart/form-data" METHOD="POST">';
- print '<input type="hidden" name="token" value="'.newToken().'">';
- print '<input type="hidden" name="max_file_size" value="'.$conf->maxfilesize.'">';
- print '<input type="hidden" value="'.$step.'" name="step">';
- print '<input type="hidden" value="'.dol_escape_htmltag($format).'" name="format">';
- print '<input type="hidden" value="'.$excludefirstline.'" name="excludefirstline">';
- print '<input type="hidden" value="'.$endatlinenb.'" name="endatlinenb">';
- print '<input type="hidden" value="'.dol_escape_htmltag($separator).'" name="separator">';
- print '<input type="hidden" value="'.dol_escape_htmltag($enclosure).'" name="enclosure">';
- print '<input type="hidden" value="'.dol_escape_htmltag($datatoimport).'" name="datatoimport">';
- print '<span class="opacitymedium">';
- $s = $langs->trans("ChooseFileToImport", '{s1}');
- $s = str_replace('{s1}', img_picto('', 'next'), $s);
- print $s;
- print '</span><br><br>';
- $filetoimport = '';
- // Input file name box
- print '<div class="marginbottomonly">';
- print '<input type="file" name="userfile" size="20" maxlength="80"> ';
- $out = (empty($conf->global->MAIN_UPLOAD_DOC) ? ' disabled' : '');
- print '<input type="submit" class="button small" value="'.$langs->trans("AddFile").'"'.$out.' name="sendit">';
- $out = '';
- if (!empty($conf->global->MAIN_UPLOAD_DOC)) {
- $max = $conf->global->MAIN_UPLOAD_DOC; // In Kb
- $maxphp = @ini_get('upload_max_filesize'); // In unknown
- if (preg_match('/k$/i', $maxphp)) {
- $maxphp = $maxphp * 1;
- }
- if (preg_match('/m$/i', $maxphp)) {
- $maxphp = $maxphp * 1024;
- }
- if (preg_match('/g$/i', $maxphp)) {
- $maxphp = $maxphp * 1024 * 1024;
- }
- if (preg_match('/t$/i', $maxphp)) {
- $maxphp = $maxphp * 1024 * 1024 * 1024;
- }
- $maxphp2 = @ini_get('post_max_size'); // In unknown
- if (preg_match('/k$/i', $maxphp2)) {
- $maxphp2 = $maxphp2 * 1;
- }
- if (preg_match('/m$/i', $maxphp2)) {
- $maxphp2 = $maxphp2 * 1024;
- }
- if (preg_match('/g$/i', $maxphp2)) {
- $maxphp2 = $maxphp2 * 1024 * 1024;
- }
- if (preg_match('/t$/i', $maxphp2)) {
- $maxphp2 = $maxphp2 * 1024 * 1024 * 1024;
- }
- // Now $max and $maxphp and $maxphp2 are in Kb
- $maxmin = $max;
- $maxphptoshow = $maxphptoshowparam = '';
- if ($maxphp > 0) {
- $maxmin = min($max, $maxphp);
- $maxphptoshow = $maxphp;
- $maxphptoshowparam = 'upload_max_filesize';
- }
- if ($maxphp2 > 0) {
- $maxmin = min($max, $maxphp2);
- if ($maxphp2 < $maxphp) {
- $maxphptoshow = $maxphp2;
- $maxphptoshowparam = 'post_max_size';
- }
- }
- $langs->load('other');
- $out .= ' ';
- $out .= info_admin($langs->trans("ThisLimitIsDefinedInSetup", $max, $maxphptoshow), 1);
- } else {
- $out .= ' ('.$langs->trans("UploadDisabled").')';
- }
- print $out;
- print '</div>';
- // Search available imports
- $filearray = dol_dir_list($conf->import->dir_temp, 'files', 0, '', '', 'name', SORT_DESC);
- if (count($filearray) > 0) {
- print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
- print '<table class="noborder centpercent" width="100%" cellpadding="4">';
- $dir = $conf->import->dir_temp;
- // Search available files to import
- $i = 0;
- foreach ($filearray as $key => $val) {
- $file = $val['name'];
- // readdir return value in ISO and we want UTF8 in memory
- if (!utf8_check($file)) {
- $file = utf8_encode($file);
- }
- if (preg_match('/^\./', $file)) {
- continue;
- }
- $modulepart = 'import';
- $urlsource = $_SERVER["PHP_SELF"].'?step='.$step.$param.'&filetoimport='.urlencode($filetoimport);
- $relativepath = $file;
- print '<tr class="oddeven">';
- print '<td>';
- print img_mime($file, '', 'pictofixedwidth');
- print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=3'.$param.'" target="_blank" rel="noopener noreferrer">';
- print $file;
- print '</a>';
- print '</td>';
- // Affiche taille fichier
- print '<td style="text-align:right">'.dol_print_size(dol_filesize($dir.'/'.$file)).'</td>';
- // Affiche date fichier
- print '<td style="text-align:right">'.dol_print_date(dol_filemtime($dir.'/'.$file), 'dayhour').'</td>';
- // Del button
- print '<td style="text-align:right"><a href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&step=3'.$param.'&urlfile='.urlencode($relativepath);
- print '">'.img_delete().'</a></td>';
- // Action button
- print '<td style="text-align:right">';
- print '<a href="'.$_SERVER['PHP_SELF'].'?step=4'.$param.'&filetoimport='.urlencode($relativepath).'">'.img_picto($langs->trans("NewImport"), 'next', 'class="fa-15x"').'</a>';
- print '</td>';
- print '</tr>';
- }
- print '</table>';
- print '</div>';
- }
- print '</form>';
- }
- // 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);
- // Create classe to use for import
- $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
- $file = "import_".$model.".modules.php";
- $classname = "Import".ucfirst($model);
- require_once $dir.$file;
- $obj = new $classname($db, $datatoimport);
- if ($model == 'csv') {
- $obj->separator = $separator_used;
- $obj->enclosure = $enclosure;
- }
- if ($model == 'xlsx') {
- if (!preg_match('/\.xlsx$/i', $filetoimport)) {
- $langs->load("errors");
- $param = '&datatoimport='.$datatoimport.'&format='.$format;
- setEventMessages($langs->trans("ErrorFileMustHaveFormat", $model), null, 'errors');
- header("Location: ".$_SERVER["PHP_SELF"].'?step=3'.$param.'&filetoimport='.urlencode($relativepath));
- exit;
- }
- }
- if (GETPOST('update')) {
- $array_match_file_to_database = array();
- }
- // Load source fields in input file
- $fieldssource = array();
- $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs);
- if ($result >= 0) {
- // Read first line
- $arrayrecord = $obj->import_read_record();
- // Put into array fieldssource starting with 1.
- $i = 1;
- foreach ($arrayrecord as $key => $val) {
- 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];
- $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)
- if (count($array_match_file_to_database) == 0) {
- // This is first input in screen, we need to define
- // $array_match_file_to_database
- // $serialized_array_match_file_to_database
- // $_SESSION["dol_array_match_file_to_database"]
- $pos = 1;
- $num = count($fieldssource);
- while ($pos <= $num) {
- if ($num >= 1 && $pos <= $num) {
- $posbis = 1;
- foreach ($fieldstarget as $key => $val) {
- if ($posbis < $pos) {
- $posbis++;
- continue;
- }
- // We found the key of targets that is at position pos
- $array_match_file_to_database[$pos] = $key;
- if ($serialized_array_match_file_to_database) {
- $serialized_array_match_file_to_database .= ',';
- }
- $serialized_array_match_file_to_database .= ($pos.'='.$key);
- break;
- }
- }
- $pos++;
- }
- // Save the match array in session. We now will use the array in session.
- $_SESSION["dol_array_match_file_to_database_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;
- // Now $array_match_file_to_database contains fieldnb(1,2,3...)=>fielddatabase(key in $array_match_file_to_database)
- $param = '&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport);
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- if ($separator) {
- $param .= '&separator='.urlencode($separator);
- }
- if ($enclosure) {
- $param .= '&enclosure='.urlencode($enclosure);
- }
- llxHeader('', $langs->trans("NewImport"), $help_url);
- $head = import_prepare_head($param, 4);
- print dol_get_fiche_head($head, 'step4', '', -2);
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border tableforfield">';
- // Module
- print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
- print '<td>';
- $titleofmodule = $objimport->array_import_module[0]['module']->getName();
- // Special cas for import common to module/services
- if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
- $titleofmodule = $langs->trans("ProductOrService");
- }
- print $titleofmodule;
- print '</td></tr>';
- // Lot de donnees a importer
- print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
- print '<td>';
- $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
- $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
- print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
- print $objimport->array_import_label[0];
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border tableforfield">';
- // Source file format
- print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
- print '<td>';
- $text = $objmodelimport->getDriverDescForKey($format);
- print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
- print '</td></tr>';
- // Separator and enclosure
- if ($model == 'csv') {
- print '<tr><td>'.$langs->trans("CsvOptions").'</td>';
- print '<td>';
- print '<form>';
- print '<input type="hidden" name="token" value="'.newToken().'">';
- print '<input type="hidden" value="'.$step.'" name="step">';
- print '<input type="hidden" value="'.$format.'" name="format">';
- print '<input type="hidden" value="'.$excludefirstline.'" name="excludefirstline">';
- print '<input type="hidden" value="'.$endatlinenb.'" name="endatlinenb">';
- print '<input type="hidden" value="'.$datatoimport.'" name="datatoimport">';
- print '<input type="hidden" value="'.$filetoimport.'" name="filetoimport">';
- print $langs->trans("Separator").' : ';
- print '<input type="text" size="1" name="separator" value="'.dol_escape_htmltag($separator).'"/>';
- print ' '.$langs->trans("Enclosure").' : ';
- print '<input type="text" size="1" name="enclosure" value="'.dol_escape_htmltag($enclosure).'"/> ';
- print '<input name="update" type="submit" value="'.$langs->trans('Update').'" class="button small" />';
- print '</form>';
- print '</td></tr>';
- }
- // File to import
- print '<tr><td>'.$langs->trans("FileToImport").'</td>';
- print '<td>';
- $modulepart = 'import';
- $relativepath = GETPOST('filetoimport');
- print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=4'.$param.'" target="_blank" rel="noopener noreferrer">';
- print img_mime($file, '', 'pictofixedwidth');
- print $filetoimport;
- print '</a>';
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print dol_get_fiche_end();
- print '<br>'."\n";
- // List of source fields
- print '<!-- List of source fields -->'."\n";
- print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
- print '<input type="hidden" name="token" value="'.newToken().'">';
- print '<input type="hidden" name="action" value="select_model">';
- print '<input type="hidden" name="step" value="4">';
- print '<input type="hidden" name="format" value="'.$format.'">';
- print '<input type="hidden" name="datatoimport" value="'.$datatoimport.'">';
- print '<input type="hidden" name="filetoimport" value="'.$filetoimport.'">';
- print '<input type="hidden" name="excludefirstline" value="'.$excludefirstline.'">';
- print '<input type="hidden" name="endatlinenb" value="'.$endatlinenb.'">';
- print '<input type="hidden" name="separator" value="'.dol_escape_htmltag($separator).'">';
- print '<input type="hidden" name="enclosure" value="'.dol_escape_htmltag($enclosure).'">';
- print '<div class="marginbottomonly">';
- print '<span class="opacitymedium">';
- $s = $langs->trans("SelectImportFieldsSource", '{s1}');
- $s = str_replace('{s1}', img_picto('', 'grip_title', '', false, 0, 0, '', '', 0), $s);
- print $s;
- print '</span> ';
- $htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1, $user->id);
- print '<input type="submit" class="button small reposition" value="'.$langs->trans("Select").'">';
- print '</div>';
- print '</form>';
- // Title of array with fields
- print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
- print '<table class="noborder centpercent">';
- print '<tr class="liste_titre">';
- print '<td>'.$langs->trans("FieldsInSourceFile").'</td>';
- print '<td>'.$langs->trans("FieldsInTargetDatabase").'</td>';
- print '</tr>';
- //var_dump($array_match_file_to_database);
- print '<tr valign="top"><td width="50%" class="nopaddingleftimp">';
- $fieldsplaced = array();
- $valforsourcefieldnb = array();
- $listofkeys = array();
- foreach ($array_match_file_to_database as $key => $val) {
- $listofkeys[$key] = 1;
- }
- print "\n<!-- Box left container -->\n";
- print '<div id="left" class="connectedSortable">'."\n";
- // List of source fields
- $lefti = 1;
- foreach ($fieldssource as $key => $val) {
- show_elem($fieldssource, $key, $val, $var); // key is field number in source file
- $listofkeys[$key] = 1;
- $fieldsplaced[$key] = 1;
- $valforsourcefieldnb[$lefti] = $key;
- $lefti++;
- if ($lefti > count($fieldstarget)) {
- break; // Other fields are in the not imported area
- }
- }
- //var_dump($valforsourcefieldnb);
- print "</div>\n";
- print "<!-- End box left container -->\n";
- print '</td><td width="50%" class="nopaddingrightimp">';
- // List of target fields
- $optionsnotused = "";
- $optionsall = array();
- foreach ($fieldstarget as $code => $line) {
- $text = '<option value="'.$code.'">';
- $text .= $langs->trans($line["label"]);
- if ($line["required"]) {
- $text .= "*";
- }
- $text .= '</option>';
- if (!$line["imported"]) {
- $optionsnotused .= $text;
- }
- $optionsall[$code] = array('label'=>$langs->trans($line["label"]), 'required'=>(empty($line["required"]) ? 0 : 1));
- }
- $height = '32px'; //needs px for css height attribute below
- $i = 0;
- $mandatoryfieldshavesource = true;
- print '<table class="nobordernopadding centpercent tableimport">';
- foreach ($fieldstarget as $code => $line) {
- if ($i == $minpos) {
- break;
- }
- print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
- $entity = (!empty($objimport->array_import_entities[0][$code]) ? $objimport->array_import_entities[0][$code] : $objimport->array_import_icon[0]);
- $tablealias = preg_replace('/(\..*)$/i', '', $code);
- $tablename = $objimport->array_import_tables[0][$tablealias];
- $entityicon = !empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity; // $entityicon must string name of picto of the field like 'project', 'company', 'contact', 'modulename', ...
- $entitylang = $entitytolang[$entity] ? $entitytolang[$entity] : $objimport->array_import_label[0]; // $entitylang must be a translation key to describe object the field is related to, like 'Company', 'Contact', 'MyModyle', ...
- //print '<td class="nowraponall" style="font-weight: normal">=> '.img_object('', $entityicon).' '.$langs->trans($entitylang).'</td>';
- print '<td class="nowraponall" style="font-weight: normal">=> </td>';
- print '<td class="nowraponall" style="font-weight: normal">';
- print '<select id="selectorderimport_'.($i+1).'" class="targetselectchange minwidth300" name="select_'.$line["label"].'">';
- if ($line["imported"]) {
- print '<option value="-1"> </option>';
- } else {
- print '<option selected="" value="-1"> </option>';
- }
- $j = 0;
- foreach ($optionsall as $code => $val) {
- $label = $val['required'] ? '<strong>' : '';
- $label .= $val['label'];
- $label .= $val['required'] ? '*</strong>' : '';
- print '<option value="'.$code.'"';
- if ($j == $i) {
- print ' selected';
- }
- print ' data-html="'.dol_escape_htmltag($label).'"';
- print '>';
- print $label;
- print '</options>';
- $j++;
- }
- print '</select>';
- print ajax_combobox('selectorderimport_'.($i+1));
- print "</td>";
- print '<td class="nowraponall" style="font-weight:normal; text-align:right">';
- $filecolumn = ($i + 1);
- // Source field info
- $htmltext = '<b><u>'.$langs->trans("FieldSource").'</u></b><br>';
- if ($filecolumn > count($fieldssource)) {
- $htmltext .= $langs->trans("DataComeFromNoWhere").'<br>';
- } else {
- if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
- $filecolumntoshow = $i + 1;
- $htmltext .= $langs->trans("DataComeFromFileFieldNb", $filecolumntoshow).'<br>';
- } else {
- if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') {
- $htmltext .= $langs->trans("DataComeFromIdFoundFromRef", $filecolumn, $langs->transnoentitiesnoconv($entitylang)).'<br>';
- }
- if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') {
- $htmltext .= $langs->trans("DataComeFromIdFoundFromCodeId", $filecolumn, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).'<br>';
- }
- }
- }
- // Source required
- $example = !empty($objimport->array_import_examplevalues[0][$code])?$objimport->array_import_examplevalues[0][$code]:"";
- // Example
- if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
- if ($example) {
- $htmltext .= $langs->trans("SourceExample").': <b>'.$example.'</b><br>';
- }
- } else {
- if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') {
- $htmltext .= $langs->trans("SourceExample").': <b>'.$langs->transnoentitiesnoconv("ExampleAnyRefFoundIntoElement", $entitylang).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'</b><br>';
- } elseif ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') {
- $htmltext .= $langs->trans("SourceExample").': <b>'.$langs->trans("ExampleAnyCodeOrIdFoundIntoDictionary", $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'</b><br>';
- } elseif ($example) {
- $htmltext .= $langs->trans("SourceExample").': <b>'.$example.'</b><br>';
- }
- }
- // Format control rule
- if (!empty($objimport->array_import_regex[0][$code])) {
- $htmltext .= $langs->trans("FormatControlRule").': <b>'.$objimport->array_import_regex[0][$code].'</b><br>';
- }
- $htmltext .= '<br>';
- // Target field info
- $htmltext .= '<b><u>'.$langs->trans("FieldTarget").'</u></b><br>';
- //$htmltext .= $langs->trans("SourceRequired").': <b>'.yn($line["label"]).'</b><br>';
- if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
- $htmltext .= $langs->trans("DataIsInsertedInto").'<br>';
- } else {
- if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') {
- $htmltext .= $langs->trans("DataIDSourceIsInsertedInto").'<br>';
- }
- if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') {
- $htmltext .= $langs->trans("DataCodeIDSourceIsInsertedInto").'<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 '</tr>';
- $i++;
- }
- print '</table>';
- print '</td></tr>';
- // List of not imported fields
- /*
- print '<tr class="liste_titre"><td colspan="2">'.$langs->trans("NotUsedFields").'</td></tr>';
- print '<tr valign="top"><td width="50%">';
- print "\n<!-- Box ignore container -->\n";
- print '<div id="right" class="connectedSortable">'."\n";
- $nbofnotimportedfields = 0;
- foreach ($fieldstarget as $key => $val) {
- if (!$fieldstarget[$key]['imported']) {
- //
- $nbofnotimportedfields++;
- show_elem($fieldstarget, $key, '', $var, 'nostyle');
- //print '> '.$lefti.'-'.$key;
- $listofkeys[$key] = 1;
- $lefti++;
- }
- }
- // Print one more empty field
- $newkey = getnewkey($fieldssource, $listofkeys);
- show_elem($fieldssource, $newkey, '', $var, 'nostyle');
- //print '> '.$lefti.'-'.$newkey;
- $listofkeys[$newkey] = 1;
- $nbofnotimportedfields++;
- print "</div>\n";
- print "<!-- End box ignore container -->\n";
- print '</td>';
- print '<td width="50%">';
- $i = 0;
- while ($i < $nbofnotimportedfields) {
- // Print empty cells
- show_elem('', '', 'none', $var, 'nostyle');
- $i++;
- }
- print '</td></tr>';
- */
- print '</table>';
- print '</div>';
- if (!empty($conf->use_javascript_ajax)) {
- print '<script type="text/javascript">'."\n";
- print 'var previousselectedvalueimport = "0";'."\n";
- print 'var previousselectedlabelimport = "0";'."\n";
- print 'var arrayofselectedvalues = [];'."\n";
- print '$(document).ready(function () {'."\n";
- print 'setOptionsToDisabled();'."\n";
- print 'saveSelection();'."\n";
- print '$(".targetselectchange").focus(function(){'."\n";
- print ' previousselectedvalueimport = $(this).val();'."\n";
- print ' previousselectedlabelimport = $(this).children("option:selected").text();'."\n";
- print ' console.log("previousselectedvalueimport="+previousselectedvalueimport)'."\n";
- print '})'."\n";
- // Function to set the disabled flag
- // - We set all option to "enabled"
- // - Then we scan all combo to get the value currently selected and save them into the array arrayofselectedvalues
- // - Then we set to disabled all fields that are selected
- print 'function setOptionsToDisabled() {'."\n";
- print ' console.log("Remove the disabled flag everywhere");'."\n";
- print ' $(".targetselectchange").not($( this )).find(\'option\').prop("disabled", false);'."\n";
- print ' arrayofselectedvalues = [];'."\n";
- print ' $(".targetselectchange").each(function(){'."\n";
- print ' value = $(this).val()'."\n";
- print ' arrayofselectedvalues.push(value);'."\n";
- print ' });'."\n";
- print ' console.log("List of all selected values");'."\n";
- print ' console.log(arrayofselectedvalues);'."\n";
- print ' console.log("Set the disabled flag for every entry in arrayofselectedvalues");'."\n";
- print ' $.each( arrayofselectedvalues, function( key, value ) {'."\n";
- print ' if (value != -1) {'."\n";
- print ' console.log("Process key="+key+" value="+value);'."\n";
- print ' $(".targetselectchange").find(\'option[value="\'+value+\'"]\').prop("disabled", true);'."\n";
- print ' }'."\n";
- print ' });'."\n";
- print '};'."\n";
- // Function to save the selection
- print 'function saveSelection() {'."\n";
- print ' arrayselectedfields = [];'."\n";
- print ' arrayselectedfields.push("0");'."\n";
- print ' $.each( arrayofselectedvalues, function( key, value ) {'."\n";
- print ' if (value != -1) {'."\n";
- print ' arrayselectedfields.push(value);'."\n";
- print ' } else {'."\n";
- print ' arrayselectedfields.push(0);'."\n";
- print ' }'."\n";
- print ' });'."\n";
- print " $.ajax({\n";
- print " type: 'POST',\n";
- print " dataType: 'json',\n";
- print " url: '".dol_escape_js($_SERVER["PHP_SELF"])."?action=saveselectorder&token=".newToken()."',\n";
- print " data: 'selectorder='+arrayselectedfields.toString(),\n";
- print " success: function(){\n";
- print " console.log('Select order saved');\n";
- print " },\n";
- print ' });'."\n";
- print '};'."\n";
- // If we make a change on a selectbox
- print '$(".targetselectchange").change(function(){'."\n";
- print ' setOptionsToDisabled();'."\n";
- print ' if(previousselectedlabelimport != "" && previousselectedvalueimport != -1) {'."\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 ' $(this).blur()'."\n";
- print ' saveSelection()'."\n";
- print '});'."\n";
- print '})'."\n";
- print '</script>'."\n";
- }
- /*
- * Action bar
- */
- print '<div class="tabsAction">';
- if (count($array_match_file_to_database)) {
- if ($mandatoryfieldshavesource) {
- 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>';
- }
- }
- print '</div>';
- // Area for profils import
- if (count($array_match_file_to_database)) {
- print '<br>'."\n";
- print '<!-- Area to add new import profile -->'."\n";
- print '<div class="marginbottomonly"><span class="opacitymedium">'.$langs->trans("SaveImportModel").'</span></div>';
- print '<form class="nocellnopadd" action="'.$_SERVER["PHP_SELF"].'" method="post">';
- print '<input type="hidden" name="token" value="'.newToken().'">';
- print '<input type="hidden" name="action" value="add_import_model">';
- print '<input type="hidden" name="step" value="'.$step.'">';
- print '<input type="hidden" name="format" value="'.$format.'">';
- print '<input type="hidden" name="datatoimport" value="'.$datatoimport.'">';
- print '<input type="hidden" name="filetoimport" value="'.$filetoimport.'">';
- print '<input type="hidden" name="hexa" value="'.$hexa.'">';
- print '<input type="hidden" name="excludefirstline" value="'.$excludefirstline.'">';
- print '<input type="hidden" name="endatlinenb" value="'.$endatlinenb.'">';
- print '<input type="hidden" name="page_y" value="">';
- print '<input type="hidden" value="'.dol_escape_htmltag($separator).'" name="separator">';
- print '<input type="hidden" value="'.dol_escape_htmltag($enclosure).'" name="enclosure">';
- print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
- print '<table summary="selectofimportprofil" class="noborder centpercent">';
- print '<tr class="liste_titre">';
- print '<td>'.$langs->trans("ImportModelName").'</td>';
- print '<td>'.$langs->trans("Visibility").'</td>';
- print '<td></td>';
- print '</tr>';
- print '<tr class="oddeven">';
- print '<td><input name="import_name" value=""></td>';
- print '<td>';
- $arrayvisibility = array('private'=>$langs->trans("Private"), 'all'=>$langs->trans("Everybody"));
- print $form->selectarray('visibility', $arrayvisibility, 'private');
- print '</td>';
- print '<td class="right">';
- print '<input type="submit" class="button small reposition" value="'.$langs->trans("SaveImportProfile").'">';
- print '</td></tr>';
- // List of existing import profils
- $sql = "SELECT rowid, label, fk_user, entity";
- $sql .= " FROM ".MAIN_DB_PREFIX."import_model";
- $sql .= " WHERE type = '".$db->escape($datatoimport)."'";
- if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner.
- $sql .= " AND fk_user IN (0, ".((int) $user->id).")";
- }
- $sql .= " ORDER BY rowid";
- $resql = $db->query($sql);
- if ($resql) {
- $num = $db->num_rows($resql);
- $tmpuser = new user($db);
- $i = 0;
- while ($i < $num) {
- $obj = $db->fetch_object($resql);
- print '<tr class="oddeven"><td>';
- print $obj->label;
- print '</td>';
- print '<td>';
- if (empty($obj->fk_user)) {
- print $langs->trans("Everybody");
- } else {
- $tmpuser->fetch($obj->fk_user);
- print $tmpuser->getNomUrl(1);
- }
- print '</td>';
- print '<td class="right">';
- print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?step='.$step.$param.'&action=deleteprof&token='.newToken().'&id='.$obj->rowid.'&filetoimport='.urlencode($filetoimport).'">';
- print img_delete();
- print '</a>';
- print '</tr>';
- $i++;
- }
- } else {
- dol_print_error($db);
- }
- print '</table>';
- print '</div>';
- print '</form>';
- }
- }
- // 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
- $max_time = @ini_get("max_execution_time");
- if ($max_time && $max_time < $max_execution_time_for_importexport) {
- 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.");
- @ini_set("max_execution_time", $max_execution_time_for_importexport); // This work only if safe mode is off. also web servers has timeout of 300
- }
- $model = $format;
- $list = $objmodelimport->liste_modeles($db);
- // Create classe to use for import
- $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
- $file = "import_".$model.".modules.php";
- $classname = "Import".ucfirst($model);
- require_once $dir.$file;
- $obj = new $classname($db, $datatoimport);
- if ($model == 'csv') {
- $obj->separator = $separator_used;
- $obj->enclosure = $enclosure;
- }
- // Load source fields in input file
- $fieldssource = array();
- $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs);
- if ($result >= 0) {
- // Read first line
- $arrayrecord = $obj->import_read_record();
- // Put into array fieldssource starting with 1.
- $i = 1;
- foreach ($arrayrecord as $key => $val) {
- $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24);
- $i++;
- }
- $obj->import_close_file();
- }
- $nboflines = $obj->import_get_nb_of_lines($conf->import->dir_temp.'/'.$filetoimport);
- $param = '&leftmenu=import&format='.urlencode($format).'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport).'&nboflines='.urlencode($nboflines).'&separator='.urlencode($separator).'&enclosure='.urlencode($enclosure);
- $param2 = $param; // $param2 = $param without excludefirstline and endatlinenb
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- if (!empty($updatekeys)) {
- $param .= '&updatekeys[]='.implode('&updatekeys[]=', $updatekeys);
- }
- llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones');
- $head = import_prepare_head($param, 5);
- print '<form action="'.$_SERVER["PHP_SELF"].'?'.$param2.'" method="POST">';
- print '<input type="hidden" name="token" value="'.newToken().'">';
- print '<input type="hidden" name="step" value="5">'; // step 5
- print '<input type="hidden" name="action" value="launchsimu">'; // step 5
- print dol_get_fiche_head($head, 'step5', '', -2);
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border tableforfield">';
- // Module
- print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
- print '<td>';
- $titleofmodule = $objimport->array_import_module[0]['module']->getName();
- // Special cas for import common to module/services
- if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
- $titleofmodule = $langs->trans("ProductOrService");
- }
- print $titleofmodule;
- print '</td></tr>';
- // Lot de donnees a importer
- print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
- print '<td>';
- $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
- $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
- print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
- print $objimport->array_import_label[0];
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border tableforfield">';
- // Source file format
- print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
- print '<td>';
- $text = $objmodelimport->getDriverDescForKey($format);
- print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
- print '</td></tr>';
- // Separator and enclosure
- if ($model == 'csv') {
- print '<tr><td>'.$langs->trans("CsvOptions").'</td>';
- print '<td>';
- print $langs->trans("Separator").' : '.dol_escape_htmltag($separator);
- print ' '.$langs->trans("Enclosure").' : '.dol_escape_htmltag($enclosure);
- print '</td></tr>';
- }
- // File to import
- print '<tr><td>'.$langs->trans("FileToImport").'</td>';
- print '<td>';
- $modulepart = 'import';
- $relativepath = GETPOST('filetoimport');
- print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=4'.$param.'" target="_blank" rel="noopener noreferrer">';
- print img_mime($file, '', 'pictofixedwidth');
- print $filetoimport;
- print '</a>';
- print '</td></tr>';
- // Total lines in source file
- print '<tr><td>';
- print $langs->trans("NbOfSourceLines");
- print '</td><td>';
- print $nboflines;
- print '</td></tr>';
- // Range of lines to import
- print '<tr><td>';
- print $langs->trans("ImportFromToLine");
- print '</td><td>';
- if ($action == 'launchsimu') {
- print '<input type="number" class="maxwidth50 right" name="excludefirstlinebis" disabled="disabled" value="'.$excludefirstline.'">';
- print '<input type="hidden" name="excludefirstline" value="'.$excludefirstline.'">';
- } else {
- print '<input type="number" class="maxwidth50 right" name="excludefirstline" value="'.$excludefirstline.'">';
- print $form->textwithpicto("", $langs->trans("SetThisValueTo2ToExcludeFirstLine"));
- }
- print ' - ';
- if ($action == 'launchsimu') {
- print '<input type="text" class="maxwidth50" name="endatlinenbbis" disabled="disabled" value="'.$endatlinenb.'">';
- print '<input type="hidden" name="endatlinenb" value="'.$endatlinenb.'">';
- } else {
- print '<input type="text" class="maxwidth50" name="endatlinenb" value="'.$endatlinenb.'">';
- print $form->textwithpicto("", $langs->trans("KeepEmptyToGoToEndOfFile"));
- }
- if ($action == 'launchsimu') {
- print ' <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)
- print '<tr><td>';
- print $langs->trans("KeysToUseForUpdates");
- print '</td><td>';
- if ($action == 'launchsimu') {
- if (count($updatekeys)) {
- print $form->multiselectarray('updatekeysbis', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '80%', 'disabled');
- } else {
- print '<span class="opacitymedium">'.$langs->trans("NoUpdateAttempt").'</span> -';
- }
- foreach ($updatekeys as $val) {
- print '<input type="hidden" name="updatekeys[]" value="'.$val.'">';
- }
- print ' <a href="'.$_SERVER["PHP_SELF"].'?step=5'.$param.'">'.$langs->trans("Modify").'</a>';
- } else {
- if (is_array($objimport->array_import_updatekeys[0]) && count($objimport->array_import_updatekeys[0])) { //TODO dropdown UL is created inside nested SPANS
- print $form->multiselectarray('updatekeys', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '80%');
- print $form->textwithpicto("", $langs->trans("SelectPrimaryColumnsForUpdateAttempt"));
- } else {
- print '<span class="opacitymedium">'.$langs->trans("UpdateNotYetSupportedForThisImport").'</span>';
- }
- }
- /*echo '<pre>';
- print_r($objimport->array_import_updatekeys);
- echo '</pre>';*/
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print load_fiche_titre($langs->trans("InformationOnTargetTables"), '', 'file-import');
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border tableforfield">';
- // Tables imported
- print '<tr><td class="titlefieldcreate">';
- print $langs->trans("TablesTarget");
- print '</td><td>';
- $listtables = array();
- $sort_array_match_file_to_database = $array_match_file_to_database;
- foreach ($array_match_file_to_database as $code => $label) {
- //var_dump($fieldssource);
- if ($code > count($fieldssource)) {
- continue;
- }
- //print $code.'-'.$label;
- $alias = preg_replace('/(\..*)$/i', '', $label);
- $listtables[$alias] = $objimport->array_import_tables[0][$alias];
- }
- if (count($listtables)) {
- $newval = '';
- //ksort($listtables);
- foreach ($listtables as $val) {
- if ($newval) {
- print ', ';
- }
- $newval = $val;
- // Link to Dolibarr wiki pages
- /*$helppagename='EN:Table_'.$newval;
- if ($helppagename && empty($conf->global->MAIN_HELP_DISABLELINK))
- {
- // Get helpbaseurl, helppage and mode from helppagename and langs
- $arrayres=getHelpParamFor($helppagename,$langs);
- $helpbaseurl=$arrayres['helpbaseurl'];
- $helppage=$arrayres['helppage'];
- $mode=$arrayres['mode'];
- $newval.=' <a href="'.sprintf($helpbaseurl,$helppage).'">'.img_picto($langs->trans($mode == 'wiki' ? 'GoToWikiHelpPage': 'GoToHelpPage'),DOL_URL_ROOT.'/theme/common/helpdoc.png','',1).'</a>';
- }*/
- print $newval;
- }
- } else {
- print $langs->trans("Error");
- }
- print '</td></tr>';
- // Fields imported
- print '<tr><td>';
- print $langs->trans("FieldsTarget").'</td><td>';
- $listfields = array();
- $i = 0;
- //print 'fieldsource='.$fieldssource;
- $sort_array_match_file_to_database = $array_match_file_to_database;
- ksort($sort_array_match_file_to_database);
- //var_dump($sort_array_match_file_to_database);
- foreach ($sort_array_match_file_to_database as $code => $label) {
- $i++;
- //var_dump($fieldssource);
- if ($code > count($fieldssource)) {
- continue;
- }
- //print $code.'-'.$label;
- $alias = preg_replace('/(\..*)$/i', '', $label);
- $listfields[$i] = $langs->trans("Field").' '.$code.'->'.$label;
- }
- print count($listfields) ? (join(', ', $listfields)) : $langs->trans("Error");
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print dol_get_fiche_end();
- if ($action != 'launchsimu') {
- // Show import id
- print '<br><span class="opacitymedium">';
- print $langs->trans("NowClickToTestTheImport", $langs->transnoentitiesnoconv("RunSimulateImportFile")).'</span><br>';
- print '<br>';
- // Actions
- print '<div class="center">';
- if ($user->rights->import->run) {
- print '<input type="submit" class="butAction" value="'.$langs->trans("RunSimulateImportFile").'">';
- } else {
- print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("RunSimulateImportFile").'</a>';
- }
- print '</div>';
- } else {
- // Launch import
- $arrayoferrors = array();
- $arrayofwarnings = array();
- $maxnboferrors = empty($conf->global->IMPORT_MAX_NB_OF_ERRORS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_ERRORS;
- $maxnbofwarnings = empty($conf->global->IMPORT_MAX_NB_OF_WARNINGS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_WARNINGS;
- $nboferrors = 0;
- $nbofwarnings = 0;
- $importid = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
- //var_dump($array_match_file_to_database);
- $db->begin();
- // Open input file
- $nbok = 0;
- $pathfile = $conf->import->dir_temp.'/'.$filetoimport;
- $result = $obj->import_open_file($pathfile, $langs);
- if ($result > 0) {
- global $tablewithentity_cache;
- $tablewithentity_cache = array();
- $sourcelinenb = 0; $endoffile = 0;
- // Loop on each input file record
- while (($sourcelinenb < $nboflines) && !$endoffile) {
- $sourcelinenb++;
- // Read line and store it into $arrayrecord
- //dol_syslog("line ".$sourcelinenb.' - '.$nboflines.' - '.$excludefirstline.' - '.$endatlinenb);
- $arrayrecord = $obj->import_read_record();
- if ($arrayrecord === false) {
- $arrayofwarnings[$sourcelinenb][0] = array('lib'=>'File has '.$nboflines.' lines. However we reach end of file after record '.$sourcelinenb.'. This may occurs when some records are split onto several lines. Ensure the complete string is delimited correctly when there is a separator character in the text string.', 'type'=>'EOF_RECORD_ON_SEVERAL_LINES');
- $endoffile++;
- continue;
- }
- if ($excludefirstline && ($sourcelinenb < $excludefirstline)) {
- continue;
- }
- if ($endatlinenb && ($sourcelinenb > $endatlinenb)) {
- break;
- }
- // Run import
- $result = $obj->import_insert($arrayrecord, $array_match_file_to_database, $objimport, count($fieldssource), $importid, $updatekeys);
- if (count($obj->errors)) {
- $arrayoferrors[$sourcelinenb] = $obj->errors;
- }
- if (count($obj->warnings)) {
- $arrayofwarnings[$sourcelinenb] = $obj->warnings;
- }
- if (!count($obj->errors) && !count($obj->warnings)) {
- $nbok++;
- }
- }
- // Close file
- $obj->import_close_file();
- } else {
- print $langs->trans("ErrorFailedToOpenFile", $pathfile);
- }
- $error = 0;
- // Run the sql after import if defined
- //var_dump($objimport->array_import_run_sql_after[0]);
- if (!empty($objimport->array_import_run_sql_after[0]) && is_array($objimport->array_import_run_sql_after[0])) {
- $i = 0;
- foreach ($objimport->array_import_run_sql_after[0] as $sqlafterimport) {
- $i++;
- $resqlafterimport = $db->query($sqlafterimport);
- if (!$resqlafterimport) {
- $arrayoferrors['none'][] = array('lib'=>$langs->trans("Error running final request: ".$sqlafterimport));
- $error++;
- }
- }
- }
- $db->rollback(); // We force rollback because this was just a simulation.
- // Show OK
- if (!count($arrayoferrors) && !count($arrayofwarnings)) {
- print '<div class="center">'.img_picto($langs->trans("OK"), 'tick').' <b>'.$langs->trans("NoError").'</b></div><br><br>';
- print '<div class="ok">';
- print $langs->trans("NbInsert", empty($obj->nbinsert) ? 0 : $obj->nbinsert).'<br>';
- print $langs->trans("NbUpdate", empty($obj->nbupdate) ? 0 : $obj->nbupdate).'<br>';
- print '</div>';
- print '<br>';
- } else {
- print '<br>';
- print '<div class="info">';
- print $langs->trans("NbOfLinesOK", $nbok).'<br>';
- print '</div>';
- print '<br>';
- }
- // Show Errors
- //var_dump($arrayoferrors);
- if (count($arrayoferrors)) {
- print img_error().' <b>'.$langs->trans("ErrorsOnXLines", count($arrayoferrors)).'</b><br>';
- print '<table width="100%" class="border"><tr><td>';
- foreach ($arrayoferrors as $key => $val) {
- $nboferrors++;
- if ($nboferrors > $maxnboferrors) {
- print $langs->trans("TooMuchErrors", (count($arrayoferrors) - $nboferrors))."<br>";
- break;
- }
- print '* '.$langs->trans("Line").' '.$key.'<br>';
- foreach ($val as $i => $err) {
- print ' > '.$err['lib'].'<br>';
- }
- }
- print '</td></tr></table>';
- print '<br>';
- }
- // Show Warnings
- //var_dump($arrayoferrors);
- if (count($arrayofwarnings)) {
- print img_warning().' <b>'.$langs->trans("WarningsOnXLines", count($arrayofwarnings)).'</b><br>';
- print '<table width="100%" class="border"><tr><td>';
- foreach ($arrayofwarnings as $key => $val) {
- $nbofwarnings++;
- if ($nbofwarnings > $maxnbofwarnings) {
- print $langs->trans("TooMuchWarnings", (count($arrayofwarnings) - $nbofwarnings))."<br>";
- break;
- }
- print ' * '.$langs->trans("Line").' '.$key.'<br>';
- foreach ($val as $i => $err) {
- print ' > '.$err['lib'].'<br>';
- }
- }
- print '</td></tr></table>';
- print '<br>';
- }
- // Show import id
- $importid = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
- print '<div class="center">';
- print '<span class="opacitymedium">'.$langs->trans("NowClickToRunTheImport", $langs->transnoentitiesnoconv("RunImportFile")).'</span><br>';
- if (empty($nboferrors)) {
- print $langs->trans("DataLoadedWithId", $importid).'<br>';
- }
- print '</div>';
- print '<br>';
- // Actions
- print '<div class="center">';
- if ($user->rights->import->run) {
- if (empty($nboferrors)) {
- print '<a class="butAction" href="'.DOL_URL_ROOT.'/imports/import.php?leftmenu=import&step=6&importid='.$importid.$param.'">'.$langs->trans("RunImportFile").'</a>';
- } else {
- //print '<input type="submit" class="butAction" value="'.dol_escape_htmltag($langs->trans("RunSimulateImportFile")).'">';
- print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("CorrectErrorBeforeRunningImport")).'">'.$langs->trans("RunImportFile").'</a>';
- }
- } else {
- print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("RunSimulateImportFile").'</a>';
- print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("RunImportFile").'</a>';
- }
- print '</div>';
- }
- print '</form>';
- }
- // STEP 6: Real import
- if ($step == 6 && $datatoimport) {
- $max_execution_time_for_importexport = (empty($conf->global->IMPORT_MAX_EXECUTION_TIME) ? 300 : $conf->global->IMPORT_MAX_EXECUTION_TIME); // 5mn if not defined
- $max_time = @ini_get("max_execution_time");
- if ($max_time && $max_time < $max_execution_time_for_importexport) {
- 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.");
- @ini_set("max_execution_time", $max_execution_time_for_importexport); // This work only if safe mode is off. also web servers has timeout of 300
- }
- $model = $format;
- $list = $objmodelimport->liste_modeles($db);
- $importid = GETPOST("importid", 'alphanohtml');
- // Create classe to use for import
- $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
- $file = "import_".$model.".modules.php";
- $classname = "Import".ucfirst($model);
- require_once $dir.$file;
- $obj = new $classname($db, $datatoimport);
- if ($model == 'csv') {
- $obj->separator = $separator_used;
- $obj->enclosure = $enclosure;
- }
- // Load source fields in input file
- $fieldssource = array();
- $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs);
- if ($result >= 0) {
- // Read first line
- $arrayrecord = $obj->import_read_record();
- // Put into array fieldssource starting with 1.
- $i = 1;
- foreach ($arrayrecord as $key => $val) {
- $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24);
- $i++;
- }
- $obj->import_close_file();
- }
- $nboflines = (!empty($_GET["nboflines"]) ? $_GET["nboflines"] : dol_count_nb_of_line($conf->import->dir_temp.'/'.$filetoimport));
- $param = '&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport).'&nboflines='.urlencode($nboflines);
- if ($excludefirstline) {
- $param .= '&excludefirstline='.urlencode($excludefirstline);
- }
- if ($endatlinenb) {
- $param .= '&endatlinenb='.urlencode($endatlinenb);
- }
- if ($separator) {
- $param .= '&separator='.urlencode($separator);
- }
- if ($enclosure) {
- $param .= '&enclosure='.urlencode($enclosure);
- }
- llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:Módulo_Importaciones');
- $head = import_prepare_head($param, 6);
- print dol_get_fiche_head($head, 'step6', '', -1);
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border">';
- // Module
- print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
- print '<td>';
- $titleofmodule = $objimport->array_import_module[0]['module']->getName();
- // Special cas for import common to module/services
- if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
- $titleofmodule = $langs->trans("ProductOrService");
- }
- print $titleofmodule;
- print '</td></tr>';
- // Lot de donnees a importer
- print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
- print '<td>';
- $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
- $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
- print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
- print $objimport->array_import_label[0];
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table width="100%" class="border">';
- // Source file format
- print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
- print '<td>';
- $text = $objmodelimport->getDriverDescForKey($format);
- print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
- print '</td></tr>';
- // Separator and enclosure
- if ($model == 'csv') {
- print '<tr><td>'.$langs->trans("CsvOptions").'</td>';
- print '<td>';
- print $langs->trans("Separator").' : ';
- print htmlentities($separator);
- print ' '.$langs->trans("Enclosure").' : ';
- print htmlentities($enclosure);
- print '</td></tr>';
- }
- // File to import
- print '<tr><td>'.$langs->trans("FileToImport").'</td>';
- print '<td>';
- $modulepart = 'import';
- $relativepath = GETPOST('filetoimport');
- print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=4'.$param.'" target="_blank" rel="noopener noreferrer">';
- print img_mime($file, '', 'pictofixedwidth');
- print $filetoimport;
- print '</a>';
- print '</td></tr>';
- // Nb of fields
- print '<tr><td>';
- print $langs->trans("NbOfSourceLines");
- print '</td><td>';
- print $nboflines;
- print '</td></tr>';
- // Do not import first lines
- print '<tr><td>';
- print $langs->trans("ImportFromLine");
- print '</td><td>';
- print '<input type="text" size="4" name="excludefirstline" disabled="disabled" value="'.$excludefirstline.'">';
- print '</td></tr>';
- // Do not import end lines
- print '<tr><td>';
- print $langs->trans("EndAtLineNb");
- print '</td><td>';
- print '<input type="text" size="4" name="endatlinenb" disabled="disabled" value="'.$endatlinenb.'">';
- print '</td></tr>';
- print '</table>';
- print '</div>';
- print '<br>';
- print '<b>'.$langs->trans("InformationOnTargetTables").'</b>';
- print '<div class="underbanner clearboth"></div>';
- print '<div class="fichecenter">';
- print '<table class="border centpercent">';
- // Tables imported
- print '<tr><td width="25%">';
- print $langs->trans("TablesTarget");
- print '</td><td>';
- $listtables = array();
- foreach ($array_match_file_to_database as $code => $label) {
- //var_dump($fieldssource);
- if ($code > count($fieldssource)) {
- continue;
- }
- //print $code.'-'.$label;
- $alias = preg_replace('/(\..*)$/i', '', $label);
- $listtables[$alias] = $objimport->array_import_tables[0][$alias];
- }
- if (count($listtables)) {
- $newval = '';
- foreach ($listtables as $val) {
- if ($newval) {
- print ', ';
- }
- $newval = $val;
- // Link to Dolibarr wiki pages
- /*$helppagename='EN:Table_'.$newval;
- if ($helppagename && empty($conf->global->MAIN_HELP_DISABLELINK))
- {
- // Get helpbaseurl, helppage and mode from helppagename and langs
- $arrayres=getHelpParamFor($helppagename,$langs);
- $helpbaseurl=$arrayres['helpbaseurl'];
- $helppage=$arrayres['helppage'];
- $mode=$arrayres['mode'];
- $newval.=' <a href="'.sprintf($helpbaseurl,$helppage).'">'.img_picto($langs->trans($mode == 'wiki' ? 'GoToWikiHelpPage': 'GoToHelpPage'),DOL_URL_ROOT.'/theme/common/helpdoc.png','',1).'</a>';
- }*/
- print $newval;
- }
- } else {
- print $langs->trans("Error");
- }
- print '</td></tr>';
- // Fields imported
- print '<tr><td>';
- print $langs->trans("FieldsTarget").'</td><td>';
- $listfields = array();
- $i = 0;
- $sort_array_match_file_to_database = $array_match_file_to_database;
- ksort($sort_array_match_file_to_database);
- //var_dump($sort_array_match_file_to_database);
- foreach ($sort_array_match_file_to_database as $code => $label) {
- $i++;
- //var_dump($fieldssource);
- if ($code > count($fieldssource)) {
- continue;
- }
- //print $code.'-'.$label;
- $alias = preg_replace('/(\..*)$/i', '', $label);
- $listfields[$i] = $langs->trans("Field").' '.$code.'->'.$label;
- }
- print count($listfields) ? (join(', ', $listfields)) : $langs->trans("Error");
- print '</td></tr>';
- print '</table>';
- print '</div>';
- // Launch import
- $arrayoferrors = array();
- $arrayofwarnings = array();
- $maxnboferrors = empty($conf->global->IMPORT_MAX_NB_OF_ERRORS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_ERRORS;
- $maxnbofwarnings = empty($conf->global->IMPORT_MAX_NB_OF_WARNINGS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_WARNINGS;
- $nboferrors = 0;
- $nbofwarnings = 0;
- $importid = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
- //var_dump($array_match_file_to_database);
- $db->begin();
- // Open input file
- $nbok = 0;
- $pathfile = $conf->import->dir_temp.'/'.$filetoimport;
- $result = $obj->import_open_file($pathfile, $langs);
- if ($result > 0) {
- global $tablewithentity_cache;
- $tablewithentity_cache = array();
- $sourcelinenb = 0; $endoffile = 0;
- while ($sourcelinenb < $nboflines && !$endoffile) {
- $sourcelinenb++;
- $arrayrecord = $obj->import_read_record();
- if ($arrayrecord === false) {
- $arrayofwarnings[$sourcelinenb][0] = array('lib'=>'File has '.$nboflines.' lines. However we reach end of file after record '.$sourcelinenb.'. This may occurs when some records are split onto several lines.', 'type'=>'EOF_RECORD_ON_SEVERAL_LINES');
- $endoffile++;
- continue;
- }
- if ($excludefirstline && ($sourcelinenb < $excludefirstline)) {
- continue;
- }
- if ($endatlinenb && ($sourcelinenb > $endatlinenb)) {
- break;
- }
- // Run import
- $result = $obj->import_insert($arrayrecord, $array_match_file_to_database, $objimport, count($fieldssource), $importid, $updatekeys);
- if (count($obj->errors)) {
- $arrayoferrors[$sourcelinenb] = $obj->errors;
- }
- if (count($obj->warnings)) {
- $arrayofwarnings[$sourcelinenb] = $obj->warnings;
- }
- if (!count($obj->errors) && !count($obj->warnings)) {
- $nbok++;
- }
- }
- // Close file
- $obj->import_close_file();
- } else {
- print $langs->trans("ErrorFailedToOpenFile", $pathfile);
- }
- if (count($arrayoferrors) > 0) {
- $db->rollback(); // We force rollback because this was errors.
- } else {
- $error = 0;
- // Run the sql after import if defined
- //var_dump($objimport->array_import_run_sql_after[0]);
- if (!empty($objimport->array_import_run_sql_after[0]) && is_array($objimport->array_import_run_sql_after[0])) {
- $i = 0;
- foreach ($objimport->array_import_run_sql_after[0] as $sqlafterimport) {
- $i++;
- $resqlafterimport = $db->query($sqlafterimport);
- if (!$resqlafterimport) {
- $arrayoferrors['none'][] = array('lib'=>$langs->trans("Error running final request: ".$sqlafterimport));
- $error++;
- }
- }
- }
- if (!$error) {
- $db->commit(); // We can commit if no errors.
- } else {
- $db->rollback();
- }
- }
- print dol_get_fiche_end();
- // Show result
- print '<br>';
- print '<div class="ok">';
- print $langs->trans("NbOfLinesImported", $nbok).'</b><br>';
- print $langs->trans("NbInsert", empty($obj->nbinsert) ? 0 : $obj->nbinsert).'<br>';
- print $langs->trans("NbUpdate", empty($obj->nbupdate) ? 0 : $obj->nbupdate).'<br><br>';
- print '</div>';
- print '<div class="center">';
- print $langs->trans("FileWasImported", $importid).'<br>';
- print '<span class="opacitymedium">'.$langs->trans("YouCanUseImportIdToFindRecord", $importid).'</span><br>';
- print '</div>';
- }
- print '<br>';
- // End of page
- llxFooter();
- $db->close();
- /**
- * Function to put the movable box of a source field
- *
- * @param array $fieldssource List of source fields
- * @param int $pos Pos
- * @param string $key Key
- * @param boolean $var Line style (odd or not). No more used.
- * @param int $nostyle Hide style
- * @return void
- */
- function show_elem($fieldssource, $pos, $key, $var, $nostyle = '')
- {
- global $langs;
- $height = '32px';
- if ($key == 'none') {
- //stop multiple duplicate ids with no number
- print "\n\n<!-- Box_no-key start-->\n";
- print '<div class="box boximport" style="padding:0;">'."\n";
- print '<table summary="boxtable_no-key" class="centpercent nobordernopadding">'."\n";
- } else {
- print "\n\n<!-- Box ".$pos." start -->\n";
- print '<div class="box boximport" style="padding: 0;" id="boxto_'.$pos.'">'."\n";
- print '<table summary="boxtable'.$pos.'" class="nobordernopadding centpercent tableimport">'."\n";
- }
- if (($pos && $pos > count($fieldssource)) && (!isset($fieldssource[$pos]["imported"]))) { // No fields
- /*
- print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
- print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
- print '</td>';
- print '<td style="font-weight: normal">';
- print $langs->trans("NoFields");
- print '</td>';
- print '</tr>';
- */
- } elseif ($key == 'none') { // Empty line
- print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
- print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
- print ' ';
- print '</td>';
- print '<td style="font-weight: normal">';
- print ' ';
- print '</td>';
- print '</tr>';
- } else {
- // Print field of source file
- print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
- 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("Field").' '.$pos, 'file', 'class="pictofixedwith"');
- print '</td>';
- 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;
- if (empty($fieldssource[$pos]['example1'])) {
- $example = $fieldssource[$pos]['label'];
- } else {
- $example = $fieldssource[$pos]['example1'];
- }
- if ($example) {
- if (!utf8_check($example)) {
- $example = utf8_encode($example);
- }
- print ' - ';
- //print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("ExampleOnFirstLine").': </span>';
- print '<i class="opacitymedium">'.$example.'</i>';
- }
- print '</td>';
- print '</tr>';
- }
- print "</table>\n";
- print "</div>\n";
- print "<!-- Box end -->\n\n";
- }
- /**
- * Return not used field number
- *
- * @param array $fieldssource Array of field source
- * @param array $listofkey Array of keys
- * @return integer
- */
- function getnewkey(&$fieldssource, &$listofkey)
- {
- $i = count($fieldssource) + 1;
- // Max number of key
- $maxkey = 0;
- foreach ($listofkey as $key => $val) {
- $maxkey = max($maxkey, $key);
- }
- // Found next empty key
- while ($i <= $maxkey) {
- if (empty($listofkey[$i])) {
- break;
- } else {
- $i++;
- }
- }
- $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;
- }
|