actions_builddoc.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /* Copyright (C) 2015 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. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/actions_builddoc.inc.php
  20. * \brief Code for actions on building or deleting documents
  21. */
  22. // $action must be defined
  23. // $id must be defined
  24. // $object must be defined and must have a method generateDocument().
  25. // $permissiontoadd must be defined
  26. // $upload_dir must be defined (example $conf->project->dir_output . "/";)
  27. // $hidedetails, $hidedesc, $hideref and $moreparams may have been set or not.
  28. if (!empty($permissioncreate) && empty($permissiontoadd)) {
  29. $permissiontoadd = $permissioncreate; // For backward compatibility
  30. }
  31. // Build doc
  32. if ($action == 'builddoc' && $permissiontoadd) {
  33. if (is_numeric(GETPOST('model', 'alpha'))) {
  34. $error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Model"));
  35. } else {
  36. // Reload to get all modified line records and be ready for hooks
  37. $ret = $object->fetch($id);
  38. $ret = $object->fetch_thirdparty();
  39. /*if (empty($object->id) || ! $object->id > 0)
  40. {
  41. dol_print_error('Object must have been loaded by a fetch');
  42. exit;
  43. }*/
  44. // Save last template used to generate document
  45. if (GETPOST('model', 'alpha')) {
  46. $object->setDocModel($user, GETPOST('model', 'alpha'));
  47. }
  48. // Special case to force bank account
  49. //if (property_exists($object, 'fk_bank'))
  50. //{
  51. if (GETPOST('fk_bank', 'int')) {
  52. // this field may come from an external module
  53. $object->fk_bank = GETPOST('fk_bank', 'int');
  54. } elseif (!empty($object->fk_account)) {
  55. $object->fk_bank = $object->fk_account;
  56. }
  57. //}
  58. $outputlangs = $langs;
  59. $newlang = '';
  60. if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  61. $newlang = GETPOST('lang_id', 'aZ09');
  62. }
  63. if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && isset($object->thirdparty->default_lang)) {
  64. $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
  65. }
  66. if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && isset($object->default_lang)) {
  67. $newlang = $object->default_lang; // for thirdparty
  68. }
  69. if (!empty($newlang)) {
  70. $outputlangs = new Translate("", $conf);
  71. $outputlangs->setDefaultLang($newlang);
  72. }
  73. // To be sure vars is defined
  74. if (empty($hidedetails)) {
  75. $hidedetails = 0;
  76. }
  77. if (empty($hidedesc)) {
  78. $hidedesc = 0;
  79. }
  80. if (empty($hideref)) {
  81. $hideref = 0;
  82. }
  83. if (empty($moreparams)) {
  84. $moreparams = null;
  85. }
  86. $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  87. if ($result <= 0) {
  88. setEventMessages($object->error, $object->errors, 'errors');
  89. $action = '';
  90. } else {
  91. if (empty($donotredirect)) { // This is set when include is done by bulk action "Bill Orders"
  92. setEventMessages($langs->trans("FileGenerated"), null);
  93. /*$urltoredirect = $_SERVER['REQUEST_URI'];
  94. $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
  95. $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
  96. header('Location: '.$urltoredirect.'#builddoc');
  97. exit;*/
  98. }
  99. }
  100. }
  101. }
  102. // Delete file in doc form
  103. if ($action == 'remove_file' && $permissiontoadd) {
  104. if (!empty($upload_dir)) {
  105. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  106. if (empty($object->id) || !$object->id > 0) {
  107. // Reload to get all modified line records and be ready for hooks
  108. $ret = $object->fetch($id);
  109. $ret = $object->fetch_thirdparty();
  110. }
  111. $langs->load("other");
  112. $filetodelete = GETPOST('file', 'alpha');
  113. $file = $upload_dir.'/'.$filetodelete;
  114. $dirthumb = dirname($file).'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
  115. $ret = dol_delete_file($file, 0, 0, 0, $object);
  116. if ($ret) {
  117. // If it exists, remove thumb.
  118. $regs = array();
  119. if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i', $file, $regs)) {
  120. $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
  121. if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
  122. dol_delete_file($dirthumb.$photo_vignette);
  123. }
  124. $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
  125. if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
  126. dol_delete_file($dirthumb.$photo_vignette);
  127. }
  128. }
  129. setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
  130. } else {
  131. setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
  132. }
  133. // Make a redirect to avoid to keep the remove_file into the url that create side effects
  134. $urltoredirect = $_SERVER['REQUEST_URI'];
  135. $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
  136. $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
  137. header('Location: '.$urltoredirect);
  138. exit;
  139. } else {
  140. setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
  141. }
  142. }