dolibarr 22.0.5
document.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
4 * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
6 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2005 Simon TOSSER <simon@kornog-computing.com>
8 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
9 * Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
10 * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
11 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
12 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27
35// Load Dolibarr environment
36require '../main.inc.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
42if (getDolGlobalString('PRODUIT_PDF_MERGE_PROPAL')) {
43 require_once DOL_DOCUMENT_ROOT.'/product/class/propalmergepdfproduct.class.php';
44}
45
54// Load translation files required by the page
55$langs->loadLangs(array('other', 'products'));
56
57
58// Get parameters
59$id = GETPOSTINT('id');
60$ref = GETPOST('ref', 'alpha');
61$action = GETPOST('action', 'aZ09');
62$confirm = GETPOST('confirm', 'alpha');
63
64// Security check
65$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
66$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
67if ($user->socid) {
68 $socid = $user->socid;
69}
70
71// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
72$hookmanager->initHooks(array('productdocuments'));
73
74// Get parameters
75$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
76$sortfield = GETPOST('sortfield', 'aZ09comma');
77$sortorder = GETPOST('sortorder', 'aZ09comma');
78$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
79if (empty($page) || $page == -1) {
80 $page = 0;
81} // If $page is not defined, or '' or -1
82$offset = $limit * $page;
83$pageprev = $page - 1;
84$pagenext = $page + 1;
85if (!$sortorder) {
86 $sortorder = "ASC";
87}
88if (!$sortfield) {
89 $sortfield = "position_name";
90}
91
92$upload_dir = '';
93$upload_dirold = '';
94// Initialize objects
95$object = new Product($db);
96if ($id > 0 || !empty($ref)) {
97 $result = $object->fetch($id, $ref);
98
99 if (isModEnabled("product")) {
100 $upload_dir = $conf->product->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
101 } elseif (isModEnabled("service")) {
102 $upload_dir = $conf->service->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
103 }
104
105 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO')) { // For backward compatibility, we scan also old dirs
106 if (isModEnabled("product")) {
107 $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";
108 } else {
109 $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";
110 }
111 }
112}
113
114$modulepart = 'produit';
115
116
117if ($object->id > 0) {
118 if ($object->type == $object::TYPE_PRODUCT) {
119 restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
120 }
121 if ($object->type == $object::TYPE_SERVICE) {
122 restrictedArea($user, 'service', $object->id, 'product&product', '', '');
123 }
124} else {
125 restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
126}
127
128$permissiontoadd = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'creer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'creer')));
129
130
131/*
132 * Actions
133 */
134
135$parameters = array('id' => $id);
136$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
137if ($reshook < 0) {
138 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
139}
140
141if (empty($reshook)) {
142 // Delete line if product propal merge is linked to a file
143 if (getDolGlobalString('PRODUIT_PDF_MERGE_PROPAL')) {
144 if ($action == 'confirm_deletefile' && $confirm == 'yes' && $permissiontoadd) {
145 //extract file name
146 $urlfile = GETPOST('urlfile', 'alpha');
147 $filename = basename($urlfile);
148 $filetomerge = new Propalmergepdfproduct($db);
149 $filetomerge->fk_product = $object->id;
150 $filetomerge->file_name = $filename;
151 $result = $filetomerge->delete_by_file($user);
152 if ($result < 0) {
153 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
154 }
155 }
156 }
157
158 // Action submit/delete file/link
159 include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
160}
161
162if ($action == 'filemerge' && $permissiontoadd) {
163 $is_refresh = GETPOST('refresh');
164 if (empty($is_refresh)) {
165 $filetomerge_file_array = GETPOST('filetoadd');
166
167 $filetomerge_file_array = GETPOST('filetoadd');
168
169 $lang_id = null;
170 if (getDolGlobalInt('MAIN_MULTILANGS')) {
171 $lang_id = GETPOST('lang_id', 'aZ09');
172 }
173
174 // Delete all file already associated
175 $filetomerge = new Propalmergepdfproduct($db);
176
177 if (getDolGlobalInt('MAIN_MULTILANGS') && $lang_id !== null) {
178 $result = $filetomerge->delete_by_product($user, $object->id, $lang_id);
179 } else {
180 $result = $filetomerge->delete_by_product($user, $object->id);
181 }
182 if ($result < 0) {
183 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
184 }
185
186 // for each file checked add it to the product
187 if (is_array($filetomerge_file_array)) {
188 foreach ($filetomerge_file_array as $filetomerge_file) {
189 $filetomerge->fk_product = $object->id;
190 $filetomerge->file_name = $filetomerge_file;
191
192 if (getDolGlobalInt('MAIN_MULTILANGS') && $lang_id !== null) {
193 $filetomerge->lang = $lang_id;
194 }
195
196 $result = $filetomerge->create($user);
197 if ($result < 0) {
198 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
199 }
200 }
201 }
202 }
203}
204
205
206/*
207 * View
208 */
209
210$form = new Form($db);
211
212$title = $langs->trans('ProductServiceCard');
213$helpurl = '';
214$shortlabel = dol_trunc($object->label, 16);
215if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
216 $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Documents');
217 $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
218}
219if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
220 $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Documents');
221 $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
222}
223
224llxHeader('', $title, $helpurl, '', 0, 0, '', '', '', 'mod-product page-card_document');
225
226
227if ($object->id > 0) {
228 $head = product_prepare_head($object);
229 $titre = $langs->trans("CardProduct".$object->type);
230 $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
231
232 print dol_get_fiche_head($head, 'documents', $titre, -1, $picto);
233
234 $parameters = array();
235 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
236 print $hookmanager->resPrint;
237 if ($reshook < 0) {
238 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
239 }
240
241 // Build file list
242 $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
243
244 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO')) { // For backward compatibility, we scan also old dirs
245 $filearrayold = dol_dir_list($upload_dirold, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
246 $filearray = array_merge($filearray, $filearrayold);
247 }
248
249 $totalsize = 0;
250 foreach ($filearray as $key => $file) {
251 $totalsize += $file['size'];
252 }
253
254
255 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1&type='.$object->type.'">'.$langs->trans("BackToList").'</a>';
256 $object->next_prev_filter = "(te.fk_product_type:=:".((int) $object->type).")";
257
258 $shownav = 1;
259 if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
260 $shownav = 0;
261 }
262
263 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
264
265 print '<div class="fichecenter">';
266
267 print '<div class="underbanner clearboth"></div>';
268 print '<table class="border tableforfield centpercent">';
269
270 print '<tr><td class="titlefield">'.$langs->trans("NbOfAttachedFiles").'</td><td colspan="3">'.count($filearray).'</td></tr>';
271 print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td colspan="3">'.dol_print_size($totalsize, 1, 1).'</td></tr>';
272 print '</table>';
273
274 print '</div>';
275 print '<div class="clearboth"></div>';
276
277 print dol_get_fiche_end();
278
279 /* To move into tpl
280 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
281
282 $formmail = new FormMail($db);
283 $formmail->withaiprompt = 'text';
284 $out = '';
285
286 $showlinktoai = ($formmail->withaiprompt && isModEnabled('ai')) ? 'textgenerationemail' : '';
287 $showlinktoailabel = $langs->trans("GenerateImage");
288
289 $formatforouput = 'image';
290 $htmlname = 'bodyemail';
291
292 print load_fiche_titre($langs->trans('GenerateWithAI'), '', '');
293 print '<table class="border centpercent">';
294
295 // Fill $out
296 require DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
297
298 print $out;
299 print '</table>';
300 */
301
302 $param = '&id='.$object->id;
303 include DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';
304
305
306 // Merge propal PDF document PDF files
307 if (getDolGlobalString('PRODUIT_PDF_MERGE_PROPAL')) {
308 $filetomerge = new Propalmergepdfproduct($db);
309
310 if (getDolGlobalInt('MAIN_MULTILANGS')) {
311 $lang_id = GETPOST('lang_id', 'aZ09');
312 $result = $filetomerge->fetch_by_product($object->id, $lang_id);
313 } else {
314 $result = $filetomerge->fetch_by_product($object->id);
315 }
316
317 $filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1);
318
319 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO')) { // For backward compatibility, we scan also old dirs
320 $filearray = array_merge($filearray, dol_dir_list($upload_dirold, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1));
321 }
322
323 // For each file build select list with PDF extension
324 if (count($filearray) > 0) {
325 print '<br>';
326 // Actual file to merge is :
327 if (count($filetomerge->lines) > 0) {
328 print $langs->trans('PropalMergePdfProductActualFile');
329 }
330
331 print '<form name="filemerge" action="'.DOL_URL_ROOT.'/product/document.php?id='.$object->id.'" method="post">';
332 print '<input type="hidden" name="token" value="'.newToken().'">';
333 print '<input type="hidden" name="action" value="filemerge">';
334 if (count($filetomerge->lines) == 0) {
335 print $langs->trans('PropalMergePdfProductChooseFile');
336 }
337
338 print '<table class="noborder">';
339
340 $default_lang = null;
341 // Get language
342 if (getDolGlobalInt('MAIN_MULTILANGS')) {
343 $langs->load("languages");
344
345 print '<tr class="liste_titre"><td>';
346
347 $default_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
348
349 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
350
351 print Form::selectarray('lang_id', $langs_available, $default_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
352
353 if (getDolGlobalInt('MAIN_MULTILANGS')) {
354 print '<input type="submit" class="button smallpaddingimp" name="refresh" value="'.$langs->trans('Refresh').'">';
355 }
356
357 print '</td></tr>';
358 }
359
360 foreach ($filearray as $filetoadd) {
361 $ext = pathinfo($filetoadd['name'], PATHINFO_EXTENSION);
362 if ($ext == 'pdf') {
363 $checked = '';
364 $filename = $filetoadd['name'];
365
366 if (getDolGlobalInt('MAIN_MULTILANGS') && $default_lang !== null) {
367 if (array_key_exists($filetoadd['name'].'_'.$default_lang, $filetomerge->lines)) {
368 $filename = $filetoadd['name'].' - '.$langs->trans('Language_'.$default_lang);
369 $checked = ' checked ';
370 }
371 } else {
372 if (array_key_exists($filetoadd['name'], $filetomerge->lines)) {
373 $checked = ' checked ';
374 }
375 }
376
377 print '<tr class="oddeven"><td>';
378 print '<input type="checkbox" '.$checked.' name="filetoadd[]" id="filetoadd" value="'.$filetoadd['name'].'">'.$filename.'</input>';
379 print '</td></tr>';
380 }
381 }
382
383 print '<tr><td>';
384 print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
385 print '</td></tr>';
386
387 print '</table>';
388
389 print '</form>';
390 }
391 }
392} else {
393 print $langs->trans("ErrorUnknown");
394}
395
396// End of page
397llxFooter();
398$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
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='minwidth75', $addjscombo=1, $moreparamonempty='', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
Class to manage products or services.
const TYPE_PRODUCT
Regular product.
const TYPE_SERVICE
Service.
Put here description of your class.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
dol_print_size($size, $shortvalue=0, $shortunit=0)
Return string with formatted size.
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_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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.
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
product_prepare_head($object)
Prepare array with list of tabs.
restrictedArea(User $user, $features, $object=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.