dolibarr  17.0.4
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 // Load Dolibarr environment
27 require '../main.inc.php';
28 require 'class/ProductAttribute.class.php';
29 require 'class/ProductAttributeValue.class.php';
30 require 'lib/variants.lib.php';
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array('products'));
34 
35 $id = GETPOST('id', 'int');
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
46 if (!isModEnabled('variants')) {
47  accessforbidden('Module not enabled');
48 }
49 if ($user->socid > 0) { // Protection if external user
51 }
52 $result = restrictedArea($user, 'variants');
53 
54 $object = new ProductAttribute($db);
55 
56 // Load object
57 include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
58 
59 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
60 $hookmanager->initHooks(array('productattributecard', 'globalcard'));
61 
62 $permissiontoread = $user->rights->variants->read;
63 $permissiontoadd = $user->rights->variants->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
64 $permissiontoedit = $user->rights->variants->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
65 $permissiontodelete = $user->rights->variants->delete;
66 
67 $error = 0;
68 
69 
70 /*
71  * Actions
72  */
73 
74 
75 $parameters = array();
76 // Note that $action and $object may be modified by some hooks
77 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
78 if ($reshook < 0) {
79  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
80 }
81 
82 if (empty($reshook)) {
83  $error = 0;
84 
85  $backurlforlist = dol_buildpath('/variants/list.php', 1);
86 
87  if (empty($backtopage) || ($cancel && empty($id))) {
88  if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
89  if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
90  $backtopage = $backurlforlist;
91  } else {
92  $backtopage = dol_buildpath('/variants/card.php', 1).'?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
93  }
94  }
95  }
96 
97  // Action to move up and down lines of object
98  include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
99  if ($cancel) {
100  if (!empty($backtopage)) {
101  header("Location: " . $backtopage);
102  exit;
103  }
104  $action = '';
105  }
106 
107  // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
108  include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
109 
110  // Action to move up and down lines of object
111  if ($action == 'up' && $permissiontoedit) {
112  $object->line_up(GETPOST('rowid'), false);
113 
114  header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
115  exit();
116  } elseif ($action == 'down' && $permissiontoedit) {
117  $object->line_down(GETPOST('rowid'), false);
118 
119  header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
120  exit();
121  }
122 
123  if ($action == 'addline' && $permissiontoedit) {
124  $line_ref = GETPOST('line_ref', 'alpha');
125  $line_value = GETPOST('line_value', 'alpha');
126 
127  $result = $object->addLine($line_ref, $line_value);
128  if ($result > 0) {
129  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
130  header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
131  exit();
132  } else {
133  setEventMessages($object->error, $object->errors, 'errors');
134  $action = '';
135  }
136  } elseif ($action == 'updateline' && $permissiontoedit) {
137  $line_ref = GETPOST('line_ref', 'alpha');
138  $line_value = GETPOST('line_value', 'alpha');
139 
140  $result = $object->updateLine($lineid, $line_ref, $line_value);
141  if ($result > 0) {
142  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
143  header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
144  exit();
145  } else {
146  setEventMessages($object->error, $object->errors, 'errors');
147  $action = 'editline';
148  }
149  }
150 }
151 
152 
153 /*
154  * View
155  */
156 
157 $title = $langs->trans('ProductAttributeName', dol_htmlentities($object->label));
158 $help_url = 'EN:Module_Products#Variants';
159 llxHeader('', $title, $help_url);
160 
161 // Part to create
162 if ($action == 'create') {
163  print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("ProductAttribute")), '', 'object_' . $object->picto);
164 
165  print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
166  print '<input type="hidden" name="token" value="' . newToken() . '">';
167  print '<input type="hidden" name="action" value="add">';
168  if ($backtopage) {
169  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
170  }
171  if ($backtopageforcancel) {
172  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
173  }
174 
175  print dol_get_fiche_head(array(), '');
176 
177  print '<table class="border centpercent tableforfieldcreate">' . "\n";
178 
179  // Common attributes
180  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php';
181 
182  print '</table>' . "\n";
183 
184  print dol_get_fiche_end();
185 
186  print '<div class="center">';
187  print '<input type="submit" class="button" name="add" value="' . dol_escape_htmltag($langs->trans("Create")) . '">';
188  print '&nbsp; ';
189  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
190  print '</div>';
191 
192  print '</form>';
193 
194  dol_set_focus('input[name="label"]');
195 } elseif (($id || $ref) && $action == 'edit') {
196  // Part to edit record
197  print load_fiche_titre($langs->trans("ProductAttribute"), '', 'object_' . $object->picto);
198 
199  print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
200  print '<input type="hidden" name="token" value="' . newToken() . '">';
201  print '<input type="hidden" name="action" value="update">';
202  print '<input type="hidden" name="id" value="' . $object->id . '">';
203  if ($backtopage) {
204  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
205  }
206  if ($backtopageforcancel) {
207  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
208  }
209 
210  print dol_get_fiche_head();
211 
212  print '<table class="border centpercent tableforfieldedit">' . "\n";
213 
214  // Common attributes
215  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';
216 
217  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
218  print $hookmanager->resPrint;
219 
220  print '</table>';
221 
222  print dol_get_fiche_end();
223 
224  print '<div class="center"><input type="submit" class="button button-save" name="save" value="' . $langs->trans("Save") . '">';
225  print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="' . $langs->trans("Cancel") . '">';
226  print '</div>';
227 
228  print '</form>';
229 } elseif ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
230  // Part to show record
231  $res = $object->fetch_optionals();
232 
233  $head = productAttributePrepareHead($object);
234  print dol_get_fiche_head($head, 'card', $langs->trans("ProductAttribute"), -1, $object->picto);
235 
236  $formconfirm = '';
237 
238  // Confirmation to delete
239  if ($action == 'delete') {
240  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteMyObject'), $langs->trans('ProductAttributeDeleteDialog'), 'confirm_delete', '', 0, 1);
241  } elseif ($action == 'ask_deleteline') {
242  // Confirmation to delete line
243  $object_value = new ProductAttributeValue($db);
244  if ($object_value->fetch($lineid) > 0) {
245  $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);
246  }
247  }
248 
249  // Call Hook formConfirm
250  $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
251  $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
252  if (empty($reshook)) {
253  $formconfirm .= $hookmanager->resPrint;
254  } elseif ($reshook > 0) {
255  $formconfirm = $hookmanager->resPrint;
256  }
257 
258  // Print form confirm
259  print $formconfirm;
260 
261  // Object card
262  // ------------------------------------------------------------
263  $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT . '/variants/list.php?leftmenu=?restore_lastsearch_values=1');
264  $linkback = '<a href="' . dol_sanitizeUrl($backtolist) . '">' . $langs->trans("BackToList") . '</a>';
265 
266  dol_banner_tab($object, 'id', $linkback);
267 
268  print '<div class="fichecenter">';
269  print '<div class="fichehalfleft">';
270  print '<div class="underbanner clearboth"></div>';
271  print '<table class="border centpercent tableforfield">' . "\n";
272 
273  // Common attributes
274  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_view.tpl.php';
275 
276  print '</table>';
277  print '</div>';
278  print '</div>';
279 
280  print '<div class="clearboth"></div>';
281 
282  print dol_get_fiche_end();
283 
284  // Buttons for actions
285  if ($action != 'editline') {
286  print '<div class="tabsAction">' . "\n";
287  $parameters = array();
288  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
289  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
290 
291  if (empty($reshook)) {
292  // Modify
293  print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit', '', $permissiontoedit);
294 
295  // Delete (need delete permission, or if draft, just need create/modify permission)
296  print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete', '', $permissiontodelete);
297  }
298  print '</div>' . "\n";
299  }
300 
301  /*
302  * Lines
303  */
304  if (!empty($object->table_element_line)) {
305  // Show object lines
306  $result = $object->getLinesArray();
307 
308  print load_fiche_titre($langs->trans("PossibleValues") . (!empty($object->lines) ? '<span class="opacitymedium colorblack paddingleft">(' . count($object->lines) . ')</span>' : ''));
309 
310  print ' <form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '' : '#line_' . GETPOST('lineid', 'int')) . '" method="POST">
311  <input type="hidden" name="token" value="' . newToken() . '">
312  <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
313  <input type="hidden" name="mode" value="">
314  <input type="hidden" name="page_y" value="">
315  <input type="hidden" name="id" value="' . $object->id . '">
316  ';
317  if ($backtopage) {
318  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
319  }
320  if ($backtopageforcancel) {
321  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
322  }
323 
324  if (!empty($conf->use_javascript_ajax)) {
325  include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
326  }
327 
328  print '<div class="div-table-responsive-no-min">';
329  if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
330  print '<table id="tablelines" class="noborder centpercent">';
331  }
332 
333  $object->printObjectLines($action, $mysoc, null, GETPOST('lineid', 'int'), 1, '/variants/tpl', ($permissiontoedit ? 1 : 0));
334 
335  if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
336  print '</table>';
337  }
338  print '</div>';
339 
340  print "</form>\n";
341  }
342 }
343 
344 // End of page
345 llxFooter();
346 $db->close();
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:118
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class ProductAttribute Used to represent a product attribute.
Class ProductAttributeValue Used to represent a product attribute value.
$parameters
Actions.
Definition: card.php:79
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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.
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
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.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
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.
isModEnabled($module)
Is Dolibarr module enabled.
$formconfirm
if ($action == 'delbookkeepingyear') {
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.