dolibarr  7.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  *
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 <http://www.gnu.org/licenses/>.
16  * or see http://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 // $permissioncreate 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 
33 // Build doc
34 if ($action == 'builddoc' && $permissioncreate)
35 {
36 
37  if (is_numeric(GETPOST('model','alpha')))
38  {
39  $error=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Model"));
40  }
41  else
42  {
43  // Reload to get all modified line records and be ready for hooks
44  $ret = $object->fetch($id);
45  $ret = $object->fetch_thirdparty();
46  /*if (empty($object->id) || ! $object->id > 0)
47  {
48  dol_print_error('Object must have been loaded by a fetch');
49  exit;
50  }*/
51 
52  // Save last template used to generate document
53  if (GETPOST('model','alpha'))
54  {
55  $object->setDocModel($user, GETPOST('model','alpha'));
56  }
57 
58  // Special case to force bank account
59  //if (property_exists($object, 'fk_bank'))
60  //{
61  if (GETPOST('fk_bank','int')) { // this field may come from an external module
62  $object->fk_bank = GETPOST('fk_bank','int');
63  } else if (! empty($object->fk_account)) {
64  $object->fk_bank = $object->fk_account;
65  }
66  //}
67 
68  $outputlangs = $langs;
69  $newlang='';
70 
71  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang=GETPOST('lang_id','aZ09');
72  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->thirdparty->default_lang)) $newlang=$object->thirdparty->default_lang; // for proposal, order, invoice, ...
73  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($object->default_lang)) $newlang=$object->default_lang; // for thirdparty
74  if (! empty($newlang))
75  {
76  $outputlangs = new Translate("",$conf);
77  $outputlangs->setDefaultLang($newlang);
78  }
79 
80  // To be sure vars is defined
81  if (empty($hidedetails)) $hidedetails=0;
82  if (empty($hidedesc)) $hidedesc=0;
83  if (empty($hideref)) $hideref=0;
84  if (empty($moreparams)) $moreparams=null;
85 
86  $result= $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
87  if ($result <= 0)
88  {
89  setEventMessages($object->error, $object->errors, 'errors');
90  $action='';
91  }
92  else
93  {
94  if (empty($donotredirect)) // This is set when include is done by bulk action "Bill Orders"
95  {
96  setEventMessages($langs->trans("FileGenerated"), null);
97 
98  $urltoredirect = $_SERVER['REQUEST_URI'];
99  $urltoredirect = preg_replace('/#builddoc$/', '', $urltoredirect);
100  $urltoredirect = preg_replace('/action=builddoc&?/', '', $urltoredirect); // To avoid infinite loop
101 
102  header('Location: '.$urltoredirect.'#builddoc');
103  exit;
104  }
105  }
106  }
107 }
108 
109 // Delete file in doc form
110 if ($action == 'remove_file' && $permissioncreate)
111 {
112  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
113 
114  if (empty($object->id) || ! $object->id > 0)
115  {
116  // Reload to get all modified line records and be ready for hooks
117  $ret = $object->fetch($id);
118  $ret = $object->fetch_thirdparty();
119  }
120 
121  $langs->load("other");
122  $filetodelete=GETPOST('file','alpha');
123  $file = $upload_dir . '/' . $filetodelete;
124  $ret=dol_delete_file($file,0,0,0,$object);
125  if ($ret) setEventMessages($langs->trans("FileWasRemoved", $filetodelete), null, 'mesgs');
126  else setEventMessages($langs->trans("ErrorFailToDeleteFile", $filetodelete), null, 'errors');
127 }
128 
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage translations.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null)
Remove a file or several files with a mask.
Definition: files.lib.php:1103