dolibarr  13.0.2
document.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
5  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
6  * Copyright (C) 2005 Simon TOSSER <simon@kornog-computing.com>
7  * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8  * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
9  * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
31 require '../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
37 if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL))
38  require_once DOL_DOCUMENT_ROOT.'/product/class/propalmergepdfproduct.class.php';
39 
40 // Load translation files required by the page
41 $langs->loadLangs(array('other', 'products'));
42 
43 $id = GETPOST('id', 'int');
44 $ref = GETPOST('ref', 'alpha');
45 $action = GETPOST('action', 'aZ09');
46 $confirm = GETPOST('confirm', 'alpha');
47 
48 // Security check
49 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
50 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
51 if ($user->socid) $socid = $user->socid;
52 $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
53 
54 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
55 $hookmanager->initHooks(array('productdocuments'));
56 
57 // Get parameters
58 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
59 $sortfield = GETPOST("sortfield", 'alpha');
60 $sortorder = GETPOST("sortorder", 'alpha');
61 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
62 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
63 $offset = $limit * $page;
64 $pageprev = $page - 1;
65 $pagenext = $page + 1;
66 if (!$sortorder) $sortorder = "ASC";
67 if (!$sortfield) $sortfield = "position_name";
68 
69 
70 $object = new Product($db);
71 if ($id > 0 || !empty($ref))
72 {
73  $result = $object->fetch($id, $ref);
74 
75  if (!empty($conf->product->enabled)) $upload_dir = $conf->product->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
76  elseif (!empty($conf->service->enabled)) $upload_dir = $conf->service->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
77 
78  if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) // For backward compatiblity, we scan also old dirs
79  {
80  if (!empty($conf->product->enabled)) $upload_dirold = $conf->product->multidir_output[$object->entity].'/'.substr(substr("000".$object->id, -2), 1, 1).'/'.substr(substr("000".$object->id, -2), 0, 1).'/'.$object->id."/photos";
81  else $upload_dirold = $conf->service->multidir_output[$object->entity].'/'.substr(substr("000".$object->id, -2), 1, 1).'/'.substr(substr("000".$object->id, -2), 0, 1).'/'.$object->id."/photos";
82  }
83 }
84 $modulepart = 'produit';
85 
86 $permissiontoadd = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
87 
88 
89 /*
90  * Actions
91  */
92 
93 $parameters = array('id'=>$id);
94 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
95 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
96 
97 if (empty($reshook))
98 {
99  // Delete line if product propal merge is linked to a file
100  if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL))
101  {
102  if ($action == 'confirm_deletefile' && $confirm == 'yes')
103  {
104  //extract file name
105  $urlfile = GETPOST('urlfile', 'alpha');
106  $filename = basename($urlfile);
107  $filetomerge = new Propalmergepdfproduct($db);
108  $filetomerge->fk_product = $object->id;
109  $filetomerge->file_name = $filename;
110  $result = $filetomerge->delete_by_file($user);
111  if ($result < 0) {
112  setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
113  }
114  }
115  }
116 
117  // Action submit/delete file/link
118  include_once DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
119 }
120 
121 if ($action == 'filemerge')
122 {
123  $is_refresh = GETPOST('refresh');
124  if (empty($is_refresh)) {
125  $filetomerge_file_array = GETPOST('filetoadd');
126 
127  $filetomerge_file_array = GETPOST('filetoadd');
128 
129  if ($conf->global->MAIN_MULTILANGS) {
130  $lang_id = GETPOST('lang_id', 'aZ09');
131  }
132 
133  // Delete all file already associated
134  $filetomerge = new Propalmergepdfproduct($db);
135 
136  if ($conf->global->MAIN_MULTILANGS) {
137  $result = $filetomerge->delete_by_product($user, $object->id, $lang_id);
138  } else {
139  $result = $filetomerge->delete_by_product($user, $object->id);
140  }
141  if ($result < 0) {
142  setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
143  }
144 
145  // for each file checked add it to the product
146  if (is_array($filetomerge_file_array)) {
147  foreach ($filetomerge_file_array as $filetomerge_file) {
148  $filetomerge->fk_product = $object->id;
149  $filetomerge->file_name = $filetomerge_file;
150 
151  if ($conf->global->MAIN_MULTILANGS) {
152  $filetomerge->lang = $lang_id;
153  }
154 
155  $result = $filetomerge->create($user);
156  if ($result < 0) {
157  setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
158  }
159  }
160  }
161  }
162 }
163 
164 
165 /*
166  * View
167  */
168 
169 $form = new Form($db);
170 
171 $title = $langs->trans('ProductServiceCard');
172 $helpurl = '';
173 $shortlabel = dol_trunc($object->label, 16);
174 if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT))
175 {
176  $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Documents');
177  $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
178 }
179 if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE))
180 {
181  $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Documents');
182  $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
183 }
184 
185 llxHeader('', $title, $helpurl);
186 
187 
188 if ($object->id)
189 {
190  $head = product_prepare_head($object);
191  $titre = $langs->trans("CardProduct".$object->type);
192  $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
193 
194  print dol_get_fiche_head($head, 'documents', $titre, -1, $picto);
195 
196  $parameters = array();
197  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
198  print $hookmanager->resPrint;
199  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
200 
201  // Build file list
202  $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1);
203 
204  if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) // For backward compatiblity, we scan also old dirs
205  {
206  $filearrayold = dol_dir_list($upload_dirold, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1);
207  $filearray = array_merge($filearray, $filearrayold);
208  }
209 
210  $totalsize = 0;
211  foreach ($filearray as $key => $file)
212  {
213  $totalsize += $file['size'];
214  }
215 
216 
217  $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
218  $object->next_prev_filter = " fk_product_type = ".$object->type;
219 
220  $shownav = 1;
221  if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav = 0;
222 
223  dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
224 
225  print '<div class="fichecenter">';
226 
227  print '<div class="underbanner clearboth"></div>';
228  print '<table class="border tableforfield centpercent">';
229 
230  print '<tr><td class="titlefield">'.$langs->trans("NbOfAttachedFiles").'</td><td colspan="3">'.count($filearray).'</td></tr>';
231  print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td colspan="3">'.dol_print_size($totalsize, 1, 1).'</td></tr>';
232  print '</table>';
233 
234  print '</div>';
235  print '<div style="clear:both"></div>';
236 
238 
239  $param = '&id='.$object->id;
240  include_once DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';
241 
242 
243  // Merge propal PDF document PDF files
244  if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL))
245  {
246  $filetomerge = new Propalmergepdfproduct($db);
247 
248  if ($conf->global->MAIN_MULTILANGS) {
249  $lang_id = GETPOST('lang_id', 'aZ09');
250  $result = $filetomerge->fetch_by_product($object->id, $lang_id);
251  } else {
252  $result = $filetomerge->fetch_by_product($object->id);
253  }
254 
255  $form = new Form($db);
256 
257 
258  $filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1);
259 
260  if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) // For backward compatiblity, we scan also old dirs
261  {
262  $filearray = array_merge($filearray, dol_dir_list($upload_dirold, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1));
263  }
264 
265  // For each file build select list with PDF extention
266  if (count($filearray) > 0)
267  {
268  print '<br>';
269  // Actual file to merge is :
270  if (count($filetomerge->lines) > 0) {
271  print $langs->trans('PropalMergePdfProductActualFile');
272  }
273 
274  print '<form name="filemerge" action="'.DOL_URL_ROOT.'/product/document.php?id='.$object->id.'" method="post">';
275  print '<input type="hidden" name="token" value="'.newToken().'">';
276  print '<input type="hidden" name="action" value="filemerge">';
277  if (count($filetomerge->lines) == 0) {
278  print $langs->trans('PropalMergePdfProductChooseFile');
279  }
280 
281  print '<table class="noborder">';
282 
283  // Get language
284  if ($conf->global->MAIN_MULTILANGS) {
285  $langs->load("languages");
286 
287  print '<tr class="liste_titre"><td>';
288 
289  $default_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
290 
291  $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
292 
293  print Form::selectarray('lang_id', $langs_available, $default_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
294 
295  if ($conf->global->MAIN_MULTILANGS) {
296  print '<input type="submit" class="button" name="refresh" value="'.$langs->trans('Refresh').'">';
297  }
298 
299  print '</td></tr>';
300  }
301 
302  foreach ($filearray as $filetoadd)
303  {
304  if ($ext = pathinfo($filetoadd['name'], PATHINFO_EXTENSION) == 'pdf')
305  {
306  $checked = '';
307  $filename = $filetoadd['name'];
308 
309  if ($conf->global->MAIN_MULTILANGS)
310  {
311  if (array_key_exists($filetoadd['name'].'_'.$default_lang, $filetomerge->lines))
312  {
313  $filename = $filetoadd['name'].' - '.$langs->trans('Language_'.$default_lang);
314  $checked = ' checked ';
315  }
316  } else {
317  if (array_key_exists($filetoadd['name'], $filetomerge->lines))
318  {
319  $checked = ' checked ';
320  }
321  }
322 
323  print '<tr class="oddeven"><td>';
324  print '<input type="checkbox" '.$checked.' name="filetoadd[]" id="filetoadd" value="'.$filetoadd['name'].'">'.$filename.'</input>';
325  print '</td></tr>';
326  }
327  }
328 
329  print '<tr><td>';
330  print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
331  print '</td></tr>';
332 
333  print '</table>';
334 
335  print '</form>';
336  }
337  }
338 } else {
339  print $langs->trans("ErrorUnknown");
340 }
341 
342 // End of page
343 llxFooter();
344 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
static selectarray($htmlname, $array, $id= '', $show_empty=0, $key_in_label=0, $value_as_key=0, $moreparam= '', $translate=0, $maxlen=0, $disabled=0, $sort= '', $morecss= '', $addjscombo=0, $moreparamonempty= '', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
Put here description of your class.
Class to manage products or services.
const TYPE_SERVICE
Service.
const TYPE_PRODUCT
Regular product.
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart= '')
Return a path to have a the directory according to object where files are stored. ...
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
dol_print_size($size, $shortvalue=0, $shortunit=0)
Return string with formated size.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.
llxFooter()
Empty footer.
Definition: wrapper.php:59
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35