PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/import/import.php

https://bitbucket.org/speedealing/speedealing
PHP | 1798 lines | 1280 code | 261 blank | 257 comment | 221 complexity | 79b9bda5edec20a571cd21a170d24e57 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.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 <http://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 . '/import/class/import.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/import/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. $langs->load("exports");
  33. $langs->load("errors");
  34. // Security check
  35. if (!empty($user->societe_id))
  36. $socid = $user->societe_id;
  37. $result = restrictedArea($user, 'import');
  38. $entitytoicon = array(
  39. 'invoice' => 'bill', 'invoice_line' => 'bill',
  40. 'order' => 'order', 'order_line' => 'order',
  41. 'intervention' => 'intervention', 'inter_line' => 'intervention',
  42. 'member' => 'user', 'member_type' => 'group', 'subscription' => 'payment',
  43. 'tax' => 'generic', 'tax_type' => 'generic',
  44. 'account' => 'account',
  45. 'payment' => 'payment',
  46. 'product' => 'product', 'stock' => 'generic', 'warehouse' => 'stock',
  47. 'category' => 'generic',
  48. 'other' => 'generic'
  49. );
  50. $entitytolang = array(// Translation code
  51. 'user' => 'User',
  52. 'company' => 'Company', 'contact' => 'Contact',
  53. 'invoice' => 'Bill', 'invoice_line' => 'InvoiceLine',
  54. 'order' => 'Order', 'order_line' => 'OrderLine',
  55. 'intervention' => 'Intervention', 'inter_line' => 'InterLine',
  56. 'member' => 'Member', 'member_type' => 'MemberType', 'subscription' => 'Subscription',
  57. 'tax' => 'SocialContribution', 'tax_type' => 'DictionnarySocialContributions',
  58. 'account' => 'BankTransactions',
  59. 'payment' => 'Payment',
  60. 'product' => 'Product', 'stock' => 'Stock', 'warehouse' => 'Warehouse',
  61. 'category' => 'Category',
  62. 'other' => 'Other'
  63. );
  64. $datatoimport = GETPOST('datatoimport');
  65. $format = GETPOST('format');
  66. $filetoimport = GETPOST('filetoimport');
  67. $action = GETPOST('action', 'alpha');
  68. $confirm = GETPOST('confirm', 'alpha');
  69. $step = (GETPOST('step') ? GETPOST('step') : 1);
  70. $import_name = GETPOST('import_name');
  71. $hexa = GETPOST('hexa');
  72. $importmodelid = GETPOST('importmodelid');
  73. $excludefirstline = (GETPOST('excludefirstline') ? GETPOST('excludefirstline') : 1);
  74. $separator = (GETPOST('separator') ? GETPOST('separator') : (!empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ? $conf->global->IMPORT_CSV_SEPARATOR_TO_USE : ';'));
  75. $enclosure = (GETPOST('enclosure') ? GETPOST('enclosure') : '"');
  76. $objimport = new Import($db);
  77. $objimport->load_arrays($user, ($step == 1 ? '' : $datatoimport));
  78. $objmodelimport = new ModeleImports();
  79. $form = new Form($db);
  80. $htmlother = new FormOther($db);
  81. $formfile = new FormFile($db);
  82. // Init $array_match_file_to_database from _SESSION
  83. $serialized_array_match_file_to_database = dol_getcache("dol_array_match_file_to_database");
  84. $array_match_file_to_database = array();
  85. $fieldsarray = explode(',', $serialized_array_match_file_to_database);
  86. foreach ($fieldsarray as $elem) {
  87. $tabelem = explode('=', $elem, 2);
  88. $key = $tabelem[0];
  89. $val = (isset($tabelem[1]) ? $tabelem[1] : '');
  90. if ($key && $val) {
  91. $array_match_file_to_database[$key] = $val;
  92. }
  93. }
  94. /*
  95. * Actions
  96. */
  97. /*
  98. if ($action=='downfield' || $action=='upfield')
  99. {
  100. $pos=$array_match_file_to_database[$_GET["field"]];
  101. if ($action=='downfield') $newpos=$pos+1;
  102. if ($action=='upfield') $newpos=$pos-1;
  103. // Recherche code avec qui switcher
  104. $newcode="";
  105. foreach($array_match_file_to_database as $code=>$value)
  106. {
  107. if ($value == $newpos)
  108. {
  109. $newcode=$code;
  110. break;
  111. }
  112. }
  113. //print("Switch pos=$pos (code=".$_GET["field"].") and newpos=$newpos (code=$newcode)");
  114. if ($newcode) // Si newcode trouve (protection contre resoumission de page)
  115. {
  116. $array_match_file_to_database[$_GET["field"]]=$newpos;
  117. $array_match_file_to_database[$newcode]=$pos;
  118. $_SESSION["dol_array_match_file_to_database"]=$serialized_array_match_file_to_database;
  119. }
  120. }
  121. */
  122. if ($action == 'builddoc') {
  123. // Build import file
  124. $result = $objimport->build_file($user, $_POST['model'], $datatoimport, $array_match_file_to_database);
  125. if ($result < 0) {
  126. $mesg = '<div class="error">' . $objimport->error . '</div>';
  127. } else {
  128. $mesg = '<div class="ok">' . $langs->trans("FileSuccessfullyBuilt") . '</div>';
  129. }
  130. }
  131. if ($action == 'deleteprof') {
  132. if ($_GET["id"]) {
  133. $objimport->fetch($_GET["id"]);
  134. $result = $objimport->delete($user);
  135. }
  136. }
  137. // Save import config to database
  138. if ($action == 'add_import_model') {
  139. if ($import_name) {
  140. // Set save string
  141. $hexa = '';
  142. foreach ($array_match_file_to_database as $key => $val) {
  143. if ($hexa)
  144. $hexa.=',';
  145. $hexa.=$key . '=' . $val;
  146. }
  147. $objimport->model_name = $import_name;
  148. $objimport->datatoimport = $datatoimport;
  149. $objimport->hexa = $hexa;
  150. $result = $objimport->create($user);
  151. if ($result >= 0) {
  152. $mesg = '<div class="ok">' . $langs->trans("ImportModelSaved", $objimport->model_name) . '</div>';
  153. } else {
  154. $langs->load("errors");
  155. if ($objimport->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  156. $mesg = '<div class="error">' . $langs->trans("ErrorImportDuplicateProfil") . '</div>';
  157. }
  158. else
  159. $mesg = '<div class="error">' . $objimport->error . '</div>';
  160. }
  161. }
  162. else {
  163. $mesg = '<div class="error">' . $langs->trans("ErrorFieldRequired", $langs->transnoentities("ImportModelName")) . '</div>';
  164. }
  165. }
  166. if ($step == 3 && $datatoimport) {
  167. if (GETPOST('sendit') && !empty($conf->global->MAIN_UPLOAD_DOC)) {
  168. dol_mkdir($conf->import->dir_temp);
  169. $nowyearmonth = dol_print_date(dol_now(), '%Y%m%d%H%M%S');
  170. $fullpath = $conf->import->dir_temp . "/" . $nowyearmonth . '-' . $_FILES['userfile']['name'];
  171. if (dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $fullpath, 1) > 0) {
  172. dol_syslog("File " . $fullpath . " was added for import");
  173. } else {
  174. $langs->load("errors");
  175. setEventMessage($langs->trans("ErrorFailedToSaveFile"), 'errors');
  176. }
  177. }
  178. // Delete file
  179. if ($action == 'confirm_deletefile' && $confirm == 'yes') {
  180. $langs->load("other");
  181. $param = '&datatoimport=' . $datatoimport . '&format=' . $format;
  182. if ($excludefirstline)
  183. $param.='&excludefirstline=1';
  184. $file = $conf->import->dir_temp . '/' . GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  185. $ret = dol_delete_file($file);
  186. if ($ret)
  187. setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
  188. else
  189. setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
  190. Header('Location: ' . $_SERVER["PHP_SELF"] . '?step=' . $step . $param);
  191. exit;
  192. }
  193. }
  194. if ($step == 4 && $action == 'select_model') {
  195. // Reinit match arrays
  196. dol_delcache("dol_array_match_file_to_database");
  197. $serialized_array_match_file_to_database = '';
  198. $array_match_file_to_database = array();
  199. // Load model from $importmodelid and set $array_match_file_to_database
  200. // and $_SESSION["dol_array_match_file_to_database"]
  201. $result = $objimport->fetch($importmodelid);
  202. if ($result > 0) {
  203. $serialized_array_match_file_to_database = $objimport->hexa;
  204. $fieldsarray = explode(',', $serialized_array_match_file_to_database);
  205. foreach ($fieldsarray as $elem) {
  206. $tabelem = explode('=', $elem);
  207. $key = $tabelem[0];
  208. $val = $tabelem[1];
  209. if ($key && $val) {
  210. $array_match_file_to_database[$key] = $val;
  211. }
  212. }
  213. dol_setcache("dol_array_match_file_to_database", $serialized_array_match_file_to_database);
  214. }
  215. }
  216. if ($action == 'saveorder') {
  217. // Enregistrement de la position des champs
  218. dol_syslog("boxorder=" . $_GET['boxorder'] . " datatoimport=" . $_GET["datatoimport"], LOG_DEBUG);
  219. $part = explode(':', $_GET['boxorder']);
  220. $colonne = $part[0];
  221. $list = $part[1];
  222. dol_syslog('column=' . $colonne . ' list=' . $list);
  223. // Init targets fields array
  224. $fieldstarget = $objimport->array_import_fields[0];
  225. // Reinit match arrays. We redefine array_match_file_to_database
  226. $serialized_array_match_file_to_database = '';
  227. $array_match_file_to_database = array();
  228. $fieldsarray = explode(',', $list);
  229. $pos = 0;
  230. foreach ($fieldsarray as $fieldnb) { // For each elem in list. fieldnb start from 1 to ...
  231. // Get name of database fields at position $pos and put it into $namefield
  232. $posbis = 0;
  233. $namefield = '';
  234. foreach ($fieldstarget as $key => $val) { // key: val:
  235. //dol_syslog('AjaxImport key='.$key.' val='.$val);
  236. if ($posbis < $pos) {
  237. $posbis++;
  238. continue;
  239. }
  240. // We found the key of targets that is at position pos
  241. $namefield = $key;
  242. //dol_syslog('AjaxImport Field name found for file field nb '.$fieldnb.'='.$namefield);
  243. break;
  244. }
  245. if ($fieldnb && $namefield) {
  246. $array_match_file_to_database[$fieldnb] = $namefield;
  247. if ($serialized_array_match_file_to_database)
  248. $serialized_array_match_file_to_database.=',';
  249. $serialized_array_match_file_to_database.=($fieldnb . '=' . $namefield);
  250. }
  251. $pos++;
  252. }
  253. // We save new matching in session
  254. dol_setcache("dol_array_match_file_to_database", $serialized_array_match_file_to_database);
  255. dol_syslog('dol_array_match_file_to_database=' . $serialized_array_match_file_to_database);
  256. }
  257. /*
  258. * View
  259. */
  260. // STEP 1: Page to select dataset to import
  261. if ($step == 1 || !$datatoimport) {
  262. // Clean saved file-database matching
  263. $serialized_array_match_file_to_database = '';
  264. $array_match_file_to_database = array();
  265. dol_delcache("dol_array_match_file_to_database");
  266. $param = '';
  267. if ($excludefirstline)
  268. $param.='&excludefirstline=1';
  269. if ($separator)
  270. $param.='&separator=' . urlencode($separator);
  271. if ($enclosure)
  272. $param.='&enclosure=' . urlencode($enclosure);
  273. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  274. print_fiche_titre($langs->trans("ImportArea"));
  275. print '<div class="with-padding">';
  276. print '<div class="columns">';
  277. print start_box($langs->trans("NewImport"), "twelve", "16-User.png", false);
  278. $head = import_prepare_head($param, 1);
  279. dol_fiche_head($head, 'step1', $langs->trans("NewImport"));
  280. print '<table class="notopnoleftnoright" width="100%">';
  281. print $langs->trans("SelectImportDataSet") . '<br>';
  282. // Affiche les modules d'imports
  283. print '<table class="noborder" width="100%">';
  284. print '<tr class="liste_titre">';
  285. print '<td>' . $langs->trans("Module") . '</td>';
  286. print '<td>' . $langs->trans("ImportableDatas") . '</td>';
  287. print '<td>&nbsp;</td>';
  288. print '</tr>';
  289. $val = true;
  290. if (count($objimport->array_import_code)) {
  291. foreach ($objimport->array_import_code as $key => $value) {
  292. $val = !$val;
  293. print '<tr ' . $bc[$val] . '><td nospan="nospan">';
  294. //print img_object($objimport->array_import_module[$key]->getName(),$import->array_import_module[$key]->picto).' ';
  295. print $objimport->array_import_module[$key]->getName();
  296. print '</td><td>';
  297. //print $value;
  298. print img_object($objimport->array_import_module[$key]->getName(), $objimport->array_import_icon[$key]) . ' ';
  299. print $objimport->array_import_label[$key];
  300. print '</td><td align="right">';
  301. if ($objimport->array_import_perms[$key]) {
  302. print '<a href="' . DOL_URL_ROOT . '/import/import.php?step=2&datatoimport=' . $objimport->array_import_code[$key] . $param . '">' . img_picto($langs->trans("NewImport"), 'filenew') . '</a>';
  303. } else {
  304. print $langs->trans("NotEnoughPermissions");
  305. }
  306. print '</td></tr>';
  307. }
  308. } else {
  309. print '<tr><td ' . $bc[false] . ' colspan="3">' . $langs->trans("NoImportableData") . '</td></tr>';
  310. }
  311. print '</table>';
  312. print '</table>';
  313. dol_fiche_end();
  314. print end_box();
  315. print '</div></div>';
  316. if ($mesg)
  317. print $mesg;
  318. }
  319. // STEP 2: Page to select input format file
  320. if ($step == 2 && $datatoimport) {
  321. $param = '&datatoimport=' . $datatoimport;
  322. if ($excludefirstline)
  323. $param.='&excludefirstline=1';
  324. if ($separator)
  325. $param.='&separator=' . urlencode($separator);
  326. if ($enclosure)
  327. $param.='&enclosure=' . urlencode($enclosure);
  328. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  329. print_fiche_titre($langs->trans("ImportArea"));
  330. print '<div class="with-padding">';
  331. print '<div class="columns">';
  332. print start_box($langs->trans("NewImport"), "twelve", "16-User.png", false);
  333. $head = import_prepare_head($param, 2);
  334. dol_fiche_head($head, 'step2', $langs->trans("NewImport"));
  335. print '<table width="100%" class="border">';
  336. // Module
  337. print '<tr><td width="25%">' . $langs->trans("Module") . '</td>';
  338. print '<td>';
  339. //print img_object($objimport->array_import_module[0]->getName(),$objimport->array_import_module[0]->picto).' ';
  340. print $objimport->array_import_module[0]->getName();
  341. print '</td></tr>';
  342. // Lot de donnees a importer
  343. print '<tr><td width="25%">' . $langs->trans("DatasetToImport") . '</td>';
  344. print '<td>';
  345. print img_object($objimport->array_import_module[0]->getName(), $objimport->array_import_icon[0]) . ' ';
  346. print $objimport->array_import_label[0];
  347. print '</td></tr>';
  348. print '</table>';
  349. print '<br>' . "\n";
  350. print '<form name="userfile" action="' . $_SERVER["PHP_SELF"] . '" enctype="multipart/form-data" METHOD="POST">';
  351. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  352. print '<input type="hidden" name="max_file_size" value="' . $conf->maxfilesize . '">';
  353. print $langs->trans("ChooseFormatOfFileToImport", img_picto('', 'filenew')) . '<br>';
  354. print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
  355. $filetoimport = '';
  356. $var = true;
  357. // Add format informations and link to download example
  358. print '<tr class="liste_titre"><td colspan="6">';
  359. print $langs->trans("FileMustHaveOneOfFollowingFormat");
  360. print '</td></tr>';
  361. $liste = $objmodelimport->liste_modeles($db);
  362. foreach ($liste as $key) {
  363. $var = !$var;
  364. print '<tr ' . $bc[$var] . '>';
  365. print '<td width="16">' . img_picto_common($key, $objmodelimport->getPicto($key)) . '</td>';
  366. $text = $objmodelimport->getDriverDesc($key);
  367. print '<td>' . $form->textwithpicto($objmodelimport->getDriverLabel($key), $text) . '</td>';
  368. print '<td align="center"><a href="' . DOL_URL_ROOT . '/import/emptyexample.php?format=' . $key . $param . '" target="_blank">' . $langs->trans("DownloadEmptyExample") . '</a></td>';
  369. // Action button
  370. print '<td align="right">';
  371. print '<a href="' . DOL_URL_ROOT . '/import/import.php?step=3&format=' . $key . $param . '">' . img_picto($langs->trans("SelectFormat"), 'filenew') . '</a>';
  372. print '</td>';
  373. print '</tr>';
  374. }
  375. print '</table></form>';
  376. dol_fiche_end();
  377. print end_box();
  378. print '</div></div>';
  379. if ($mesg)
  380. print $mesg;
  381. }
  382. // STEP 3: Page to select file
  383. if ($step == 3 && $datatoimport) {
  384. $param = '&datatoimport=' . $datatoimport . '&format=' . $format;
  385. if ($excludefirstline)
  386. $param.='&excludefirstline=1';
  387. if ($separator)
  388. $param.='&separator=' . urlencode($separator);
  389. if ($enclosure)
  390. $param.='&enclosure=' . urlencode($enclosure);
  391. $liste = $objmodelimport->liste_modeles($db);
  392. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  393. print_fiche_titre($langs->trans("ImportArea"));
  394. print '<div class="with-padding">';
  395. print '<div class="columns">';
  396. print start_box($langs->trans("NewImport"), "twelve", "16-User.png", false);
  397. $head = import_prepare_head($param, 3);
  398. dol_fiche_head($head, 'step3', $langs->trans("NewImport"));
  399. /*
  400. * Confirm delete file
  401. */
  402. if ($action == 'delete') {
  403. $ret = $form->form_confirm($_SERVER["PHP_SELF"] . '?urlfile=' . urlencode(GETPOST('urlfile')) . '&step=3' . $param, $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', 0, 1);
  404. if ($ret == 'html')
  405. print '<br>';
  406. }
  407. print '<table width="100%" class="border">';
  408. // Module
  409. print '<tr><td width="25%">' . $langs->trans("Module") . '</td>';
  410. print '<td>';
  411. //print img_object($objimport->array_import_module[0]->getName(),$objimport->array_import_module[0]->picto).' ';
  412. print $objimport->array_import_module[0]->getName();
  413. print '</td></tr>';
  414. // Lot de donnees a importer
  415. print '<tr><td width="25%">' . $langs->trans("DatasetToImport") . '</td>';
  416. print '<td>';
  417. print img_object($objimport->array_import_module[0]->getName(), $objimport->array_import_icon[0]) . ' ';
  418. print $objimport->array_import_label[0];
  419. print '</td></tr>';
  420. print '</table><br>';
  421. print '<b>' . $langs->trans("InformationOnSourceFile") . '</b>';
  422. print '<table width="100%" class="border">';
  423. //print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnSourceFile").'</b></td></tr>';
  424. // Source file format
  425. print '<tr><td width="25%">' . $langs->trans("SourceFileFormat") . '</td>';
  426. print '<td>';
  427. $text = $objmodelimport->getDriverDesc($format);
  428. print $form->textwithpicto($objmodelimport->getDriverLabel($format), $text);
  429. print '</td><td align="right" nowrap="nowrap"><a href="' . DOL_URL_ROOT . '/import/emptyexample.php?format=' . $format . $param . '" target="_blank">' . $langs->trans("DownloadEmptyExample") . '</a>';
  430. print '</td></tr>';
  431. print '</table>';
  432. print '<br>' . "\n";
  433. print '<form name="userfile" action="' . $_SERVER["PHP_SELF"] . '" enctype="multipart/form-data" METHOD="POST">';
  434. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  435. print '<input type="hidden" name="max_file_size" value="' . $conf->maxfilesize . '">';
  436. print '<table class="noborder" width="100%" cellspacing="0" cellpadding="4">';
  437. $filetoimport = '';
  438. $var = true;
  439. print '<tr><td colspan="6">' . $langs->trans("ChooseFileToImport", img_picto('', 'filenew')) . '</td></tr>';
  440. print '<tr class="liste_titre"><td colspan="6">' . $langs->trans("FileWithDataToImport") . '</td></tr>';
  441. // Input file name box
  442. $var = false;
  443. print '<tr ' . $bc[$var] . '><td colspan="6">';
  444. print '<input type="file" name="userfile" size="20" maxlength="80"> &nbsp; &nbsp; ';
  445. print '<input type="submit" class="button" value="' . $langs->trans("AddFile") . '" name="sendit">';
  446. print '<input type="hidden" value="' . $step . '" name="step">';
  447. print '<input type="hidden" value="' . $format . '" name="format">';
  448. print '<input type="hidden" value="' . $excludefirstline . '" name="excludefirstline">';
  449. print '<input type="hidden" value="' . $separator . '" name="separator">';
  450. print '<input type="hidden" value="' . $enclosure . '" name="enclosure">';
  451. print '<input type="hidden" value="' . $datatoimport . '" name="datatoimport">';
  452. print "</tr>\n";
  453. // Search available imports
  454. $filearray = dol_dir_list($conf->import->dir_temp, 'files', 0, '', '', 'name', SORT_DESC);
  455. if (count($filearray) > 0) {
  456. $dir = $conf->import->dir_temp;
  457. // Search available files to import
  458. $i = 0;
  459. foreach ($filearray as $key => $val) {
  460. $file = $val['name'];
  461. // readdir return value in ISO and we want UTF8 in memory
  462. if (!utf8_check($file))
  463. $file = utf8_encode($file);
  464. if (preg_match('/^\./', $file))
  465. continue;
  466. $modulepart = 'import';
  467. $urlsource = $_SERVER["PHP_SELF"] . '?step=' . $step . $param . '&filetoimport=' . urlencode($filetoimport);
  468. $relativepath = $file;
  469. $var = !$var;
  470. print '<tr ' . $bc[$var] . '>';
  471. print '<td width="16">' . img_mime($file) . '</td>';
  472. print '<td>';
  473. print '<a href="' . DOL_URL_ROOT . '/document.php?modulepart=' . $modulepart . '&file=' . urlencode($relativepath) . '&step=3' . $param . '" target="_blank">';
  474. print $file;
  475. print '</a>';
  476. print '</td>';
  477. // Affiche taille fichier
  478. print '<td align="right">' . dol_print_size(dol_filesize($dir . '/' . $file)) . '</td>';
  479. // Affiche date fichier
  480. print '<td align="right">' . dol_print_date(dol_filemtime($dir . '/' . $file), 'dayhour') . '</td>';
  481. // Del button
  482. print '<td align="right"><a href="' . $_SERVER['PHP_SELF'] . '?action=delete&step=3' . $param . '&urlfile=' . urlencode($relativepath);
  483. print '">' . img_delete() . '</a></td>';
  484. // Action button
  485. print '<td align="right">';
  486. print '<a href="' . $_SERVER['PHP_SELF'] . '?step=4' . $param . '&filetoimport=' . urlencode($relativepath) . '">' . img_picto($langs->trans("NewImport"), 'filenew') . '</a>';
  487. print '</td>';
  488. print '</tr>';
  489. }
  490. }
  491. print '</table></form>';
  492. dol_fiche_end();
  493. print end_box();
  494. print '</div></div>';
  495. if ($mesg)
  496. print $mesg;
  497. }
  498. // STEP 4: Page to make matching between source file and database fields
  499. if ($step == 4 && $datatoimport) {
  500. $model = $format;
  501. $liste = $objmodelimport->liste_modeles($db);
  502. // Create classe to use for import
  503. $dir = DOL_DOCUMENT_ROOT . "/import/core/modules/import/";
  504. $file = "import_" . $model . ".modules.php";
  505. $classname = "Import" . ucfirst($model);
  506. require_once $dir . $file;
  507. $obj = new $classname($db, $datatoimport);
  508. if ($model == 'csv') {
  509. $obj->separator = $separator;
  510. $obj->enclosure = $enclosure;
  511. }
  512. // Load source fields in input file
  513. $fieldssource = array();
  514. $result = $obj->import_open_file($conf->import->dir_temp . '/' . $filetoimport, $langs);
  515. if ($result >= 0) {
  516. // Read first line
  517. $arrayrecord = $obj->import_read_record();
  518. // Put into array fieldssource starting with 1.
  519. $i = 1;
  520. foreach ($arrayrecord as $key => $val) {
  521. $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24);
  522. $i++;
  523. }
  524. $obj->import_close_file();
  525. }
  526. // Load targets fields in database
  527. $fieldstarget = $objimport->array_import_fields[0];
  528. $maxpos = max(count($fieldssource), count($fieldstarget));
  529. //var_dump($array_match_file_to_database);
  530. // Is it a first time in page (if yes, we must initialize array_match_file_to_database)
  531. if (count($array_match_file_to_database) == 0) {
  532. // This is first input in screen, we need to define
  533. // $array_match_file_to_database
  534. // $serialized_array_match_file_to_database
  535. // $_SESSION["dol_array_match_file_to_database"]
  536. $pos = 1;
  537. $num = count($fieldssource);
  538. while ($pos <= $num) {
  539. if ($num >= 1 && $pos <= $num) {
  540. $posbis = 1;
  541. foreach ($fieldstarget as $key => $val) {
  542. if ($posbis < $pos) {
  543. $posbis++;
  544. continue;
  545. }
  546. // We found the key of targets that is at position pos
  547. $array_match_file_to_database[$pos] = $key;
  548. if ($serialized_array_match_file_to_database)
  549. $serialized_array_match_file_to_database.=',';
  550. $serialized_array_match_file_to_database.=($pos . '=' . $key);
  551. break;
  552. }
  553. }
  554. $pos++;
  555. }
  556. // Save the match array in session. We now will use the array in session.
  557. dol_setcache("dol_array_match_file_to_database", $serialized_array_match_file_to_database);
  558. }
  559. $array_match_database_to_file = array_flip($array_match_file_to_database);
  560. //print $serialized_array_match_file_to_database;
  561. //print $_SESSION["dol_array_match_file_to_database"];
  562. //var_dump($array_match_file_to_database);exit;
  563. // Now $array_match_file_to_database contains fieldnb(1,2,3...)=>fielddatabase(key in $array_match_file_to_database)
  564. $param = '&format=' . $format . '&datatoimport=' . $datatoimport . '&filetoimport=' . urlencode($filetoimport);
  565. if ($excludefirstline)
  566. $param.='&excludefirstline=1';
  567. if ($separator)
  568. $param.='&separator=' . urlencode($separator);
  569. if ($enclosure)
  570. $param.='&enclosure=' . urlencode($enclosure);
  571. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  572. print_fiche_titre($langs->trans("ImportArea"));
  573. print '<div class="with-padding">';
  574. print '<div class="columns">';
  575. print start_box($langs->trans("NewImport"), "twelve", "16-User.png", false);
  576. $head = import_prepare_head($param, 4);
  577. dol_fiche_head($head, 'step4', $langs->trans("NewImport"));
  578. print '<table width="100%" class="border">';
  579. // Module
  580. print '<tr><td width="25%">' . $langs->trans("Module") . '</td>';
  581. print '<td>';
  582. //print img_object($objimport->array_import_module[0]->getName(),$objimport->array_import_module[0]->picto).' ';
  583. print $objimport->array_import_module[0]->getName();
  584. print '</td></tr>';
  585. // Lot de donnees a importer
  586. print '<tr><td width="25%">' . $langs->trans("DatasetToImport") . '</td>';
  587. print '<td>';
  588. print img_object($objimport->array_import_module[0]->getName(), $objimport->array_import_icon[0]) . ' ';
  589. print $objimport->array_import_label[0];
  590. print '</td></tr>';
  591. print '</table><br>';
  592. print '<b>' . $langs->trans("InformationOnSourceFile") . '</b>';
  593. print '<table width="100%" class="border">';
  594. //print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnSourceFile").'</b></td></tr>';
  595. // Source file format
  596. print '<tr><td width="25%">' . $langs->trans("SourceFileFormat") . '</td>';
  597. print '<td>';
  598. $text = $objmodelimport->getDriverDesc($format);
  599. print $form->textwithpicto($objmodelimport->getDriverLabel($format), $text);
  600. print '</td></tr>';
  601. // Separator and enclosure
  602. if ($model == 'csv') {
  603. print '<tr><td width="25%">' . $langs->trans("CsvOptions") . '</td>';
  604. print '<td>';
  605. print '<form>';
  606. print '<input type="hidden" value="' . $step . '" name="step">';
  607. print '<input type="hidden" value="' . $format . '" name="format">';
  608. print '<input type="hidden" value="' . $excludefirstline . '" name="excludefirstline">';
  609. print '<input type="hidden" value="' . $datatoimport . '" name="datatoimport">';
  610. print '<input type="hidden" value="' . $filetoimport . '" name="filetoimport">';
  611. print $langs->trans("Separator") . ' : ';
  612. print '<input type="text" size="1" name="separator" value="' . htmlentities($separator) . '"/>';
  613. print '&nbsp;&nbsp;&nbsp;&nbsp;' . $langs->trans("Enclosure") . ' : ';
  614. print '<input type="text" size="1" name="enclosure" value="' . htmlentities($enclosure) . '"/>';
  615. print '<input type="submit" value="' . $langs->trans('Update') . '" class="button" />';
  616. print '</form>';
  617. print '</td></tr>';
  618. }
  619. // File to import
  620. print '<tr><td width="25%">' . $langs->trans("FileToImport") . '</td>';
  621. print '<td>';
  622. $modulepart = 'import';
  623. $relativepath = GETPOST('filetoimport');
  624. print '<a href="' . DOL_URL_ROOT . '/document.php?modulepart=' . $modulepart . '&file=' . urlencode($relativepath) . '&step=4' . $param . '" target="_blank">';
  625. print $filetoimport;
  626. print '</a>';
  627. print '</td></tr>';
  628. print '</table>';
  629. print '<br>' . "\n";
  630. // List of source fields
  631. print '<!-- List of source fields -->' . "\n";
  632. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">';
  633. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  634. print '<input type="hidden" name="action" value="select_model">';
  635. print '<input type="hidden" name="step" value="4">';
  636. print '<input type="hidden" name="format" value="' . $format . '">';
  637. print '<input type="hidden" name="datatoimport" value="' . $datatoimport . '">';
  638. print '<input type="hidden" name="filetoimport" value="' . $filetoimport . '">';
  639. print '<input type="hidden" name="excludefirstline" value="' . $excludefirstline . '">';
  640. print '<input type="hidden" name="separator" value="' . $separator . '">';
  641. print '<input type="hidden" name="enclosure" value="' . $enclosure . '">';
  642. print '<table><tr><td colspan="2">';
  643. print $langs->trans("SelectImportFields", img_picto('', 'uparrow', '')) . ' ';
  644. $htmlother->select_import_model($importmodelid, 'importmodelid', $datatoimport, 1);
  645. print '<input type="submit" class="button" value="' . $langs->trans("Select") . '">';
  646. print '</td></tr></table>';
  647. print '</form>';
  648. // Title of array with fields
  649. print '<table class="nobordernopadding" width="100%">';
  650. print '<tr class="liste_titre">';
  651. print '<td>' . $langs->trans("FieldsInSourceFile") . '</td>';
  652. print '<td>' . $langs->trans("FieldsInTargetDatabase") . '</td>';
  653. print '</tr>';
  654. //var_dump($array_match_file_to_database);
  655. print '<tr valign="top"><td width="50%">';
  656. $fieldsplaced = array();
  657. $valforsourcefieldnb = array();
  658. $listofkeys = array();
  659. foreach ($array_match_file_to_database as $key => $val) {
  660. $listofkeys[$key] = 1;
  661. }
  662. print "\n<!-- Box left container -->\n";
  663. print '<div id="left" class="connectedSortable">' . "\n";
  664. // List of source fields
  665. $var = true;
  666. $lefti = 1;
  667. foreach ($array_match_file_to_database as $key => $val) {
  668. $var = !$var;
  669. show_elem($fieldssource, $key, $val, $var); // key is field number in source file
  670. //print '> '.$lefti.'-'.$key.'-'.$val;
  671. $listofkeys[$key] = 1;
  672. $fieldsplaced[$key] = 1;
  673. $valforsourcefieldnb[$lefti] = $key;
  674. $lefti++;
  675. if ($lefti > count($fieldstarget))
  676. break; // Other fields are in the not imported area
  677. }
  678. //var_dump($valforsourcefieldnb);
  679. // Complete source fields from count($fieldssource)+1 to count($fieldstarget)
  680. $more = 1;
  681. $num = count($fieldssource);
  682. while ($lefti <= $num) {
  683. $var = !$var;
  684. $newkey = getnewkey($fieldssource, $listofkeys);
  685. show_elem($fieldssource, $newkey, '', $var); // key start after field number in source file
  686. //print '> '.$lefti.'-'.$newkey;
  687. $listofkeys[$key] = 1;
  688. $lefti++;
  689. $more++;
  690. }
  691. print "</div>\n";
  692. print "<!-- End box container -->\n";
  693. print '</td><td width="50%">';
  694. // List of targets fields
  695. $i = 0;
  696. $var = true;
  697. $mandatoryfieldshavesource = true;
  698. print '<table width="100%" class="nobordernopadding">';
  699. foreach ($fieldstarget as $code => $label) {
  700. $var = !$var;
  701. print '<tr ' . $bc[$var] . ' height="20">';
  702. $i++;
  703. $entity = (!empty($objimport->array_import_entities[0][$code]) ? $objimport->array_import_entities[0][$code] : $objimport->array_import_icon[0]);
  704. $tablealias = preg_replace('/(\..*)$/i', '', $code);
  705. $tablename = $objimport->array_import_tables[0][$tablealias];
  706. $entityicon = $entitytoicon[$entity] ? $entitytoicon[$entity] : $entity;
  707. $entitylang = $entitytolang[$entity] ? $entitytolang[$entity] : $entity;
  708. print '<td nowrap="nowrap" style="font-weight: normal">=>' . img_object('', $entityicon) . ' ' . $langs->trans($entitylang) . '</td>';
  709. print '<td style="font-weight: normal">';
  710. $newlabel = preg_replace('/\*$/', '', $label);
  711. $text = $langs->trans($newlabel);
  712. $more = '';
  713. if (preg_match('/\*$/', $label)) {
  714. $text = '<span class="fieldrequired">' . $text . '</span>';
  715. $more = ((!empty($valforsourcefieldnb[$i]) && $valforsourcefieldnb[$i] <= count($fieldssource)) ? '' : img_warning($langs->trans("FieldNeedSource")));
  716. if ($mandatoryfieldshavesource)
  717. $mandatoryfieldshavesource = (!empty($valforsourcefieldnb[$i]) && ($valforsourcefieldnb[$i] <= count($fieldssource)));
  718. //print 'xx'.($i).'-'.$valforsourcefieldnb[$i].'-'.$mandatoryfieldshavesource;
  719. }
  720. print $text;
  721. print '</td>';
  722. // Info field
  723. print '<td style="font-weight: normal" align="right">';
  724. $filecolumn = $array_match_database_to_file[$code];
  725. // Source field info
  726. $htmltext = '<b><u>' . $langs->trans("FieldSource") . '</u></b><br>';
  727. if ($filecolumn > count($fieldssource))
  728. $htmltext.=$langs->trans("DataComeFromNoWhere") . '<br>';
  729. else {
  730. if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
  731. $filecolumntoshow = $filecolumn;
  732. $htmltext.=$langs->trans("DataComeFromFileFieldNb", $filecolumntoshow) . '<br>';
  733. } else {
  734. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref')
  735. $htmltext.=$langs->trans("DataComeFromIdFoundFromRef", $filecolumn, $langs->transnoentitiesnoconv($entitylang)) . '<br>';
  736. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid')
  737. $htmltext.=$langs->trans("DataComeFromIdFoundFromCodeId", $filecolumn, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])) . '<br>';
  738. }
  739. }
  740. $htmltext.=$langs->trans("SourceRequired") . ': <b>' . yn(preg_match('/\*$/', $label)) . '</b><br>';
  741. $example = $objimport->array_import_examplevalues[0][$code];
  742. if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
  743. if ($example)
  744. $htmltext.=$langs->trans("SourceExample") . ': <b>' . $example . '</b><br>';
  745. }
  746. else {
  747. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref')
  748. $htmltext.=$langs->trans("SourceExample") . ': <b>' . $langs->transnoentitiesnoconv("ExampleAnyRefFoundIntoElement", $entitylang) . ($example ? ' (' . $langs->transnoentitiesnoconv("Example") . ': ' . $example . ')' : '') . '</b><br>';
  749. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid')
  750. $htmltext.=$langs->trans("SourceExample") . ': <b>' . $langs->trans("ExampleAnyCodeOrIdFoundIntoDictionnary", $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$code]['dict'])) . ($example ? ' (' . $langs->transnoentitiesnoconv("Example") . ': ' . $example . ')' : '') . '</b><br>';
  751. }
  752. $htmltext.='<br>';
  753. // Target field info
  754. $htmltext.='<b><u>' . $langs->trans("FieldTarget") . '</u></b><br>';
  755. if (empty($objimport->array_import_convertvalue[0][$code])) { // If source file does not need convertion
  756. $htmltext.=$langs->trans("DataIsInsertedInto") . '<br>';
  757. } else {
  758. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromref')
  759. $htmltext.=$langs->trans("DataIDSourceIsInsertedInto") . '<br>';
  760. if ($objimport->array_import_convertvalue[0][$code]['rule'] == 'fetchidfromcodeid')
  761. $htmltext.=$langs->trans("DataCodeIDSourceIsInsertedInto") . '<br>';
  762. }
  763. $htmltext.=$langs->trans("FieldTitle") . ": <b>" . $langs->trans($newlabel) . "</b><br>";
  764. $htmltext.=$langs->trans("Table") . " -> " . $langs->trans("Field") . ': <b>' . $tablename . " -> " . preg_replace('/^.*\./', '', $code) . "</b><br>";
  765. print $form->textwithpicto($more, $htmltext);
  766. print '</td>';
  767. print '</tr>';
  768. $save_select.=$bit;
  769. }
  770. print '</table>';
  771. print '</td></tr>';
  772. // List of not imported fields
  773. print '<tr class="liste_titre"><td colspan="2">' . $langs->trans("NotImportedFields") . '</td></tr>';
  774. print '<tr valign="top"><td width="50%">';
  775. print "\n<!-- Box ignore container -->\n";
  776. print '<div id="right" class="connectedSortable">' . "\n";
  777. $nbofnotimportedfields = 0;
  778. foreach ($fieldssource as $key => $val) {
  779. if (empty($fieldsplaced[$key])) {
  780. //$var=!$var;
  781. $nbofnotimportedfields++;
  782. show_elem($fieldssource, $key, '', $var, 'nostyle');
  783. //print '> '.$lefti.'-'.$key;
  784. $listofkeys[$key] = 1;
  785. $lefti++;
  786. }
  787. }
  788. // Print one more empty field
  789. $newkey = getnewkey($fieldssource, $listofkeys);
  790. show_elem($fieldssource, $newkey, '', $var, 'nostyle');
  791. //print '> '.$lefti.'-'.$newkey;
  792. $listofkeys[$newkey] = 1;
  793. $nbofnotimportedfields++;
  794. print "</div>\n";
  795. print "<!-- End box ignore container -->\n";
  796. print '</td>';
  797. print '<td width="50%">';
  798. $i = 0;
  799. while ($i < $nbofnotimportedfields) {
  800. // Print empty cells
  801. show_elem('', '', 'none', $var, 'nostyle');
  802. $i++;
  803. }
  804. print '</td></tr>';
  805. print '</table>';
  806. dol_fiche_end();
  807. print '<script type="text/javascript" language="javascript">';
  808. print 'jQuery(function() {
  809. jQuery("#left, #right").sortable({
  810. /* placeholder: \'ui-state-highlight\', */
  811. handle: \'.boxhandle\',
  812. revert: \'invalid\',
  813. items: \'.box\',
  814. containment: \'.fiche\',
  815. connectWith: \'.connectedSortable\',
  816. stop: function(event, ui) {
  817. updateOrder();
  818. }
  819. });
  820. });
  821. ';
  822. print "\n";
  823. print 'function updateOrder(){' . "\n";
  824. print 'var left_list = cleanSerialize(jQuery("#left").sortable("serialize" ));' . "\n";
  825. //print 'var right_list = cleanSerialize(jQuery("#right").sortable("serialize" ));'."\n";
  826. print 'var boxorder = \'A:\' + left_list;' . "\n";
  827. //print 'var boxorder = \'A:\' + left_list + \'-B:\' + right_list;'."\n";
  828. //print 'alert(\'boxorder=\' + boxorder);';
  829. //print 'var userid = \''.$user->id.'\';'."\n";
  830. //print 'var datatoimport = "'.$datatoimport.'";'."\n";
  831. // print 'jQuery.ajax({ url: "ajaximport.php?step=4&boxorder=" + boxorder + "&userid=" + userid + "&datatoimport=" + datatoimport,
  832. // async: false
  833. // });'."\n";
  834. // Now reload page
  835. print 'var newlocation= \'' . $_SERVER["PHP_SELF"] . '?step=4' . $param . '&action=saveorder&boxorder=\' + boxorder;' . "\n";
  836. //print 'alert(newlocation);';
  837. print 'window.location.href=newlocation;' . "\n";
  838. print '}' . "\n";
  839. print '</script>' . "\n";
  840. if ($mesg)
  841. print $mesg;
  842. /*
  843. * Barre d'action
  844. */
  845. print '<div class="tabsAction">';
  846. if (count($array_match_file_to_database)) {
  847. if ($mandatoryfieldshavesource) {
  848. print '<a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?step=5' . $param . '&filetoimport=' . urlencode($filetoimport) . '">' . $langs->trans("NextStep") . '</a>';
  849. } else {
  850. print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryFieldHaveNoSource")) . '">' . $langs->trans("NextStep") . '</a>';
  851. }
  852. }
  853. print '</div>';
  854. // Area for profils import
  855. if (count($array_match_file_to_database)) {
  856. print '<br>' . "\n";
  857. print '<!-- Area to add new import profile -->' . "\n";
  858. print $langs->trans("SaveImportModel");
  859. print '<form class="nocellnopadd" action="' . $_SERVER["PHP_SELF"] . '" method="post">';
  860. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  861. print '<input type="hidden" name="action" value="add_import_model">';
  862. print '<input type="hidden" name="step" value="' . $step . '">';
  863. print '<input type="hidden" name="format" value="' . $format . '">';
  864. print '<input type="hidden" name="datatoimport" value="' . $datatoimport . '">';
  865. print '<input type="hidden" name="filetoimport" value="' . $filetoimport . '">';
  866. print '<input type="hidden" name="hexa" value="' . $hexa . '">';
  867. print '<input type="hidden" name="excludefirstline" value="' . $excludefirstline . '">';
  868. print '<input type="hidden" value="' . $separator . '" name="separator">';
  869. print '<input type="hidden" value="' . $enclosure . '" name="enclosure">';
  870. print '<table summary="selectofimportprofil" class="noborder" width="100%">';
  871. print '<tr class="liste_titre">';
  872. print '<td>' . $langs->trans("ImportModelName") . '</td>';
  873. print '<td>&nbsp;</td>';
  874. print '</tr>';
  875. $var = false;
  876. print '<tr ' . $bc[$var] . '>';
  877. print '<td><input name="import_name" size="48" value=""></td><td align="right">';
  878. print '<input type="submit" class="button" value="' . $langs->trans("SaveImportProfile") . '">';
  879. print '</td></tr>';
  880. // List of existing import profils
  881. $sql = "SELECT rowid, label";
  882. $sql.= " FROM " . MAIN_DB_PREFIX . "import_model";
  883. $sql.= " WHERE type = '" . $datatoimport . "'";
  884. $sql.= " ORDER BY rowid";
  885. $resql = $db->query($sql);
  886. if ($resql) {
  887. $num = $db->num_rows($resql);
  888. $i = 0;
  889. $var = false;
  890. while ($i < $num) {
  891. $var = !$var;
  892. $obj = $db->fetch_object($resql);
  893. print '<tr ' . $bc[$var] . '><td>';
  894. print $obj->label;
  895. print '</td><td align="right">';
  896. print '<a href="' . $_SERVER["PHP_SELF"] . '?step=' . $step . $param . '&action=deleteprof&id=' . $obj->rowid . '&filetoimport=' . urlencode($filetoimport) . '">';
  897. print img_delete();
  898. print '</a>';
  899. print '</tr>';
  900. $i++;
  901. }
  902. } else {
  903. dol_print_error($db);
  904. }
  905. print '</table>';
  906. print '</form>';
  907. }
  908. print end_box();
  909. print '</div></div>';
  910. }
  911. // STEP 5: Summary of choices and launch simulation
  912. if ($step == 5 && $datatoimport) {
  913. $model = $format;
  914. $liste = $objmodelimport->liste_modeles($db);
  915. // Create classe to use for import
  916. $dir = DOL_DOCUMENT_ROOT . "/import/core/modules/import/";
  917. $file = "import_" . $model . ".modules.php";
  918. $classname = "Import" . ucfirst($model);
  919. require_once $dir . $file;
  920. $obj = new $classname($db, $datatoimport);
  921. if ($model == 'csv') {
  922. $obj->separator = $separator;
  923. $obj->enclosure = $enclosure;
  924. }
  925. // Load source fields in input file
  926. $fieldssource = array();
  927. $result = $obj->import_open_file($conf->import->dir_temp . '/' . $filetoimport, $langs);
  928. if ($result >= 0) {
  929. // Read first line
  930. $arrayrecord = $obj->import_read_record();
  931. // Put into array fieldssource starting with 1.
  932. $i = 1;
  933. foreach ($arrayrecord as $key => $val) {
  934. $fieldssource[$i]['example1'] = dol_trunc($val['val'], 24);
  935. $i++;
  936. }
  937. $obj->import_close_file();
  938. }
  939. $nboflines = dol_count_nb_of_line($conf->import->dir_temp . '/' . $filetoimport);
  940. $param = '&leftmenu=import&format=' . $format . '&datatoimport=' . $datatoimport . '&filetoimport=' . urlencode($filetoimport) . '&nboflines=' . $nboflines . '&separator=' . urlencode($separator) . '&enclosure=' . urlencode($enclosure);
  941. $param2 = $param;
  942. if ($excludefirstline) {
  943. $param.='&excludefirstline=1';
  944. }
  945. llxHeader('', $langs->trans("NewImport"), 'EN:Module_Imports_En|FR:Module_Imports|ES:M&oacute;dulo_Importaciones');
  946. print_fiche_titre($langs->trans("ImportArea"));
  947. print '<div class="with-padding">';
  948. print '<div class="columns">';
  949. print start_box($langs->trans("NewImport"), "twelve", "16-User.png", false);
  950. $head = import_prepare_head($param, 5);
  951. dol_fiche_head($head, 'step5', $langs->trans("NewImport"));
  952. print '<table width="100%" class="border">';
  953. // Module
  954. print '<tr><td width="25%">' . $langs->trans("Module") . '</td>';
  955. print '<td>';
  956. //print img_object($objimport->array_import_module[0]->getName(),$objimport->array_import_module[0]->picto).' ';
  957. print $objimport->array_import_module[0]->getName();
  958. print '</td></tr>';
  959. // Lot de donnees a importer
  960. print '<tr><td>' . $langs->trans("DatasetToImport") . '</td>';
  961. print '<td>';
  962. print img_object($objimport->array_import_module[0]->getName(), $objimport->array_import_icon[0]) . ' ';
  963. print $objimport->array_import_label[0];
  964. print '</td></tr>';
  965. print '</table><br>';
  966. print '<b>' . $langs->trans("InformationOnSourceFile") . '</b>';
  967. print '<table width="100%" class="border">';
  968. //print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnSourceFile").'</b></td></tr>';
  969. // Source file format
  970. print '<tr><td width="25%">' . $langs->trans("SourceFileFormat") . '</td>';
  971. print '<td>';
  972. $text = $objmodelimport->getDriverDesc($format);
  973. print $form->textwithpicto($objmodelimport->getDriverLabel($format), $text);
  974. print '</td></tr>';
  975. // File to import
  976. print '<tr><td>' . $langs->trans("FileToImport") . '</td>';
  977. print '<td>';
  978. $modulepart = 'import';
  979. $relativepath = GETPOST('filetoimport');
  980. print '<a href="' . DOL_URL_ROOT . '/document.php?modulepart=' . $modulepart . '&file=' . urlencode($relativepath) . '&step=4' . $param . '" target="_blank">';
  981. print $filetoimport;
  982. print '</a>';
  983. print '</td></tr>';
  984. // Nb of fields
  985. print '<tr><td>';
  986. print $langs->trans("NbOfSourceLines");
  987. print '</td><td>';
  988. print $nboflines;
  989. print '</td></tr>';
  990. // Checkbox do not import first line
  991. print '<tr><td>';
  992. print $langs->trans("Option");
  993. print '</td><td>';
  994. print '<input type="checkbox" name="excludefirstline" value="1"';
  995. print ($excludefirstline ? ' checked="checked"' : '');
  996. print ' onClick="javascript: window.location=\'' . $_SERVER["PHP_SELF"] . '?step=5&excludefirstline=' . ($excludefirstline ? '0' : '1') . $param2 . '\';">';
  997. print ' ' . $langs->trans("DoNotImportFirstLine");
  998. print '</td></tr>';
  999. print '</table>';
  1000. print '<br>';
  1001. print '<b>' . $langs->trans("InformationOnTargetTables") . '</b>';
  1002. print '<table width="100%" class="border">';
  1003. //print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnTargetTables").'</b></td></tr>';
  1004. // Tables imported
  1005. print '<tr><td width="25%">';
  1006. print $langs->trans("TablesTarget");
  1007. print '</td><td>';
  1008. $listtables = array();
  1009. $sort_array_match_file_to_database = $array_match_file_to_database;
  1010. foreach ($array_match_file_to_database as $code => $label) {
  1011. //var_dump($fieldssource);
  1012. if ($code > count($fieldssource))
  1013. continue;
  1014. //print $code.'-'.$label;
  1015. $alias = preg_replace('/(\..*)$/i', '', $label);
  1016. $listtables[$alias] = $objimport->array_import_tables[0][$alias];
  1017. }
  1018. if (count($listtables)) {
  1019. $newval = '';
  1020. //ksort($listtables);
  1021. foreach ($listtables as $val) {
  1022. if ($newval)
  1023. print ', ';
  1024. $newval = $val;
  1025. print $newval;
  1026. }
  1027. }
  1028. else
  1029. print $langs->trans("Error");
  1030. print '</td></tr>';
  1031. // Fields imported
  1032. print '<tr><td>';
  1033. print $langs->trans("FieldsTarget") . '</td><td>';
  1034. $listfields = array();
  1035. $i = 0;
  1036. //print 'fieldsource='.$fieldssource;
  1037. $sort_array_match_file_to_database = $array_match_file_to_database;
  1038. ksort($sort_array_match_file_to_database);
  1039. //var_dump($sort_array_match_file_to_database);
  1040. foreach ($sort_array_match_file_to_database as $code => $label) {
  1041. $i++;

Large files files are truncated, but you can click here to view the full file