dolibarr  16.0.5
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->project->dir_output . "/";)
30 // $hidedetails, $hidedesc, $hideref and $moreparams may have been set or not.
31 
32 if (!empty($permissioncreate) && empty($permissiontoadd)) {
33  $permissiontoadd = $permissioncreate; // For backward compatibility
34 }
35 
36 // Build doc
37 if ($action == 'builddoc' && $permissiontoadd) {
38  if (is_numeric(GETPOST('model', 'alpha'))) {
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  $object->setDocModel($user, GETPOST('model', 'alpha'));
53  }
54 
55  // Special case to force bank account
56  //if (property_exists($object, 'fk_bank'))
57  //{
58  if (GETPOST('fk_bank', 'int')) {
59  // this field may come from an external module
60  $object->fk_bank = GETPOST('fk_bank', 'int');
61  } elseif (!empty($object->fk_account)) {
62  $object->fk_bank = $object->fk_account;
63  }
64  //}
65 
66  $outputlangs = $langs;
67  $newlang = '';
68 
69  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
70  $newlang = GETPOST('lang_id', 'aZ09');
71  }
72  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && isset($object->thirdparty->default_lang)) {
73  $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
74  }
75  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && isset($object->default_lang)) {
76  $newlang = $object->default_lang; // for thirdparty
77  }
78  if (!empty($newlang)) {
79  $outputlangs = new Translate("", $conf);
80  $outputlangs->setDefaultLang($newlang);
81  }
82 
83  // To be sure vars is defined
84  if (empty($hidedetails)) {
85  $hidedetails = 0;
86  }
87  if (empty($hidedesc)) {
88  $hidedesc = 0;
89  }
90  if (empty($hideref)) {
91  $hideref = 0;
92  }
93  if (empty($moreparams)) {
94  $moreparams = null;
95  }
96 
97  $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
98  if ($result <= 0) {
99  setEventMessages($object->error, $object->errors, 'errors');
100  $action = '';
101  } else {
102  if (empty($donotredirect)) { // This is set when include is done by bulk action "Bill Orders"
103  setEventMessages($langs->trans("FileGenerated"), null);
104 
105  /*$urltoredirect = $_SERVER['REQUEST_URI'];
106  $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
107  $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
108 
109  header('Location: '.$urltoredirect.'#builddoc');
110  exit;*/
111  }
112  }
113  }
114 }
115 
116 // Delete file in doc form
117 if ($action == 'remove_file' && $permissiontoadd) {
118  if (!empty($upload_dir)) {
119  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
120 
121  if (empty($object->id) || !$object->id > 0) {
122  // Reload to get all modified line records and be ready for hooks
123  $ret = $object->fetch($id);
124  $ret = $object->fetch_thirdparty();
125  }
126 
127  $langs->load("other");
128  $filetodelete = GETPOST('file', 'alpha');
129  $file = $upload_dir.'/'.$filetodelete;
130  $dirthumb = dirname($file).'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
131  $ret = dol_delete_file($file, 0, 0, 0, $object);
132  if ($ret) {
133  // If it exists, remove thumb.
134  $regs = array();
135  if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i', $file, $regs)) {
136  $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
137  if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
138  dol_delete_file($dirthumb.$photo_vignette);
139  }
140 
141  $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
142  if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
143  dol_delete_file($dirthumb.$photo_vignette);
144  }
145  }
146 
147  setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
148  } else {
149  setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
150  }
151 
152  // Make a redirect to avoid to keep the remove_file into the url that create side effects
153  $urltoredirect = $_SERVER['REQUEST_URI'];
154  $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
155  $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
156 
157  header('Location: '.$urltoredirect);
158  exit;
159  } else {
160  setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
161  }
162 }
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
Translate
Class to manage translations.
Definition: translate.class.php:30
dol_delete_file
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1231
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137