prefix . 'wheel_prizes';
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$name = isset($_POST['prize_name']) ? sanitize_text_field($_POST['prize_name']) : '';
$description = isset($_POST['prize_description']) ? sanitize_textarea_field($_POST['prize_description']) : '';
$probability = isset($_POST['prize_probability']) ? floatval($_POST['prize_probability']) : 0;
$is_active = isset($_POST['prize_is_active']) ? 1 : 0;
$redemption_code = isset($_POST['prize_redemption_code']) ? sanitize_text_field($_POST['prize_redemption_code']) : '';
$is_discount = isset($_POST['prize_is_discount']) ? 1 : 0;
$discount_value = isset($_POST['prize_discount_value']) ? floatval($_POST['prize_discount_value']) : 0;
$email_subject = isset($_POST['prize_email_subject']) ? sanitize_text_field($_POST['prize_email_subject']) : '';
$email_template = isset($_POST['prize_email_template']) ? wp_kses_post($_POST['prize_email_template']) : '';
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (!empty($name) && $wheel_id > 0) {
$result = $wpdb->insert(
$prizes_table,
[
'wheel_id' => $wheel_id,
'name' => $name,
'description' => $description,
'probability' => $probability,
'is_active' => $is_active,
'redemption_code' => $redemption_code,
'is_discount' => $is_discount,
'discount_value' => $discount_value,
'email_subject' => $email_subject,
'email_template' => $email_template,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s', '%d', '%s', '%f']
);
if ($result !== false) {
echo '
' . __('Prize added successfully!', 'wheel-of-fortune') . '
';
} else {
echo '' . __('Error adding prize. Please try again.', 'wheel-of-fortune') . '
';
}
} else {
echo '' . __('Please fill in all required fields.', 'wheel-of-fortune') . '
';
}
} elseif ($_POST['action'] === 'edit_prize' && check_admin_referer('wheel_prizes_nonce')) {
global $wpdb;
$prizes_table = $wpdb->prefix . 'wheel_prizes';
$prize_id = isset($_POST['prize_id']) ? intval($_POST['prize_id']) : 0;
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$name = isset($_POST['prize_name']) ? sanitize_text_field($_POST['prize_name']) : '';
$description = isset($_POST['prize_description']) ? sanitize_textarea_field($_POST['prize_description']) : '';
$probability = isset($_POST['prize_probability']) ? floatval($_POST['prize_probability']) : 0;
$is_active = isset($_POST['prize_is_active']) ? 1 : 0;
$redemption_code = isset($_POST['prize_redemption_code']) ? sanitize_text_field($_POST['prize_redemption_code']) : '';
$is_discount = isset($_POST['prize_is_discount']) ? 1 : 0;
$discount_value = isset($_POST['prize_discount_value']) ? floatval($_POST['prize_discount_value']) : 0;
$email_subject = isset($_POST['prize_email_subject']) ? sanitize_text_field($_POST['prize_email_subject']) : '';
$email_template = isset($_POST['prize_email_template']) ? wp_kses_post($_POST['prize_email_template']) : '';
// Dodaj nova polja
$email_template_id = isset($_POST['email_template_id']) ? intval($_POST['email_template_id']) : 0;
$funded_account_name = isset($_POST['funded_account_name']) ? sanitize_text_field($_POST['funded_account_name']) : null;
$funded_account_value = isset($_POST['funded_account_value']) ? floatval($_POST['funded_account_value']) : null;
if (!empty($name) && $prize_id > 0 && $wheel_id > 0) {
$result = $wpdb->update(
$prizes_table,
[
'wheel_id' => $wheel_id,
'name' => $name,
'description' => $description,
'probability' => $probability,
'is_active' => $is_active,
'redemption_code' => $redemption_code,
'is_discount' => $is_discount,
'discount_value' => $discount_value,
'email_subject' => $email_subject,
'email_template' => $email_template,
// Dodaj nova polja
'email_template_id' => $email_template_id,
'funded_account_name' => $funded_account_name,
'funded_account_value' => $funded_account_value
],
['id' => $prize_id],
['%d', '%s', '%s', '%f', '%d', '%s', '%d', '%f', '%s', '%s', '%d', '%s', '%f'],
['%d']
);
if ($result !== false) {
echo '' . __('Prize updated successfully!', 'wheel-of-fortune') . '
';
} else {
echo '' . __('Error updating prize. Please try again.', 'wheel-of-fortune') . '
';
}
} else {
echo '' . __('Please fill in all required fields.', 'wheel-of-fortune') . '
';
}
}
}
if (
$_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) &&
in_array($_POST['action'], ['add_wheel_product', 'delete_wheel_product'])
) {
global $wpdb;
$wheels_table = $wpdb->prefix . 'wof_wheels';
$wheel_products_table = $wpdb->prefix . 'wheel_of_fortune_products';
if ($_POST['action'] === 'add_wheel_product' && check_admin_referer('wheel_products_nonce')) {
$wheel_id = isset($_POST['wheel_id']) ? intval($_POST['wheel_id']) : 0;
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
$spins_per_purchase = isset($_POST['spins_per_purchase']) ? intval($_POST['spins_per_purchase']) : 1;
// Debug informacije
error_log("=== WHEEL PRODUCT DEBUG ===");
error_log("POST data: " . print_r($_POST, true));
error_log("Wheel ID: " . $wheel_id);
error_log("Product ID: " . $product_id);
error_log("Spins per purchase: " . $spins_per_purchase);
error_log("Table name: " . $wheel_products_table);
// Preveri, ali tabela obstaja
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$wheel_products_table'") == $wheel_products_table;
error_log("Table exists: " . ($table_exists ? 'YES' : 'NO'));
if (!$table_exists) {
error_log("ERROR: Tabela $wheel_products_table ne obstaja!");
echo '' . __('Napaka: Tabela za produkte ne obstaja. Prosimo, deaktivirajte in ponovno aktivirajte plugin.', 'wheel-of-fortune') . '
';
return;
}
if ($wheel_id > 0 && $product_id > 0 && $spins_per_purchase > 0) {
// Preveri, ali kolo obstaja
$wheel_exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wheels_table WHERE id = %d", $wheel_id));
error_log("Wheel exists: " . $wheel_exists);
if (!$wheel_exists) {
error_log("ERROR: Kolo z ID $wheel_id ne obstaja!");
echo '' . __('Napaka: Izbrano kolo ne obstaja.', 'wheel-of-fortune') . '
';
return;
}
// Preveri, ali produkt obstaja
if (class_exists('WooCommerce')) {
$product = wc_get_product($product_id);
if (!$product) {
error_log("ERROR: Produkt z ID $product_id ne obstaja!");
echo '' . __('Napaka: Izbrani produkt ne obstaja.', 'wheel-of-fortune') . '
';
return;
}
}
$data = [
'wheel_id' => $wheel_id,
'product_id' => $product_id,
'spins_per_purchase' => $spins_per_purchase
];
error_log("Data to insert: " . print_r($data, true));
// Preveri, ali je produkt že povezan s tem kolesom
$existing_product = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM $wheel_products_table WHERE wheel_id = %d AND product_id = %d",
$wheel_id, $product_id
));
if ($existing_product) {
error_log("Product already exists for this wheel, updating...");
} else {
error_log("Adding new product to wheel...");
}
$result = $wpdb->replace(
$wheel_products_table,
$data,
['%d', '%d', '%d']
);
error_log("SQL result: " . $result);
error_log("Last SQL query: " . $wpdb->last_query);
error_log("Last SQL error: " . $wpdb->last_error);
if ($result !== false) {
echo '' . __('Produkt je bil uspešno dodan ali posodobljen.', 'wheel-of-fortune') . '
';
} else {
echo '' . __('Napaka pri dodajanju produkta. Preveri vnos.', 'wheel-of-fortune') . '
';
}
} else {
error_log("Validation failed - Wheel ID: $wheel_id, Product ID: $product_id, Spins: $spins_per_purchase");
echo '' . __('Napaka pri dodajanju produkta. Preveri vnos.', 'wheel-of-fortune') . '
';
}
} elseif ($_POST['action'] === 'delete_wheel_product' && check_admin_referer('wheel_products_nonce')) {
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
if ($id > 0) {
$wpdb->delete($wheel_products_table, ['id' => $id], ['%d']);
echo '' . __('Produkt je bil izbrisan.', 'wheel-of-fortune') . '
';
}
}
}
global $wpdb;
$wheels_table = $wpdb->prefix . 'wof_wheels';
$prizes_table = $wpdb->prefix . 'wheel_prizes';
// Get the current wheel ID from URL
$wheel_id = isset($_GET['wheel_id']) ? intval($_GET['wheel_id']) : 0;
// Fetch wheel data
$wheel = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wheels_table WHERE id = %d", $wheel_id), ARRAY_A);
if (!$wheel) {
echo '' . __('Wheel not found.', 'wheel-of-fortune') . '
';
return;
}
// Fetch prizes for this specific wheel
$prizes = $wpdb->get_results($wpdb->prepare("SELECT * FROM $prizes_table WHERE wheel_id = %d ORDER BY id ASC", $wheel_id), ARRAY_A);
$total_probability = array_sum(wp_list_pluck($prizes, 'probability'));
// Fetch povezane produkte za to kolo
$wheel_products_table = $wpdb->prefix . 'wheel_of_fortune_products';
$products = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wheel_products_table WHERE wheel_id = %d", $wheel_id), ARRAY_A);
// Pridobi vse WooCommerce produkte za dropdown
if (class_exists('WooCommerce')) {
$all_products = wc_get_products(array('limit' => -1, 'status' => 'publish'));
} else {
$all_products = array();
}
?>
←
1): ?>
' . esc_html($total_probability) . ''); ?>
0 && $total_probability < 1): ?>
' . esc_html($total_probability) . ''); ?>
get_name()) : esc_html($prod['product_id']); ?>
// Funkcija za prikaz/skrivanje polj za e-pošto po meri
function handleEmailSourceChange() {
// Za formo za dodajanje
if ($('#prize_email_template_id').val() == '0') {
$('.custom-email-fields').show();
} else {
$('.custom-email-fields').hide();
}
// Za modalno okno za urejanje
if ($('#edit-prize-email-template-id').val() == '0') {
$('.custom-email-fields-edit').show();
} else {
$('.custom-email-fields-edit').hide();
}
}
// Poveži event handler
$('body').on('change', '#prize_email_template_id, #edit-prize-email-template-id', handleEmailSourceChange);
// Sproži ob nalaganju strani za formo za dodajanje
handleEmailSourceChange();
$('.delete-wheel-product').on('click', function(e) {
e.preventDefault();
if (!confirm('Ali res želiš izbrisati ta produkt?')) return;
var row = $(this).closest('tr');
var id = $(this).data('id');
$.post(ajaxurl, {
action: 'wof_delete_wheel_product',
id: id,
_ajax_nonce: ''
}, function(response) {
if (response.success) {
row.fadeOut(300, function() { $(this).remove(); });
} else {
alert(response.data || 'Napaka pri brisanju.');
}
});
});
$('.spins-editable').on('click', function() {
var td = $(this);
if (td.find('input').length) return;
var current = td.text();
var id = td.data('id');
var input = $(' ').val(current);
td.html(input);
input.focus();
input.on('blur', function() {
var val = input.val();
if (val && val != current) {
$.post(ajaxurl, {
action: 'wof_update_wheel_product_spins',
id: id,
spins: val,
_ajax_nonce: ''
}, function(response) {
if (response.success) {
td.text(val);
} else {
alert(response.data || 'Napaka pri shranjevanju.');
td.text(current);
}
});
} else {
td.text(current);
}
});
});
// Modalno okno za urejanje nagrad
var modal = $('#edit-prize-modal');
var closeBtn = modal.find('.close');
$('.add-new-prize').on('click', function() {
$('html, body').animate({
scrollTop: $('.wheel-card:contains("Add New Prize")').offset().top
}, 500);
});
closeBtn.on('click', function() {
modal.hide();
});
$(window).on('click', function(e) {
if (e.target === modal[0]) {
modal.hide();
}
});
$('.edit-prize').on('click', function(e) {
e.preventDefault();
var prizeId = $(this).data('id');
// AJAX klic za pridobitev podatkov o nagradi
$.post(ajaxurl, {
action: 'wheel_get_prize_details',
prize_id: prizeId,
_ajax_nonce: ''
}, function(response) {
if (response.success) {
var prize = response.data;
// Napolni obrazec
$('#edit-prize-id').val(prize.id);
$('#edit-wheel-id').val(prize.wheel_id);
$('#edit-prize-name').val(prize.name);
$('#edit-prize-description').val(prize.description);
$('#edit-prize-probability').val(prize.probability);
$('#edit-prize-is-active').prop('checked', prize.is_active == 1);
$('#edit-prize-redemption-code').val(prize.redemption_code);
$('#edit-prize-is-discount').prop('checked', prize.is_discount == 1);
$('#edit-prize-discount-value').val(prize.discount_value);
$('#edit-prize-email-subject').val(prize.email_subject);
$('#edit-prize-email-template').val(prize.email_template);
// Napolni nova polja
$('#edit-funded-account-name').val(prize.funded_account_name);
$('#edit-funded-account-value').val(prize.funded_account_value);
$('#edit-prize-email-template-id').val(prize.email_template_id);
// Sproži spremembo, da se pravilno prikažejo/skrijejo polja
$('#edit-prize-email-template-id').trigger('change');
modal.show();
} else {
alert(response.data.message || 'Napaka pri pridobivanju podatkov o nagradi.');
}
}).fail(function() {
alert('Napaka pri komunikaciji s strežnikom.');
});
});
$('.delete-prize').on('click', function(e) {
e.preventDefault();
if (!confirm('Ali res želiš izbrisati to nagrado?')) return;
var row = $(this).closest('tr');
var prizeId = $(this).data('id');
$.post(ajaxurl, {
action: 'wheel_delete_prize',
prize_id: prizeId,
_ajax_nonce: ''
}, function(response) {
if (response.success) {
row.fadeOut(300, function() { $(this).remove(); });
} else {
alert(response.data.message || 'Napaka pri brisanju nagrade.');
}
}).fail(function() {
alert('Napaka pri komunikaciji s strežnikom.');
});
});
});