dolibarr  13.0.2
actions_builddoc.inc.php
Go to the documentation of this file.
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 
25 // $action must be defined
26 // $id must be defined
27 // $object must be defined and must have a method generateDocument().
28 // $permissiontoadd must be defined
29 // $upload_dir must be defined (example $conf->projet->dir_output . "/";)
30 // $hidedetails, $hidedesc, $hideref and $moreparams may have been set or not.
31 
32 if (!empty($permissioncreate) && empty($permissiontoadd)) $permissiontoadd = $permissioncreate; // For backward compatibility
33 
34 // Build doc
35 if ($action == 'builddoc' && $permissiontoadd)
36 {
37  if (is_numeric(GETPOST('model', 'alpha')))
38  {
39  $error = $langs->trans("ErrorFieldRequired", $langs->transnoentities("Model"));
40  } else {
41  // Reload to get all modified line records and be ready for hooks
42  $ret = $object->fetch($id);
43  $ret = $object->fetch_thirdparty();
44  /*if (empty($object->id) || ! $object->id > 0)
45  {
46  dol_print_error('Object must have been loaded by a fetch');
47  exit;
48  }*/
49 
50  // Save last template used to generate document
51  if (GETPOST('model', 'alpha'))
52  {
53  $object->setDocModel($user, GETPOST('model', 'alpha'));
54  }
55 
56  // Special case to force bank account
57  //if (property_exists($object, 'fk_bank'))
58  //{
59  if (GETPOST('fk_bank', 'int')) {
60  // this field may come from an external module
61  $object->fk_bank = GETPOST('fk_bank', 'int');
62  } elseif (!empty($object->fk_account)) {
63  $object->fk_bank = $object->fk_account;
64  }
65  //}
66 
67  $outputlangs = $langs;
68  $newlang = '';
69 
70  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
71  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->thirdparty->default_lang)) $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
72  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->default_lang)) $newlang = $object->default_lang; // for thirdparty
73  if (!empty($newlang))
74  {
75  $outputlangs = new Translate("", $conf);
76  $outputlangs->setDefaultLang($newlang);
77  }
78 
79  // To be sure vars is defined
80  if (empty($hidedetails)) $hidedetails = 0;
81  if (empty($hidedesc)) $hidedesc = 0;
82  if (empty($hideref)) $hideref = 0;
83  if (empty($moreparams)) $moreparams = null;
84 
85  $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
86  if ($result <= 0)
87  {
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  {
93  setEventMessages($langs->trans("FileGenerated"), null);
94 
95  $urltoredirect = $_SERVER['REQUEST_URI'];
96  $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
97  $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
98 
99  header('Location: '.$urltoredirect.'#builddoc');
100  exit;
101  }
102  }
103  }
104 }
105 
106 // Delete file in doc form
107 if ($action == 'remove_file' && $permissiontoadd)
108 {
109  if (!empty($upload_dir)) {
110  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
111 
112  if (empty($object->id) || !$object->id > 0)
113  {
114  // Reload to get all modified line records and be ready for hooks
115  $ret = $object->fetch($id);
116  $ret = $object->fetch_thirdparty();
117  }
118 
119  $langs->load("other");
120  $filetodelete = GETPOST('file', 'alpha');
121  $file = $upload_dir.'/'.$filetodelete;
122  $ret = dol_delete_file($file, 0, 0, 0, $object);
123  if ($ret) setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
124  else setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
125 
126  // Make a redirect to avoid to keep the remove_file into the url that create side effects
127  $urltoredirect = $_SERVER['REQUEST_URI'];
128  $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
129  $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
130 
131  header('Location: '.$urltoredirect);
132  exit;
133  } else {
134  setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
135  }
136 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1144
Class to manage translations.
print $_SERVER["PHP_SELF"]
Edit parameters.