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