dolibarr 21.0.3
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 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.
46if (!empty($permissioncreate) && empty($permissiontoadd)) {
47 $permissiontoadd = $permissioncreate; // For backward compatibility
48}
49
50// Build doc
51if ($action == 'builddoc' && ($permissiontoadd || !empty($usercangeneretedoc))) {
52 if (is_numeric(GETPOST('model', 'alpha'))) {
53 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Model")), null, 'errors');
54 } else {
55 // Reload to get all modified line records and be ready for hooks
56 $ret = $object->fetch($id);
57 $ret = $object->fetch_thirdparty();
58 /*if (empty($object->id) || ! $object->id > 0)
59 {
60 dol_print_error(null, 'Object must have been loaded by a fetch');
61 exit;
62 }*/
63
64 // Save last template used to generate document
65 if (GETPOST('model', 'alpha')) {
66 $object->setDocModel($user, GETPOST('model', 'alpha'));
67 }
68
69 // Special case to force bank account
70 if (GETPOSTINT('fk_bank')) {
71 // this field may come from an external module
72 $object->fk_bank = GETPOSTINT('fk_bank'); // For compatibility
73 $object->fk_account = GETPOSTINT('fk_bank');
74 } elseif (!empty($object->fk_account)) {
75 $object->fk_bank = $object->fk_account; // For compatibility
76 }
77
78 $outputlangs = $langs;
79 $newlang = '';
80
81 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
82 $newlang = GETPOST('lang_id', 'aZ09');
83 }
84 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($object->thirdparty->default_lang)) {
85 $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
86 }
87 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($object->default_lang)) {
88 $newlang = $object->default_lang; // for thirdparty
89 }
90 if (!empty($newlang)) {
91 $outputlangs = new Translate("", $conf);
92 $outputlangs->setDefaultLang($newlang);
93 }
94
95 // To be sure vars is defined
96 $hidedetails = isset($hidedetails) ? $hidedetails : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0);
97 $hidedesc = isset($hidedesc) ? $hidedesc : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0);
98 $hideref = isset($hideref) ? $hideref : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0);
99 $moreparams = isset($moreparams) ? $moreparams : null;
100
101 $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
102 if ($result <= 0) {
103 setEventMessages($object->error, $object->errors, 'errors');
104 $action = '';
105 } else {
106 if (empty($donotredirect)) { // This is set when include is done by bulk action "Bill Orders"
107 setEventMessages($langs->trans("FileGenerated"), null);
108
109 /*$urltoredirect = $_SERVER['REQUEST_URI'];
110 $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
111 $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
112
113 header('Location: '.$urltoredirect.'#builddoc');
114 exit;*/
115 }
116 }
117 }
118}
119
120// Delete file in doc form
121if ($action == 'remove_file' && $permissiontoadd) {
122 if (!empty($upload_dir)) {
123 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
124
125 if (empty($object->id) || !$object->id > 0) {
126 // Reload to get all modified line records and be ready for hooks
127 $ret = $object->fetch($id);
128 $ret = $object->fetch_thirdparty();
129 }
130
131 $langs->load("other");
132 $filetodelete = GETPOST('file', 'alpha');
133 $file = $upload_dir.'/'.$filetodelete;
134 $dirthumb = dirname($file).'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
135 $ret = dol_delete_file($file, 0, 0, 0, $object);
136 if ($ret) {
137 // If it exists, remove thumb.
138 $regs = array();
139 if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i', $file, $regs)) {
140 $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
141 if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
142 dol_delete_file($dirthumb.$photo_vignette);
143 }
144
145 $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
146 if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
147 dol_delete_file($dirthumb.$photo_vignette);
148 }
149 }
150
151 setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
152 } else {
153 setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
154 }
155
156 // Make a redirect to avoid to keep the remove_file into the url that create side effects
157 $urltoredirect = $_SERVER['REQUEST_URI'];
158 $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
159 $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
160
161 header('Location: '.$urltoredirect);
162 exit;
163 } else {
164 setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
165 }
166}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage translations.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79