modules_cards.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/member/modules_cards.php
  23. * \ingroup member
  24. * \brief File of parent class of document generator for members cards.
  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 members cards.
  30. */
  31. class ModelePDFCards
  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 static function liste_modeles($db, $maxfilenamelength = 0)
  46. {
  47. // phpcs:enable
  48. global $conf;
  49. $type = 'member';
  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. * Cree un fichier de cartes de visites en fonction du modele de ADHERENT_CARDS_ADDON_PDF
  59. *
  60. * @param DoliDB $db Database handler
  61. * @param array $arrayofmembers Array of members
  62. * @param string $modele Force modele to use ('' to not force)
  63. * @param Translate $outputlangs Object langs to use for translation
  64. * @param string $outputdir Output directory
  65. * @param string $template pdf generenate document class to use default 'standard'
  66. * @param string $filename Name of output file (without extension)
  67. * @return int <0 if KO, >0 if OK
  68. */
  69. function members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs, $outputdir = '', $template = 'standard', $filename = 'tmp_cards')
  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_CARDS_ADDON_PDF)) {
  85. $code = $conf->global->ADHERENT_CARDS_ADDON_PDF;
  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. // Search template files
  101. $file = '';
  102. $classname = '';
  103. $filefound = 0;
  104. $dirmodels = array('/');
  105. if (is_array($conf->modules_parts['models'])) {
  106. $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
  107. }
  108. foreach ($dirmodels as $reldir) {
  109. foreach (array('doc', 'pdf') as $prefix) {
  110. $file = $prefix."_".$template.".class.php";
  111. // We check that file of doc generaotr exists
  112. $file = dol_buildpath($reldir."core/modules/member/doc/".$file, 0);
  113. if (file_exists($file)) {
  114. $filefound = 1;
  115. $classname = $prefix.'_'.$template;
  116. break;
  117. }
  118. }
  119. if ($filefound) {
  120. break;
  121. }
  122. }
  123. // Charge le modele
  124. if ($filefound) {
  125. require_once $file;
  126. $obj = new $classname($db);
  127. // We save charset_output to restore it because write_file can change it if needed for
  128. // output format that does not support UTF8.
  129. $sav_charset_output = $outputlangs->charset_output;
  130. if ($obj->write_file($arrayofmembers, $outputlangs, $srctemplatepath, 'member', 0, $filename) > 0) {
  131. $outputlangs->charset_output = $sav_charset_output;
  132. return 1;
  133. } else {
  134. $outputlangs->charset_output = $sav_charset_output;
  135. dol_print_error($db, "members_card_pdf_create Error: ".$obj->error);
  136. return -1;
  137. }
  138. } else {
  139. dol_print_error('', $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file));
  140. return -1;
  141. }
  142. }