dolibarr 22.0.5
card.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
3 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require 'class/ProductAttribute.class.php';
30require 'class/ProductAttributeValue.class.php';
31require 'lib/variants.lib.php';
32
43// Load translation files required by the page
44$langs->loadLangs(array('products'));
45
46$id = GETPOSTINT('id');
47$ref = GETPOST('ref', 'alpha');
48$action = GETPOST('action', 'aZ09');
49$confirm = GETPOST('confirm', 'alpha');
50$cancel = GETPOST('cancel', 'alpha');
51$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'productattribute'; // To manage different context of search
52$backtopage = GETPOST('backtopage', 'alpha');
53$backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
54$lineid = GETPOSTINT('lineid');
55
56// Security check
57if (!isModEnabled('variants')) {
58 accessforbidden('Module not enabled');
59}
60if ($user->socid > 0) { // Protection if external user
62}
63
64// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
65$hookmanager->initHooks(array('productattributecard', 'globalcard'));
66
67$result = restrictedArea($user, 'variants');
68
69$object = new ProductAttribute($db);
70$extrafields = new ExtraFields($db);
71
72// Fetch optionals attributes and labels
73$extrafields->fetch_name_optionals_label($object->table_element);
74
75// Load object
76include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be 'include', not 'include_once'
77
78$permissiontoread = $user->hasRight('variants', 'read');
79$permissiontoadd = $user->hasRight('variants', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
80$permissiontoedit = $user->hasRight('variants', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
81$permissiontodelete = $user->hasRight('variants', 'delete');
82
83$error = 0;
84
85
86/*
87 * Actions
88 */
89
90
91$parameters = array();
92// Note that $action and $object may be modified by some hooks
93$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
94if ($reshook < 0) {
95 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
96}
97
98if (empty($reshook)) {
99 $error = 0;
100
101 $backurlforlist = dol_buildpath('/variants/list.php', 1);
102
103 if (empty($backtopage) || ($cancel && empty($id))) {
104 if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
105 if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
106 $backtopage = $backurlforlist;
107 } else {
108 $backtopage = dol_buildpath('/variants/card.php', 1).'?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
109 }
110 }
111 }
112
113 // Action to move up and down lines of object
114 include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
115 if ($cancel) {
116 if (!empty($backtopage)) {
117 header("Location: " . $backtopage);
118 exit;
119 }
120 $action = '';
121 }
122
123 // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
124 include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
125
126 // Action to move up and down lines of object
127 if ($action == 'up' && $permissiontoedit) {
128 $object->line_up(GETPOSTINT('rowid'), false);
129
130 header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOSTINT('rowid'));
131 exit();
132 } elseif ($action == 'down' && $permissiontoedit) {
133 $object->line_down(GETPOSTINT('rowid'), false);
134
135 header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOSTINT('rowid'));
136 exit();
137 }
138
139 if ($action == 'addline' && $permissiontoedit) {
140 $line_ref = GETPOST('line_ref', 'alpha');
141 $line_value = GETPOST('line_value', 'alpha');
142
143 $result = $object->addLine($line_ref, $line_value);
144 if ($result > 0) {
145 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
146 header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
147 exit();
148 } else {
149 setEventMessages($object->error, $object->errors, 'errors');
150 $action = '';
151 }
152 } elseif ($action == 'updateline' && $permissiontoedit) {
153 $line_ref = GETPOST('line_ref', 'alpha');
154 $line_value = GETPOST('line_value', 'alpha');
155
156 $result = $object->updateLine($lineid, $line_ref, $line_value);
157 if ($result > 0) {
158 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
159 header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
160 exit();
161 } else {
162 setEventMessages($object->error, $object->errors, 'errors');
163 $action = 'editline';
164 }
165 }
166}
167
168
169/*
170 * View
171 */
172
173$title = $langs->trans('ProductAttributeName', dol_htmlentities($object->label));
174$help_url = 'EN:Module_Products#Variants';
175llxHeader('', $title, $help_url);
176
177// Part to create
178if ($action == 'create') {
179 print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("ProductAttribute")), '', 'object_' . $object->picto);
180
181 print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
182 print '<input type="hidden" name="token" value="' . newToken() . '">';
183 print '<input type="hidden" name="action" value="add">';
184 if ($backtopage) {
185 print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
186 }
187 if ($backtopageforcancel) {
188 print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
189 }
190
191 print dol_get_fiche_head(array(), '');
192
193 print '<table class="border centpercent tableforfieldcreate">' . "\n";
194
195 // Common attributes
196 include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php';
197
198 // Other attributes
199 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
200
201 print '</table>' . "\n";
202
203 print dol_get_fiche_end();
204
205 print '<div class="center">';
206 print '<input type="submit" class="button" name="add" value="' . dol_escape_htmltag($langs->trans("Create")) . '">';
207 print '&nbsp; ';
208 print '<input type="' . ($backtopage ? "submit" : "button") . '" class="button button-cancel" name="cancel" value="' . dol_escape_htmltag($langs->trans("Cancel")) . '"' . ($backtopage ? '' : ' onclick="history.go(-1)"') . '>'; // Cancel for create does not post form if we don't know the backtopage
209 print '</div>';
210
211 print '</form>';
212
213 dol_set_focus('input[name="ref"]');
214} elseif (($id || $ref) && $action == 'edit') {
215 // Part to edit record
216 print load_fiche_titre($langs->trans("ProductAttribute"), '', 'object_' . $object->picto);
217
218 print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
219 print '<input type="hidden" name="token" value="' . newToken() . '">';
220 print '<input type="hidden" name="action" value="update">';
221 print '<input type="hidden" name="id" value="' . $object->id . '">';
222 if ($backtopage) {
223 print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
224 }
225 if ($backtopageforcancel) {
226 print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
227 }
228
229 print dol_get_fiche_head();
230
231 print '<table class="border centpercent tableforfieldedit">' . "\n";
232
233 // Common attributes
234 include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';
235
236 // Other attributes
237 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
238
239 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
240 print $hookmanager->resPrint;
241
242 print '</table>';
243
244 print dol_get_fiche_end();
245
246 print '<div class="center"><input type="submit" class="button button-save" name="save" value="' . $langs->trans("Save") . '">';
247 print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="' . $langs->trans("Cancel") . '">';
248 print '</div>';
249
250 print '</form>';
251} elseif ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
252 // Part to show record
253 $res = $object->fetch_optionals();
254
256 print dol_get_fiche_head($head, 'card', $langs->trans("ProductAttribute"), -1, $object->picto);
257
258 $formconfirm = '';
259
260 // Confirmation to delete
261 if ($action == 'delete') {
262 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteMyObject'), $langs->trans('ProductAttributeDeleteDialog'), 'confirm_delete', '', 0, 1);
263 } elseif ($action == 'ask_deleteline') {
264 // Confirmation to delete line
265 $object_value = new ProductAttributeValue($db);
266 if ($object_value->fetch($lineid) > 0) {
267 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id . '&lineid=' . $lineid, $langs->trans('DeleteLine'), $langs->trans('ProductAttributeValueDeleteDialog', dol_htmlentities($object_value->value), dol_htmlentities($object_value->ref)), 'confirm_deleteline', '', 0, 1);
268 }
269 }
270
271 // Call Hook formConfirm
272 $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
273 $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
274 if (empty($reshook)) {
275 $formconfirm .= $hookmanager->resPrint;
276 } elseif ($reshook > 0) {
277 $formconfirm = $hookmanager->resPrint;
278 }
279
280 // Print form confirm
281 print $formconfirm;
282
283 // Object card
284 // ------------------------------------------------------------
285 $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT . '/variants/list.php?leftmenu=?restore_lastsearch_values=1');
286 $linkback = '<a href="' . dol_sanitizeUrl($backtolist) . '">' . $langs->trans("BackToList") . '</a>';
287
288 dol_banner_tab($object, 'id', $linkback);
289
290 print '<div class="fichecenter">';
291 print '<div class="fichehalfleft">';
292 print '<div class="underbanner clearboth"></div>';
293 print '<table class="border centpercent tableforfield">' . "\n";
294
295 // Common attributes
296 include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_view.tpl.php';
297
298 // Other attributes
299 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
300
301 print '</table>';
302 print '</div>';
303 print '</div>';
304
305 print '<div class="clearboth"></div>';
306
307 print dol_get_fiche_end();
308
309 // Buttons for actions
310 if ($action != 'editline') {
311 print '<div class="tabsAction">' . "\n";
312 $parameters = array();
313 $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
314 if ($reshook < 0) {
315 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
316 }
317
318 if (empty($reshook)) {
319 // Modify
320 print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit', '', $permissiontoedit);
321
322 // Delete (need delete permission, or if draft, just need create/modify permission)
323 print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete', '', $permissiontodelete);
324 }
325 print '</div>' . "\n";
326 }
327
328 /*
329 * Lines
330 */
331 if (!empty($object->table_element_line)) {
332 // Show object lines
333 $result = $object->getLinesArray();
334
335 print load_fiche_titre($langs->trans("PossibleValues") . (!empty($object->lines) ? '<span class="opacitymedium colorblack paddingleft">(' . count($object->lines) . ')</span>' : ''));
336
337 print '<form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '' : '#line_' . GETPOSTINT('lineid')) . '" method="POST">
338 <input type="hidden" name="token" value="' . newToken() . '">
339 <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
340 <input type="hidden" name="mode" value="">
341 <input type="hidden" name="page_y" value="">
342 <input type="hidden" name="id" value="' . $object->id . '">
343 ';
344 if ($backtopage) {
345 print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
346 }
347 if ($backtopageforcancel) {
348 print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
349 }
350
351 if (!empty($conf->use_javascript_ajax)) {
352 include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
353 }
354
355 print '<div class="div-table-responsive-no-min">';
356 if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
357 print '<table id="tablelines" class="noborder centpercent">';
358 }
359
360 $object->printObjectLines($action, $mysoc, null, GETPOSTINT('lineid'), 1, '/variants/tpl', ($permissiontoedit ? 1 : 0));
361
362 if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
363 print '</table>';
364 }
365 print '</div>';
366
367 print "</form>\n";
368 }
369}
370
371// End of page
372llxFooter();
373$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 standard extra fields.
Class ProductAttribute Used to represent a Product attribute Examples:
Class ProductAttributeValue Used to represent a product attribute value.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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.
dol_set_focus($selector)
Set focus onto field with selector (similar behaviour of 'autofocus' HTML5 tag)
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dolGetButtonAction($label, $text='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
dol_sanitizeUrl($stringtoclean, $type=1)
Clean a string to use it as an URL (into a href or src attribute)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
productAttributePrepareHead($object)
Prepare array with list of tabs.