dolibarr 24.0.0-beta
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-2025 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([
73 'productdocument',
74 'productdocuments' // Deprecated : standard hook name have no s
75]);
76
77// Get parameters
78$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
79$sortfield = GETPOST('sortfield', 'aZ09comma');
80$sortorder = GETPOST('sortorder', 'aZ09comma');
81$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
82if (empty($page) || $page == -1) {
83 $page = 0;
84} // If $page is not defined, or '' or -1
85$offset = $limit * $page;
86$pageprev = $page - 1;
87$pagenext = $page + 1;
88if (!$sortorder) {
89 $sortorder = "ASC";
90}
91if (!$sortfield) {
92 $sortfield = "position_name";
93}
94
95$upload_dir = null;
96$upload_dirold = null;
97// Initialize objects
98$object = new Product($db);
99if ($id > 0 || !empty($ref)) {
100 $result = $object->fetch($id, $ref);
101
102 if (isModEnabled("product")) {
103 $upload_dir = $conf->product->multidir_output[$object->entity ?? $conf->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
104 } elseif (isModEnabled("service")) {
105 $upload_dir = $conf->service->multidir_output[$object->entity ?? $conf->entity].'/'.get_exdir(0, 0, 0, 1, $object, 'product');
106 }
107
108 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO')) { // For backward compatibility, we scan also old dirs
109 if (isModEnabled("product")) {
110 $upload_dirold = $conf->product->multidir_output[$object->entity ?? $conf->entity].'/'.substr(substr("000".$object->id, -2), 1, 1).'/'.substr(substr("000".$object->id, -2), 0, 1).'/'.$object->id."/photos";
111 } else {
112 $upload_dirold = $conf->service->multidir_output[$object->entity ?? $conf->entity].'/'.substr(substr("000".$object->id, -2), 1, 1).'/'.substr(substr("000".$object->id, -2), 0, 1).'/'.$object->id."/photos";
113 }
114 }
115}
116
117$modulepart = 'product';
118
119
120if ($object->id > 0) {
121 if ($object->type == $object::TYPE_PRODUCT) {
122 restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
123 }
124 if ($object->type == $object::TYPE_SERVICE) {
125 restrictedArea($user, 'service', $object->id, 'product&product', '', '');
126 }
127} else {
128 restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
129}
130
131$permissiontoadd = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'creer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'creer')));
132
133
134/*
135 * Actions
136 */
137
138$parameters = array('id' => $id);
139$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
140if ($reshook < 0) {
141 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
142}
143
144if (empty($reshook)) {
145 // Delete line if product propal merge is linked to a file
146 if (getDolGlobalString('PRODUIT_PDF_MERGE_PROPAL')) {
147 if ($action == 'confirm_deletefile' && $confirm == 'yes' && $permissiontoadd) {
148 //extract file name
149 $urlfile = GETPOST('urlfile', 'alpha');
150 $filename = basename($urlfile);
151 $filetomerge = new Propalmergepdfproduct($db);
152 $filetomerge->fk_product = $object->id;
153 $filetomerge->file_name = $filename;
154 $result = $filetomerge->delete_by_file($user);
155 if ($result < 0) {
156 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
157 }
158 }
159 }
160
161 // Action submit/delete file/link
162 include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
163}
164
165if ($action == 'filemerge' && $permissiontoadd) {
166 $is_refresh = GETPOST('refresh');
167 if (empty($is_refresh)) {
168 $filetomerge_file_array = GETPOST('filetoadd');
169
170 $filetomerge_file_array = GETPOST('filetoadd');
171
172 $lang_id = null;
173 if (getDolGlobalInt('MAIN_MULTILANGS')) {
174 $lang_id = GETPOST('lang_id', 'aZ09');
175 }
176
177 // Delete all file already associated
178 $filetomerge = new Propalmergepdfproduct($db);
179
180 if (getDolGlobalInt('MAIN_MULTILANGS') && $lang_id !== null) {
181 $result = $filetomerge->delete_by_product($user, $object->id, $lang_id);
182 } else {
183 $result = $filetomerge->delete_by_product($user, $object->id);
184 }
185 if ($result < 0) {
186 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
187 }
188
189 // for each file checked add it to the product
190 if (is_array($filetomerge_file_array)) {
191 foreach ($filetomerge_file_array as $filetomerge_file) {
192 $filetomerge->fk_product = $object->id;
193 $filetomerge->file_name = $filetomerge_file;
194
195 if (getDolGlobalInt('MAIN_MULTILANGS') && $lang_id !== null) {
196 $filetomerge->lang = $lang_id;
197 }
198
199 $result = $filetomerge->create($user);
200 if ($result < 0) {
201 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
202 }
203 }
204 }
205 }
206}
207
208
209/*
210 * View
211 */
212
213$form = new Form($db);
214
215$title = $langs->trans('ProductServiceCard');
216$helpurl = '';
217$shortlabel = dol_trunc($object->label, 16);
218if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
219 $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Documents');
220 $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
221}
222if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
223 $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Documents');
224 $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
225}
226
227llxHeader('', $title, $helpurl, '', 0, 0, '', '', '', 'mod-product page-card_document');
228
229
230if ($object->id > 0 && $upload_dir !== null) {
231 $head = product_prepare_head($object);
232 $titre = $langs->trans("CardProduct".$object->type);
233 $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
234
235 print dol_get_fiche_head($head, 'documents', $titre, -1, $picto, 0, '', '', 0, '', 1);
236
237 $parameters = array();
238 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
239 print $hookmanager->resPrint;
240 if ($reshook < 0) {
241 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
242 }
243
244 // Build file list
245 $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
246
247 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO') && $upload_dirold !== null) { // For backward compatibility, we scan also old dirs
248 $filearrayold = dol_dir_list($upload_dirold, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
249 $filearray = array_merge($filearray, $filearrayold);
250 }
251
252 $totalsize = 0;
253 foreach ($filearray as $key => $file) {
254 $totalsize += $file['size'];
255 }
256
257
258 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1&type='.$object->type.'">'.$langs->trans("BackToList").'</a>';
259 $object->next_prev_filter = "(te.fk_product_type:=:".((int) $object->type).")";
260
261 $shownav = 1;
262 if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
263 $shownav = 0;
264 }
265
266 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
267
268 print '<div class="fichecenter">';
269
270 print '<div class="underbanner clearboth"></div>';
271 print '<table class="border tableforfield centpercent">';
272
273 print '<tr><td class="titlefield">'.$langs->trans("NbOfAttachedFiles").'</td><td colspan="3">'.count($filearray).'</td></tr>';
274 print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td colspan="3">'.dol_print_size($totalsize, 1, 1).'</td></tr>';
275 print '</table>';
276
277 print '</div>';
278 print '<div class="clearboth"></div>';
279
280 print dol_get_fiche_end();
281
282 /* To move into tpl
283 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
284
285 $formmail = new FormMail($db);
286 $formmail->withaiprompt = 'text';
287 $out = '';
288
289 $showlinktoai = ($formmail->withaiprompt && isModEnabled('ai')) ? 'textgenerationemail' : '';
290 $showlinktoailabel = $langs->trans("GenerateImage");
291
292 $formatforouput = 'image';
293 $htmlname = 'bodyemail';
294
295 print load_fiche_titre($langs->trans('GenerateWithAI'), '', '');
296 print '<table class="border centpercent">';
297
298 // Fill $out
299 require DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
300
301 print $out;
302 print '</table>';
303 */
304
305 $param = '&id='.$object->id;
306 include DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';
307
308
309 // Merge propal PDF document PDF files
310 if (getDolGlobalString('PRODUIT_PDF_MERGE_PROPAL')) {
311 $filetomerge = new Propalmergepdfproduct($db);
312
313 if (getDolGlobalInt('MAIN_MULTILANGS')) {
314 $lang_id = GETPOST('lang_id', 'aZ09');
315 $result = $filetomerge->fetch_by_product($object->id, $lang_id);
316 } else {
317 $result = $filetomerge->fetch_by_product($object->id);
318 }
319
320 $filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1);
321
322 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO') && $upload_dirold !== null) { // For backward compatibility, we scan also old dirs
323 $filearray = array_merge($filearray, dol_dir_list($upload_dirold, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1));
324 }
325
326 // For each file build select list with PDF extension
327 if (count($filearray) > 0) {
328 print '<br>';
329 // Actual file to merge is :
330 if (count($filetomerge->lines) > 0) {
331 print $langs->trans('PropalMergePdfProductActualFile');
332 }
333
334 print '<form name="filemerge" action="'.DOL_URL_ROOT.'/product/document.php?id='.$object->id.'" method="post">';
335 print '<input type="hidden" name="token" value="'.newToken().'">';
336 print '<input type="hidden" name="action" value="filemerge">';
337 if (count($filetomerge->lines) == 0) {
338 print $langs->trans('PropalMergePdfProductChooseFile');
339 }
340
341 print '<table class="noborder">';
342
343 $default_lang = null;
344 // Get language
345 if (getDolGlobalInt('MAIN_MULTILANGS')) {
346 $langs->load("languages");
347
348 print '<tr class="liste_titre"><td>';
349
350 $default_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
351
352 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
353
354 print Form::selectarray('lang_id', $langs_available, $default_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
355
356 if (getDolGlobalInt('MAIN_MULTILANGS')) {
357 print '<input type="submit" class="button smallpaddingimp" name="refresh" value="'.$langs->trans('Refresh').'">';
358 }
359
360 print '</td></tr>';
361 }
362
363 foreach ($filearray as $filetoadd) {
364 $ext = pathinfo($filetoadd['name'], PATHINFO_EXTENSION);
365 if ($ext == 'pdf') {
366 $checked = '';
367 $filename = $filetoadd['name'];
368
369 if (getDolGlobalInt('MAIN_MULTILANGS') && $default_lang !== null) {
370 if (array_key_exists($filetoadd['name'].'_'.$default_lang, $filetomerge->lines)) {
371 $filename = $filetoadd['name'].' - '.$langs->trans('Language_'.$default_lang);
372 $checked = ' checked ';
373 }
374 } else {
375 if (array_key_exists($filetoadd['name'], $filetomerge->lines)) {
376 $checked = ' checked ';
377 }
378 }
379
380 print '<tr class="oddeven"><td>';
381 print '<input type="checkbox" '.$checked.' name="filetoadd[]" id="filetoadd" value="'.$filetoadd['name'].'">'.$filename.'</input>';
382 print '</td></tr>';
383 }
384 }
385
386 print '<tr><td>';
387 print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
388 print '</td></tr>';
389
390 print '</table>';
391
392 print '</form>';
393 }
394 }
395} else {
396 print $langs->trans("ErrorUnknown");
397}
398
399// End of page
400llxFooter();
401$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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:64
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.
isModEnabled($module)
Is Dolibarr module enabled.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
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.