dolibarr 24.0.0-beta
actions_builddoc.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
26// $action must be defined
27// $id must be defined
28// $object must be defined and must have a method generateDocument().
29// $permissiontoadd must be defined
30// $upload_dir must be defined (example $conf->project->dir_output . "/";)
31// $hidedetails, $hidedesc, $hideref and $moreparams may have been set or not.
32
52'
53@phan-var-force int $id
54@phan-var-force int $permissiontoadd
55@phan-var-force ?array<string,mixed> $moreparams
56@phan-var-force CommonObject|Societe $object
57@phan-var-force int $id
58';
59
60if (!empty($permissioncreate) && empty($permissiontoadd)) {
61 $permissiontoadd = $permissioncreate; // For backward compatibility
62}
63
64// Build doc
65if ($action == 'builddoc' && ($permissiontoadd || !empty($usercangeneratedoc))) {
66 if (is_numeric(GETPOST('model', 'alpha'))) {
67 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Model")), null, 'errors');
68 } else {
69 // Reload to get all modified line records and be ready for hooks
70 $ret = $object->fetch($id);
71 $ret = $object->fetch_thirdparty();
72
73 // Save last template used to generate document
74 if (GETPOST('model', 'alpha')) {
75 $object->setDocModel($user, GETPOST('model', 'alpha'));
76 }
77
78 // Special case to force bank account
79 if (GETPOSTINT('fk_bank')) {
80 // this field may come from an external module
81 $object->fk_bank = GETPOSTINT('fk_bank'); // For compatibility
82 $object->fk_account = GETPOSTINT('fk_bank');
83 } elseif (!empty($object->fk_account)) {
84 $object->fk_bank = $object->fk_account; // For compatibility
85 }
86
87 $outputlangs = $langs;
88 $newlang = '';
89 if (getDolGlobalInt('MAIN_MULTILANGS') /* && empty($newlang) */ && GETPOST('lang_id', 'aZ09')) {
90 $newlang = GETPOST('lang_id', 'aZ09');
91 }
92 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($object->thirdparty->default_lang)) {
93 $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
94 }
95 if (getDolGlobalInt('MAIN_MULTILANGS') && property_exists($object, 'default_lang') && empty($newlang) && isset($object->default_lang)) {
96 $newlang = $object->default_lang; // for thirdparty
97 }
98 if (!empty($newlang)) {
99 $outputlangs = new Translate("", $conf);
100 $outputlangs->setDefaultLang($newlang);
101 }
102
103 // To be sure vars is defined
104 $hidedetails = isset($hidedetails) ? $hidedetails : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0);
105 $hidedesc = isset($hidedesc) ? $hidedesc : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0);
106 $hideref = isset($hideref) ? $hideref : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0);
107 $moreparams = isset($moreparams) ? $moreparams : null;
108
109 $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
110
111 if ($result <= 0) {
112 setEventMessages($object->error, $object->errors, 'errors');
113 setEventMessages($object->warning, $object->warnings, 'warnings');
114 $action = '';
115 } else {
116 if (empty($donotredirect)) { // This is set when include is done by bulk action "Bill Orders"
117 setEventMessages($langs->trans("FileGenerated"), null);
118
119 /*$urltoredirect = $_SERVER['REQUEST_URI'];
120 $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
121 $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
122
123 header('Location: '.$urltoredirect.'#builddoc');
124 exit;*/
125 }
126 }
127 }
128}
129
130// Delete file in doc form
131if ($action == 'remove_file' && $permissiontoadd) {
132 if (!empty($upload_dir)) {
133 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
134
135 if (empty($object->id) || !$object->id > 0) {
136 // Reload to get all modified line records and be ready for hooks
137 $ret = $object->fetch($id);
138 $ret = $object->fetch_thirdparty();
139 }
140
141 $langs->load("other");
142 $filetodelete = GETPOST('file', 'alpha');
143 $file = $upload_dir.'/'.$filetodelete;
144 $dirthumb = dirname($file).'/thumbs/'; // Path to the folder containing the thumbnail (if file is an image)
145 $ret = dol_delete_file($file, 0, 0, 0, $object);
146 if ($ret) {
147 // If it exists, remove thumb.
148 $regs = array();
149 if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff|\.webp|\.xpm|\.xbm|\.avif)$/i', $file, $regs)) {
150 $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
151 if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
152 dol_delete_file($dirthumb.$photo_vignette);
153 }
154
155 $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
156 if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
157 dol_delete_file($dirthumb.$photo_vignette);
158 }
159 }
160
161 setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
162 } else {
163 setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
164 }
165
166 // Make a redirect to avoid to keep the remove_file into the url that create side effects
167 $urltoredirect = $_SERVER['REQUEST_URI'];
168 $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
169 $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
170
171 header('Location: '.$urltoredirect);
172 exit;
173 } else {
174 setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
175 }
176}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage translations.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.