Clean code

This commit is contained in:
Laurent Destailleur 2020-11-30 13:18:23 +01:00
parent 2de3a3d143
commit 379bca0b61
6 changed files with 189 additions and 177 deletions

View File

@ -2083,3 +2083,4 @@ CountryIfSpecificToOneCountry=Country (if specific to a given country)
YouMayFindSecurityAdviceHere=You may find security advisory here
ModuleActivatedMayExposeInformation=This module may expose sensitive data. If you don't need it, disable it.
ModuleActivatedDoNotUseInProduction=A module designed for the development has been enabled. Do not enable it on a production environment.
CombinationsSeparator=Separator character for product combinations

View File

@ -80,13 +80,11 @@ if ($action == 'addcontact' && $user->rights->projet->creer)
}
}
if ($result >= 0)
{
if ($result >= 0) {
header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id.($withproject ? '&withproject=1' : ''));
exit;
} else {$nbofcontacts
if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
{
} else {
if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
} else {
@ -98,8 +96,7 @@ if ($action == 'addcontact' && $user->rights->projet->creer)
// bascule du statut d'un contact
if ($action == 'swapstatut' && $user->rights->projet->creer)
{
if ($object->fetch($id, $ref))
{
if ($object->fetch($id, $ref)) {
$result = $object->swapContactStatus(GETPOST('ligne'));
} else {
dol_print_error($db);
@ -408,7 +405,6 @@ if ($id > 0 || !empty($ref))
print '<input type="hidden" name="id" value="'.$object->id.'">';
if ($withproject) print '<input type="hidden" name="withproject" value="'.$withproject.'">';
print '<tr class="oddeven">';
print '<td class="nowrap">';

View File

@ -22,44 +22,57 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
$langs->loadLangs(array("admin", "products"));
$action = GETPOST('action', 'alphanohtml');
// Security check
if (!$user->admin || (empty($conf->product->enabled) && empty($conf->service->enabled)))
accessforbidden();
if ($_POST) {
$error = 0;
/*
* Actions
*/
if ($action) {
$value = GETPOST('PRODUIT_ATTRIBUTES_HIDECHILD');
if (dolibarr_set_const($db, 'PRODUIT_ATTRIBUTES_HIDECHILD', $value, 'chaine', 0, '', $conf->entity)) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
} else {
if (!dolibarr_set_const($db, 'PRODUIT_ATTRIBUTES_HIDECHILD', $value, 'chaine', 0, '', $conf->entity)) {
setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors');
$error++;
}
if (dolibarr_set_const($db, 'PRODUIT_ATTRIBUTES_SEPARATOR', GETPOST('PRODUIT_ATTRIBUTES_SEPARATOR'), 'chaine', 0, '', $conf->entity)) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
} else {
if (!dolibarr_set_const($db, 'PRODUIT_ATTRIBUTES_SEPARATOR', GETPOST('PRODUIT_ATTRIBUTES_SEPARATOR'), 'chaine', 0, '', $conf->entity)) {
setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors');
$error++;
}
if (!$error) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
}
}
$title = $langs->trans('ModuleSetup').' '.$langs->trans('ProductAttributes');
$title = $langs->trans('ModuleSetup').' '.$langs->trans('Module610Name');
llxHeader('', $title);
$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($title, $linkback, 'title_setup');
print dol_get_fiche_head(array(), 'general', $tab, 0, 'product');
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 '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<th>'.$langs->trans("Parameters").'</td>'."\n";
print '<th class="right" width="60">'.$langs->trans("Value").'</td>'."\n";
print '<th width="80">&nbsp;</td></tr>'."\n";
print '<th>'.$langs->trans("Parameters").'</th>'."\n";
print '<th class="right" width="60">'.$langs->trans("Value").'</th>'."\n";
print '</tr>'."\n";
print '<tr class="oddeven"><td>'.$langs->trans('HideProductCombinations').'</td><td>';
print $form->selectyesno("PRODUIT_ATTRIBUTES_HIDECHILD", $conf->global->PRODUIT_ATTRIBUTES_HIDECHILD, 1).'</td></tr>';
print '<tr class="oddeven"><td>'.$langs->trans('CombinationsSeparator').'</td>';
if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
$separator = $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR;
@ -67,8 +80,11 @@ if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
$separator = "_";
}
print '<td class="right"><input size="3" type="text" class="flat" name="PRODUIT_ATTRIBUTES_SEPARATOR" value="'.$separator.'"></td></tr>';
print '</table>';
print '<br><div class="center"><input type="submit" value="'.$langs->trans("Save").'" class="button button-save"></div>';
print '</form>';
// End of page

View File

@ -43,7 +43,7 @@ if ($object->fetch($id) < 1) {
if ($cancel) $action = '';
if ($_POST) {
if ($action) {
if ($action == 'edit') {
$object->ref = $ref;
$object->label = $label;

View File

@ -72,7 +72,7 @@ $selectedvariant = $_SESSION['addvariant_'.$object->id];
if ($cancel) {
$action = '';
$massactions = '';
$massaction = '';
unset($_SESSION['addvariant_'.$object->id]);
}
@ -101,179 +101,177 @@ $prodcomb2val = new ProductCombination2ValuePair($db);
$productCombination2ValuePairs1 = array();
if ($_POST) {
if (($action == 'add' || $action == 'create') && empty($massaction) && !GETPOST('selectvariant', 'alpha')) // We click on Create all defined combinations
{
//$features = GETPOST('features', 'array');
$features = $_SESSION['addvariant_'.$object->id];
if (($action == 'add' || $action == 'create') && empty($massaction) && !GETPOST('selectvariant', 'alpha')) // We click on Create all defined combinations
{
//$features = GETPOST('features', 'array');
$features = $_SESSION['addvariant_'.$object->id];
if (!$features) {
setEventMessages($langs->trans('ErrorFieldsRequired'), null, 'errors');
} else {
$reference = trim($reference);
if (empty($reference)) {
$reference = false;
}
$weight_impact = price2num($weight_impact);
$price_impact = price2num($price_impact);
// for conf PRODUIT_MULTIPRICES
if ($conf->global->PRODUIT_MULTIPRICES) {
$level_price_impact = array_map('price2num', $level_price_impact);
}
else {
$level_price_impact = array(1 => $price_impact);
$level_price_impact_percent = array(1 => $price_impact_percent);
}
$sanit_features = array();
//First, sanitize
foreach ($features as $feature) {
$explode = explode(':', $feature);
if ($prodattr->fetch($explode[0]) < 0) {
continue;
}
if ($prodattr_val->fetch($explode[1]) < 0) {
continue;
}
// Valuepair
$sanit_features[$explode[0]] = $explode[1];
$tmp = new ProductCombination2ValuePair($db);
$tmp->fk_prod_attr = $explode[0];
$tmp->fk_prod_attr_val = $explode[1];
$productCombination2ValuePairs1[] = $tmp;
}
$db->begin();
// sanit_feature is an array with 1 (and only 1) value per attribute.
// For example: Color->blue, Size->Small, Option->2
//var_dump($sanit_features);
if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features))
{
$result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $level_price_impact_percent, $level_price_impact, $weight_impact, $reference);
if ($result > 0)
{
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
unset($_SESSION['addvariant_'.$object->id]);
$db->commit();
header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
exit();
} else {
$langs->load("errors");
setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
}
} else {
setEventMessages($langs->trans('ErrorRecordAlreadyExists'), null, 'errors');
}
$db->rollback();
if (!$features) {
setEventMessages($langs->trans('ErrorFieldsRequired'), null, 'errors');
} else {
$reference = trim($reference);
if (empty($reference)) {
$reference = false;
}
} elseif (!empty($massaction))
{
$bulkaction = $massaction;
$error = 0;
$db->begin();
foreach ($toselect as $prodid) {
// need create new of Product to prevent rename dir behavior
$prodstatic = new Product($db);
if ($prodstatic->fetch($prodid) < 0) {
continue;
}
if ($bulkaction == 'on_sell') {
$prodstatic->status = 1;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'on_buy') {
$prodstatic->status_buy = 1;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'not_sell') {
$prodstatic->status = 0;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'not_buy') {
$prodstatic->status_buy = 0;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'delete') {
$res = $prodstatic->delete($user, $prodstatic->id);
} else {
break;
}
if ($res <= 0) {
$error++;
break;
}
}
if ($error) {
$db->rollback();
if ($prodstatic->error) {
setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
} else {
setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors');
}
} else {
$db->commit();
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
}
} elseif ($valueid > 0) {
if ($prodcomb->fetch($valueid) < 0) {
dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
exit();
}
$prodcomb->variation_weight = $weight_impact;
$weight_impact = price2num($weight_impact);
$price_impact = price2num($price_impact);
// for conf PRODUIT_MULTIPRICES
if ($conf->global->PRODUIT_MULTIPRICES) {
$level_price_impact = array_map('price2num', $level_price_impact);
$prodcomb->variation_price = $level_price_impact[1];
$prodcomb->variation_price_percentage = (bool) $level_price_impact_percent[1];
}
else {
$level_price_impact = array(1 => $price_impact);
$level_price_impact_percent = array(1 => $price_impact_percent);
$prodcomb->variation_price = $price_impact;
$prodcomb->variation_price_percentage = $price_impact_percent;
}
if ($conf->global->PRODUIT_MULTIPRICES) {
$prodcomb->combination_price_levels = array();
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
$productCombinationLevel = new ProductCombinationLevel($db);
$productCombinationLevel->fk_product_attribute_combination = $prodcomb->id;
$productCombinationLevel->fk_price_level = $i;
$productCombinationLevel->variation_price = $level_price_impact[$i];
$productCombinationLevel->variation_price_percentage = (bool) $level_price_impact_percent[$i];
$prodcomb->combination_price_levels[$i] = $productCombinationLevel;
$sanit_features = array();
//First, sanitize
foreach ($features as $feature) {
$explode = explode(':', $feature);
if ($prodattr->fetch($explode[0]) < 0) {
continue;
}
if ($prodattr_val->fetch($explode[1]) < 0) {
continue;
}
// Valuepair
$sanit_features[$explode[0]] = $explode[1];
$tmp = new ProductCombination2ValuePair($db);
$tmp->fk_prod_attr = $explode[0];
$tmp->fk_prod_attr_val = $explode[1];
$productCombination2ValuePairs1[] = $tmp;
}
if ($prodcomb->update($user) > 0) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
exit();
$db->begin();
// sanit_feature is an array with 1 (and only 1) value per attribute.
// For example: Color->blue, Size->Small, Option->2
//var_dump($sanit_features);
if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features))
{
$result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $level_price_impact_percent, $level_price_impact, $weight_impact, $reference);
if ($result > 0)
{
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
unset($_SESSION['addvariant_'.$object->id]);
$db->commit();
header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
exit();
} else {
$langs->load("errors");
setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
}
} else {
setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
setEventMessages($langs->trans('ErrorRecordAlreadyExists'), null, 'errors');
}
$db->rollback();
}
} elseif (!empty($massaction)) {
$bulkaction = $massaction;
$error = 0;
$db->begin();
foreach ($toselect as $prodid) {
// need create new of Product to prevent rename dir behavior
$prodstatic = new Product($db);
if ($prodstatic->fetch($prodid) < 0) {
continue;
}
if ($bulkaction == 'on_sell') {
$prodstatic->status = 1;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'on_buy') {
$prodstatic->status_buy = 1;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'not_sell') {
$prodstatic->status = 0;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'not_buy') {
$prodstatic->status_buy = 0;
$res = $prodstatic->update($prodstatic->id, $user);
} elseif ($bulkaction == 'delete') {
$res = $prodstatic->delete($user, $prodstatic->id);
} else {
break;
}
if ($res <= 0) {
$error++;
break;
}
}
if ($error) {
$db->rollback();
if ($prodstatic->error) {
setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
} else {
setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors');
}
} else {
$db->commit();
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
}
} elseif ($valueid > 0) {
if ($prodcomb->fetch($valueid) < 0) {
dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
exit();
}
$prodcomb->variation_weight = $weight_impact;
// for conf PRODUIT_MULTIPRICES
if ($conf->global->PRODUIT_MULTIPRICES) {
$level_price_impact = array_map('price2num', $level_price_impact);
$prodcomb->variation_price = $level_price_impact[1];
$prodcomb->variation_price_percentage = (bool) $level_price_impact_percent[1];
}
else {
$level_price_impact = array(1 => $price_impact);
$level_price_impact_percent = array(1 => $price_impact_percent);
$prodcomb->variation_price = $price_impact;
$prodcomb->variation_price_percentage = $price_impact_percent;
}
if ($conf->global->PRODUIT_MULTIPRICES) {
$prodcomb->combination_price_levels = array();
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
$productCombinationLevel = new ProductCombinationLevel($db);
$productCombinationLevel->fk_product_attribute_combination = $prodcomb->id;
$productCombinationLevel->fk_price_level = $i;
$productCombinationLevel->variation_price = $level_price_impact[$i];
$productCombinationLevel->variation_price_percentage = (bool) $level_price_impact_percent[$i];
$prodcomb->combination_price_levels[$i] = $productCombinationLevel;
}
}
if ($prodcomb->update($user) > 0) {
setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
exit();
} else {
setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
}
}
// Reload variants
$productCombinations = $prodcomb->fetchAllByFkProductParent($object->id);

View File

@ -22,13 +22,14 @@ require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
$ref = GETPOST('ref', 'alpha');
$label = GETPOST('label', 'alpha');
$backtopage = GETPOST('backtopage', 'alpha');
$action = GETPOST('action', 'alpha');
/*
* Actions
*/
if ($_POST) {
if ($action == 'create') {
if (empty($ref) || empty($label)) {
setEventMessages($langs->trans('ErrorFieldsRequired'), null, 'errors');
} else {