actions_builddoc.inc.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <http://www.gnu.org/licenses/>.
  16. * or see http://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. // $permissioncreate 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. // Build doc
  29. if ($action == 'builddoc' && $permissioncreate)
  30. {
  31. if (is_numeric(GETPOST('model')))
  32. {
  33. $error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Model"));
  34. }
  35. else
  36. {
  37. // Reload to get all modified line records and be ready for hooks
  38. $ret = $object->fetch($id);
  39. $ret = $object->fetch_thirdparty();
  40. /*if (empty($object->id) || ! $object->id > 0)
  41. {
  42. dol_print_error('Object must have been loaded by a fetch');
  43. exit;
  44. }*/
  45. // Save last template used to generate document
  46. if (GETPOST('model'))
  47. {
  48. $object->setDocModel($user, GETPOST('model','alpha'));
  49. }
  50. // Special case to force bank account
  51. //if (property_exists($object, 'fk_bank'))
  52. //{
  53. if (GETPOST('fk_bank')) { // this field may come from an external module
  54. $object->fk_bank = GETPOST('fk_bank');
  55. } else if (! empty($object->fk_account)) {
  56. $object->fk_bank = $object->fk_account;
  57. }
  58. //}
  59. $outputlangs = $langs;
  60. $newlang='';
  61. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang=GETPOST('lang_id');
  62. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->thirdparty->default_lang)) $newlang=$object->thirdparty->default_lang; // for proposal, order, invoice, ...
  63. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->default_lang)) $newlang=$object->default_lang; // for thirdparty
  64. if (! empty($newlang))
  65. {
  66. $outputlangs = new Translate("",$conf);
  67. $outputlangs->setDefaultLang($newlang);
  68. }
  69. // To be sure vars is defined
  70. if (empty($hidedetails)) $hidedetails=0;
  71. if (empty($hidedesc)) $hidedesc=0;
  72. if (empty($hideref)) $hideref=0;
  73. if (empty($moreparams)) $moreparams=null;
  74. $result= $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  75. if ($result <= 0)
  76. {
  77. setEventMessages($object->error, $object->errors, 'errors');
  78. $action='';
  79. }
  80. else
  81. {
  82. setEventMessages($langs->trans("FileGenerated"), null);
  83. header('Location: '.$_SERVER['REQUEST_URI'].'#builddoc');
  84. exit;
  85. }
  86. }
  87. }
  88. // Delete file in doc form
  89. if ($action == 'remove_file' && $permissioncreate)
  90. {
  91. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  92. if (empty($object->id) || ! $object->id > 0)
  93. {
  94. // Reload to get all modified line records and be ready for hooks
  95. $ret = $object->fetch($id);
  96. $ret = $object->fetch_thirdparty();
  97. }
  98. $langs->load("other");
  99. $filetodelete=GETPOST('file','alpha');
  100. $file = $upload_dir . '/' . $filetodelete;
  101. $ret=dol_delete_file($file,0,0,0,$object);
  102. if ($ret) setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
  103. else setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
  104. }