dolibarr/htdocs/variants/card.php

305 lines
9.2 KiB
PHP
Raw Normal View History

2016-07-23 16:37:21 +02:00
<?php
2018-07-25 09:36:34 +02:00
/* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
2016-07-23 16:37:21 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-07-23 16:37:21 +02:00
*/
require '../main.inc.php';
require 'class/ProductAttribute.class.php';
require 'class/ProductAttributeValue.class.php';
$id = GETPOST('id', 'int');
$valueid = GETPOST('valueid', 'alpha');
2020-09-16 19:39:50 +02:00
$action = GETPOST('action', 'aZ09');
$label = GETPOST('label', 'alpha');
$ref = GETPOST('ref', 'alpha');
$confirm = GETPOST('confirm', 'alpha');
$cancel = GETPOST('cancel', 'alpha');
2016-07-23 16:37:21 +02:00
2017-04-12 13:01:41 +02:00
$object = new ProductAttribute($db);
$objectval = new ProductAttributeValue($db);
2016-07-23 16:37:21 +02:00
2017-04-12 13:01:41 +02:00
if ($object->fetch($id) < 1) {
2016-07-23 16:37:21 +02:00
dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
2017-02-08 12:52:31 +01:00
exit();
2016-07-23 16:37:21 +02:00
}
2021-03-20 18:58:34 +01:00
$permissiontoread = $user->rights->produit->lire || $user->rights->service->lire;
// Security check
if (empty($conf->variants->enabled)) {
accessforbidden('Module not enabled');
}
if ($user->socid > 0) { // Protection if external user
accessforbidden();
}
//$result = restrictedArea($user, 'variant');
if (!$permissiontoread) accessforbidden();
/*
* Actions
*/
if ($cancel) {
$action = '';
}
2020-11-30 13:18:23 +01:00
if ($action) {
if ($action == 'update') {
2017-04-12 13:01:41 +02:00
$object->ref = $ref;
2017-11-26 15:08:00 +01:00
$object->label = $label;
2016-07-23 16:37:21 +02:00
2017-11-26 15:08:00 +01:00
if ($object->update($user) < 1) {
2018-07-25 09:36:34 +02:00
setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors');
2016-07-23 16:37:21 +02:00
} else {
2018-07-27 14:49:33 +02:00
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
header('Location: '.dol_buildpath('/variants/card.php?id='.$id, 2));
2017-02-08 12:52:31 +01:00
exit();
2016-07-23 16:37:21 +02:00
}
} elseif ($action == 'update_value') {
2017-04-12 13:01:41 +02:00
if ($objectval->fetch($valueid) > 0) {
$objectval->ref = $ref;
$objectval->value = GETPOST('value', 'alpha');
2017-11-26 15:08:00 +01:00
if (empty($objectval->ref)) {
2017-11-26 15:08:00 +01:00
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref")), null, 'errors');
}
if (empty($objectval->value)) {
2017-11-26 15:08:00 +01:00
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
}
2016-07-23 16:37:21 +02:00
if (!$error) {
2017-11-26 15:08:00 +01:00
if ($objectval->update($user) > 0) {
2018-07-27 14:49:33 +02:00
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
2017-11-26 15:08:00 +01:00
} else {
setEventMessage($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors');
2017-11-26 15:08:00 +01:00
}
2016-07-23 16:37:21 +02:00
}
}
2017-04-12 13:01:41 +02:00
header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
2017-02-08 12:52:31 +01:00
exit();
2016-07-23 16:37:21 +02:00
}
}
if ($confirm == 'yes') {
if ($action == 'confirm_delete') {
$db->begin();
2020-08-08 22:14:53 +02:00
$res = $objectval->deleteByFkAttribute($object->id, $user);
2016-07-23 16:37:21 +02:00
2020-08-08 22:14:53 +02:00
if ($res < 1 || ($object->delete($user) < 1)) {
2016-07-23 16:37:21 +02:00
$db->rollback();
2018-07-25 09:36:34 +02:00
setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors');
2017-04-12 13:01:41 +02:00
header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
2016-07-23 16:37:21 +02:00
} else {
$db->commit();
2018-07-27 14:49:33 +02:00
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
header('Location: '.dol_buildpath('/variants/list.php', 2));
2016-07-23 16:37:21 +02:00
}
2017-02-08 12:52:31 +01:00
exit();
} elseif ($action == 'confirm_deletevalue') {
2017-04-12 13:01:41 +02:00
if ($objectval->fetch($valueid) > 0) {
2020-08-08 22:14:53 +02:00
if ($objectval->delete($user) < 1) {
2018-07-25 09:36:34 +02:00
setEventMessages($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors');
2016-07-23 16:37:21 +02:00
} else {
2018-07-27 14:49:33 +02:00
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
2016-07-23 16:37:21 +02:00
}
2017-04-12 13:01:41 +02:00
header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
2017-02-08 12:52:31 +01:00
exit();
2016-07-23 16:37:21 +02:00
}
}
}
/*
* View
*/
2016-07-23 16:37:21 +02:00
$langs->load('products');
$help_url = 'EN:Module_Products#Variants';
2017-04-12 13:01:41 +02:00
$title = $langs->trans('ProductAttributeName', dol_htmlentities($object->label));
2016-07-23 16:37:21 +02:00
llxHeader('', $title, $help_url);
2016-07-23 16:37:21 +02:00
2018-09-09 15:52:55 +02:00
//print load_fiche_titre($title);
2017-04-12 13:01:41 +02:00
$h = 0;
2017-04-12 13:01:41 +02:00
$head[$h][0] = DOL_URL_ROOT.'/variants/card.php?id='.$object->id;
2020-05-17 13:34:16 +02:00
$head[$h][1] = $langs->trans("ProductAttributeName");
2017-04-12 13:01:41 +02:00
$head[$h][2] = 'variant';
$h++;
2016-07-23 16:37:21 +02:00
2020-10-22 22:50:03 +02:00
print dol_get_fiche_head($head, 'variant', $langs->trans('ProductAttributeName'), -1, 'generic');
2016-07-23 16:37:21 +02:00
if ($action == 'edit') {
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update">';
print '<input type="hidden" name="id" value="'.$id.'">';
print '<input type="hidden" name="valueid" value="'.$valueid.'">';
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
2020-12-21 18:49:49 +01:00
}
2016-07-23 16:37:21 +02:00
2017-04-12 13:01:41 +02:00
2019-10-26 20:53:39 +02:00
if ($action != 'edit') {
print '<div class="fichecenter">';
print '<div class="underbanner clearboth"></div>';
2017-04-12 13:01:41 +02:00
}
2020-01-20 12:25:25 +01:00
print '<table class="border centpercent tableforfield">';
2019-10-26 20:53:39 +02:00
print '<tr>';
2020-01-20 12:25:25 +01:00
print '<td class="titlefield'.($action == 'edit' ? ' fieldrequired' : '').'">'.$langs->trans('Ref').'</td>';
2019-10-26 20:53:39 +02:00
print '<td>';
if ($action == 'edit') {
print '<input type="text" name="ref" value="'.$object->ref.'">';
} else {
print dol_htmlentities($object->ref);
}
print '</td>';
print '</tr>';
print '<tr>';
2020-01-20 12:25:25 +01:00
print '<td'.($action == 'edit' ? ' class="fieldrequired"' : '').'>'.$langs->trans('Label').'</td>';
2019-10-26 20:53:39 +02:00
print '<td>';
if ($action == 'edit') {
print '<input type="text" name="label" value="'.$object->label.'">';
} else {
print dol_htmlentities($object->label);
}
print '</td>';
print '</tr>';
2016-07-23 16:37:21 +02:00
2019-10-26 20:53:39 +02:00
print '</table>';
2016-07-23 16:37:21 +02:00
2017-04-12 13:01:41 +02:00
2019-10-26 20:53:39 +02:00
if ($action != 'edit') {
print '</div>';
2017-04-12 13:01:41 +02:00
}
2020-10-27 18:19:31 +01:00
print dol_get_fiche_end();
2016-07-23 16:37:21 +02:00
2019-10-26 20:53:39 +02:00
if ($action == 'edit') {
print '<div style="text-align: center;">';
print '<div class="inline-block divButAction">';
print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
2019-10-26 20:53:39 +02:00
print '&nbsp; &nbsp;';
print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
2019-10-26 20:53:39 +02:00
print '</div>';
print '</div></form>';
} else {
2016-07-23 16:37:21 +02:00
if ($action == 'delete') {
$form = new Form($db);
2019-10-26 20:53:39 +02:00
print $form->formconfirm(
2017-04-12 13:01:41 +02:00
"card.php?id=".$object->id,
2016-07-23 16:37:21 +02:00
$langs->trans('Delete'),
$langs->trans('ProductAttributeDeleteDialog'),
"confirm_delete",
'',
0,
1
);
} elseif ($action == 'delete_value') {
2017-04-12 13:01:41 +02:00
if ($objectval->fetch($valueid) > 0) {
2016-07-23 16:37:21 +02:00
$form = new Form($db);
2019-10-26 20:53:39 +02:00
print $form->formconfirm(
2017-04-12 13:01:41 +02:00
"card.php?id=".$object->id."&valueid=".$objectval->id,
2016-07-23 16:37:21 +02:00
$langs->trans('Delete'),
2017-04-12 13:01:41 +02:00
$langs->trans('ProductAttributeValueDeleteDialog', dol_htmlentities($objectval->value), dol_htmlentities($objectval->ref)),
2016-07-23 16:37:21 +02:00
"confirm_deletevalue",
'',
0,
1
);
}
}
?>
<div class="tabsAction">
<div class="inline-block divButAction">
<a href="card.php?id=<?php echo $object->id ?>&action=edit&token=<?php echo newToken(); ?>" class="butAction"><?php echo $langs->trans('Modify') ?></a>
<a href="card.php?id=<?php echo $object->id ?>&action=delete&token=<?php echo newToken(); ?>" class="butAction"><?php echo $langs->trans('Delete') ?></a>
2016-07-23 16:37:21 +02:00
</div>
</div>
2017-11-26 10:13:32 +01:00
<?php
2018-09-09 15:52:55 +02:00
print load_fiche_titre($langs->trans("PossibleValues"));
2017-11-26 10:13:32 +01:00
if ($action == 'edit_value') {
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update_value">';
2017-11-26 10:13:32 +01:00
print '<input type="hidden" name="id" value="'.$id.'">';
print '<input type="hidden" name="valueid" value="'.$valueid.'">';
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
2019-10-26 20:53:39 +02:00
}
print '<table class="liste">';
print '<tr class="liste_titre">';
print '<th class="liste_titre titlefield">'.$langs->trans('Ref').'</th>';
print '<th class="liste_titre">'.$langs->trans('Value').'</th>';
print '<th class="liste_titre"></th>';
print '</tr>';
foreach ($objectval->fetchAllByProductAttribute($object->id) as $attrval) {
print '<tr class="oddeven">';
if ($action == 'edit_value' && ($valueid == $attrval->id)) {
?>
2016-07-23 16:37:21 +02:00
<td><input type="text" name="ref" value="<?php echo $attrval->ref ?>"></td>
<td><input type="text" name="value" value="<?php echo $attrval->value ?>"></td>
2019-02-16 08:00:16 +01:00
<td class="right">
<input type="submit" value="<?php echo $langs->trans("Save") ?>" class="button button-save">
2017-11-26 10:13:32 +01:00
&nbsp; &nbsp;
<input type="submit" name="cancel" value="<?php echo $langs->trans("Cancel") ?>" class="button button-cancel">
2016-07-23 16:37:21 +02:00
</td>
2019-10-26 20:53:39 +02:00
<?php
} else {
?>
2016-07-23 16:37:21 +02:00
<td><?php echo dol_htmlentities($attrval->ref) ?></td>
<td><?php echo dol_htmlentities($attrval->value) ?></td>
2019-02-16 08:00:16 +01:00
<td class="right">
2020-06-12 19:30:23 +02:00
<a class="editfielda marginrightonly" href="card.php?id=<?php echo $object->id ?>&action=edit_value&valueid=<?php echo $attrval->id ?>"><?php echo img_edit() ?></a>
<a href="card.php?id=<?php echo $object->id ?>&action=delete_value&token=<?php echo newToken(); ?>&valueid=<?php echo $attrval->id ?>"><?php echo img_delete() ?></a>
2016-07-23 16:37:21 +02:00
</td>
2019-10-26 20:53:39 +02:00
<?php
2017-11-26 10:13:32 +01:00
}
2019-10-26 20:53:39 +02:00
print '</tr>';
}
print '</table>';
2016-07-23 16:37:21 +02:00
2019-10-26 20:53:39 +02:00
if ($action == 'edit_value') {
print '</form>';
}
2016-07-23 16:37:21 +02:00
2019-10-26 20:53:39 +02:00
print '<div class="tabsAction">';
print '<div class="inline-block divButAction">';
print '<a href="create_val.php?id='.$object->id.'" class="butAction">'.$langs->trans('Create').'</a>';
print '</div>';
print '</div>';
2016-07-23 16:37:21 +02:00
}
2018-08-04 15:58:05 +02:00
// End of page
llxFooter();
2018-11-26 16:09:05 +01:00
$db->close();