dolibarr 19.0.4
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
32if (!empty($permissioncreate) && empty($permissiontoadd)) {
33 $permissiontoadd = $permissioncreate; // For backward compatibility
34}
35
36// Build doc
37if ($action == 'builddoc' && ($permissiontoadd || !empty($usercangeneretedoc))) {
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 (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
70 $newlang = GETPOST('lang_id', 'aZ09');
71 }
72 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($object->thirdparty->default_lang)) {
73 $newlang = $object->thirdparty->default_lang; // for proposal, order, invoice, ...
74 }
75 if (getDolGlobalInt('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 $hidedetails = isset($hidedetails) ? $hidedetails : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0);
85 $hidedesc = isset($hidedesc) ? $hidedesc : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0);
86 $hideref = isset($hideref) ? $hideref : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0);
87 $moreparams = isset($moreparams) ? $moreparams : null;
88
89 $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
90 if ($result <= 0) {
91 setEventMessages($object->error, $object->errors, 'errors');
92 $action = '';
93 } else {
94 if (empty($donotredirect)) { // This is set when include is done by bulk action "Bill Orders"
95 setEventMessages($langs->trans("FileGenerated"), null);
96
97 /*$urltoredirect = $_SERVER['REQUEST_URI'];
98 $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
99 $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
100
101 header('Location: '.$urltoredirect.'#builddoc');
102 exit;*/
103 }
104 }
105 }
106}
107
108// Delete file in doc form
109if ($action == 'remove_file' && $permissiontoadd) {
110 if (!empty($upload_dir)) {
111 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
112
113 if (empty($object->id) || !$object->id > 0) {
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 $dirthumb = dirname($file).'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
123 $ret = dol_delete_file($file, 0, 0, 0, $object);
124 if ($ret) {
125 // If it exists, remove thumb.
126 $regs = array();
127 if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i', $file, $regs)) {
128 $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
129 if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
130 dol_delete_file($dirthumb.$photo_vignette);
131 }
132
133 $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
134 if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
135 dol_delete_file($dirthumb.$photo_vignette);
136 }
137 }
138
139 setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
140 } else {
141 setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
142 }
143
144 // Make a redirect to avoid to keep the remove_file into the url that create side effects
145 $urltoredirect = $_SERVER['REQUEST_URI'];
146 $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
147 $urltoredirect = preg_replace('/action=remove_file&?/', '', $urltoredirect);
148
149 header('Location: '.$urltoredirect);
150 exit;
151 } else {
152 setEventMessages('BugFoundVarUploaddirnotDefined', null, 'errors');
153 }
154}
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.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.