import.php 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427
  1. <?php
  2. /* Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/imports/import.php
  21. * \ingroup import
  22. * \brief Pages of import Wizard
  23. */
  24. require_once '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/imports/class/import.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/import.lib.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array('exports', 'compta', 'errors'));
  34. // Security check
  35. $result = restrictedArea($user, 'import');
  36. // Map icons, array duplicated in export.php, was not synchronized, TODO put it somewhere only once
  37. $entitytoicon = array(
  38. 'invoice' => 'bill',
  39. 'invoice_line' => 'bill',
  40. 'order' => 'order',
  41. 'order_line' => 'order',
  42. 'propal' => 'propal',
  43. 'propal_line' => 'propal',
  44. 'intervention' => 'intervention',
  45. 'inter_line' => 'intervention',
  46. 'member' => 'user',
  47. 'member_type' => 'group',
  48. 'subscription' => 'payment',
  49. 'payment' => 'payment',
  50. 'tax' => 'bill',
  51. 'tax_type' => 'generic',
  52. 'other' => 'generic',
  53. 'account' => 'account',
  54. 'product' => 'product',
  55. 'virtualproduct'=>'product',
  56. 'subproduct' => 'product',
  57. 'product_supplier_ref' => 'product',
  58. 'stock' => 'stock',
  59. 'warehouse' => 'stock',
  60. 'batch' => 'stock',
  61. 'stockbatch' => 'stock',
  62. 'category' => 'category',
  63. 'shipment' => 'sending',
  64. 'shipment_line'=> 'sending',
  65. 'reception'=> 'sending',
  66. 'reception_line'=> 'sending',
  67. 'expensereport'=> 'trip',
  68. 'expensereport_line'=> 'trip',
  69. 'holiday' => 'holiday',
  70. 'contract_line' => 'contract',
  71. 'translation' => 'generic',
  72. 'bomm' => 'bom',
  73. 'bomline' => 'bom'
  74. );
  75. // Translation code, array duplicated in export.php, was not synchronized, TODO put it somewhere only once
  76. $entitytolang = array(
  77. 'user' => 'User',
  78. 'company' => 'Company',
  79. 'contact' => 'Contact',
  80. 'invoice' => 'Bill',
  81. 'invoice_line' => 'InvoiceLine',
  82. 'order' => 'Order',
  83. 'order_line' => 'OrderLine',
  84. 'propal' => 'Proposal',
  85. 'propal_line' => 'ProposalLine',
  86. 'intervention' => 'Intervention',
  87. 'inter_line' => 'InterLine',
  88. 'member' => 'Member',
  89. 'member_type' => 'MemberType',
  90. 'subscription' => 'Subscription',
  91. 'tax' => 'SocialContribution',
  92. 'tax_type' => 'DictionarySocialContributions',
  93. 'account' => 'BankTransactions',
  94. 'payment' => 'Payment',
  95. 'product' => 'Product',
  96. 'virtualproduct' => 'AssociatedProducts',
  97. 'subproduct' => 'SubProduct',
  98. 'product_supplier_ref' => 'SupplierPrices',
  99. 'service' => 'Service',
  100. 'stock' => 'Stock',
  101. 'movement' => 'StockMovement',
  102. 'batch' => 'Batch',
  103. 'stockbatch' => 'StockDetailPerBatch',
  104. 'warehouse' => 'Warehouse',
  105. 'category' => 'Category',
  106. 'other' => 'Other',
  107. 'trip' => 'TripsAndExpenses',
  108. 'shipment' => 'Shipments',
  109. 'shipment_line'=> 'ShipmentLine',
  110. 'project' => 'Projects',
  111. 'projecttask' => 'Tasks',
  112. 'task_time' => 'TaskTimeSpent',
  113. 'action' => 'Event',
  114. 'expensereport'=> 'ExpenseReport',
  115. 'expensereport_line'=> 'ExpenseReportLine',
  116. 'holiday' => 'TitreRequestCP',
  117. 'contract' => 'Contract',
  118. 'contract_line'=> 'ContractLine',
  119. 'translation' => 'Translation',
  120. 'bom' => 'BOM',
  121. 'bomline' => 'BOMLine'
  122. );
  123. $datatoimport = GETPOST('datatoimport');
  124. $format = GETPOST('format');
  125. $filetoimport = GETPOST('filetoimport');
  126. $action = GETPOST('action', 'alpha');
  127. $confirm = GETPOST('confirm', 'alpha');
  128. $step = (GETPOST('step') ? GETPOST('step') : 1);
  129. $import_name = GETPOST('import_name');
  130. $hexa = GETPOST('hexa');
  131. $importmodelid = GETPOST('importmodelid');
  132. $excludefirstline = (GETPOST('excludefirstline') ? GETPOST('excludefirstline') : 2);
  133. $endatlinenb = (GETPOST('endatlinenb') ? GETPOST('endatlinenb') : '');
  134. $updatekeys = (GETPOST('updatekeys', 'array') ? GETPOST('updatekeys', 'array') : array());
  135. $separator = (GETPOST('separator', 'nohtml') ? GETPOST('separator', 'nohtml') : (!empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ? $conf->global->IMPORT_CSV_SEPARATOR_TO_USE : ','));
  136. $enclosure = (GETPOST('enclosure', 'nohtml') ? GETPOST('enclosure', 'nohtml') : '"');
  137. $separator_used = str_replace('\t', "\t", $separator);
  138. $objimport = new Import($db);
  139. $objimport->load_arrays($user, ($step == 1 ? '' : $datatoimport));
  140. $objmodelimport = new ModeleImports();
  141. $form = new Form($db);
  142. $htmlother = new FormOther($db);
  143. $formfile = new FormFile($db);
  144. // Init $array_match_file_to_database from _SESSION
  145. $serialized_array_match_file_to_database = isset($_SESSION["dol_array_match_file_to_database"]) ? $_SESSION["dol_array_match_file_to_database"] : '';
  146. $array_match_file_to_database = array();
  147. $fieldsarray = explode(',', $serialized_array_match_file_to_database);
  148. foreach ($fieldsarray as $elem) {
  149. $tabelem = explode('=', $elem, 2);
  150. $key = $tabelem[0];
  151. $val = (isset($tabelem[1]) ? $tabelem[1] : '');
  152. if ($key && $val) {
  153. $array_match_file_to_database[$key] = $val;
  154. }
  155. }
  156. if (empty($array_match_file_to_database)) {
  157. $serialized_array_match_file_to_database = isset($_SESSION["dol_array_match_file_to_database_select"]) ? $_SESSION["dol_array_match_file_to_database_select"] : '';
  158. $array_match_file_to_database = array();
  159. $fieldsarray = explode(',', $serialized_array_match_file_to_database);
  160. foreach ($fieldsarray as $elem) {
  161. $tabelem = explode('=', $elem, 2);
  162. $key = $tabelem[0];
  163. $val = (isset($tabelem[1]) ? $tabelem[1] : '');
  164. if ($key && $val) {
  165. $array_match_file_to_database[$key] = $val;
  166. }
  167. }
  168. }
  169. /*
  170. * Actions
  171. */
  172. /*
  173. if ($action=='downfield' || $action=='upfield')
  174. {
  175. $pos=$array_match_file_to_database[$_GET["field"]];
  176. if ($action=='downfield') $newpos=$pos+1;
  177. if ($action=='upfield') $newpos=$pos-1;
  178. // Recherche code avec qui switcher
  179. $newcode="";
  180. foreach($array_match_file_to_database as $code=>$value)
  181. {
  182. if ($value == $newpos)
  183. {
  184. $newcode=$code;
  185. break;
  186. }
  187. }
  188. //print("Switch pos=$pos (code=".$_GET["field"].") and newpos=$newpos (code=$newcode)");
  189. if ($newcode) // Si newcode trouve (protection contre resoumission de page)
  190. {
  191. $array_match_file_to_database[$_GET["field"]]=$newpos;
  192. $array_match_file_to_database[$newcode]=$pos;
  193. $_SESSION["dol_array_match_file_to_database"]=$serialized_array_match_file_to_database;
  194. }
  195. }
  196. */
  197. if ($action == 'builddoc') {
  198. // Build import file
  199. $result = $objimport->build_file($user, GETPOST('model', 'alpha'), $datatoimport, $array_match_file_to_database);
  200. if ($result < 0) {
  201. setEventMessages($objimport->error, $objimport->errors, 'errors');
  202. } else {
  203. setEventMessages($langs->trans("FileSuccessfullyBuilt"), null, 'mesgs');
  204. }
  205. }
  206. if ($action == 'deleteprof') {
  207. if (GETPOST("id", 'int')) {
  208. $objimport->fetch(GETPOST("id", 'int'));
  209. $result = $objimport->delete($user);
  210. }
  211. }
  212. // Save import config to database
  213. if ($action == 'add_import_model') {
  214. if ($import_name) {
  215. // Set save string
  216. $hexa = '';
  217. foreach ($array_match_file_to_database as $key => $val) {
  218. if ($hexa) {
  219. $hexa .= ',';
  220. }
  221. $hexa .= $key.'='.$val;
  222. }
  223. $objimport->model_name = $import_name;
  224. $objimport->datatoimport = $datatoimport;
  225. $objimport->hexa = $hexa;
  226. $objimport->fk_user = (GETPOST('visibility', 'aZ09') == 'all' ? 0 : $user->id);
  227. $result = $objimport->create($user);
  228. if ($result >= 0) {
  229. setEventMessages($langs->trans("ImportModelSaved", $objimport->model_name), null, 'mesgs');
  230. } else {
  231. $langs->load("errors");
  232. if ($objimport->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  233. setEventMessages($langs->trans("ErrorImportDuplicateProfil"), null, 'errors');
  234. } else {
  235. setEventMessages($objimport->error, null, 'errors');
  236. }
  237. }
  238. } else {
  239. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("ImportModelName")), null, 'errors');
  240. }
  241. }
  242. if ($step == 3 && $datatoimport) {
  243. if (GETPOST('sendit') && !empty($conf->global->MAIN_UPLOAD_DOC)) {
  244. dol_mkdir($conf->import->dir_temp);
  245. $nowyearmonth = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
  246. $fullpath = $conf->import->dir_temp."/".$nowyearmonth.'-'.$_FILES['userfile']['name'];
  247. if (dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $fullpath, 1) > 0) {
  248. dol_syslog("File ".$fullpath." was added for import");
  249. } else {
  250. $langs->load("errors");
  251. setEventMessages($langs->trans("ErrorFailedToSaveFile"), null, 'errors');
  252. }
  253. }
  254. // Delete file
  255. if ($action == 'confirm_deletefile' && $confirm == 'yes') {
  256. $langs->load("other");
  257. $param = '&datatoimport='.urlencode($datatoimport).'&format='.urlencode($format);
  258. if ($excludefirstline) {
  259. $param .= '&excludefirstline='.urlencode($excludefirstline);
  260. }
  261. if ($endatlinenb) {
  262. $param .= '&endatlinenb='.urlencode($endatlinenb);
  263. }
  264. $file = $conf->import->dir_temp.'/'.GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  265. $ret = dol_delete_file($file);
  266. if ($ret) {
  267. setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
  268. } else {
  269. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
  270. }
  271. Header('Location: '.$_SERVER["PHP_SELF"].'?step='.$step.$param);
  272. exit;
  273. }
  274. }
  275. if ($step == 4 && $action == 'select_model') {
  276. // Reinit match arrays
  277. $_SESSION["dol_array_match_file_to_database"] = '';
  278. $serialized_array_match_file_to_database = '';
  279. $array_match_file_to_database = array();
  280. // Load model from $importmodelid and set $array_match_file_to_database
  281. // and $_SESSION["dol_array_match_file_to_database"]
  282. $result = $objimport->fetch($importmodelid);
  283. if ($result > 0) {
  284. $serialized_array_match_file_to_database = $objimport->hexa;
  285. $fieldsarray = explode(',', $serialized_array_match_file_to_database);
  286. foreach ($fieldsarray as $elem) {
  287. $tabelem = explode('=', $elem);
  288. $key = $tabelem[0];
  289. $val = $tabelem[1];
  290. if ($key && $val) {
  291. $array_match_file_to_database[$key] = $val;
  292. }
  293. }
  294. $_SESSION["dol_array_match_file_to_database"] = $serialized_array_match_file_to_database;
  295. $_SESSION['dol_array_match_file_to_database_select'] = $_SESSION["dol_array_match_file_to_database"];
  296. }
  297. }
  298. if ($action == 'saveselectorder') {
  299. // Enregistrement de la position des champs
  300. $serialized_array_match_file_to_database = '';
  301. dol_syslog("selectorder=".GETPOST('selectorder'), LOG_DEBUG);
  302. $selectorder = explode(",", GETPOST('selectorder'));
  303. $fieldtarget = $fieldstarget = $objimport->array_import_fields[0];
  304. foreach ($selectorder as $key => $code) {
  305. $serialized_array_match_file_to_database .= $key.'='.$code;
  306. $serialized_array_match_file_to_database .= ',';
  307. }
  308. $serialized_array_match_file_to_database = substr($serialized_array_match_file_to_database, 0, -1);
  309. dol_syslog('dol_array_match_file_to_database_select='.$serialized_array_match_file_to_database);
  310. $_SESSION["dol_array_match_file_to_database_select"] = $serialized_array_match_file_to_database;
  311. echo "{}";
  312. exit(0);
  313. }
  314. /*
  315. * View
  316. */
  317. $help_url = 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones';
  318. // STEP 1: Page to select dataset to import
  319. if ($step == 1 || !$datatoimport) {
  320. // Clean saved file-database matching
  321. $serialized_array_match_file_to_database = '';
  322. $array_match_file_to_database = array();
  323. $_SESSION["dol_array_match_file_to_database"] = '';
  324. $_SESSION["dol_array_match_file_to_database_select"] = '';
  325. $param = '';
  326. if ($excludefirstline) {
  327. $param .= '&excludefirstline='.urlencode($excludefirstline);
  328. }
  329. if ($endatlinenb) {
  330. $param .= '&endatlinenb='.urlencode($endatlinenb);
  331. }
  332. if ($separator) {
  333. $param .= '&separator='.urlencode($separator);
  334. }
  335. if ($enclosure) {
  336. $param .= '&enclosure='.urlencode($enclosure);
  337. }
  338. llxHeader('', $langs->trans("NewImport"), $help_url);
  339. $head = import_prepare_head($param, 1);
  340. print dol_get_fiche_head($head, 'step1', '', -1);
  341. print '<div class="opacitymedium">'.$langs->trans("SelectImportDataSet").'</div><br>';
  342. // Affiche les modules d'imports
  343. 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
  344. print '<table class="noborder centpercent">';
  345. print '<tr class="liste_titre">';
  346. print '<td>'.$langs->trans("Module").'</td>';
  347. print '<td>'.$langs->trans("ImportableDatas").'</td>';
  348. print '<td>&nbsp;</td>';
  349. print '</tr>';
  350. if (count($objimport->array_import_module)) {
  351. $sortedarrayofmodules = dol_sort_array($objimport->array_import_module, 'position_of_profile', 'asc', 0, 0, 1);
  352. foreach ($sortedarrayofmodules as $key => $value) {
  353. //var_dump($key.' '.$value['position_of_profile'].' '.$value['import_code'].' '.$objimport->array_import_module[$key]['module']->getName().' '.$objimport->array_import_code[$key]);
  354. print '<tr class="oddeven"><td>';
  355. $titleofmodule = $objimport->array_import_module[$key]['module']->getName();
  356. // Special cas for import common to module/services
  357. if (in_array($objimport->array_import_code[$key], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
  358. $titleofmodule = $langs->trans("ProductOrService");
  359. }
  360. print $titleofmodule;
  361. print '</td><td>';
  362. $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[$key]);
  363. $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
  364. print img_object($objimport->array_import_module[$key]['module']->getName(), $entityicon).' ';
  365. print $objimport->array_import_label[$key];
  366. print '</td><td style="text-align: right">';
  367. if ($objimport->array_import_perms[$key]) {
  368. 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>';
  369. } else {
  370. print $langs->trans("NotEnoughPermissions");
  371. }
  372. print '</td></tr>';
  373. }
  374. } else {
  375. print '<tr><td class="oddeven" colspan="3">'.$langs->trans("NoImportableData").'</td></tr>';
  376. }
  377. print '</table>';
  378. print '</div>';
  379. print dol_get_fiche_end();
  380. }
  381. // STEP 2: Page to select input format file
  382. if ($step == 2 && $datatoimport) {
  383. $param = '&datatoimport='.urlencode($datatoimport);
  384. if ($excludefirstline) {
  385. $param .= '&excludefirstline='.urlencode($excludefirstline);
  386. }
  387. if ($endatlinenb) {
  388. $param .= '&endatlinenb='.urlencode($endatlinenb);
  389. }
  390. if ($separator) {
  391. $param .= '&separator='.urlencode($separator);
  392. }
  393. if ($enclosure) {
  394. $param .= '&enclosure='.urlencode($enclosure);
  395. }
  396. llxHeader('', $langs->trans("NewImport"), $help_url);
  397. $head = import_prepare_head($param, 2);
  398. print dol_get_fiche_head($head, 'step2', '', -2);
  399. print '<div class="underbanner clearboth"></div>';
  400. print '<div class="fichecenter">';
  401. print '<table class="border tableforfield centpercent">';
  402. // Module
  403. print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
  404. print '<td>';
  405. $titleofmodule = $objimport->array_import_module[0]['module']->getName();
  406. // Special cas for import common to module/services
  407. if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
  408. $titleofmodule = $langs->trans("ProductOrService");
  409. }
  410. print $titleofmodule;
  411. print '</td></tr>';
  412. // Dataset to import
  413. print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
  414. print '<td>';
  415. $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
  416. $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
  417. print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
  418. print $objimport->array_import_label[0];
  419. print '</td></tr>';
  420. print '</table>';
  421. print '</div>';
  422. print dol_get_fiche_end();
  423. print '<form name="userfile" action="'.$_SERVER["PHP_SELF"].'" enctype="multipart/form-data" METHOD="POST">';
  424. print '<input type="hidden" name="token" value="'.newToken().'">';
  425. print '<input type="hidden" name="max_file_size" value="'.$conf->maxfilesize.'">';
  426. print '<br>';
  427. print '<span class="opacitymedium">';
  428. $s = $langs->trans("ChooseFormatOfFileToImport", '{s1}');
  429. $s = str_replace('{s1}', img_picto('', 'next'), $s);
  430. print $s;
  431. print '</span><br><br>';
  432. print '<br>';
  433. 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
  434. print '<table class="noborder centpercent" cellpadding="4">';
  435. $filetoimport = '';
  436. // Add format informations and link to download example
  437. print '<tr class="liste_titre"><td colspan="6">';
  438. print $langs->trans("FileMustHaveOneOfFollowingFormat");
  439. print '</td></tr>';
  440. $list = $objmodelimport->liste_modeles($db);
  441. foreach ($list as $key) {
  442. print '<tr class="oddeven">';
  443. print '<td width="16">'.img_picto_common($key, $objmodelimport->getPictoForKey($key)).'</td>';
  444. $text = $objmodelimport->getDriverDescForKey($key);
  445. print '<td>'.$form->textwithpicto($objmodelimport->getDriverLabelForKey($key), $text).'</td>';
  446. print '<td style="text-align:center">';
  447. 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");
  448. print '</a>';
  449. print ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("StarAreMandatory").')</span>';
  450. print '</td>';
  451. // Action button
  452. print '<td style="text-align:right">';
  453. print '<a href="'.DOL_URL_ROOT.'/imports/import.php?step=3&format='.$key.$param.'">'.img_picto($langs->trans("SelectFormat"), 'next', 'class="fa-15x"').'</a>';
  454. print '</td>';
  455. print '</tr>';
  456. }
  457. print '</table>';
  458. print '</div>';
  459. print '</form>';
  460. }
  461. // STEP 3: Page to select file
  462. if ($step == 3 && $datatoimport) {
  463. $param = '&datatoimport='.urlencode($datatoimport).'&format='.urlencode($format);
  464. if ($excludefirstline) {
  465. $param .= '&excludefirstline='.urlencode($excludefirstline);
  466. }
  467. if ($endatlinenb) {
  468. $param .= '&endatlinenb='.urlencode($endatlinenb);
  469. }
  470. if ($separator) {
  471. $param .= '&separator='.urlencode($separator);
  472. }
  473. if ($enclosure) {
  474. $param .= '&enclosure='.urlencode($enclosure);
  475. }
  476. $list = $objmodelimport->liste_modeles($db);
  477. llxHeader('', $langs->trans("NewImport"), $help_url);
  478. $head = import_prepare_head($param, 3);
  479. print dol_get_fiche_head($head, 'step3', '', -2);
  480. /*
  481. * Confirm delete file
  482. */
  483. if ($action == 'delete') {
  484. print $form->formconfirm($_SERVER["PHP_SELF"].'?urlfile='.urlencode(GETPOST('urlfile')).'&step=3'.$param, $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1);
  485. }
  486. print '<div class="underbanner clearboth"></div>';
  487. print '<div class="fichecenter">';
  488. print '<table class="border tableforfield centpercent">';
  489. // Module
  490. print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
  491. print '<td>';
  492. $titleofmodule = $objimport->array_import_module[0]['module']->getName();
  493. // Special cas for import common to module/services
  494. if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
  495. $titleofmodule = $langs->trans("ProductOrService");
  496. }
  497. print $titleofmodule;
  498. print '</td></tr>';
  499. // Lot de donnees a importer
  500. print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
  501. print '<td>';
  502. $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
  503. $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
  504. print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
  505. print $objimport->array_import_label[0];
  506. print '</td></tr>';
  507. print '</table>';
  508. print '</div>';
  509. print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
  510. print '<div class="underbanner clearboth"></div>';
  511. print '<div class="fichecenter">';
  512. print '<table width="100%" class="border tableforfield">';
  513. // Source file format
  514. print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
  515. print '<td class="nowraponall">';
  516. $text = $objmodelimport->getDriverDescForKey($format);
  517. print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
  518. print '</td><td style="text-align:right" class="nowrap">';
  519. 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");
  520. print '</a>';
  521. print ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("StarAreMandatory").')</span>';
  522. print '</td></tr>';
  523. print '</table>';
  524. print '</div>';
  525. print dol_get_fiche_end();
  526. if ($format == 'xlsx' && !class_exists('XMLWriter')) {
  527. $langs->load("install");
  528. print info_admin($langs->trans("ErrorPHPDoesNotSupport", 'php-xml'), 0, 0, 1, 'error');
  529. }
  530. print '<br>';
  531. print '<form name="userfile" action="'.$_SERVER["PHP_SELF"].'" enctype="multipart/form-data" METHOD="POST">';
  532. print '<input type="hidden" name="token" value="'.newToken().'">';
  533. print '<input type="hidden" name="max_file_size" value="'.$conf->maxfilesize.'">';
  534. print '<input type="hidden" value="'.$step.'" name="step">';
  535. print '<input type="hidden" value="'.dol_escape_htmltag($format).'" name="format">';
  536. print '<input type="hidden" value="'.$excludefirstline.'" name="excludefirstline">';
  537. print '<input type="hidden" value="'.$endatlinenb.'" name="endatlinenb">';
  538. print '<input type="hidden" value="'.dol_escape_htmltag($separator).'" name="separator">';
  539. print '<input type="hidden" value="'.dol_escape_htmltag($enclosure).'" name="enclosure">';
  540. print '<input type="hidden" value="'.dol_escape_htmltag($datatoimport).'" name="datatoimport">';
  541. print '<span class="opacitymedium">';
  542. $s = $langs->trans("ChooseFileToImport", '{s1}');
  543. $s = str_replace('{s1}', img_picto('', 'next'), $s);
  544. print $s;
  545. print '</span><br><br>';
  546. $filetoimport = '';
  547. // Input file name box
  548. print '<div class="marginbottomonly">';
  549. print '<input type="file" name="userfile" size="20" maxlength="80"> &nbsp; &nbsp; ';
  550. $out = (empty($conf->global->MAIN_UPLOAD_DOC) ? ' disabled' : '');
  551. print '<input type="submit" class="button small" value="'.$langs->trans("AddFile").'"'.$out.' name="sendit">';
  552. $out = '';
  553. if (!empty($conf->global->MAIN_UPLOAD_DOC)) {
  554. $max = $conf->global->MAIN_UPLOAD_DOC; // In Kb
  555. $maxphp = @ini_get('upload_max_filesize'); // In unknown
  556. if (preg_match('/k$/i', $maxphp)) {
  557. $maxphp = $maxphp * 1;
  558. }
  559. if (preg_match('/m$/i', $maxphp)) {
  560. $maxphp = $maxphp * 1024;
  561. }
  562. if (preg_match('/g$/i', $maxphp)) {
  563. $maxphp = $maxphp * 1024 * 1024;
  564. }
  565. if (preg_match('/t$/i', $maxphp)) {
  566. $maxphp = $maxphp * 1024 * 1024 * 1024;
  567. }
  568. $maxphp2 = @ini_get('post_max_size'); // In unknown
  569. if (preg_match('/k$/i', $maxphp2)) {
  570. $maxphp2 = $maxphp2 * 1;
  571. }
  572. if (preg_match('/m$/i', $maxphp2)) {
  573. $maxphp2 = $maxphp2 * 1024;
  574. }
  575. if (preg_match('/g$/i', $maxphp2)) {
  576. $maxphp2 = $maxphp2 * 1024 * 1024;
  577. }
  578. if (preg_match('/t$/i', $maxphp2)) {
  579. $maxphp2 = $maxphp2 * 1024 * 1024 * 1024;
  580. }
  581. // Now $max and $maxphp and $maxphp2 are in Kb
  582. $maxmin = $max;
  583. $maxphptoshow = $maxphptoshowparam = '';
  584. if ($maxphp > 0) {
  585. $maxmin = min($max, $maxphp);
  586. $maxphptoshow = $maxphp;
  587. $maxphptoshowparam = 'upload_max_filesize';
  588. }
  589. if ($maxphp2 > 0) {
  590. $maxmin = min($max, $maxphp2);
  591. if ($maxphp2 < $maxphp) {
  592. $maxphptoshow = $maxphp2;
  593. $maxphptoshowparam = 'post_max_size';
  594. }
  595. }
  596. $langs->load('other');
  597. $out .= ' ';
  598. $out .= info_admin($langs->trans("ThisLimitIsDefinedInSetup", $max, $maxphptoshow), 1);
  599. } else {
  600. $out .= ' ('.$langs->trans("UploadDisabled").')';
  601. }
  602. print $out;
  603. print '</div>';
  604. // Search available imports
  605. $filearray = dol_dir_list($conf->import->dir_temp, 'files', 0, '', '', 'name', SORT_DESC);
  606. if (count($filearray) > 0) {
  607. 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
  608. print '<table class="noborder centpercent" width="100%" cellpadding="4">';
  609. $dir = $conf->import->dir_temp;
  610. // Search available files to import
  611. $i = 0;
  612. foreach ($filearray as $key => $val) {
  613. $file = $val['name'];
  614. // readdir return value in ISO and we want UTF8 in memory
  615. if (!utf8_check($file)) {
  616. $file = utf8_encode($file);
  617. }
  618. if (preg_match('/^\./', $file)) {
  619. continue;
  620. }
  621. $modulepart = 'import';
  622. $urlsource = $_SERVER["PHP_SELF"].'?step='.$step.$param.'&filetoimport='.urlencode($filetoimport);
  623. $relativepath = $file;
  624. print '<tr class="oddeven">';
  625. print '<td>';
  626. print img_mime($file, '', 'pictofixedwidth');
  627. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=3'.$param.'" target="_blank" rel="noopener noreferrer">';
  628. print $file;
  629. print '</a>';
  630. print '</td>';
  631. // Affiche taille fichier
  632. print '<td style="text-align:right">'.dol_print_size(dol_filesize($dir.'/'.$file)).'</td>';
  633. // Affiche date fichier
  634. print '<td style="text-align:right">'.dol_print_date(dol_filemtime($dir.'/'.$file), 'dayhour').'</td>';
  635. // Del button
  636. print '<td style="text-align:right"><a href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&step=3'.$param.'&urlfile='.urlencode($relativepath);
  637. print '">'.img_delete().'</a></td>';
  638. // Action button
  639. print '<td style="text-align:right">';
  640. print '<a href="'.$_SERVER['PHP_SELF'].'?step=4'.$param.'&filetoimport='.urlencode($relativepath).'">'.img_picto($langs->trans("NewImport"), 'next', 'class="fa-15x"').'</a>';
  641. print '</td>';
  642. print '</tr>';
  643. }
  644. print '</table>';
  645. print '</div>';
  646. }
  647. print '</form>';
  648. }
  649. // STEP 4: Page to make matching between source file and database fields
  650. if ($step == 4 && $datatoimport) {
  651. //var_dump($_SESSION["dol_array_match_file_to_database_select"]);
  652. $serialized_array_match_file_to_database = isset($_SESSION["dol_array_match_file_to_database_select"]) ? $_SESSION["dol_array_match_file_to_database_select"] : '';
  653. $array_match_file_to_database = array();
  654. $fieldsarray = explode(',', $serialized_array_match_file_to_database);
  655. foreach ($fieldsarray as $elem) {
  656. $tabelem = explode('=', $elem, 2);
  657. $key = $tabelem[0];
  658. $val = (isset($tabelem[1]) ? $tabelem[1] : '');
  659. if ($key && $val) {
  660. $array_match_file_to_database[$key] = $val;
  661. }
  662. }
  663. $model = $format;
  664. $list = $objmodelimport->liste_modeles($db);
  665. // Create classe to use for import
  666. $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
  667. $file = "import_".$model.".modules.php";
  668. $classname = "Import".ucfirst($model);
  669. require_once $dir.$file;
  670. $obj = new $classname($db, $datatoimport);
  671. if ($model == 'csv') {
  672. $obj->separator = $separator_used;
  673. $obj->enclosure = $enclosure;
  674. }
  675. if ($model == 'xlsx') {
  676. if (!preg_match('/\.xlsx$/i', $filetoimport)) {
  677. $langs->load("errors");
  678. $param = '&datatoimport='.$datatoimport.'&format='.$format;
  679. setEventMessages($langs->trans("ErrorFileMustHaveFormat", $model), null, 'errors');
  680. header("Location: ".$_SERVER["PHP_SELF"].'?step=3'.$param.'&filetoimport='.urlencode($relativepath));
  681. exit;
  682. }
  683. }
  684. if (GETPOST('update')) {
  685. $array_match_file_to_database = array();
  686. }
  687. // Load source fields in input file
  688. $fieldssource = array();
  689. $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs);
  690. if ($result >= 0) {
  691. // Read first line
  692. $arrayrecord = $obj->import_read_record();
  693. // Put into array fieldssource starting with 1.
  694. $i = 1;
  695. foreach ($arrayrecord as $key => $val) {
  696. if ($val["type"] != -1) {
  697. $fieldssource[$i]['example1'] = dol_trunc($val['val'], 128);
  698. $i++;
  699. }
  700. }
  701. $obj->import_close_file();
  702. }
  703. // Load targets fields in database
  704. $fieldstarget = $objimport->array_import_fields[0];
  705. $minpos = min(count($fieldssource), count($fieldstarget));
  706. //var_dump($array_match_file_to_database);
  707. $initialloadofstep4 = false;
  708. if (empty($_SESSION['dol_array_match_file_to_database_select'])) {
  709. $initialloadofstep4 = true;
  710. }
  711. // Is it a first time in page (if yes, we must initialize array_match_file_to_database)
  712. if (count($array_match_file_to_database) == 0) {
  713. // This is first input in screen, we need to define
  714. // $array_match_file_to_database
  715. // $serialized_array_match_file_to_database
  716. // $_SESSION["dol_array_match_file_to_database"]
  717. $pos = 1;
  718. $num = count($fieldssource);
  719. while ($pos <= $num) {
  720. if ($num >= 1 && $pos <= $num) {
  721. $posbis = 1;
  722. foreach ($fieldstarget as $key => $val) {
  723. if ($posbis < $pos) {
  724. $posbis++;
  725. continue;
  726. }
  727. // We found the key of targets that is at position pos
  728. $array_match_file_to_database[$pos] = $key;
  729. if ($serialized_array_match_file_to_database) {
  730. $serialized_array_match_file_to_database .= ',';
  731. }
  732. $serialized_array_match_file_to_database .= ($pos.'='.$key);
  733. break;
  734. }
  735. }
  736. $pos++;
  737. }
  738. // Save the match array in session. We now will use the array in session.
  739. $_SESSION["dol_array_match_file_to_database_select"] = $serialized_array_match_file_to_database;
  740. }
  741. $array_match_database_to_file = array_flip($array_match_file_to_database);
  742. $fieldstarget_tmp = array();
  743. $arraykeysfieldtarget = array_keys($fieldstarget);
  744. $position = 0;
  745. foreach ($fieldstarget as $key => $label) {
  746. $isrequired = preg_match('/\*$/', $label);
  747. if (!empty($isrequired)) {
  748. $newlabel = substr($label, 0, -1);
  749. $fieldstarget_tmp[$key] = array("label"=>$newlabel,"required"=>true);
  750. } else {
  751. $fieldstarget_tmp[$key] = array("label"=>$label,"required"=>false);
  752. }
  753. if (!empty($array_match_database_to_file[$key])) {
  754. $fieldstarget_tmp[$key]["imported"] = true;
  755. $fieldstarget_tmp[$key]["position"] = $array_match_database_to_file[$key]-1;
  756. $keytoswap = $key;
  757. while (!empty($array_match_database_to_file[$keytoswap])) {
  758. if ($position+1 > $array_match_database_to_file[$keytoswap]) {
  759. $keytoswapwith = $array_match_database_to_file[$keytoswap]-1;
  760. $tmp = [$keytoswap=>$fieldstarget_tmp[$keytoswap]];
  761. unset($fieldstarget_tmp[$keytoswap]);
  762. $fieldstarget_tmp = arrayInsert($fieldstarget_tmp, $keytoswapwith, $tmp);
  763. $keytoswapwith = $arraykeysfieldtarget[$array_match_database_to_file[$keytoswap]-1];
  764. $tmp = $fieldstarget_tmp[$keytoswapwith];
  765. unset($fieldstarget_tmp[$keytoswapwith]);
  766. $fieldstarget_tmp[$keytoswapwith] = $tmp;
  767. $keytoswap = $keytoswapwith;
  768. } else {
  769. break;
  770. }
  771. }
  772. } else {
  773. $fieldstarget_tmp[$key]["imported"] = false;
  774. }
  775. $position++;
  776. }
  777. $fieldstarget = $fieldstarget_tmp;
  778. //print $serialized_array_match_file_to_database;
  779. //print $_SESSION["dol_array_match_file_to_database"];
  780. //print $_SESSION["dol_array_match_file_to_database_select"];
  781. //var_dump($array_match_file_to_database);exit;
  782. // Now $array_match_file_to_database contains fieldnb(1,2,3...)=>fielddatabase(key in $array_match_file_to_database)
  783. $param = '&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport);
  784. if ($excludefirstline) {
  785. $param .= '&excludefirstline='.urlencode($excludefirstline);
  786. }
  787. if ($endatlinenb) {
  788. $param .= '&endatlinenb='.urlencode($endatlinenb);
  789. }
  790. if ($separator) {
  791. $param .= '&separator='.urlencode($separator);
  792. }
  793. if ($enclosure) {
  794. $param .= '&enclosure='.urlencode($enclosure);
  795. }
  796. llxHeader('', $langs->trans("NewImport"), $help_url);
  797. $head = import_prepare_head($param, 4);
  798. print dol_get_fiche_head($head, 'step4', '', -2);
  799. print '<div class="underbanner clearboth"></div>';
  800. print '<div class="fichecenter">';
  801. print '<table class="centpercent border tableforfield">';
  802. // Module
  803. print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
  804. print '<td>';
  805. $titleofmodule = $objimport->array_import_module[0]['module']->getName();
  806. // Special cas for import common to module/services
  807. if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
  808. $titleofmodule = $langs->trans("ProductOrService");
  809. }
  810. print $titleofmodule;
  811. print '</td></tr>';
  812. // Lot de donnees a importer
  813. print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
  814. print '<td>';
  815. $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
  816. $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
  817. print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
  818. print $objimport->array_import_label[0];
  819. print '</td></tr>';
  820. print '</table>';
  821. print '</div>';
  822. print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
  823. print '<div class="underbanner clearboth"></div>';
  824. print '<div class="fichecenter">';
  825. print '<table width="100%" class="border tableforfield">';
  826. // Source file format
  827. print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
  828. print '<td>';
  829. $text = $objmodelimport->getDriverDescForKey($format);
  830. print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
  831. print '</td></tr>';
  832. // Separator and enclosure
  833. if ($model == 'csv') {
  834. print '<tr><td>'.$langs->trans("CsvOptions").'</td>';
  835. print '<td>';
  836. print '<form>';
  837. print '<input type="hidden" name="token" value="'.newToken().'">';
  838. print '<input type="hidden" value="'.$step.'" name="step">';
  839. print '<input type="hidden" value="'.$format.'" name="format">';
  840. print '<input type="hidden" value="'.$excludefirstline.'" name="excludefirstline">';
  841. print '<input type="hidden" value="'.$endatlinenb.'" name="endatlinenb">';
  842. print '<input type="hidden" value="'.$datatoimport.'" name="datatoimport">';
  843. print '<input type="hidden" value="'.$filetoimport.'" name="filetoimport">';
  844. print $langs->trans("Separator").' : ';
  845. print '<input type="text" size="1" name="separator" value="'.dol_escape_htmltag($separator).'"/>';
  846. print '&nbsp;&nbsp;&nbsp;&nbsp;'.$langs->trans("Enclosure").' : ';
  847. print '<input type="text" size="1" name="enclosure" value="'.dol_escape_htmltag($enclosure).'"/> ';
  848. print '<input name="update" type="submit" value="'.$langs->trans('Update').'" class="button small" />';
  849. print '</form>';
  850. print '</td></tr>';
  851. }
  852. // File to import
  853. print '<tr><td>'.$langs->trans("FileToImport").'</td>';
  854. print '<td>';
  855. $modulepart = 'import';
  856. $relativepath = GETPOST('filetoimport');
  857. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=4'.$param.'" target="_blank" rel="noopener noreferrer">';
  858. print img_mime($file, '', 'pictofixedwidth');
  859. print $filetoimport;
  860. print '</a>';
  861. print '</td></tr>';
  862. print '</table>';
  863. print '</div>';
  864. print dol_get_fiche_end();
  865. print '<br>'."\n";
  866. // List of source fields
  867. print '<!-- List of source fields -->'."\n";
  868. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  869. print '<input type="hidden" name="token" value="'.newToken().'">';
  870. print '<input type="hidden" name="action" value="select_model">';
  871. print '<input type="hidden" name="step" value="4">';
  872. print '<input type="hidden" name="format" value="'.$format.'">';
  873. print '<input type="hidden" name="datatoimport" value="'.$datatoimport.'">';
  874. print '<input type="hidden" name="filetoimport" value="'.$filetoimport.'">';
  875. print '<input type="hidden" name="excludefirstline" value="'.$excludefirstline.'">';
  876. print '<input type="hidden" name="endatlinenb" value="'.$endatlinenb.'">';
  877. print '<input type="hidden" name="separator" value="'.dol_escape_htmltag($separator).'">';
  878. print '<input type="hidden" name="enclosure" value="'.dol_escape_htmltag($enclosure).'">';
  879. print '<div class="marginbottomonly">';
  880. print '<span class="opacitymedium">';
  881. $s = $langs->trans("SelectImportFieldsSource", '{s1}');
  882. $s = str_replace('{s1}', img_picto('', 'grip_title', '', false, 0, 0, '', '', 0), $s);
  883. print $s;
  884. print '</span> ';
  885. $htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1, $user->id);
  886. print '<input type="submit" class="button small reposition" value="'.$langs->trans("Select").'">';
  887. print '</div>';
  888. print '</form>';
  889. // Title of array with fields
  890. 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
  891. print '<table class="noborder centpercent">';
  892. print '<tr class="liste_titre">';
  893. print '<td>'.$langs->trans("FieldsInSourceFile").'</td>';
  894. print '<td>'.$langs->trans("FieldsInTargetDatabase").'</td>';
  895. print '</tr>';
  896. //var_dump($array_match_file_to_database);
  897. print '<tr valign="top"><td width="50%" class="nopaddingleftimp">';
  898. $fieldsplaced = array();
  899. $valforsourcefieldnb = array();
  900. $listofkeys = array();
  901. foreach ($array_match_file_to_database as $key => $val) {
  902. $listofkeys[$key] = 1;
  903. }
  904. print "\n<!-- Box left container -->\n";
  905. print '<div id="left" class="connectedSortable">'."\n";
  906. // List of source fields
  907. $var = false;
  908. $lefti = 1;
  909. foreach ($fieldssource as $key => $val) {
  910. show_elem($fieldssource, $key, $val, $var); // key is field number in source file
  911. $listofkeys[$key] = 1;
  912. $fieldsplaced[$key] = 1;
  913. $valforsourcefieldnb[$lefti] = $key;
  914. $lefti++;
  915. if ($lefti > count($fieldstarget)) {
  916. break; // Other fields are in the not imported area
  917. }
  918. }
  919. //var_dump($valforsourcefieldnb);
  920. print "</div>\n";
  921. print "<!-- End box left container -->\n";
  922. print '</td><td width="50%" class="nopaddingrightimp">';
  923. // Set the list of all possible target fields in Dolibarr.
  924. $optionsnotused = "";
  925. $optionsall = array();
  926. foreach ($fieldstarget as $code => $line) {
  927. $text = '<option value="'.$code.'">';
  928. $text .= $langs->trans($line["label"]);
  929. if ($line["required"]) {
  930. $text .= "*";
  931. }
  932. $text .= '</option>';
  933. if (!$line["imported"]) {
  934. $optionsnotused .= $text;
  935. }
  936. $optionsall[$code] = array('label'=>$langs->trans($line["label"]), 'required'=>(empty($line["required"]) ? 0 : 1), 'position'=>$line['position']);
  937. }
  938. // $optionsall is an array of all possible fields. key=>array('label'=>..., 'xxx')
  939. $height = '32px'; //needs px for css height attribute below
  940. $i = 0;
  941. $mandatoryfieldshavesource = true;
  942. //var_dump($fieldstarget);
  943. //var_dump($optionsall);
  944. //exit;
  945. /*
  946. var_dump($_SESSION['dol_array_match_file_to_database']);
  947. var_dump($_SESSION['dol_array_match_file_to_database_select']);
  948. var_dump($optionsall);
  949. var_dump($fieldssource);
  950. var_dump($fieldstarget);
  951. */
  952. print '<table class="nobordernopadding centpercent tableimport">';
  953. foreach ($fieldssource as $code => $line) { // $fieldssource is an array code=column num, line=content on first line for column in source file.
  954. if ($i == $minpos) {
  955. break;
  956. }
  957. print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
  958. $entity = (!empty($objimport->array_import_entities[0][$code]) ? $objimport->array_import_entities[0][$code] : $objimport->array_import_icon[0]);
  959. $tablealias = preg_replace('/(\..*)$/i', '', $code);
  960. $tablename = $objimport->array_import_tables[0][$tablealias];
  961. $entityicon = !empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity; // $entityicon must string name of picto of the field like 'project', 'company', 'contact', 'modulename', ...
  962. $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', ...
  963. //print '<td class="nowraponall" style="font-weight: normal">=> '.img_object('', $entityicon).' '.$langs->trans($entitylang).'</td>';
  964. print '<td class="nowraponall" style="font-weight: normal">=> </td>';
  965. print '<td class="nowraponall" style="font-weight: normal">';
  966. $modetoautofillmapping = 'session'; // Use setup in session
  967. if ($initialloadofstep4) {
  968. $modetoautofillmapping = 'guess';
  969. }
  970. //var_dump($_SESSION['dol_array_match_file_to_database_select']);
  971. //var_dump($modetoautofillmapping);
  972. //var_dump($_SESSION['dol_array_match_file_to_database']);
  973. //var_dump($modetoautofillmapping);
  974. print '<select id="selectorderimport_'.($i+1).'" class="targetselectchange minwidth300" name="select_'.$line["label"].'">';
  975. if ($line["imported"]) {
  976. print '<option value="-1">&nbsp;</option>';
  977. } else {
  978. print '<option selected="" value="-1">&nbsp;</option>';
  979. }
  980. $j = 0;
  981. foreach ($optionsall as $tmpcode => $tmpval) { // Loop on each entry to add into each combo list.
  982. $label = $tmpval['required'] ? '<strong>' : '';
  983. $label .= $tmpval['label'];
  984. $label .= $tmpval['required'] ? '*</strong>' : '';
  985. // If we must guess how to fill the preselected value, and we can't because input value are not string
  986. if ($modetoautofillmapping == 'guess' && $j == 0 && is_numeric($tmpval)) {
  987. $modetoautofillmapping = 'orderoftargets';
  988. }
  989. print '<option value="'.$tmpcode.'"';
  990. if ($modetoautofillmapping == 'orderoftargets') {
  991. // The mode where we fill the preselected value of combo one by one in order of available targets fields in the declaration in descriptor file.
  992. if ($j == $i) {
  993. print ' selected';
  994. }
  995. } elseif ($modetoautofillmapping == 'guess') {
  996. // The mode where we try to guess which value to preselect from the name in first column of source file.
  997. $regs = array();
  998. if (preg_match('/^(.+)\((.+)\)$/', $line['example1'], $regs)) {
  999. $tmpstring1 = $regs[1];
  1000. $tmpstring2 = $regs[2];
  1001. } else {
  1002. $tmpstring1 = $line['example1'];
  1003. $tmpstring2 = '';
  1004. }
  1005. $tmpstring1 = str_replace('*', '', trim($tmpstring1));
  1006. $tmpstring2 = str_replace('*', '', trim($tmpstring2));
  1007. if ($tmpstring1 && ($tmpstring1 == $tmpcode || $tmpstring1 == $tmpval)) {
  1008. print ' selected';
  1009. // TODO Check that $tmpcode not already selected
  1010. } elseif ($tmpstring2 && ($tmpstring2 == $tmpcode || $tmpstring2 == $tmpval)) {
  1011. print ' selected';
  1012. // TODO Check that $tmpcode not already selected
  1013. }
  1014. } elseif ($modetoautofillmapping == 'session' && !empty($_SESSION['dol_array_match_file_to_database_select'])) {
  1015. $tmpselectioninsession = dolExplodeIntoArray($_SESSION['dol_array_match_file_to_database_select'], ',', '=');
  1016. //var_dump($code);
  1017. //var_dump($tmpselectioninsession);
  1018. //if ($tmpselectioninsession[$j] == $code) {
  1019. if ($tmpselectioninsession[($i+1)] == $tmpcode) {
  1020. print ' selected';
  1021. }
  1022. print ' data-debug="'.$tmpcode.'-'.$code.'-'.$j.'-'.$tmpselectioninsession[($i+1)].'"';
  1023. }
  1024. print ' data-html="'.dol_escape_htmltag($label).'"';
  1025. print '>';
  1026. print $label;
  1027. print '</options>';
  1028. $j++;
  1029. }
  1030. print '</select>';
  1031. print ajax_combobox('selectorderimport_'.($i+1));
  1032. print "</td>";
  1033. print '<td class="nowraponall" style="font-weight:normal; text-align:right">';
  1034. $filecolumn = ($i + 1);
  1035. // Source field info
  1036. $htmltext = '<b><u>'.$langs->trans("FieldSource").'</u></b><br>';
  1037. if ($filecolumn > count($fieldssource)) {
  1038. $htmltext .= $langs->trans("DataComeFromNoWhere").'<br>';
  1039. } else {
  1040. if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
  1041. $filecolumntoshow = num2Alpha($i);
  1042. $htmltext .= $langs->trans("DataComeFromFileFieldNb", $filecolumntoshow).'<br>';
  1043. } else {
  1044. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') {
  1045. $htmltext .= $langs->trans("DataComeFromIdFoundFromRef", $filecolumn, $langs->transnoentitiesnoconv($entitylang)).'<br>';
  1046. }
  1047. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') {
  1048. $htmltext .= $langs->trans("DataComeFromIdFoundFromCodeId", $filecolumn, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).'<br>';
  1049. }
  1050. }
  1051. }
  1052. // Source required
  1053. $example = !empty($objimport->array_import_examplevalues[0][$code])?$objimport->array_import_examplevalues[0][$code]:"";
  1054. // Example
  1055. if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
  1056. if ($example) {
  1057. $htmltext .= $langs->trans("SourceExample").': <b>'.$example.'</b><br>';
  1058. }
  1059. } else {
  1060. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') {
  1061. $htmltext .= $langs->trans("SourceExample").': <b>'.$langs->transnoentitiesnoconv("ExampleAnyRefFoundIntoElement", $entitylang).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'</b><br>';
  1062. } elseif ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') {
  1063. $htmltext .= $langs->trans("SourceExample").': <b>'.$langs->trans("ExampleAnyCodeOrIdFoundIntoDictionary", $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])).($example ? ' ('.$langs->transnoentitiesnoconv("Example").': '.$example.')' : '').'</b><br>';
  1064. } elseif ($example) {
  1065. $htmltext .= $langs->trans("SourceExample").': <b>'.$example.'</b><br>';
  1066. }
  1067. }
  1068. // Format control rule
  1069. if (!empty($objimport->array_import_regex[0][$code])) {
  1070. $htmltext .= $langs->trans("FormatControlRule").': <b>'.$objimport->array_import_regex[0][$code].'</b><br>';
  1071. }
  1072. $htmltext .= '<br>';
  1073. // Target field info
  1074. $htmltext .= '<b><u>'.$langs->trans("FieldTarget").'</u></b><br>';
  1075. //$htmltext .= $langs->trans("SourceRequired").': <b>'.yn($line["label"]).'</b><br>';
  1076. if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
  1077. $htmltext .= $langs->trans("DataIsInsertedInto").'<br>';
  1078. } else {
  1079. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref') {
  1080. $htmltext .= $langs->trans("DataIDSourceIsInsertedInto").'<br>';
  1081. }
  1082. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid') {
  1083. $htmltext .= $langs->trans("DataCodeIDSourceIsInsertedInto").'<br>';
  1084. }
  1085. }
  1086. $htmltext .= $langs->trans("FieldTitle").": <b>".$langs->trans($line["label"])."</b><br>";
  1087. $htmltext .= $langs->trans("Table")." -> ".$langs->trans("Field").': <b>'.$tablename." -> ".preg_replace('/^.*\./', '', $code)."</b><br>";
  1088. print $form->textwithpicto($more, $htmltext);
  1089. print '</tr>';
  1090. $i++;
  1091. }
  1092. print '</table>';
  1093. print '</td></tr>';
  1094. // List of not imported fields
  1095. /*
  1096. print '<tr class="liste_titre"><td colspan="2">'.$langs->trans("NotUsedFields").'</td></tr>';
  1097. print '<tr valign="top"><td width="50%">';
  1098. print "\n<!-- Box ignore container -->\n";
  1099. print '<div id="right" class="connectedSortable">'."\n";
  1100. $nbofnotimportedfields = 0;
  1101. foreach ($fieldstarget as $key => $val) {
  1102. if (!$fieldstarget[$key]['imported']) {
  1103. //
  1104. $nbofnotimportedfields++;
  1105. show_elem($fieldstarget, $key, '', $var, 'nostyle');
  1106. //print '> '.$lefti.'-'.$key;
  1107. $listofkeys[$key] = 1;
  1108. $lefti++;
  1109. }
  1110. }
  1111. // Print one more empty field
  1112. $newkey = getnewkey($fieldssource, $listofkeys);
  1113. show_elem($fieldssource, $newkey, '', $var, 'nostyle');
  1114. //print '> '.$lefti.'-'.$newkey;
  1115. $listofkeys[$newkey] = 1;
  1116. $nbofnotimportedfields++;
  1117. print "</div>\n";
  1118. print "<!-- End box ignore container -->\n";
  1119. print '</td>';
  1120. print '<td width="50%">';
  1121. $i = 0;
  1122. while ($i < $nbofnotimportedfields) {
  1123. // Print empty cells
  1124. show_elem('', '', 'none', $var, 'nostyle');
  1125. $i++;
  1126. }
  1127. print '</td></tr>';
  1128. */
  1129. print '</table>';
  1130. print '</div>';
  1131. if (!empty($conf->use_javascript_ajax)) {
  1132. print '<script type="text/javascript">'."\n";
  1133. print 'var previousselectedvalueimport = "0";'."\n";
  1134. print 'var previousselectedlabelimport = "0";'."\n";
  1135. print 'var arrayofselectedvalues = [];'."\n";
  1136. print '$(document).ready(function () {'."\n";
  1137. print 'setOptionsToDisabled();'."\n";
  1138. print 'saveSelection();'."\n";
  1139. print '$(".targetselectchange").focus(function(){'."\n";
  1140. print ' previousselectedvalueimport = $(this).val();'."\n";
  1141. print ' previousselectedlabelimport = $(this).children("option:selected").text();'."\n";
  1142. print ' console.log("previousselectedvalueimport="+previousselectedvalueimport)'."\n";
  1143. print '})'."\n";
  1144. // Function to set the disabled flag
  1145. // - We set all option to "enabled"
  1146. // - Then we scan all combo to get the value currently selected and save them into the array arrayofselectedvalues
  1147. // - Then we set to disabled all fields that are selected
  1148. print 'function setOptionsToDisabled() {'."\n";
  1149. print ' console.log("Remove the disabled flag everywhere");'."\n";
  1150. print ' $("select.targetselectchange").not($( this )).find(\'option\').prop("disabled", false);'."\n"; // Enable all options
  1151. print ' arrayofselectedvalues = [];'."\n";
  1152. print ' $("select.targetselectchange").each(function(){'."\n";
  1153. print ' id = $(this).attr(\'id\')'."\n";
  1154. print ' value = $(this).val()'."\n";
  1155. //print ' console.log("a selected value has been found for component "+id+" = "+value);'."\n";
  1156. print ' arrayofselectedvalues.push(value);'."\n";
  1157. print ' });'."\n";
  1158. print ' console.log("List of all selected values");'."\n";
  1159. print ' console.log(arrayofselectedvalues);'."\n";
  1160. print ' console.log("Set the option to disabled for every entry that is currently selected somewhere else (so into arrayofselectedvalues)");'."\n";
  1161. print ' $.each( arrayofselectedvalues, function( key, value ) {'."\n"; // Loop on each selected value
  1162. print ' if (value != -1) {'."\n";
  1163. //print ' console.log("Process key="+key+" value="+value+" to disable.");'."\n";
  1164. print ' $("select.targetselectchange").find(\'option[value="\'+value+\'"]:not(:selected)\').prop("disabled", true);'."\n"; // Set to disabled except if currently selected
  1165. print ' }'."\n";
  1166. print ' });'."\n";
  1167. print '};'."\n";
  1168. // Function to save the selection in database
  1169. print 'function saveSelection() {'."\n";
  1170. //print ' console.log(arrayofselectedvalues);'."\n";
  1171. print ' arrayselectedfields = [];'."\n";
  1172. print ' arrayselectedfields.push("0");'."\n";
  1173. print ' $.each( arrayofselectedvalues, function( key, value ) {'."\n";
  1174. print ' if (value != -1) {'."\n";
  1175. print ' arrayselectedfields.push(value);'."\n";
  1176. print ' } else {'."\n";
  1177. print ' arrayselectedfields.push(0);'."\n";
  1178. print ' }'."\n";
  1179. print ' });'."\n";
  1180. print " $.ajax({\n";
  1181. print " type: 'POST',\n";
  1182. print " dataType: 'json',\n";
  1183. print " url: '".dol_escape_js($_SERVER["PHP_SELF"])."?action=saveselectorder&token=".newToken()."',\n";
  1184. print " data: 'selectorder='+arrayselectedfields.toString(),\n";
  1185. print " success: function(){\n";
  1186. print " console.log('Select order saved');\n";
  1187. print " },\n";
  1188. print ' });'."\n";
  1189. print '};'."\n";
  1190. // If we make a change on a selectbox
  1191. print '$(".targetselectchange").change(function(){'."\n";
  1192. print ' setOptionsToDisabled();'."\n";
  1193. print ' if(previousselectedlabelimport != "" && previousselectedvalueimport != -1) {'."\n";
  1194. print ' let valuetochange = $(this).val(); '."\n";
  1195. print ' $(".boxtdunused").each(function(){'."\n";
  1196. print ' if ($(this).text().includes(valuetochange)){'."\n";
  1197. print ' arraychild = $(this)[0].childNodes'."\n";
  1198. print ' arraytexttomodify = arraychild[0].textContent.split(" ")'."\n";
  1199. print ' arraytexttomodify[1] = previousselectedvalueimport '."\n";
  1200. print ' textmodified = arraytexttomodify.join(" ") '."\n";
  1201. print ' arraychild[0].textContent = textmodified'."\n";
  1202. print ' arraychild[1].innerHTML = previousselectedlabelimport'."\n";
  1203. print ' }'."\n";
  1204. print ' })'."\n";
  1205. print ' }'."\n";
  1206. print ' $(this).blur()'."\n";
  1207. print ' saveSelection()'."\n";
  1208. print '});'."\n";
  1209. print '})'."\n";
  1210. print '</script>'."\n";
  1211. }
  1212. /*
  1213. * Action bar
  1214. */
  1215. print '<div class="tabsAction">';
  1216. if (count($array_match_file_to_database)) {
  1217. if ($mandatoryfieldshavesource) {
  1218. print '<a class="butAction saveorderselect" href="import.php?step=5'.$param.'&filetoimport='.urlencode($filetoimport).'">'.$langs->trans("NextStep").'</a>';
  1219. } else {
  1220. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("SomeMandatoryFieldHaveNoSource")).'">'.$langs->trans("NextStep").'</a>';
  1221. }
  1222. }
  1223. print '</div>';
  1224. // Area for profils import
  1225. if (count($array_match_file_to_database)) {
  1226. print '<br>'."\n";
  1227. print '<!-- Area to add new import profile -->'."\n";
  1228. print '<div class="marginbottomonly"><span class="opacitymedium">'.$langs->trans("SaveImportModel").'</span></div>';
  1229. print '<form class="nocellnopadd" action="'.$_SERVER["PHP_SELF"].'" method="post">';
  1230. print '<input type="hidden" name="token" value="'.newToken().'">';
  1231. print '<input type="hidden" name="action" value="add_import_model">';
  1232. print '<input type="hidden" name="step" value="'.$step.'">';
  1233. print '<input type="hidden" name="format" value="'.$format.'">';
  1234. print '<input type="hidden" name="datatoimport" value="'.$datatoimport.'">';
  1235. print '<input type="hidden" name="filetoimport" value="'.$filetoimport.'">';
  1236. print '<input type="hidden" name="hexa" value="'.$hexa.'">';
  1237. print '<input type="hidden" name="excludefirstline" value="'.$excludefirstline.'">';
  1238. print '<input type="hidden" name="endatlinenb" value="'.$endatlinenb.'">';
  1239. print '<input type="hidden" name="page_y" value="">';
  1240. print '<input type="hidden" value="'.dol_escape_htmltag($separator).'" name="separator">';
  1241. print '<input type="hidden" value="'.dol_escape_htmltag($enclosure).'" name="enclosure">';
  1242. 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
  1243. print '<table summary="selectofimportprofil" class="noborder centpercent">';
  1244. print '<tr class="liste_titre">';
  1245. print '<td>'.$langs->trans("ImportModelName").'</td>';
  1246. print '<td>'.$langs->trans("Visibility").'</td>';
  1247. print '<td></td>';
  1248. print '</tr>';
  1249. print '<tr class="oddeven">';
  1250. print '<td><input name="import_name" value=""></td>';
  1251. print '<td>';
  1252. $arrayvisibility = array('private'=>$langs->trans("Private"), 'all'=>$langs->trans("Everybody"));
  1253. print $form->selectarray('visibility', $arrayvisibility, 'private');
  1254. print '</td>';
  1255. print '<td class="right">';
  1256. print '<input type="submit" class="button small reposition" value="'.$langs->trans("SaveImportProfile").'">';
  1257. print '</td></tr>';
  1258. // List of existing import profils
  1259. $sql = "SELECT rowid, label, fk_user, entity";
  1260. $sql .= " FROM ".MAIN_DB_PREFIX."import_model";
  1261. $sql .= " WHERE type = '".$db->escape($datatoimport)."'";
  1262. if (empty($conf->global->EXPORTS_SHARE_MODELS)) { // EXPORTS_SHARE_MODELS means all templates are visible, whatever is owner.
  1263. $sql .= " AND fk_user IN (0, ".((int) $user->id).")";
  1264. }
  1265. $sql .= " ORDER BY rowid";
  1266. $resql = $db->query($sql);
  1267. if ($resql) {
  1268. $num = $db->num_rows($resql);
  1269. $tmpuser = new user($db);
  1270. $i = 0;
  1271. while ($i < $num) {
  1272. $obj = $db->fetch_object($resql);
  1273. print '<tr class="oddeven"><td>';
  1274. print $obj->label;
  1275. print '</td>';
  1276. print '<td>';
  1277. if (empty($obj->fk_user)) {
  1278. print $langs->trans("Everybody");
  1279. } else {
  1280. $tmpuser->fetch($obj->fk_user);
  1281. print $tmpuser->getNomUrl(1);
  1282. }
  1283. print '</td>';
  1284. print '<td class="right">';
  1285. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?step='.$step.$param.'&action=deleteprof&token='.newToken().'&id='.$obj->rowid.'&filetoimport='.urlencode($filetoimport).'">';
  1286. print img_delete();
  1287. print '</a>';
  1288. print '</tr>';
  1289. $i++;
  1290. }
  1291. } else {
  1292. dol_print_error($db);
  1293. }
  1294. print '</table>';
  1295. print '</div>';
  1296. print '</form>';
  1297. }
  1298. }
  1299. // STEP 5: Summary of choices and launch simulation
  1300. if ($step == 5 && $datatoimport) {
  1301. $max_execution_time_for_importexport = (empty($conf->global->IMPORT_MAX_EXECUTION_TIME) ? 300 : $conf->global->IMPORT_MAX_EXECUTION_TIME); // 5mn if not defined
  1302. $max_time = @ini_get("max_execution_time");
  1303. if ($max_time && $max_time < $max_execution_time_for_importexport) {
  1304. 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.");
  1305. @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
  1306. }
  1307. $model = $format;
  1308. $list = $objmodelimport->liste_modeles($db);
  1309. // Create classe to use for import
  1310. $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
  1311. $file = "import_".$model.".modules.php";
  1312. $classname = "Import".ucfirst($model);
  1313. require_once $dir.$file;
  1314. $obj = new $classname($db, $datatoimport);
  1315. if ($model == 'csv') {
  1316. $obj->separator = $separator_used;
  1317. $obj->enclosure = $enclosure;
  1318. }
  1319. // Load source fields in input file
  1320. $fieldssource = array();
  1321. $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs);
  1322. if ($result >= 0) {
  1323. // Read first line
  1324. $arrayrecord = $obj->import_read_record();
  1325. // Put into array fieldssource starting with 1.
  1326. $i = 1;
  1327. foreach ($arrayrecord as $key => $val) {
  1328. $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24);
  1329. $i++;
  1330. }
  1331. $obj->import_close_file();
  1332. }
  1333. $nboflines = $obj->import_get_nb_of_lines($conf->import->dir_temp.'/'.$filetoimport);
  1334. $param = '&leftmenu=import&format='.urlencode($format).'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport).'&nboflines='.urlencode($nboflines).'&separator='.urlencode($separator).'&enclosure='.urlencode($enclosure);
  1335. $param2 = $param; // $param2 = $param without excludefirstline and endatlinenb
  1336. if ($excludefirstline) {
  1337. $param .= '&excludefirstline='.urlencode($excludefirstline);
  1338. }
  1339. if ($endatlinenb) {
  1340. $param .= '&endatlinenb='.urlencode($endatlinenb);
  1341. }
  1342. if (!empty($updatekeys)) {
  1343. $param .= '&updatekeys[]='.implode('&updatekeys[]=', $updatekeys);
  1344. }
  1345. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  1346. $head = import_prepare_head($param, 5);
  1347. print '<form action="'.$_SERVER["PHP_SELF"].'?'.$param2.'" method="POST">';
  1348. print '<input type="hidden" name="token" value="'.newToken().'">';
  1349. print '<input type="hidden" name="step" value="5">'; // step 5
  1350. print '<input type="hidden" name="action" value="launchsimu">'; // step 5
  1351. print dol_get_fiche_head($head, 'step5', '', -2);
  1352. print '<div class="underbanner clearboth"></div>';
  1353. print '<div class="fichecenter">';
  1354. print '<table width="100%" class="border tableforfield">';
  1355. // Module
  1356. print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
  1357. print '<td>';
  1358. $titleofmodule = $objimport->array_import_module[0]['module']->getName();
  1359. // Special cas for import common to module/services
  1360. if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
  1361. $titleofmodule = $langs->trans("ProductOrService");
  1362. }
  1363. print $titleofmodule;
  1364. print '</td></tr>';
  1365. // Lot de donnees a importer
  1366. print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
  1367. print '<td>';
  1368. $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
  1369. $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
  1370. print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
  1371. print $objimport->array_import_label[0];
  1372. print '</td></tr>';
  1373. print '</table>';
  1374. print '</div>';
  1375. print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
  1376. print '<div class="underbanner clearboth"></div>';
  1377. print '<div class="fichecenter">';
  1378. print '<table width="100%" class="border tableforfield">';
  1379. // Source file format
  1380. print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
  1381. print '<td>';
  1382. $text = $objmodelimport->getDriverDescForKey($format);
  1383. print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
  1384. print '</td></tr>';
  1385. // Separator and enclosure
  1386. if ($model == 'csv') {
  1387. print '<tr><td>'.$langs->trans("CsvOptions").'</td>';
  1388. print '<td>';
  1389. print $langs->trans("Separator").' : '.dol_escape_htmltag($separator);
  1390. print '&nbsp;&nbsp;&nbsp;&nbsp;'.$langs->trans("Enclosure").' : '.dol_escape_htmltag($enclosure);
  1391. print '</td></tr>';
  1392. }
  1393. // File to import
  1394. print '<tr><td>'.$langs->trans("FileToImport").'</td>';
  1395. print '<td>';
  1396. $modulepart = 'import';
  1397. $relativepath = GETPOST('filetoimport');
  1398. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=4'.$param.'" target="_blank" rel="noopener noreferrer">';
  1399. print img_mime($file, '', 'pictofixedwidth');
  1400. print $filetoimport;
  1401. print '</a>';
  1402. print '</td></tr>';
  1403. // Total lines in source file
  1404. print '<tr><td>';
  1405. print $langs->trans("NbOfSourceLines");
  1406. print '</td><td>';
  1407. print $nboflines;
  1408. print '</td></tr>';
  1409. // Range of lines to import
  1410. print '<tr><td>';
  1411. print $langs->trans("ImportFromToLine");
  1412. print '</td><td>';
  1413. if ($action == 'launchsimu') {
  1414. print '<input type="number" class="maxwidth50 right" name="excludefirstlinebis" disabled="disabled" value="'.$excludefirstline.'">';
  1415. print '<input type="hidden" name="excludefirstline" value="'.$excludefirstline.'">';
  1416. } else {
  1417. print '<input type="number" class="maxwidth50 right" name="excludefirstline" value="'.$excludefirstline.'">';
  1418. print $form->textwithpicto("", $langs->trans("SetThisValueTo2ToExcludeFirstLine"));
  1419. }
  1420. print ' - ';
  1421. if ($action == 'launchsimu') {
  1422. print '<input type="text" class="maxwidth50" name="endatlinenbbis" disabled="disabled" value="'.$endatlinenb.'">';
  1423. print '<input type="hidden" name="endatlinenb" value="'.$endatlinenb.'">';
  1424. } else {
  1425. print '<input type="text" class="maxwidth50" name="endatlinenb" value="'.$endatlinenb.'">';
  1426. print $form->textwithpicto("", $langs->trans("KeepEmptyToGoToEndOfFile"));
  1427. }
  1428. if ($action == 'launchsimu') {
  1429. print ' &nbsp; <a href="'.$_SERVER["PHP_SELF"].'?step=5'.$param.'">'.$langs->trans("Modify").'</a>';
  1430. }
  1431. if ($excludefirstline == 2) {
  1432. print $form->textwithpicto("", $langs->trans("WarningFirstImportedLine", $excludefirstline), 1, 'warning', "warningexcludefirstline");
  1433. print '<script>
  1434. $( document ).ready(function() {
  1435. $("input[name=\'excludefirstline\']").on("change",function(){
  1436. if($(this).val() <= 1){
  1437. $(".warningexcludefirstline").hide();
  1438. }else{
  1439. $(".warningexcludefirstline").show();
  1440. }
  1441. })
  1442. });
  1443. </script>';
  1444. }
  1445. print '</td></tr>';
  1446. // Keys for data UPDATE (not INSERT of new data)
  1447. print '<tr><td>';
  1448. print $langs->trans("KeysToUseForUpdates");
  1449. print '</td><td>';
  1450. if ($action == 'launchsimu') {
  1451. if (count($updatekeys)) {
  1452. print $form->multiselectarray('updatekeysbis', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '80%', 'disabled');
  1453. } else {
  1454. print '<span class="opacitymedium">'.$langs->trans("NoUpdateAttempt").'</span> &nbsp; -';
  1455. }
  1456. foreach ($updatekeys as $val) {
  1457. print '<input type="hidden" name="updatekeys[]" value="'.$val.'">';
  1458. }
  1459. print ' &nbsp; <a href="'.$_SERVER["PHP_SELF"].'?step=5'.$param.'">'.$langs->trans("Modify").'</a>';
  1460. } else {
  1461. if (is_array($objimport->array_import_updatekeys[0]) && count($objimport->array_import_updatekeys[0])) { //TODO dropdown UL is created inside nested SPANS
  1462. print $form->multiselectarray('updatekeys', $objimport->array_import_updatekeys[0], $updatekeys, 0, 0, '', 1, '80%');
  1463. print $form->textwithpicto("", $langs->trans("SelectPrimaryColumnsForUpdateAttempt"));
  1464. } else {
  1465. print '<span class="opacitymedium">'.$langs->trans("UpdateNotYetSupportedForThisImport").'</span>';
  1466. }
  1467. }
  1468. /*echo '<pre>';
  1469. print_r($objimport->array_import_updatekeys);
  1470. echo '</pre>';*/
  1471. print '</td></tr>';
  1472. print '</table>';
  1473. print '</div>';
  1474. print load_fiche_titre($langs->trans("InformationOnTargetTables"), '', 'file-import');
  1475. print '<div class="underbanner clearboth"></div>';
  1476. print '<div class="fichecenter">';
  1477. print '<table width="100%" class="border tableforfield">';
  1478. // Tables imported
  1479. print '<tr><td class="titlefieldcreate">';
  1480. print $langs->trans("TablesTarget");
  1481. print '</td><td>';
  1482. $listtables = array();
  1483. $sort_array_match_file_to_database = $array_match_file_to_database;
  1484. foreach ($array_match_file_to_database as $code => $label) {
  1485. //var_dump($fieldssource);
  1486. if ($code > count($fieldssource)) {
  1487. continue;
  1488. }
  1489. //print $code.'-'.$label;
  1490. $alias = preg_replace('/(\..*)$/i', '', $label);
  1491. $listtables[$alias] = $objimport->array_import_tables[0][$alias];
  1492. }
  1493. if (count($listtables)) {
  1494. $newval = '';
  1495. //ksort($listtables);
  1496. foreach ($listtables as $val) {
  1497. if ($newval) {
  1498. print ', ';
  1499. }
  1500. $newval = $val;
  1501. // Link to Dolibarr wiki pages
  1502. /*$helppagename='EN:Table_'.$newval;
  1503. if ($helppagename && empty($conf->global->MAIN_HELP_DISABLELINK))
  1504. {
  1505. // Get helpbaseurl, helppage and mode from helppagename and langs
  1506. $arrayres=getHelpParamFor($helppagename,$langs);
  1507. $helpbaseurl=$arrayres['helpbaseurl'];
  1508. $helppage=$arrayres['helppage'];
  1509. $mode=$arrayres['mode'];
  1510. $newval.=' <a href="'.sprintf($helpbaseurl,$helppage).'">'.img_picto($langs->trans($mode == 'wiki' ? 'GoToWikiHelpPage': 'GoToHelpPage'),DOL_URL_ROOT.'/theme/common/helpdoc.png','',1).'</a>';
  1511. }*/
  1512. print $newval;
  1513. }
  1514. } else {
  1515. print $langs->trans("Error");
  1516. }
  1517. print '</td></tr>';
  1518. // Fields imported
  1519. print '<tr><td>';
  1520. print $langs->trans("FieldsTarget").'</td><td>';
  1521. $listfields = array();
  1522. $i = 0;
  1523. //print 'fieldsource='.$fieldssource;
  1524. $sort_array_match_file_to_database = $array_match_file_to_database;
  1525. ksort($sort_array_match_file_to_database);
  1526. //var_dump($sort_array_match_file_to_database);
  1527. foreach ($sort_array_match_file_to_database as $code => $label) {
  1528. $i++;
  1529. //var_dump($fieldssource);
  1530. if ($code > count($fieldssource)) {
  1531. continue;
  1532. }
  1533. //print $code.'-'.$label;
  1534. $alias = preg_replace('/(\..*)$/i', '', $label);
  1535. $listfields[$i] = $langs->trans("Field").' '.$code.'->'.$label;
  1536. }
  1537. print count($listfields) ? (join(', ', $listfields)) : $langs->trans("Error");
  1538. print '</td></tr>';
  1539. print '</table>';
  1540. print '</div>';
  1541. print dol_get_fiche_end();
  1542. if ($action != 'launchsimu') {
  1543. // Show import id
  1544. print '<br><span class="opacitymedium">';
  1545. print $langs->trans("NowClickToTestTheImport", $langs->transnoentitiesnoconv("RunSimulateImportFile")).'</span><br>';
  1546. print '<br>';
  1547. // Actions
  1548. print '<div class="center">';
  1549. if ($user->rights->import->run) {
  1550. print '<input type="submit" class="butAction" value="'.$langs->trans("RunSimulateImportFile").'">';
  1551. } else {
  1552. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("RunSimulateImportFile").'</a>';
  1553. }
  1554. print '</div>';
  1555. } else {
  1556. // Launch import
  1557. $arrayoferrors = array();
  1558. $arrayofwarnings = array();
  1559. $maxnboferrors = empty($conf->global->IMPORT_MAX_NB_OF_ERRORS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_ERRORS;
  1560. $maxnbofwarnings = empty($conf->global->IMPORT_MAX_NB_OF_WARNINGS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_WARNINGS;
  1561. $nboferrors = 0;
  1562. $nbofwarnings = 0;
  1563. $importid = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
  1564. //var_dump($array_match_file_to_database);
  1565. $db->begin();
  1566. // Open input file
  1567. $nbok = 0;
  1568. $pathfile = $conf->import->dir_temp.'/'.$filetoimport;
  1569. $result = $obj->import_open_file($pathfile, $langs);
  1570. if ($result > 0) {
  1571. global $tablewithentity_cache;
  1572. $tablewithentity_cache = array();
  1573. $sourcelinenb = 0; $endoffile = 0;
  1574. // Loop on each input file record
  1575. while (($sourcelinenb < $nboflines) && !$endoffile) {
  1576. $sourcelinenb++;
  1577. // Read line and store it into $arrayrecord
  1578. //dol_syslog("line ".$sourcelinenb.' - '.$nboflines.' - '.$excludefirstline.' - '.$endatlinenb);
  1579. $arrayrecord = $obj->import_read_record();
  1580. if ($arrayrecord === false) {
  1581. $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');
  1582. $endoffile++;
  1583. continue;
  1584. }
  1585. if ($excludefirstline && ($sourcelinenb < $excludefirstline)) {
  1586. continue;
  1587. }
  1588. if ($endatlinenb && ($sourcelinenb > $endatlinenb)) {
  1589. break;
  1590. }
  1591. // Run import
  1592. $result = $obj->import_insert($arrayrecord, $array_match_file_to_database, $objimport, count($fieldssource), $importid, $updatekeys);
  1593. if (count($obj->errors)) {
  1594. $arrayoferrors[$sourcelinenb] = $obj->errors;
  1595. }
  1596. if (count($obj->warnings)) {
  1597. $arrayofwarnings[$sourcelinenb] = $obj->warnings;
  1598. }
  1599. if (!count($obj->errors) && !count($obj->warnings)) {
  1600. $nbok++;
  1601. }
  1602. }
  1603. // Close file
  1604. $obj->import_close_file();
  1605. } else {
  1606. print $langs->trans("ErrorFailedToOpenFile", $pathfile);
  1607. }
  1608. $error = 0;
  1609. // Run the sql after import if defined
  1610. //var_dump($objimport->array_import_run_sql_after[0]);
  1611. if (!empty($objimport->array_import_run_sql_after[0]) && is_array($objimport->array_import_run_sql_after[0])) {
  1612. $i = 0;
  1613. foreach ($objimport->array_import_run_sql_after[0] as $sqlafterimport) {
  1614. $i++;
  1615. $resqlafterimport = $db->query($sqlafterimport);
  1616. if (!$resqlafterimport) {
  1617. $arrayoferrors['none'][] = array('lib'=>$langs->trans("Error running final request: ".$sqlafterimport));
  1618. $error++;
  1619. }
  1620. }
  1621. }
  1622. $db->rollback(); // We force rollback because this was just a simulation.
  1623. // Show OK
  1624. if (!count($arrayoferrors) && !count($arrayofwarnings)) {
  1625. print '<div class="center">'.img_picto($langs->trans("OK"), 'tick').' <b>'.$langs->trans("NoError").'</b></div><br><br>';
  1626. print '<div class="ok">';
  1627. print $langs->trans("NbInsert", empty($obj->nbinsert) ? 0 : $obj->nbinsert).'<br>';
  1628. print $langs->trans("NbUpdate", empty($obj->nbupdate) ? 0 : $obj->nbupdate).'<br>';
  1629. print '</div>';
  1630. print '<br>';
  1631. } else {
  1632. print '<br>';
  1633. print '<div class="info">';
  1634. print $langs->trans("NbOfLinesOK", $nbok).'<br>';
  1635. print '</div>';
  1636. print '<br>';
  1637. }
  1638. // Show Errors
  1639. //var_dump($arrayoferrors);
  1640. if (count($arrayoferrors)) {
  1641. print img_error().' <b>'.$langs->trans("ErrorsOnXLines", count($arrayoferrors)).'</b><br>';
  1642. print '<table width="100%" class="border"><tr><td>';
  1643. foreach ($arrayoferrors as $key => $val) {
  1644. $nboferrors++;
  1645. if ($nboferrors > $maxnboferrors) {
  1646. print $langs->trans("TooMuchErrors", (count($arrayoferrors) - $nboferrors))."<br>";
  1647. break;
  1648. }
  1649. print '* '.$langs->trans("Line").' '.$key.'<br>';
  1650. foreach ($val as $i => $err) {
  1651. print ' &nbsp; &nbsp; > '.$err['lib'].'<br>';
  1652. }
  1653. }
  1654. print '</td></tr></table>';
  1655. print '<br>';
  1656. }
  1657. // Show Warnings
  1658. //var_dump($arrayoferrors);
  1659. if (count($arrayofwarnings)) {
  1660. print img_warning().' <b>'.$langs->trans("WarningsOnXLines", count($arrayofwarnings)).'</b><br>';
  1661. print '<table width="100%" class="border"><tr><td>';
  1662. foreach ($arrayofwarnings as $key => $val) {
  1663. $nbofwarnings++;
  1664. if ($nbofwarnings > $maxnbofwarnings) {
  1665. print $langs->trans("TooMuchWarnings", (count($arrayofwarnings) - $nbofwarnings))."<br>";
  1666. break;
  1667. }
  1668. print ' * '.$langs->trans("Line").' '.$key.'<br>';
  1669. foreach ($val as $i => $err) {
  1670. print ' &nbsp; &nbsp; > '.$err['lib'].'<br>';
  1671. }
  1672. }
  1673. print '</td></tr></table>';
  1674. print '<br>';
  1675. }
  1676. // Show import id
  1677. $importid = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
  1678. print '<div class="center">';
  1679. print '<span class="opacitymedium">'.$langs->trans("NowClickToRunTheImport", $langs->transnoentitiesnoconv("RunImportFile")).'</span><br>';
  1680. if (empty($nboferrors)) {
  1681. print $langs->trans("DataLoadedWithId", $importid).'<br>';
  1682. }
  1683. print '</div>';
  1684. print '<br>';
  1685. // Actions
  1686. print '<div class="center">';
  1687. if ($user->rights->import->run) {
  1688. if (empty($nboferrors)) {
  1689. print '<a class="butAction" href="'.DOL_URL_ROOT.'/imports/import.php?leftmenu=import&step=6&importid='.$importid.$param.'">'.$langs->trans("RunImportFile").'</a>';
  1690. } else {
  1691. //print '<input type="submit" class="butAction" value="'.dol_escape_htmltag($langs->trans("RunSimulateImportFile")).'">';
  1692. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("CorrectErrorBeforeRunningImport")).'">'.$langs->trans("RunImportFile").'</a>';
  1693. }
  1694. } else {
  1695. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("RunSimulateImportFile").'</a>';
  1696. print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("RunImportFile").'</a>';
  1697. }
  1698. print '</div>';
  1699. }
  1700. print '</form>';
  1701. }
  1702. // STEP 6: Real import
  1703. if ($step == 6 && $datatoimport) {
  1704. $max_execution_time_for_importexport = (empty($conf->global->IMPORT_MAX_EXECUTION_TIME) ? 300 : $conf->global->IMPORT_MAX_EXECUTION_TIME); // 5mn if not defined
  1705. $max_time = @ini_get("max_execution_time");
  1706. if ($max_time && $max_time < $max_execution_time_for_importexport) {
  1707. 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.");
  1708. @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
  1709. }
  1710. $model = $format;
  1711. $list = $objmodelimport->liste_modeles($db);
  1712. $importid = GETPOST("importid", 'alphanohtml');
  1713. // Create classe to use for import
  1714. $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
  1715. $file = "import_".$model.".modules.php";
  1716. $classname = "Import".ucfirst($model);
  1717. require_once $dir.$file;
  1718. $obj = new $classname($db, $datatoimport);
  1719. if ($model == 'csv') {
  1720. $obj->separator = $separator_used;
  1721. $obj->enclosure = $enclosure;
  1722. }
  1723. // Load source fields in input file
  1724. $fieldssource = array();
  1725. $result = $obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport, $langs);
  1726. if ($result >= 0) {
  1727. // Read first line
  1728. $arrayrecord = $obj->import_read_record();
  1729. // Put into array fieldssource starting with 1.
  1730. $i = 1;
  1731. foreach ($arrayrecord as $key => $val) {
  1732. $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24);
  1733. $i++;
  1734. }
  1735. $obj->import_close_file();
  1736. }
  1737. $nboflines = (!empty($_GET["nboflines"]) ? $_GET["nboflines"] : dol_count_nb_of_line($conf->import->dir_temp.'/'.$filetoimport));
  1738. $param = '&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport).'&nboflines='.urlencode($nboflines);
  1739. if ($excludefirstline) {
  1740. $param .= '&excludefirstline='.urlencode($excludefirstline);
  1741. }
  1742. if ($endatlinenb) {
  1743. $param .= '&endatlinenb='.urlencode($endatlinenb);
  1744. }
  1745. if ($separator) {
  1746. $param .= '&separator='.urlencode($separator);
  1747. }
  1748. if ($enclosure) {
  1749. $param .= '&enclosure='.urlencode($enclosure);
  1750. }
  1751. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  1752. $head = import_prepare_head($param, 6);
  1753. print dol_get_fiche_head($head, 'step6', '', -1);
  1754. print '<div class="underbanner clearboth"></div>';
  1755. print '<div class="fichecenter">';
  1756. print '<table width="100%" class="border">';
  1757. // Module
  1758. print '<tr><td class="titlefieldcreate">'.$langs->trans("Module").'</td>';
  1759. print '<td>';
  1760. $titleofmodule = $objimport->array_import_module[0]['module']->getName();
  1761. // Special cas for import common to module/services
  1762. if (in_array($objimport->array_import_code[0], array('produit_supplierprices', 'produit_multiprice', 'produit_languages'))) {
  1763. $titleofmodule = $langs->trans("ProductOrService");
  1764. }
  1765. print $titleofmodule;
  1766. print '</td></tr>';
  1767. // Lot de donnees a importer
  1768. print '<tr><td>'.$langs->trans("DatasetToImport").'</td>';
  1769. print '<td>';
  1770. $entity = preg_replace('/:.*$/', '', $objimport->array_import_icon[0]);
  1771. $entityicon = strtolower(!empty($entitytoicon[$entity]) ? $entitytoicon[$entity] : $entity);
  1772. print img_object($objimport->array_import_module[0]['module']->getName(), $entityicon).' ';
  1773. print $objimport->array_import_label[0];
  1774. print '</td></tr>';
  1775. print '</table>';
  1776. print '</div>';
  1777. print load_fiche_titre($langs->trans("InformationOnSourceFile"), '', 'file-export');
  1778. print '<div class="underbanner clearboth"></div>';
  1779. print '<div class="fichecenter">';
  1780. print '<table width="100%" class="border">';
  1781. // Source file format
  1782. print '<tr><td class="titlefieldcreate">'.$langs->trans("SourceFileFormat").'</td>';
  1783. print '<td>';
  1784. $text = $objmodelimport->getDriverDescForKey($format);
  1785. print $form->textwithpicto($objmodelimport->getDriverLabelForKey($format), $text);
  1786. print '</td></tr>';
  1787. // Separator and enclosure
  1788. if ($model == 'csv') {
  1789. print '<tr><td>'.$langs->trans("CsvOptions").'</td>';
  1790. print '<td>';
  1791. print $langs->trans("Separator").' : ';
  1792. print htmlentities($separator);
  1793. print '&nbsp;&nbsp;&nbsp;&nbsp;'.$langs->trans("Enclosure").' : ';
  1794. print htmlentities($enclosure);
  1795. print '</td></tr>';
  1796. }
  1797. // File to import
  1798. print '<tr><td>'.$langs->trans("FileToImport").'</td>';
  1799. print '<td>';
  1800. $modulepart = 'import';
  1801. $relativepath = GETPOST('filetoimport');
  1802. print '<a data-ajax="false" href="'.DOL_URL_ROOT.'/document.php?modulepart='.$modulepart.'&file='.urlencode($relativepath).'&step=4'.$param.'" target="_blank" rel="noopener noreferrer">';
  1803. print img_mime($file, '', 'pictofixedwidth');
  1804. print $filetoimport;
  1805. print '</a>';
  1806. print '</td></tr>';
  1807. // Nb of fields
  1808. print '<tr><td>';
  1809. print $langs->trans("NbOfSourceLines");
  1810. print '</td><td>';
  1811. print $nboflines;
  1812. print '</td></tr>';
  1813. // Do not import first lines
  1814. print '<tr><td>';
  1815. print $langs->trans("ImportFromLine");
  1816. print '</td><td>';
  1817. print '<input type="text" size="4" name="excludefirstline" disabled="disabled" value="'.$excludefirstline.'">';
  1818. print '</td></tr>';
  1819. // Do not import end lines
  1820. print '<tr><td>';
  1821. print $langs->trans("EndAtLineNb");
  1822. print '</td><td>';
  1823. print '<input type="text" size="4" name="endatlinenb" disabled="disabled" value="'.$endatlinenb.'">';
  1824. print '</td></tr>';
  1825. print '</table>';
  1826. print '</div>';
  1827. print '<br>';
  1828. print '<b>'.$langs->trans("InformationOnTargetTables").'</b>';
  1829. print '<div class="underbanner clearboth"></div>';
  1830. print '<div class="fichecenter">';
  1831. print '<table class="border centpercent">';
  1832. // Tables imported
  1833. print '<tr><td width="25%">';
  1834. print $langs->trans("TablesTarget");
  1835. print '</td><td>';
  1836. $listtables = array();
  1837. foreach ($array_match_file_to_database as $code => $label) {
  1838. //var_dump($fieldssource);
  1839. if ($code > count($fieldssource)) {
  1840. continue;
  1841. }
  1842. //print $code.'-'.$label;
  1843. $alias = preg_replace('/(\..*)$/i', '', $label);
  1844. $listtables[$alias] = $objimport->array_import_tables[0][$alias];
  1845. }
  1846. if (count($listtables)) {
  1847. $newval = '';
  1848. foreach ($listtables as $val) {
  1849. if ($newval) {
  1850. print ', ';
  1851. }
  1852. $newval = $val;
  1853. // Link to Dolibarr wiki pages
  1854. /*$helppagename='EN:Table_'.$newval;
  1855. if ($helppagename && empty($conf->global->MAIN_HELP_DISABLELINK))
  1856. {
  1857. // Get helpbaseurl, helppage and mode from helppagename and langs
  1858. $arrayres=getHelpParamFor($helppagename,$langs);
  1859. $helpbaseurl=$arrayres['helpbaseurl'];
  1860. $helppage=$arrayres['helppage'];
  1861. $mode=$arrayres['mode'];
  1862. $newval.=' <a href="'.sprintf($helpbaseurl,$helppage).'">'.img_picto($langs->trans($mode == 'wiki' ? 'GoToWikiHelpPage': 'GoToHelpPage'),DOL_URL_ROOT.'/theme/common/helpdoc.png','',1).'</a>';
  1863. }*/
  1864. print $newval;
  1865. }
  1866. } else {
  1867. print $langs->trans("Error");
  1868. }
  1869. print '</td></tr>';
  1870. // Fields imported
  1871. print '<tr><td>';
  1872. print $langs->trans("FieldsTarget").'</td><td>';
  1873. $listfields = array();
  1874. $i = 0;
  1875. $sort_array_match_file_to_database = $array_match_file_to_database;
  1876. ksort($sort_array_match_file_to_database);
  1877. //var_dump($sort_array_match_file_to_database);
  1878. foreach ($sort_array_match_file_to_database as $code => $label) {
  1879. $i++;
  1880. //var_dump($fieldssource);
  1881. if ($code > count($fieldssource)) {
  1882. continue;
  1883. }
  1884. //print $code.'-'.$label;
  1885. $alias = preg_replace('/(\..*)$/i', '', $label);
  1886. $listfields[$i] = $langs->trans("Field").' '.$code.'->'.$label;
  1887. }
  1888. print count($listfields) ? (join(', ', $listfields)) : $langs->trans("Error");
  1889. print '</td></tr>';
  1890. print '</table>';
  1891. print '</div>';
  1892. // Launch import
  1893. $arrayoferrors = array();
  1894. $arrayofwarnings = array();
  1895. $maxnboferrors = empty($conf->global->IMPORT_MAX_NB_OF_ERRORS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_ERRORS;
  1896. $maxnbofwarnings = empty($conf->global->IMPORT_MAX_NB_OF_WARNINGS) ? 50 : $conf->global->IMPORT_MAX_NB_OF_WARNINGS;
  1897. $nboferrors = 0;
  1898. $nbofwarnings = 0;
  1899. $importid = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
  1900. //var_dump($array_match_file_to_database);
  1901. $db->begin();
  1902. // Open input file
  1903. $nbok = 0;
  1904. $pathfile = $conf->import->dir_temp.'/'.$filetoimport;
  1905. $result = $obj->import_open_file($pathfile, $langs);
  1906. if ($result > 0) {
  1907. global $tablewithentity_cache;
  1908. $tablewithentity_cache = array();
  1909. $sourcelinenb = 0; $endoffile = 0;
  1910. while ($sourcelinenb < $nboflines && !$endoffile) {
  1911. $sourcelinenb++;
  1912. $arrayrecord = $obj->import_read_record();
  1913. if ($arrayrecord === false) {
  1914. $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');
  1915. $endoffile++;
  1916. continue;
  1917. }
  1918. if ($excludefirstline && ($sourcelinenb < $excludefirstline)) {
  1919. continue;
  1920. }
  1921. if ($endatlinenb && ($sourcelinenb > $endatlinenb)) {
  1922. break;
  1923. }
  1924. // Run import
  1925. $result = $obj->import_insert($arrayrecord, $array_match_file_to_database, $objimport, count($fieldssource), $importid, $updatekeys);
  1926. if (count($obj->errors)) {
  1927. $arrayoferrors[$sourcelinenb] = $obj->errors;
  1928. }
  1929. if (count($obj->warnings)) {
  1930. $arrayofwarnings[$sourcelinenb] = $obj->warnings;
  1931. }
  1932. if (!count($obj->errors) && !count($obj->warnings)) {
  1933. $nbok++;
  1934. }
  1935. }
  1936. // Close file
  1937. $obj->import_close_file();
  1938. } else {
  1939. print $langs->trans("ErrorFailedToOpenFile", $pathfile);
  1940. }
  1941. if (count($arrayoferrors) > 0) {
  1942. $db->rollback(); // We force rollback because this was errors.
  1943. } else {
  1944. $error = 0;
  1945. // Run the sql after import if defined
  1946. //var_dump($objimport->array_import_run_sql_after[0]);
  1947. if (!empty($objimport->array_import_run_sql_after[0]) && is_array($objimport->array_import_run_sql_after[0])) {
  1948. $i = 0;
  1949. foreach ($objimport->array_import_run_sql_after[0] as $sqlafterimport) {
  1950. $i++;
  1951. $resqlafterimport = $db->query($sqlafterimport);
  1952. if (!$resqlafterimport) {
  1953. $arrayoferrors['none'][] = array('lib'=>$langs->trans("Error running final request: ".$sqlafterimport));
  1954. $error++;
  1955. }
  1956. }
  1957. }
  1958. if (!$error) {
  1959. $db->commit(); // We can commit if no errors.
  1960. } else {
  1961. $db->rollback();
  1962. }
  1963. }
  1964. print dol_get_fiche_end();
  1965. // Show result
  1966. print '<br>';
  1967. print '<div class="ok">';
  1968. print $langs->trans("NbOfLinesImported", $nbok).'</b><br>';
  1969. print $langs->trans("NbInsert", empty($obj->nbinsert) ? 0 : $obj->nbinsert).'<br>';
  1970. print $langs->trans("NbUpdate", empty($obj->nbupdate) ? 0 : $obj->nbupdate).'<br><br>';
  1971. print '</div>';
  1972. print '<div class="center">';
  1973. print $langs->trans("FileWasImported", $importid).'<br>';
  1974. print '<span class="opacitymedium">'.$langs->trans("YouCanUseImportIdToFindRecord", $importid).'</span><br>';
  1975. print '</div>';
  1976. }
  1977. print '<br>';
  1978. // End of page
  1979. llxFooter();
  1980. $db->close();
  1981. /**
  1982. * Function to put the movable box of a source field
  1983. *
  1984. * @param array $fieldssource List of source fields
  1985. * @param int $pos Pos
  1986. * @param string $key Key
  1987. * @param boolean $var Line style (odd or not). No more used.
  1988. * @param int $nostyle Hide style
  1989. * @return void
  1990. */
  1991. function show_elem($fieldssource, $pos, $key, $var, $nostyle = '')
  1992. {
  1993. global $langs;
  1994. $height = '32px';
  1995. if ($key == 'none') {
  1996. //stop multiple duplicate ids with no number
  1997. print "\n\n<!-- Box_no-key start-->\n";
  1998. print '<div class="box boximport" style="padding:0;">'."\n";
  1999. print '<table summary="boxtable_no-key" class="centpercent nobordernopadding">'."\n";
  2000. } else {
  2001. print "\n\n<!-- Box ".$pos." start -->\n";
  2002. print '<div class="box boximport" style="padding: 0;" id="boxto_'.$pos.'">'."\n";
  2003. print '<table summary="boxtable'.$pos.'" class="nobordernopadding centpercent tableimport">'."\n";
  2004. }
  2005. if (($pos && $pos > count($fieldssource)) && (!isset($fieldssource[$pos]["imported"]))) { // No fields
  2006. /*
  2007. print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
  2008. print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
  2009. print '</td>';
  2010. print '<td style="font-weight: normal">';
  2011. print $langs->trans("NoFields");
  2012. print '</td>';
  2013. print '</tr>';
  2014. */
  2015. } elseif ($key == 'none') { // Empty line
  2016. print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
  2017. print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
  2018. print '&nbsp;';
  2019. print '</td>';
  2020. print '<td style="font-weight: normal">';
  2021. print '&nbsp;';
  2022. print '</td>';
  2023. print '</tr>';
  2024. } else {
  2025. // Print field of source file
  2026. print '<tr style="height:'.$height.'" class="trimport oddevenimport">';
  2027. print '<td class="nocellnopadding" width="16" style="font-weight: normal">';
  2028. // 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
  2029. //print img_picto($langs->trans("MoveField", $pos), 'grip_title', 'class="boxhandle" style="cursor:move;"');
  2030. print img_picto($langs->trans("Column").' '.num2Alpha($pos - 1), 'file', 'class="pictofixedwith"');
  2031. print '</td>';
  2032. if (isset($fieldssource[$pos]['imported']) && $fieldssource[$pos]['imported'] == false) {
  2033. print '<td class="nowraponall boxtdunused" style="font-weight: normal">';
  2034. } else {
  2035. print '<td class="nowraponall" style="font-weight: normal">';
  2036. }
  2037. print $langs->trans("Column").' '.num2Alpha($pos - 1).' (#'.$pos.')';
  2038. if (empty($fieldssource[$pos]['example1'])) {
  2039. $example = $fieldssource[$pos]['label'];
  2040. } else {
  2041. $example = $fieldssource[$pos]['example1'];
  2042. }
  2043. if ($example) {
  2044. if (!utf8_check($example)) {
  2045. $example = utf8_encode($example);
  2046. }
  2047. print ' - ';
  2048. //print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("ExampleOnFirstLine").': </span>';
  2049. print '<i class="opacitymedium">'.$example.'</i>';
  2050. }
  2051. print '</td>';
  2052. print '</tr>';
  2053. }
  2054. print "</table>\n";
  2055. print "</div>\n";
  2056. print "<!-- Box end -->\n\n";
  2057. }
  2058. /**
  2059. * Return a numeric into an Excel like column number
  2060. *
  2061. * @param string $n Numeric value
  2062. * @return string Column in Excel format
  2063. */
  2064. function num2Alpha($n)
  2065. {
  2066. for ($r = ""; $n >= 0; $n = intval($n / 26) - 1)
  2067. $r = chr($n%26 + 0x41) . $r;
  2068. return $r;
  2069. }
  2070. /**
  2071. * Return not used field number
  2072. *
  2073. * @param array $fieldssource Array of field source
  2074. * @param array $listofkey Array of keys
  2075. * @return integer
  2076. */
  2077. function getnewkey(&$fieldssource, &$listofkey)
  2078. {
  2079. $i = count($fieldssource) + 1;
  2080. // Max number of key
  2081. $maxkey = 0;
  2082. foreach ($listofkey as $key => $val) {
  2083. $maxkey = max($maxkey, $key);
  2084. }
  2085. // Found next empty key
  2086. while ($i <= $maxkey) {
  2087. if (empty($listofkey[$i])) {
  2088. break;
  2089. } else {
  2090. $i++;
  2091. }
  2092. }
  2093. $listofkey[$i] = 1;
  2094. return $i;
  2095. }
  2096. /**
  2097. * Return array with element inserted in it at position $position
  2098. *
  2099. * @param array $array Array of field source
  2100. * @param mixed $position key of postion to insert to
  2101. * @param array $insertArray Array to insert
  2102. * @return array
  2103. */
  2104. function arrayInsert($array, $position, $insertArray)
  2105. {
  2106. $ret = [];
  2107. if ($position == count($array)) {
  2108. $ret = $array + $insertArray;
  2109. } else {
  2110. $i = 0;
  2111. foreach ($array as $key => $value) {
  2112. if ($position == $i++) {
  2113. $ret += $insertArray;
  2114. }
  2115. $ret[$key] = $value;
  2116. }
  2117. }
  2118. return $ret;
  2119. }