dolibarr  16.0.5
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 
41 // Load translation files required by the page
42 $langs->loadLangs(array('other', 'products'));
43 
44 $id = GETPOST('id', 'int');
45 $ref = GETPOST('ref', 'alpha');
46 $action = GETPOST('action', 'aZ09');
47 $confirm = GETPOST('confirm', 'alpha');
48 
49 // Security check
50 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
51 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
52 if ($user->socid) {
53  $socid = $user->socid;
54 }
55 
56 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
57 $hookmanager->initHooks(array('productdocuments'));
58 
59 // Get parameters
60 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
61 $sortfield = GETPOST('sortfield', 'aZ09comma');
62 $sortorder = GETPOST('sortorder', 'aZ09comma');
63 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
64 if (empty($page) || $page == -1) {
65  $page = 0;
66 } // If $page is not defined, or '' or -1
67 $offset = $limit * $page;
68 $pageprev = $page - 1;
69 $pagenext = $page + 1;
70 if (!$sortorder) {
71  $sortorder = "ASC";
72 }
73 if (!$sortfield) {
74  $sortfield = "position_name";
75 }
76 
77 
78 $object = new Product($db);
79 if ($id > 0 || !empty($ref)) {
80  $result = $object->fetch($id, $ref);
81 
82  if (!empty($conf->product->enabled)) {
83  $upload_dir = $conf->product->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
84  } elseif (!empty($conf->service->enabled)) {
85  $upload_dir = $conf->service->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
86  }
87 
88  if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { // For backward compatiblity, we scan also old dirs
89  if (!empty($conf->product->enabled)) {
90  $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";
91  } else {
92  $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";
93  }
94  }
95 }
96 $modulepart = 'produit';
97 
98 
99 if ($object->id > 0) {
100  if ($object->type == $object::TYPE_PRODUCT) {
101  restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
102  }
103  if ($object->type == $object::TYPE_SERVICE) {
104  restrictedArea($user, 'service', $object->id, 'product&product', '', '');
105  }
106 } else {
107  restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
108 }
109 
110 $permissiontoadd = (($object->type == Product::TYPE_PRODUCT && $user->rights->produit->creer) || ($object->type == Product::TYPE_SERVICE && $user->rights->service->creer));
111 
112 
113 /*
114  * Actions
115  */
116 
117 $parameters = array('id'=>$id);
118 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
119 if ($reshook < 0) {
120  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
121 }
122 
123 if (empty($reshook)) {
124  // Delete line if product propal merge is linked to a file
125  if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL)) {
126  if ($action == 'confirm_deletefile' && $confirm == 'yes' && $permissiontoadd) {
127  //extract file name
128  $urlfile = GETPOST('urlfile', 'alpha');
129  $filename = basename($urlfile);
130  $filetomerge = new Propalmergepdfproduct($db);
131  $filetomerge->fk_product = $object->id;
132  $filetomerge->file_name = $filename;
133  $result = $filetomerge->delete_by_file($user);
134  if ($result < 0) {
135  setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
136  }
137  }
138  }
139 
140  // Action submit/delete file/link
141  include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
142 }
143 
144 if ($action == 'filemerge' && $permissiontoadd) {
145  $is_refresh = GETPOST('refresh');
146  if (empty($is_refresh)) {
147  $filetomerge_file_array = GETPOST('filetoadd');
148 
149  $filetomerge_file_array = GETPOST('filetoadd');
150 
151  if ($conf->global->MAIN_MULTILANGS) {
152  $lang_id = GETPOST('lang_id', 'aZ09');
153  }
154 
155  // Delete all file already associated
156  $filetomerge = new Propalmergepdfproduct($db);
157 
158  if ($conf->global->MAIN_MULTILANGS) {
159  $result = $filetomerge->delete_by_product($user, $object->id, $lang_id);
160  } else {
161  $result = $filetomerge->delete_by_product($user, $object->id);
162  }
163  if ($result < 0) {
164  setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
165  }
166 
167  // for each file checked add it to the product
168  if (is_array($filetomerge_file_array)) {
169  foreach ($filetomerge_file_array as $filetomerge_file) {
170  $filetomerge->fk_product = $object->id;
171  $filetomerge->file_name = $filetomerge_file;
172 
173  if ($conf->global->MAIN_MULTILANGS) {
174  $filetomerge->lang = $lang_id;
175  }
176 
177  $result = $filetomerge->create($user);
178  if ($result < 0) {
179  setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
180  }
181  }
182  }
183  }
184 }
185 
186 
187 /*
188  * View
189  */
190 
191 $form = new Form($db);
192 
193 $title = $langs->trans('ProductServiceCard');
194 $helpurl = '';
195 $shortlabel = dol_trunc($object->label, 16);
196 if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
197  $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Documents');
198  $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
199 }
200 if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
201  $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Documents');
202  $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
203 }
204 
205 llxHeader('', $title, $helpurl);
206 
207 
208 if ($object->id) {
209  $head = product_prepare_head($object);
210  $titre = $langs->trans("CardProduct".$object->type);
211  $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
212 
213  print dol_get_fiche_head($head, 'documents', $titre, -1, $picto);
214 
215  $parameters = array();
216  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
217  print $hookmanager->resPrint;
218  if ($reshook < 0) {
219  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
220  }
221 
222  // Build file list
223  $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1);
224 
225  if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { // For backward compatiblity, we scan also old dirs
226  $filearrayold = dol_dir_list($upload_dirold, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1);
227  $filearray = array_merge($filearray, $filearrayold);
228  }
229 
230  $totalsize = 0;
231  foreach ($filearray as $key => $file) {
232  $totalsize += $file['size'];
233  }
234 
235 
236  $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
237  $object->next_prev_filter = " fk_product_type = ".$object->type;
238 
239  $shownav = 1;
240  if ($user->socid && !in_array('product', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) {
241  $shownav = 0;
242  }
243 
244  dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
245 
246  print '<div class="fichecenter">';
247 
248  print '<div class="underbanner clearboth"></div>';
249  print '<table class="border tableforfield centpercent">';
250 
251  print '<tr><td class="titlefield">'.$langs->trans("NbOfAttachedFiles").'</td><td colspan="3">'.count($filearray).'</td></tr>';
252  print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td colspan="3">'.dol_print_size($totalsize, 1, 1).'</td></tr>';
253  print '</table>';
254 
255  print '</div>';
256  print '<div style="clear:both"></div>';
257 
258  print dol_get_fiche_end();
259 
260  $param = '&id='.$object->id;
261  include DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';
262 
263 
264  // Merge propal PDF document PDF files
265  if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL)) {
266  $filetomerge = new Propalmergepdfproduct($db);
267 
268  if ($conf->global->MAIN_MULTILANGS) {
269  $lang_id = GETPOST('lang_id', 'aZ09');
270  $result = $filetomerge->fetch_by_product($object->id, $lang_id);
271  } else {
272  $result = $filetomerge->fetch_by_product($object->id);
273  }
274 
275  $form = new Form($db);
276 
277 
278  $filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1);
279 
280  if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) { // For backward compatiblity, we scan also old dirs
281  $filearray = array_merge($filearray, dol_dir_list($upload_dirold, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1));
282  }
283 
284  // For each file build select list with PDF extention
285  if (count($filearray) > 0) {
286  print '<br>';
287  // Actual file to merge is :
288  if (count($filetomerge->lines) > 0) {
289  print $langs->trans('PropalMergePdfProductActualFile');
290  }
291 
292  print '<form name="filemerge" action="'.DOL_URL_ROOT.'/product/document.php?id='.$object->id.'" method="post">';
293  print '<input type="hidden" name="token" value="'.newToken().'">';
294  print '<input type="hidden" name="action" value="filemerge">';
295  if (count($filetomerge->lines) == 0) {
296  print $langs->trans('PropalMergePdfProductChooseFile');
297  }
298 
299  print '<table class="noborder">';
300 
301  // Get language
302  if ($conf->global->MAIN_MULTILANGS) {
303  $langs->load("languages");
304 
305  print '<tr class="liste_titre"><td>';
306 
307  $default_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
308 
309  $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
310 
311  print Form::selectarray('lang_id', $langs_available, $default_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
312 
313  if ($conf->global->MAIN_MULTILANGS) {
314  print '<input type="submit" class="button" name="refresh" value="'.$langs->trans('Refresh').'">';
315  }
316 
317  print '</td></tr>';
318  }
319 
320  foreach ($filearray as $filetoadd) {
321  if ($ext = pathinfo($filetoadd['name'], PATHINFO_EXTENSION) == 'pdf') {
322  $checked = '';
323  $filename = $filetoadd['name'];
324 
325  if ($conf->global->MAIN_MULTILANGS) {
326  if (array_key_exists($filetoadd['name'].'_'.$default_lang, $filetomerge->lines)) {
327  $filename = $filetoadd['name'].' - '.$langs->trans('Language_'.$default_lang);
328  $checked = ' checked ';
329  }
330  } else {
331  if (array_key_exists($filetoadd['name'], $filetomerge->lines)) {
332  $checked = ' checked ';
333  }
334  }
335 
336  print '<tr class="oddeven"><td>';
337  print '<input type="checkbox" '.$checked.' name="filetoadd[]" id="filetoadd" value="'.$filetoadd['name'].'">'.$filename.'</input>';
338  print '</td></tr>';
339  }
340  }
341 
342  print '<tr><td>';
343  print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
344  print '</td></tr>';
345 
346  print '</table>';
347 
348  print '</form>';
349  }
350  }
351 } else {
352  print $langs->trans("ErrorUnknown");
353 }
354 
355 // End of page
356 llxFooter();
357 $db->close();
dol_trunc
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
Definition: functions.lib.php:3805
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
llxFooter
llxFooter()
Footer empty.
Definition: document.php:89
product_prepare_head
product_prepare_head($object)
Prepare array with list of tabs.
Definition: product.lib.php:35
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
dol_dir_list
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_banner_tab
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.
Definition: functions.lib.php:2046
llxHeader
if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(isset($_GET["hashp"]) &&!defined("NOLOGIN")) if((isset($_GET["modulepart"]) && $_GET["modulepart"]=='medias')) llxHeader()
Header empty.
Definition: document.php:80
get_exdir
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
Definition: functions.lib.php:6549
Propalmergepdfproduct
Put here description of your class.
Definition: propalmergepdfproduct.class.php:32
dol_print_size
dol_print_size($size, $shortvalue=0, $shortunit=0)
Return string with formated size.
Definition: functions.lib.php:2884
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
Form\selectarray
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=1, $moreparamonempty='', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
Definition: html.form.class.php:7879
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:504
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
Product\TYPE_PRODUCT
const TYPE_PRODUCT
Regular product.
Definition: product.class.php:500