Adds global variable 'PRODUCT_GENERATE_REF_AFTER_FORM' to allow product ref generation after the form validation

This commit is contained in:
Vincent Dieltiens 2021-08-13 11:23:55 +02:00
parent 0cf2164260
commit 2aef13c3b4

View File

@ -71,7 +71,7 @@ $mesg = ''; $error = 0; $errors = array();
$refalreadyexists = 0;
$id = GETPOST('id', 'int');
$ref = GETPOST('ref', 'alpha');
$ref = (GETPOST('ref', 'alpha') !== '') ? GETPOST('ref', 'alpha') : null;
$type = (GETPOST('type', 'int') !== '') ? GETPOST('type', 'int') : Product::TYPE_PRODUCT;
$action = (GETPOST('action', 'alpha') ? GETPOST('action', 'alpha') : 'view');
$cancel = GETPOST('cancel', 'alpha');
@ -93,6 +93,18 @@ $label_security_check = empty($conf->global->MAIN_SECURITY_ALLOW_UNSECURED_LABEL
if (!empty($user->socid)) $socid = $user->socid;
// Load object modCodeProduct
$module = (!empty($conf->global->PRODUCT_CODEPRODUCT_ADDON) ? $conf->global->PRODUCT_CODEPRODUCT_ADDON : 'mod_codeproduct_leopard');
if (substr($module, 0, 16) == 'mod_codeproduct_' && substr($module, -3) == 'php')
{
$module = substr($module, 0, dol_strlen($module) - 4);
}
$result = dol_include_once('/core/modules/product/'.$module.'.php');
if ($result > 0)
{
$modCodeProduct = new $module();
}
$object = new Product($db);
$object->type = $type; // so test later to fill $usercancxxx is correct
$extrafields = new ExtraFields($db);
@ -212,9 +224,11 @@ if (empty($reshook))
}
if (empty($ref))
{
setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Ref')), null, 'errors');
$action = "create";
$error++;
if (empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) {
setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Ref')), null, 'errors');
$action = "create";
$error++;
}
}
if (!empty($duration_value) && empty($duration_unit))
{
@ -371,6 +385,11 @@ if (empty($reshook))
$ret = $extrafields->setOptionalsFromPost(null, $object);
if ($ret < 0) $error++;
if (!$ref && !empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) {
// Generate ref...
$ref = $modCodeProduct->getNextValue($object, $type);
}
if (!$error)
{
$id = $object->create($user);
@ -411,7 +430,9 @@ if (empty($reshook))
{
$object->oldcopy = clone $object;
$object->ref = $ref;
if (empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) {
$object->ref = $ref;
}
$object->label = GETPOST('label', $label_security_check);
$object->description = dol_htmlcleanlastbr(GETPOST('desc', 'restricthtml'));
$object->url = GETPOST('url');
@ -995,15 +1016,17 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) {
print '<table class="border centpercent">';
print '<tr>';
$tmpcode = '';
if (!empty($modCodeProduct->code_auto)) $tmpcode = $modCodeProduct->getNextValue($object, $type);
print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td colspan="3"><input id="ref" name="ref" class="maxwidth200" maxlength="128" value="'.dol_escape_htmltag(GETPOSTISSET('ref') ? GETPOST('ref', 'alphanohtml') : $tmpcode).'">';
if ($refalreadyexists)
{
print $langs->trans("RefAlreadyExists");
if (empty($conf->global->PRODUCT_GENERATE_REF_AFTER_FORM)) {
print '<tr>';
$tmpcode = '';
if (!empty($modCodeProduct->code_auto)) $tmpcode = $modCodeProduct->getNextValue($object, $type);
print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td colspan="3"><input id="ref" name="ref" class="maxwidth200" maxlength="128" value="'.dol_escape_htmltag(GETPOSTISSET('ref') ? GETPOST('ref', 'alphanohtml') : $tmpcode).'">';
if ($refalreadyexists)
{
print $langs->trans("RefAlreadyExists");
}
print '</td></tr>';
}
print '</td></tr>';
// Label
print '<tr><td class="fieldrequired">'.$langs->trans("Label").'</td><td colspan="3"><input name="label" class="minwidth300 maxwidth400onsmartphone" maxlength="255" value="'.dol_escape_htmltag(GETPOST('label', $label_security_check)).'"></td></tr>';
@ -2176,18 +2199,6 @@ if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) {
}
}
// Load object modCodeProduct
$module = (!empty($conf->global->PRODUCT_CODEPRODUCT_ADDON) ? $conf->global->PRODUCT_CODEPRODUCT_ADDON : 'mod_codeproduct_leopard');
if (substr($module, 0, 16) == 'mod_codeproduct_' && substr($module, -3) == 'php')
{
$module = substr($module, 0, dol_strlen($module) - 4);
}
$result = dol_include_once('/core/modules/product/'.$module.'.php');
if ($result > 0)
{
$modCodeProduct = new $module();
}
$tmpcode = '';
if (!empty($modCodeProduct->code_auto)) $tmpcode = $modCodeProduct->getNextValue($object, $object->type);