dolibarr  16.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 Frédéric France <frederic.france@netlogic.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 require '../main.inc.php';
27 require 'class/ProductAttribute.class.php';
28 require 'class/ProductAttributeValue.class.php';
29 require 'lib/variants.lib.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array('products'));
33 
34 $id = GETPOST('id', 'int');
35 $ref = GETPOST('ref', 'alpha');
36 $action = GETPOST('action', 'aZ09');
37 $confirm = GETPOST('confirm', 'alpha');
38 $cancel = GETPOST('cancel', 'alpha');
39 $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'productattribute'; // To manage different context of search
40 $backtopage = GETPOST('backtopage', 'alpha');
41 $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
42 $lineid = GETPOST('lineid', 'alpha');
43 
44 // Security check
45 if (empty($conf->variants->enabled)) {
46  accessforbidden('Module not enabled');
47 }
48 if ($user->socid > 0) { // Protection if external user
50 }
51 $result = restrictedArea($user, 'variants');
52 
53 $object = new ProductAttribute($db);
54 
55 // Load object
56 include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
57 
58 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
59 $hookmanager->initHooks(array('productattributecard', 'globalcard'));
60 
61 $permissiontoread = $user->rights->variants->read;
62 $permissiontoadd = $user->rights->variants->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
63 $permissiontoedit = $user->rights->variants->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
64 $permissiontodelete = $user->rights->variants->delete;
65 
66 $error = 0;
67 
68 
69 /*
70  * Actions
71  */
72 
73 
74 $parameters = array();
75 // Note that $action and $object may be modified by some hooks
76 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
77 if ($reshook < 0) {
78  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
79 }
80 
81 if (empty($reshook)) {
82  $error = 0;
83 
84  $backurlforlist = dol_buildpath('/variants/list.php', 1);
85 
86  if (empty($backtopage) || ($cancel && empty($id))) {
87  if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
88  if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
89  $backtopage = $backurlforlist;
90  } else {
91  $backtopage = dol_buildpath('/variants/card.php', 1).'?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
92  }
93  }
94  }
95 
96  // Action to move up and down lines of object
97  include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
98  if ($cancel) {
99  if (!empty($backtopage)) {
100  header("Location: " . $backtopage);
101  exit;
102  }
103  $action = '';
104  }
105 
106  // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
107  include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
108 
109  // Action to move up and down lines of object
110  if ($action == 'up' && $permissiontoedit) {
111  $object->line_up(GETPOST('rowid'), false);
112 
113  header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
114  exit();
115  } elseif ($action == 'down' && $permissiontoedit) {
116  $object->line_down(GETPOST('rowid'), false);
117 
118  header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
119  exit();
120  }
121 
122  if ($action == 'addline' && $permissiontoedit) {
123  $line_ref = GETPOST('line_ref', 'alpha');
124  $line_value = GETPOST('line_value', 'alpha');
125 
126  $result = $object->addLine($line_ref, $line_value);
127  if ($result > 0) {
128  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
129  header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
130  exit();
131  } else {
132  setEventMessages($object->error, $object->errors, 'errors');
133  $action = '';
134  }
135  } elseif ($action == 'updateline' && $permissiontoedit) {
136  $line_ref = GETPOST('line_ref', 'alpha');
137  $line_value = GETPOST('line_value', 'alpha');
138 
139  $result = $object->updateLine($lineid, $line_ref, $line_value);
140  if ($result > 0) {
141  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
142  header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
143  exit();
144  } else {
145  setEventMessages($object->error, $object->errors, 'errors');
146  $action = 'editline';
147  }
148  }
149 }
150 
151 
152 /*
153  * View
154  */
155 
156 $title = $langs->trans('ProductAttributeName', dol_htmlentities($object->label));
157 $help_url = 'EN:Module_Products#Variants';
158 llxHeader('', $title, $help_url);
159 
160 // Part to create
161 if ($action == 'create') {
162  print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("ProductAttribute")), '', 'object_' . $object->picto);
163 
164  print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
165  print '<input type="hidden" name="token" value="' . newToken() . '">';
166  print '<input type="hidden" name="action" value="add">';
167  if ($backtopage) {
168  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
169  }
170  if ($backtopageforcancel) {
171  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
172  }
173 
174  print dol_get_fiche_head(array(), '');
175 
176  print '<table class="border centpercent tableforfieldcreate">' . "\n";
177 
178  // Common attributes
179  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php';
180 
181  print '</table>' . "\n";
182 
183  print dol_get_fiche_end();
184 
185  print '<div class="center">';
186  print '<input type="submit" class="button" name="add" value="' . dol_escape_htmltag($langs->trans("Create")) . '">';
187  print '&nbsp; ';
188  print '<input type="' . ($backtopage ? "submit" : "button") . '" class="button button-cancel" name="cancel" value="' . dol_escape_htmltag($langs->trans("Cancel")) . '"' . ($backtopage ? '' : ' onclick="javascript:history.go(-1)"') . '>'; // Cancel for create does not post form if we don't know the backtopage
189  print '</div>';
190 
191  print '</form>';
192 
193  dol_set_focus('input[name="label"]');
194 } elseif (($id || $ref) && $action == 'edit') {
195  // Part to edit record
196  print load_fiche_titre($langs->trans("ProductAttribute"), '', 'object_' . $object->picto);
197 
198  print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
199  print '<input type="hidden" name="token" value="' . newToken() . '">';
200  print '<input type="hidden" name="action" value="update">';
201  print '<input type="hidden" name="id" value="' . $object->id . '">';
202  if ($backtopage) {
203  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
204  }
205  if ($backtopageforcancel) {
206  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
207  }
208 
209  print dol_get_fiche_head();
210 
211  print '<table class="border centpercent tableforfieldedit">' . "\n";
212 
213  // Common attributes
214  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';
215 
216  print '</table>';
217 
218  print dol_get_fiche_end();
219 
220  print '<div class="center"><input type="submit" class="button button-save" name="save" value="' . $langs->trans("Save") . '">';
221  print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="' . $langs->trans("Cancel") . '">';
222  print '</div>';
223 
224  print '</form>';
225 } elseif ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
226  // Part to show record
227  $res = $object->fetch_optionals();
228 
229  $head = productAttributePrepareHead($object);
230  print dol_get_fiche_head($head, 'card', $langs->trans("ProductAttribute"), -1, $object->picto);
231 
232  $formconfirm = '';
233 
234  // Confirmation to delete
235  if ($action == 'delete') {
236  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteMyObject'), $langs->trans('ProductAttributeDeleteDialog'), 'confirm_delete', '', 0, 1);
237  } elseif ($action == 'ask_deleteline') {
238  // Confirmation to delete line
239  $object_value = new ProductAttributeValue($db);
240  if ($object_value->fetch($lineid) > 0) {
241  $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);
242  }
243  }
244 
245  // Call Hook formConfirm
246  $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
247  $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
248  if (empty($reshook)) {
249  $formconfirm .= $hookmanager->resPrint;
250  } elseif ($reshook > 0) {
251  $formconfirm = $hookmanager->resPrint;
252  }
253 
254  // Print form confirm
255  print $formconfirm;
256 
257  // Object card
258  // ------------------------------------------------------------
259  $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT . '/variants/list.php?leftmenu=?restore_lastsearch_values=1');
260  $linkback = '<a href="' . dol_sanitizeUrl($backtolist) . '">' . $langs->trans("BackToList") . '</a>';
261 
262  dol_banner_tab($object, 'id', $linkback);
263 
264  print '<div class="fichecenter">';
265  print '<div class="fichehalfleft">';
266  print '<div class="underbanner clearboth"></div>';
267  print '<table class="border centpercent tableforfield">' . "\n";
268 
269  // Common attributes
270  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_view.tpl.php';
271 
272  print '</table>';
273  print '</div>';
274  print '</div>';
275 
276  print '<div class="clearboth"></div>';
277 
278  print dol_get_fiche_end();
279 
280  // Buttons for actions
281  if ($action != 'editline') {
282  print '<div class="tabsAction">' . "\n";
283  $parameters = array();
284  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
285  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
286 
287  if (empty($reshook)) {
288  // Modify
289  print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit', '', $permissiontoedit);
290 
291  // Delete (need delete permission, or if draft, just need create/modify permission)
292  print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete', '', $permissiontodelete);
293  }
294  print '</div>' . "\n";
295  }
296 
297  /*
298  * Lines
299  */
300  if (!empty($object->table_element_line)) {
301  // Show object lines
302  $result = $object->getLinesArray();
303 
304  print load_fiche_titre($langs->trans("PossibleValues") . (!empty($object->lines) ? ' (' . count($object->lines) . ')' : ''));
305 
306  print ' <form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '' : '#line_' . GETPOST('lineid', 'int')) . '" method="POST">
307  <input type="hidden" name="token" value="' . newToken() . '">
308  <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
309  <input type="hidden" name="mode" value="">
310  <input type="hidden" name="page_y" value="">
311  <input type="hidden" name="id" value="' . $object->id . '">
312  ';
313  if ($backtopage) {
314  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
315  }
316  if ($backtopageforcancel) {
317  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
318  }
319 
320  if (!empty($conf->use_javascript_ajax)) {
321  include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
322  }
323 
324  print '<div class="div-table-responsive-no-min">';
325  if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
326  print '<table id="tablelines" class="noborder noshadow" width="100%">';
327  }
328 
329  // Form to add new line
330  if ($permissiontoedit && $action != 'selectlines') {
331  if ($action != 'editline') {
332  // Add products/services form
333 
334  $parameters = array();
335  $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
336  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
337  if (empty($reshook))
338  $object->formAddObjectLine(1, $mysoc, $soc);
339  }
340  }
341 
342  if (!empty($object->lines)) {
343  $object->printObjectLines($action, $mysoc, null, GETPOST('lineid', 'int'), 1);
344  }
345 
346  if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
347  print '</table>';
348  }
349  print '</div>';
350 
351  print "</form>\n";
352  }
353 }
354 
355 // End of page
356 llxFooter();
357 $db->close();
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
ProductAttributeValue
Class ProductAttributeValue Used to represent a product attribute value.
Definition: ProductAttributeValue.class.php:25
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
dolGetButtonAction
dolGetButtonAction($label, $html='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
Definition: functions.lib.php:10450
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
$help_url
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:116
ProductAttribute
Class ProductAttribute Used to represent a product attribute.
Definition: ProductAttribute.class.php:25
$formconfirm
$formconfirm
if ($action == 'delbookkeepingyear') {
Definition: listbyaccount.php:576
dol_sanitizeUrl
dol_sanitizeUrl($stringtoclean, $type=1)
Clean a string to use it as an URL (into a href or src attribute)
Definition: functions.lib.php:1271
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
dol_set_focus
dol_set_focus($selector)
Set focus onto field with selector (similar behaviour of 'autofocus' HTML5 tag)
Definition: functions.lib.php:9379
$parameters
$parameters
Actions.
Definition: card.php:78
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
productAttributePrepareHead
productAttributePrepareHead($object)
Prepare array with list of tabs.
Definition: variants.lib.php:31
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
dol_htmlentities
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
Definition: functions.lib.php:7075