emptyexample.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $entity = $objimport->array_import_entities[0][$code];
  66. $entityicon = $entitytoicon[$entity] ? $entitytoicon[$entity] : $entity;
  67. $entitylang = $entitytolang[$entity] ? $entitytolang[$entity] : $entity;
  68. $fieldstarget = $objimport->array_import_fields[0];
  69. $valuestarget = $objimport->array_import_examplevalues[0];
  70. $attachment = true;
  71. if (isset($_GET["attachment"])) {
  72. $attachment = $_GET["attachment"];
  73. }
  74. //$attachment = false;
  75. $contenttype = dol_mimetype($format);
  76. if (isset($_GET["contenttype"])) {
  77. $contenttype = $_GET["contenttype"];
  78. }
  79. //$contenttype='text/plain';
  80. $outputencoding = 'UTF-8';
  81. if ($contenttype) {
  82. header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
  83. }
  84. if ($attachment) {
  85. header('Content-Disposition: attachment; filename="'.$filename.'"');
  86. }
  87. // List of targets fields
  88. $headerlinefields = array(); // Array of fields (label to show)
  89. $contentlinevalues = array(); // Array of example values
  90. $i = 0;
  91. foreach ($fieldstarget as $code => $label) {
  92. $withoutstar = preg_replace('/\*/', '', $fieldstarget[$code]);
  93. $headerlinefields[] = $langs->transnoentities($withoutstar).($withoutstar != $fieldstarget[$code] ? '*' : '').' ('.$code.')';
  94. $contentlinevalues[] = (isset($valuestarget[$code]) ? $valuestarget[$code] : '');
  95. }
  96. //var_dump($headerlinefields);
  97. //var_dump($contentlinevalues);
  98. print $objimport->build_example_file($format, $headerlinefields, $contentlinevalues, $datatoimport);