dolibarr 25.0.0-alpha
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 $lang_id = null;
171 if (getDolGlobalInt('MAIN_MULTILANGS')) {
172 $lang_id = GETPOST('lang_id', 'aZ09');
173 }
174
175 // Delete all file already associated
176 $filetomerge = new Propalmergepdfproduct($db);
177
178 if (getDolGlobalInt('MAIN_MULTILANGS') && $lang_id !== null) {
179 $result = $filetomerge->delete_by_product($user, $object->id, $lang_id);
180 } else {
181 $result = $filetomerge->delete_by_product($user, $object->id);
182 }
183 if ($result < 0) {
184 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
185 }
186
187 // for each file checked add it to the product
188 if (is_array($filetomerge_file_array)) {
189 foreach ($filetomerge_file_array as $filetomerge_file) {
190 $filetomerge->fk_product = $object->id;
191 $filetomerge->file_name = $filetomerge_file;
192
193 if (getDolGlobalInt('MAIN_MULTILANGS') && $lang_id !== null) {
194 $filetomerge->lang = $lang_id;
195 }
196
197 $result = $filetomerge->create($user);
198 if ($result < 0) {
199 setEventMessages($filetomerge->error, $filetomerge->errors, 'errors');
200 }
201 }
202 }
203 }
204}
205
206
207/*
208 * View
209 */
210
211$form = new Form($db);
212
213$title = $langs->trans('ProductServiceCard');
214$helpurl = '';
215$shortlabel = dol_trunc($object->label, 16);
216if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT)) {
217 $title = $langs->trans('Product')." ".$shortlabel." - ".$langs->trans('Documents');
218 $helpurl = 'EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
219}
220if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE)) {
221 $title = $langs->trans('Service')." ".$shortlabel." - ".$langs->trans('Documents');
222 $helpurl = 'EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
223}
224
225llxHeader('', $title, $helpurl, '', 0, 0, '', '', '', 'mod-product page-card_document');
226
227
228if ($object->id > 0 && $upload_dir !== null) {
229 $head = product_prepare_head($object);
230 $titre = $langs->trans("CardProduct".$object->type);
231 $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
232
233 print dol_get_fiche_head($head, 'documents', $titre, -1, $picto, 0, '', '', 0, '', 1);
234
235 $parameters = array();
236 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
237 print $hookmanager->resPrint;
238 if ($reshook < 0) {
239 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
240 }
241
242 // Build file list
243 $filearray = dol_dir_list($upload_dir, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
244
245 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO') && $upload_dirold !== null) { // For backward compatibility, we scan also old dirs
246 $filearrayold = dol_dir_list($upload_dirold, "files", 0, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
247 $filearray = array_merge($filearray, $filearrayold);
248 }
249
250 $totalsize = 0;
251 foreach ($filearray as $key => $file) {
252 $totalsize += $file['size'];
253 }
254
255
256 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1&type='.$object->type.'">'.$langs->trans("BackToList").'</a>';
257 $object->next_prev_filter = "(te.fk_product_type:=:".((int) $object->type).")";
258
259 $shownav = 1;
260 if ($user->socid && !in_array('product', explode(',', getDolGlobalString('MAIN_MODULES_FOR_EXTERNAL')))) {
261 $shownav = 0;
262 }
263
264 dol_banner_tab($object, 'ref', $linkback, $shownav, 'ref');
265
266 print '<div class="fichecenter">';
267
268 print '<div class="underbanner clearboth"></div>';
269 print '<table class="border tableforfield centpercent">';
270
271 print '<tr><td class="titlefield">'.$langs->trans("NbOfAttachedFiles").'</td><td colspan="3">'.count($filearray).'</td></tr>';
272 print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td colspan="3">'.dol_print_size($totalsize, 1, 1).'</td></tr>';
273 print '</table>';
274
275 print '</div>';
276 print '<div class="clearboth"></div>';
277
278 print dol_get_fiche_end();
279
280 /* To move into tpl
281 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
282
283 $formmail = new FormMail($db);
284 $formmail->withaiprompt = 'text';
285 $out = '';
286
287 $showlinktoai = ($formmail->withaiprompt && isModEnabled('ai')) ? 'textgenerationemail' : '';
288 $showlinktoailabel = $langs->trans("GenerateImage");
289
290 $formatforouput = 'image';
291 $htmlname = 'bodyemail';
292
293 print load_fiche_titre($langs->trans('GenerateWithAI'), '', '');
294 print '<table class="border centpercent">';
295
296 // Fill $out
297 require DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
298
299 print $out;
300 print '</table>';
301 */
302
303 $param = '&id='.$object->id;
304 include DOL_DOCUMENT_ROOT.'/core/tpl/document_actions_post_headers.tpl.php';
305
306
307 // Merge propal PDF document PDF files
308 if (getDolGlobalString('PRODUIT_PDF_MERGE_PROPAL')) {
309 $filetomerge = new Propalmergepdfproduct($db);
310
311 if (getDolGlobalInt('MAIN_MULTILANGS')) {
312 $lang_id = GETPOST('lang_id', 'aZ09');
313 $result = $filetomerge->fetch_by_product($object->id, $lang_id);
314 } else {
315 $result = $filetomerge->fetch_by_product($object->id);
316 }
317
318 $filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1);
319
320 if (getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO') && $upload_dirold !== null) { // For backward compatibility, we scan also old dirs
321 $filearray = array_merge($filearray, dol_dir_list($upload_dirold, "files", 0, '', '\.meta$', 'name', SORT_ASC, 1));
322 }
323
324 // For each file build select list with PDF extension
325 if (count($filearray) > 0) {
326 print '<br>';
327 // Actual file to merge is :
328 if (count($filetomerge->lines) > 0) {
329 print $langs->trans('PropalMergePdfProductActualFile');
330 }
331
332 print '<form name="filemerge" action="'.DOL_URL_ROOT.'/product/document.php?id='.$object->id.'" method="post">';
333 print '<input type="hidden" name="token" value="'.newToken().'">';
334 print '<input type="hidden" name="action" value="filemerge">';
335 if (count($filetomerge->lines) == 0) {
336 print $langs->trans('PropalMergePdfProductChooseFile');
337 }
338
339 print '<table class="noborder">';
340
341 $default_lang = null;
342 // Get language
343 if (getDolGlobalInt('MAIN_MULTILANGS')) {
344 $langs->load("languages");
345
346 print '<tr class="liste_titre"><td>';
347
348 $default_lang = empty($lang_id) ? $langs->getDefaultLang() : $lang_id;
349
350 $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
351
352 print Form::selectarray('lang_id', $langs_available, $default_lang, 0, 0, 0, '', 0, 0, 0, 'ASC');
353
354 if (getDolGlobalInt('MAIN_MULTILANGS')) {
355 print '<input type="submit" class="button smallpaddingimp" name="refresh" value="'.$langs->trans('Refresh').'">';
356 }
357
358 print '</td></tr>';
359 }
360
361 foreach ($filearray as $filetoadd) {
362 $ext = pathinfo($filetoadd['name'], PATHINFO_EXTENSION);
363 if ($ext == 'pdf') {
364 $checked = '';
365 $filename = $filetoadd['name'];
366
367 if (getDolGlobalInt('MAIN_MULTILANGS') && $default_lang !== null) {
368 if (array_key_exists($filetoadd['name'].'_'.$default_lang, $filetomerge->lines)) {
369 $filename = $filetoadd['name'].' - '.$langs->trans('Language_'.$default_lang);
370 $checked = ' checked ';
371 }
372 } else {
373 if (array_key_exists($filetoadd['name'], $filetomerge->lines)) {
374 $checked = ' checked ';
375 }
376 }
377
378 print '<tr class="oddeven"><td>';
379 print '<input type="checkbox" '.$checked.' name="filetoadd[]" id="filetoadd" value="'.$filetoadd['name'].'">'.$filename.'</input>';
380 print '</td></tr>';
381 }
382 }
383
384 print '<tr><td>';
385 print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
386 print '</td></tr>';
387
388 print '</table>';
389
390 print '</form>';
391 }
392 }
393} else {
394 print $langs->trans("ErrorUnknown");
395}
396
397// End of page
398llxFooter();
399$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:
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:65
dol_print_size($size, $shortvalue=0, $shortunit=0)
Return string with formatted size.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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.
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.
Definition html.lib.php:540
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:738
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, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.