emptyexample.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /* Copyright (C) 2009-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/imports/emptyexample.php
  19. * \ingroup import
  20. * \brief Show example of import file
  21. */
  22. if (!defined('NOTOKENRENEWAL')) {
  23. define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  24. }
  25. /**
  26. * This file is a wrapper, so empty header
  27. *
  28. * @ignore
  29. * @return void
  30. */
  31. function llxHeader()
  32. {
  33. print '<html><title>Build an import example file</title><body>';
  34. }
  35. /**
  36. * This file is a wrapper, so empty footer
  37. *
  38. * @ignore
  39. * @return void
  40. */
  41. function llxFooter()
  42. {
  43. print '</body></html>';
  44. }
  45. // Load Dolibarr environment
  46. require '../main.inc.php';
  47. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  48. require_once DOL_DOCUMENT_ROOT.'/imports/class/import.class.php';
  49. require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.php';
  50. $datatoimport = GETPOST('datatoimport');
  51. $format = GETPOST('format');
  52. // Load translation files required by the page
  53. $langs->load("exports");
  54. // Check exportkey
  55. if (empty($datatoimport)) {
  56. $user->getrights();
  57. llxHeader();
  58. print '<div class="error">Bad value for datatoimport.</div>';
  59. llxFooter();
  60. exit;
  61. }
  62. $filename = $langs->trans("ExampleOfImportFile").'_'.$datatoimport.'.'.$format;
  63. $objimport = new Import($db);
  64. $objimport->load_arrays($user, $datatoimport);
  65. // Load arrays from descriptor module
  66. $fieldstarget = $objimport->array_import_fields[0];
  67. $valuestarget = $objimport->array_import_examplevalues[0];
  68. $attachment = true;
  69. if (isset($_GET["attachment"])) {
  70. $attachment = $_GET["attachment"];
  71. }
  72. //$attachment = false;
  73. $contenttype = dol_mimetype($format);
  74. if (isset($_GET["contenttype"])) {
  75. $contenttype = $_GET["contenttype"];
  76. }
  77. //$contenttype='text/plain';
  78. $outputencoding = 'UTF-8';
  79. if ($contenttype) {
  80. header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
  81. }
  82. if ($attachment) {
  83. header('Content-Disposition: attachment; filename="'.$filename.'"');
  84. }
  85. // List of targets fields
  86. $headerlinefields = array(); // Array of fields (label to show)
  87. $contentlinevalues = array(); // Array of example values
  88. $i = 0;
  89. foreach ($fieldstarget as $code => $label) {
  90. $withoutstar = preg_replace('/\*/', '', $fieldstarget[$code]);
  91. $headerlinefields[] = $langs->transnoentities($withoutstar).($withoutstar != $fieldstarget[$code] ? '*' : '').' ('.$code.')';
  92. $contentlinevalues[] = (isset($valuestarget[$code]) ? $valuestarget[$code] : '');
  93. }
  94. //var_dump($headerlinefields);
  95. //var_dump($contentlinevalues);
  96. print $objimport->build_example_file($format, $headerlinefields, $contentlinevalues, $datatoimport);