actions_builddoc.inc.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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->projet->dir_output . "/";)
  27. // $hidedetails, $hidedesc, $hideref and $moreparams may have been set or not.
  28. if (!empty($permissioncreate) && empty($permissiontoadd)) $permissiontoadd = $permissioncreate; // For backward compatibility
  29. // Build doc
  30. if ($action == 'builddoc' && $permissiontoadd)
  31. {
  32. if (is_numeric(GETPOST('model', 'alpha')))
  33. {
  34. $error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Model"));
  35. }
  36. else
  37. {
  38. // Reload to get all modified line records and be ready for hooks
  39. $ret = $object->fetch($id);
  40. $ret = $object->fetch_thirdparty();
  41. /*if (empty($object->id) || ! $object->id > 0)
  42. {
  43. dol_print_error('Object must have been loaded by a fetch');
  44. exit;
  45. }*/
  46. // Save last template used to generate document
  47. if (GETPOST('model', 'alpha'))
  48. {
  49. $object->setDocModel($user, GETPOST('model', 'alpha'));
  50. }
  51. // Special case to force bank account
  52. //if (property_exists($object, 'fk_bank'))
  53. //{
  54. if (GETPOST('fk_bank', 'int')) {
  55. // this field may come from an external module
  56. $object->fk_bank = GETPOST('fk_bank', 'int');
  57. } elseif (!empty($object->fk_account)) {
  58. $object->fk_bank = $object->fk_account;
  59. }
  60. //}
  61. $outputlangs = $langs;
  62. $newlang = '';
  63. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
  64. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->thirdparty->default_lang)) $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
  65. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->default_lang)) $newlang = $object->default_lang; // for thirdparty
  66. if (!empty($newlang))
  67. {
  68. $outputlangs = new Translate("", $conf);
  69. $outputlangs->setDefaultLang($newlang);
  70. }
  71. // To be sure vars is defined
  72. if (empty($hidedetails)) $hidedetails = 0;
  73. if (empty($hidedesc)) $hidedesc = 0;
  74. if (empty($hideref)) $hideref = 0;
  75. if (empty($moreparams)) $moreparams = null;
  76. $result = $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  77. if ($result <= 0)
  78. {
  79. setEventMessages($object->error, $object->errors, 'errors');
  80. $action = '';
  81. }
  82. else
  83. {
  84. if (empty($donotredirect)) // This is set when include is done by bulk action "Bill Orders"
  85. {
  86. setEventMessages($langs->trans("FileGenerated"), null);
  87. $urltoredirect = $_SERVER['REQUEST_URI'];
  88. $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
  89. $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
  90. header('Location: '.$urltoredirect.'#builddoc');
  91. exit;
  92. }
  93. }
  94. }
  95. }
  96. // Delete file in doc form
  97. if ($action == 'remove_file' && $permissiontoadd)
  98. {
  99. if (!empty($upload_dir)) {
  100. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  101. if (empty($object->id) || !$object->id > 0)
  102. {
  103. // Reload to get all modified line records and be ready for hooks
  104. $ret = $object->fetch($id);
  105. $ret = $object->fetch_thirdparty();
  106. }
  107. $langs->load("other");
  108. $filetodelete = GETPOST('file', 'alpha');
  109. $file = $upload_dir.'/'.$filetodelete;
  110. $ret = dol_delete_file($file, 0, 0, 0, $object);
  111. if ($ret) setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
  112. else setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
  113. // Make a redirect to avoid to keep the remove_file into the url that create side effects
  114. $urltoredirect = $_SERVER['REQUEST_URI'];
  115. $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
  116. $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
  117. header('Location: '.$urltoredirect);
  118. exit;
  119. }
  120. else {
  121. setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
  122. }
  123. }