modules_labels.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. * or see https://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/modules/printsheet/modules_labels.php
  23. * \ingroup member
  24. * \brief File of parent class of document generator for members labels sheets.
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. /**
  29. * Parent class of document generator for address sheet.
  30. */
  31. class ModelePDFLabels
  32. {
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error = '';
  37. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  38. /**
  39. * Return list of active generation modules
  40. *
  41. * @param DoliDB $db Database handler
  42. * @param integer $maxfilenamelength Max length of value to show
  43. * @return array List of templates
  44. */
  45. public function liste_modeles($db, $maxfilenamelength = 0)
  46. {
  47. // phpcs:enable
  48. global $conf;
  49. $type = 'members_labels';
  50. $list = array();
  51. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  52. $list = getListOfModels($db, $type, $maxfilenamelength);
  53. return $list;
  54. }
  55. }
  56. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  57. /**
  58. * Create a document onto disk according to template module.
  59. *
  60. * @param DoliDB $db Database handler
  61. * @param array $arrayofrecords Array of records
  62. * @param string $modele Force le modele a utiliser ('' to not force)
  63. * @param Translate $outputlangs Objet lang a utiliser pour traduction
  64. * @param string $outputdir Output directory
  65. * @param string $template pdf generenate document class to use default 'standardlabel'
  66. * @param string $filename Short file name of PDF output file
  67. * @return int <0 if KO, >0 if OK
  68. */
  69. function doc_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir = '', $template = 'standardlabel', $filename = 'tmp_address_sheet.pdf')
  70. {
  71. // phpcs:enable
  72. global $conf, $langs;
  73. $langs->load("members");
  74. $error = 0;
  75. // Increase limit for PDF build
  76. $err = error_reporting();
  77. error_reporting(0);
  78. @set_time_limit(120);
  79. error_reporting($err);
  80. $code = '';
  81. $srctemplatepath = '';
  82. // Positionne le modele sur le nom du modele a utiliser
  83. if (!dol_strlen($modele)) {
  84. if (!empty($conf->global->ADHERENT_ETIQUETTE_TYPE)) {
  85. $code = $conf->global->ADHERENT_ETIQUETTE_TYPE;
  86. } else {
  87. $code = $modele;
  88. }
  89. } else {
  90. $code = $modele;
  91. }
  92. // If selected modele is a filename template (then $modele="modelname:filename")
  93. $tmp = explode(':', $template, 2);
  94. if (!empty($tmp[1])) {
  95. $template = $tmp[0];
  96. $srctemplatepath = $tmp[1];
  97. } else {
  98. $srctemplatepath = $code;
  99. }
  100. dol_syslog("modele=".$modele." outputdir=".$outputdir." template=".$template." code=".$code." srctemplatepath=".$srctemplatepath." filename=".$filename, LOG_DEBUG);
  101. // Search template files
  102. $file = '';
  103. $classname = '';
  104. $filefound = 0;
  105. $dirmodels = array('/');
  106. if (is_array($conf->modules_parts['models'])) {
  107. $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
  108. }
  109. foreach ($dirmodels as $reldir) {
  110. foreach (array('doc', 'pdf') as $prefix) {
  111. $file = $prefix."_".$template.".class.php";
  112. // On verifie l'emplacement du modele
  113. $file = dol_buildpath($reldir."core/modules/printsheet/doc/".$file, 0);
  114. if (file_exists($file)) {
  115. $filefound = 1;
  116. $classname = $prefix.'_'.$template;
  117. break;
  118. }
  119. }
  120. if ($filefound) {
  121. break;
  122. }
  123. }
  124. // Charge le modele
  125. if ($filefound) {
  126. require_once $file;
  127. $obj = new $classname($db);
  128. // We save charset_output to restore it because write_file can change it if needed for
  129. // output format that does not support UTF8.
  130. $sav_charset_output = $outputlangs->charset_output;
  131. if ($obj->write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir, $filename) > 0) {
  132. $outputlangs->charset_output = $sav_charset_output;
  133. $fullpath = $obj->result['fullpath'];
  134. // Output to http stream
  135. clearstatcache();
  136. $attachment = true;
  137. if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) {
  138. $attachment = false;
  139. }
  140. $type = dol_mimetype($filename);
  141. //if ($encoding) header('Content-Encoding: '.$encoding);
  142. if ($type) {
  143. header('Content-Type: '.$type);
  144. }
  145. if ($attachment) {
  146. header('Content-Disposition: attachment; filename="'.$filename.'"');
  147. } else {
  148. header('Content-Disposition: inline; filename="'.$filename.'"');
  149. }
  150. // Ajout directives pour resoudre bug IE
  151. header('Cache-Control: Public, must-revalidate');
  152. header('Pragma: public');
  153. readfile($fullpath);
  154. return 1;
  155. } else {
  156. $outputlangs->charset_output = $sav_charset_output;
  157. dol_print_error($db, "doc_label_pdf_create Error: ".$obj->error);
  158. return -1;
  159. }
  160. } else {
  161. dol_print_error('', $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file));
  162. return -1;
  163. }
  164. }