modules_action.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. * or see https://www.gnu.org/
  21. */
  22. require_once DOL_DOCUMENT_ROOT.'/core/class/commondocgenerator.class.php';
  23. /**
  24. * \class ModeleAction
  25. * \brief Parent class for product models of doc generators
  26. */
  27. abstract class ModeleAction extends CommonDocGenerator
  28. {
  29. /**
  30. * @var string Error code (or message)
  31. */
  32. public $error = '';
  33. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  34. /**
  35. * Return list of active generation modules
  36. *
  37. * @param DoliDB $db Database handler
  38. * @param integer $maxfilenamelength Max length of value to show
  39. * @return array List of templates
  40. */
  41. public static function liste_modeles($db, $maxfilenamelength = 0)
  42. {
  43. // phpcs:enable
  44. global $conf;
  45. $type = 'action';
  46. $list = array();
  47. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  48. $list = getListOfModels($db, $type, $maxfilenamelength);
  49. return $list;
  50. }
  51. }
  52. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  53. /**
  54. * Create a product document on disk using template defined into PRODUCT_ADDON_PDF
  55. *
  56. * @param DoliDB $db data base object
  57. * @param Object $object Object fichinter
  58. * @param string $modele forces the model to use ('' by default)
  59. * @param Translate $outputlangs lang object to use for translation
  60. * @param int $hidedetails Hide details of lines
  61. * @param int $hidedesc Hide description
  62. * @param int $hideref Hide ref
  63. * @return int 0 if KO, 1 if OK
  64. */
  65. function action_create($db, $object, $modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  66. {
  67. // phpcs:enable
  68. global $conf, $langs, $user;
  69. $langs->load("action");
  70. $error = 0;
  71. $srctemplatepath = '';
  72. // Position modele on the name of fichinter model to use
  73. if (!dol_strlen($modele)) {
  74. if (!empty($conf->global->ACTION_EVENT_ADDON_PDF)) {
  75. $modele = $conf->global->ACTION_EVENT_ADDON_PDF;
  76. } else {
  77. $modele = 'soleil';
  78. }
  79. }
  80. // If selected modele is a filename template (then $modele="modelname:filename")
  81. $tmp = explode(':', $modele, 2);
  82. if (!empty($tmp[1])) {
  83. $modele = $tmp[0];
  84. $srctemplatepath = $tmp[1];
  85. }
  86. // Search template files
  87. $file = '';
  88. $classname = '';
  89. $filefound = 0;
  90. $dirmodels = array('/');
  91. if (is_array($conf->modules_parts['models'])) {
  92. $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
  93. }
  94. foreach ($dirmodels as $reldir) {
  95. foreach (array('doc', 'pdf') as $prefix) {
  96. $file = $prefix."_".$modele.".modules.php";
  97. // On verifie l'emplacement du modele
  98. $file = dol_buildpath($reldir."core/modules/action/doc/".$file, 0);
  99. if (file_exists($file)) {
  100. $filefound = 1;
  101. $classname = $prefix.'_'.$modele;
  102. break;
  103. }
  104. }
  105. if ($filefound) {
  106. break;
  107. }
  108. }
  109. // Charge le modele
  110. if ($filefound) {
  111. require_once $file;
  112. $obj = new $classname($db);
  113. // We save charset_output to restore it because write_file can change it if needed for
  114. // output format that does not support UTF8.
  115. $sav_charset_output = $outputlangs->charset_output;
  116. if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
  117. $outputlangs->charset_output = $sav_charset_output;
  118. // We delete old preview
  119. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  120. dol_delete_preview($object);
  121. return 1;
  122. } else {
  123. $outputlangs->charset_output = $sav_charset_output;
  124. dol_print_error($db, "action_pdf_create Error: ".$obj->error);
  125. return 0;
  126. }
  127. } else {
  128. print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
  129. return 0;
  130. }
  131. }