emptyexample.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. require '../main.inc.php';
  46. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  47. require_once DOL_DOCUMENT_ROOT.'/imports/class/import.class.php';
  48. require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.php';
  49. $datatoimport = GETPOST('datatoimport');
  50. $format = GETPOST('format');
  51. // Load translation files required by the page
  52. $langs->load("exports");
  53. // Check exportkey
  54. if (empty($datatoimport)) {
  55. $user->getrights();
  56. llxHeader();
  57. print '<div class="error">Bad value for datatoimport.</div>';
  58. llxFooter();
  59. exit;
  60. }
  61. $filename = $langs->trans("ExampleOfImportFile").'_'.$datatoimport.'.'.$format;
  62. $objimport = new Import($db);
  63. $objimport->load_arrays($user, $datatoimport);
  64. // Load arrays from descriptor module
  65. $fieldstarget = $objimport->array_import_fields[0];
  66. $valuestarget = $objimport->array_import_examplevalues[0];
  67. $attachment = true;
  68. if (isset($_GET["attachment"])) {
  69. $attachment = $_GET["attachment"];
  70. }
  71. //$attachment = false;
  72. $contenttype = dol_mimetype($format);
  73. if (isset($_GET["contenttype"])) {
  74. $contenttype = $_GET["contenttype"];
  75. }
  76. //$contenttype='text/plain';
  77. $outputencoding = 'UTF-8';
  78. if ($contenttype) {
  79. header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
  80. }
  81. if ($attachment) {
  82. header('Content-Disposition: attachment; filename="'.$filename.'"');
  83. }
  84. // List of targets fields
  85. $headerlinefields = array(); // Array of fields (label to show)
  86. $contentlinevalues = array(); // Array of example values
  87. $i = 0;
  88. foreach ($fieldstarget as $code => $label) {
  89. $withoutstar = preg_replace('/\*/', '', $fieldstarget[$code]);
  90. $headerlinefields[] = $langs->transnoentities($withoutstar).($withoutstar != $fieldstarget[$code] ? '*' : '').' ('.$code.')';
  91. $contentlinevalues[] = (isset($valuestarget[$code]) ? $valuestarget[$code] : '');
  92. }
  93. //var_dump($headerlinefields);
  94. //var_dump($contentlinevalues);
  95. print $objimport->build_example_file($format, $headerlinefields, $contentlinevalues, $datatoimport);