Refactor ShopCategory class and grid.js to improve nested field handling and code readability
This commit is contained in:
@@ -190,6 +190,7 @@ class ShopCategory
|
||||
foreach ( $title as $key => $val )
|
||||
{
|
||||
if ( $translation_id = $mdb -> get( 'pp_shop_categories_langs', 'id', [ 'AND' => [ 'category_id' => $category_id, 'lang_id' => $key ] ] ) )
|
||||
{
|
||||
$mdb -> update( 'pp_shop_categories_langs', [
|
||||
'lang_id' => $key,
|
||||
'title' => $title[$key] != '' ? $title[$key] : null,
|
||||
@@ -205,6 +206,7 @@ class ShopCategory
|
||||
], [
|
||||
'id' => $translation_id
|
||||
] );
|
||||
}
|
||||
else
|
||||
$mdb -> insert( 'pp_shop_categories_langs', [
|
||||
'category_id' => (int)$category_id,
|
||||
|
||||
@@ -511,17 +511,25 @@ jQuery( 'body' ).on( 'click', '#g-save, #g-edit-save', function()
|
||||
var formattedValues = {};
|
||||
|
||||
jQuery.each(values, function(i, field) {
|
||||
var fieldName = field.name.replace(/\[(\w*)\]$/, ''); // usuwa [] na końcu nazwy
|
||||
var isArrayField = /\[\]$/.test(field.name); // sprawdza, czy pole jest tablicą
|
||||
var fieldName = field.name.replace(/\[\]$/, ''); // usuwa [] na końcu, aby traktować jako jedną tablicę
|
||||
var nestedFieldMatch = fieldName.match(/^(.+?)\[(.+?)\]$/); // dopasowuje pola z zagnieżdżeniem, np. title[pl]
|
||||
|
||||
if (isArrayField) {
|
||||
// jeśli pole jest tablicą, upewnij się, że mamy tablicę w obiekcie docelowym
|
||||
if (!Array.isArray(formattedValues[fieldName])) {
|
||||
if (nestedFieldMatch) {
|
||||
// Obsługa zagnieżdżonych pól, takich jak title[pl] lub title[en]
|
||||
var mainField = nestedFieldMatch[1];
|
||||
var subField = nestedFieldMatch[2];
|
||||
if (!formattedValues[mainField]) {
|
||||
formattedValues[mainField] = {};
|
||||
}
|
||||
formattedValues[mainField][subField] = field.value;
|
||||
} else if (field.name.endsWith('[]')) {
|
||||
// Obsługa pól tablicowych, takich jak categories[]
|
||||
if (!formattedValues[fieldName]) {
|
||||
formattedValues[fieldName] = [];
|
||||
}
|
||||
formattedValues[fieldName].push(field.value); // dodaj wartość do tablicy
|
||||
formattedValues[fieldName].push(field.value);
|
||||
} else {
|
||||
// dla zwykłych pól
|
||||
// Obsługa pojedynczych pól
|
||||
formattedValues[fieldName] = field.value;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user