feat: Add Google Taxonomy integration and product editing enhancements

- Implemented Google Taxonomy loading via AJAX in main_view.php, allowing users to select categories for products.
- Enhanced product editing modal to include fields for product title, description, and Google category selection.
- Updated AJAX calls to save product data, including custom title, description, and selected Google category.
- Added character count validation for product title input.
- Integrated Select2 for improved category selection UI.
- Created google-taxonomy.php to fetch and cache Google Taxonomy data, ensuring efficient retrieval and fallback mechanisms.
- Removed outdated custom feed XML file.
- Updated layout-logged.php to include necessary Select2 styles and scripts.
This commit is contained in:
2025-11-20 23:46:21 +01:00
parent 92ce677f7c
commit f5db5263ab
11 changed files with 1424 additions and 38 deletions

View File

@@ -56,6 +56,38 @@
</div>
</div>
<script type="text/javascript">
var GOOGLE_TAXONOMY_ENDPOINT = '/tools/google-taxonomy.php'; // dostosuj ścieżkę
var googleCategories = [];
// główna funkcja używaj jej w modalu
function loadGoogleCategories(callback) {
if (googleCategories.length) {
// już załadowane wcześniej
callback(googleCategories);
return;
}
$.ajax({
url: GOOGLE_TAXONOMY_ENDPOINT,
dataType: 'json',
success: function (res) {
if (res.status === 'ok') {
googleCategories = res.categories || [];
callback(googleCategories);
} else {
console.error('Błąd pobierania taksonomii:', res);
callback([]);
}
},
error: function (xhr, status, error) {
console.error('AJAX error przy pobieraniu taksonomii:', status, error);
callback([]);
}
});
}
$( function()
{
$( 'body' ).on( 'change', '#client_id', function()
@@ -161,13 +193,21 @@
$( 'body' ).on( 'click', '.edit-product-title', function(e)
{
$.confirm({
title: 'Edytuj tytuł',
title: 'Edytuj produkt',
content: '' +
'<form action="" class="formName">' +
'<div class="form-group">' +
'<input type="text" value="' + escapeHtml( $( this ).siblings( 'a' ).text().trim() ) + '" product_id="' + $( this ).attr( 'product_id' ) + '" class="name form-control" required />' +
'<input type="text" value="" product_id="' + $( this ).attr( 'product_id' ) + '" placeholder="Tytuł produktu" class="name form-control" required />' +
'<small>0/150 znaków</small>' +
'</div>' +
'<div class="form-group">' +
'<textarea class="form-control description" rows="4" placeholder="Opis produktu (opcjonalnie)"></textarea>' +
'</div>' +
'<div class="form-group">' +
' <select class="form-control google-category" id="google_category" style="width: 100%">' +
' <option value="">— wybierz kategorię —</option>' +
' </select>' +
'</div>' +
'</form>',
columnClass: 'col-md-8 col-md-offset-2 col-12',
theme: 'modern',
@@ -180,6 +220,7 @@
var jc = this;
var product_id = this.$content.find( '.name' ).attr( 'product_id' );
var customTitle = this.$content.find('.name').val();
var googleProductCategory = this.$content.find('.google-category').val(); // NOWOŚĆ
if ( !customTitle )
{
@@ -196,11 +237,13 @@
jc.showLoading(true);
$.ajax({
url: '/products/save_custom_title/',
url: '/products/save_product_data/',
type: 'POST',
data: {
product_id: product_id,
custom_title: customTitle
custom_title: customTitle,
custom_description: this.$content.find('.description').val(),
google_product_category: googleProductCategory // NOWOŚĆ zapis
},
success: function(response) {
data = JSON.parse(response);
@@ -208,7 +251,7 @@
if ( data.status == 'ok' )
{
$.alert( 'Tytuł został pomyślnie zapisany' );
$.alert( 'Dane produktu zostały zapisane.' );
jc.close();
}
else
@@ -231,26 +274,80 @@
},
},
onContentReady: function () {
var jc = this;
var jc = this;
var inputField = this.$content.find('.name');
var charCount = this.$content.find('small');
var $form = this.$content.find('form');
var $inputField = this.$content.find('.name');
var $charCount = this.$content.find('small').first();
var $description = this.$content.find('.description');
var $googleCategory = this.$content.find('.google-category');
inputField.on('input', function() {
var currentLength = $(this).val().length;
charCount.text(currentLength + '/150 znaków');
// 1) Pobierz dane produktu
var product_id = $inputField.attr('product_id');
if (currentLength > 150) {
$(this).addClass('is-invalid');
} else {
$(this).removeClass('is-invalid');
}
});
$.ajax({
url: '/products/get_product_data/',
type: 'POST',
data: { product_id: product_id },
success: function (response) {
var data = JSON.parse(response);
this.$content.find('form').on('submit', function (e) {
e.preventDefault();
jc.$$formSubmit.trigger('click');
});
if (data.status == 'ok') {
if (data.product_details.title) {
$inputField.val(data.product_details.title);
var currentLength = data.product_details.title.length;
$charCount.text(currentLength + '/150 znaków');
}
if (data.product_details.description) {
$description.val(data.product_details.description);
}
// zapamiętujemy ID kategorii z backendu
jc.preselectedGoogleCategory = data.product_details.google_product_category || "";
} else {
$.alert('Błąd: ' + response);
}
}.bind(this),
error: function () {
$.alert('Wystąpił błąd podczas pobierania szczegółów produktu. Spróbuj ponownie.');
}
});
// 2) Pobierz kategorie z Twojego endpointu (bez CORS) i zainicjuj Select2
loadGoogleCategories(function (cats) {
if (typeof $.fn.select2 !== 'undefined') {
$googleCategory.select2({
placeholder: 'Wpisz fragment nazwy kategorii...',
allowClear: true,
data: cats,
dropdownParent: jc.$content.closest('.jconfirm-box')
});
if (jc.preselectedGoogleCategory) {
$googleCategory.val(jc.preselectedGoogleCategory).trigger('change');
}
} else {
console.warn('Select2 nie jest załadowany pole kategorii nie będzie miało wyszukiwarki.');
}
});
// licznik znaków
$inputField.on('input', function () {
var currentLength = $(this).val().length;
$charCount.text(currentLength + '/150 znaków');
if (currentLength > 150) {
$(this).addClass('is-invalid');
} else {
$(this).removeClass('is-invalid');
}
});
$form.on('submit', function (e) {
e.preventDefault();
jc.$$formSubmit.trigger('click');
});
}
});
});