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