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