233 lines
12 KiB
PHP
233 lines
12 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
require_once 'includes/auth.php';
|
|
require_login();
|
|
require_once 'includes/helpers.php';
|
|
|
|
// This line is now required at the top of pages that use the header.
|
|
require_once 'includes/i18n.php';
|
|
|
|
$product_id = $_GET['id'] ?? null;
|
|
|
|
if (!$product_id) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT p.* FROM products p WHERE p.id = :product_id");
|
|
$stmt->execute(['product_id' => $product_id]);
|
|
$product = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$product) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
// Get the correct price pair using the centralized function
|
|
$prices = getEffectivePrice($pdo, $product['id'], $_SESSION['client_id']);
|
|
|
|
// Fetch product images
|
|
$img_stmt = $pdo->prepare("SELECT * FROM product_images WHERE product_id = ? ORDER BY is_primary DESC, id ASC");
|
|
$img_stmt->execute([$product_id]);
|
|
$product_images = $img_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
$primary_image = $product_images[0] ?? null;
|
|
|
|
} catch (PDOException $e) {
|
|
die(t('db_connection_error') . $e->getMessage());
|
|
}
|
|
|
|
$page_title = htmlspecialchars($product['name']);
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="<?= get_lang() ?>">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo $page_title; ?> - ExtraB2B</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
<?php require_once 'includes/header.php'; ?>
|
|
|
|
<main class="container my-5">
|
|
<a href="index.php" class="btn btn-outline-secondary mb-3">
|
|
← <?= t('back_to_product_list') ?>
|
|
</a>
|
|
<div class="row">
|
|
<!-- Product Image Gallery -->
|
|
<div class="col-lg-6 mb-4 mb-lg-0">
|
|
<div class="text-center">
|
|
<?php
|
|
$primary_image_url = 'https://placehold.co/600x400/EEE/31343C?text='.t('no_image_placeholder');
|
|
if (!empty($product_images)) {
|
|
$primary_image_url = 'uploads/products/' . htmlspecialchars($product_images[0]['file_path']);
|
|
}
|
|
?>
|
|
<img src="<?= $primary_image_url ?>" alt="<?= htmlspecialchars($product['name']) ?>" class="img-fluid rounded shadow-sm mb-3" id="main-product-image">
|
|
</div>
|
|
|
|
<?php if (count($product_images) > 1): ?>
|
|
<div class="row gx-2 justify-content-center">
|
|
<?php foreach ($product_images as $image): ?>
|
|
<div class="col-2">
|
|
<a href="uploads/products/<?= htmlspecialchars($image['file_path']) ?>" class="product-thumbnail d-block border rounded">
|
|
<img src="uploads/products/<?= htmlspecialchars($image['file_path']) ?>" alt="<?= t('product_thumbnail_alt') ?>" class="img-fluid">
|
|
</a>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- Product Details -->
|
|
<div class="col-lg-6">
|
|
<h1 class="mb-3"><?= htmlspecialchars($product['name']) ?></h1>
|
|
|
|
<div class="bg-light p-4 rounded mb-4">
|
|
<p class="h4 fw-bold mb-1"><?= htmlspecialchars(number_format($prices['gross'], 2, ',', ' ')) ?> zł <span class="fs-6 fw-normal"><?= t('gross') ?></span></p>
|
|
<p class="text-muted mb-0"><?= htmlspecialchars(number_format($prices['net'], 2, ',', ' ')) ?> zł <span class="fs-6 fw-normal"><?= t('net') ?></span></p>
|
|
<hr class="my-2">
|
|
<small class="text-muted"><?= t('price_per') ?>: <?= htmlspecialchars($product['unit']) ?></small>
|
|
</div>
|
|
|
|
<form action="cart_actions.php" method="post" class="d-flex align-items-center">
|
|
<input type="hidden" name="action" value="add">
|
|
<input type="hidden" name="product_id" value="<?= $product['id'] ?>">
|
|
<div style="max-width: 200px;" class="me-3">
|
|
<label for="quantity" class="form-label"><?= t('quantity_label') ?> (<?= htmlspecialchars($product['unit']) ?>):</label>
|
|
<input type="number" id="quantity" name="quantity" class="form-control" value="1" min="1">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary mt-4">
|
|
<i class="bi bi-cart-plus"></i> <?= t('btn_add_to_cart') ?>
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Additional Info Tabs -->
|
|
<div class="mt-5">
|
|
<ul class="nav nav-tabs" id="productTabs" role="tablist">
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link active" id="description-tab" data-bs-toggle="tab" data-bs-target="#description" type="button" role="tab" aria-controls="description" aria-selected="true"><?= t('description_tab') ?></button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="specs-tab" data-bs-toggle="tab" data-bs-target="#specs" type="button" role="tab" aria-controls="specs" aria-selected="false"><?= t('technical_data_tab') ?></button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="documents-tab" data-bs-toggle="tab" data-bs-target="#documents" type="button" role="tab" aria-controls="documents" aria-selected="false"><?= t('documents_tab') ?></button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link" id="related-tab" data-bs-toggle="tab" data-bs-target="#related" type="button" role="tab" aria-controls="related" aria-selected="false"><?= t('related_products_tab') ?></button>
|
|
</li>
|
|
</ul>
|
|
<div class="tab-content p-3 border border-top-0" id="productTabsContent">
|
|
<div class="tab-pane fade show active" id="description" role="tabpanel" aria-labelledby="description-tab">
|
|
<p class="lead mb-4"><?= nl2br(htmlspecialchars($product['description'])) ?></p>
|
|
</div>
|
|
<div class="tab-pane fade" id="specs" role="tabpanel" aria-labelledby="specs-tab">
|
|
<?php
|
|
$attrs_stmt = $pdo->prepare("SELECT ak.name, pa.value FROM product_attributes pa JOIN attribute_keys ak ON pa.attribute_key_id = ak.id WHERE pa.product_id = ? AND pa.value IS NOT NULL AND pa.value != '' ORDER BY ak.name");
|
|
$attrs_stmt->execute([$product_id]);
|
|
$product_attributes = $attrs_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($product_attributes) {
|
|
echo '<table class="table table-striped">';
|
|
echo '<tbody>';
|
|
foreach ($product_attributes as $attr) {
|
|
echo '<tr>';
|
|
echo '<th>' . htmlspecialchars($attr['name']) . '</th>';
|
|
echo '<td>' . htmlspecialchars($attr['value']) . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
echo '</tbody>';
|
|
echo '</table>';
|
|
} else {
|
|
echo '<p>' . t('no_technical_data') . '</p>';
|
|
}
|
|
?>
|
|
</div>
|
|
<div class="tab-pane fade" id="documents" role="tabpanel" aria-labelledby="documents-tab">
|
|
<?php
|
|
$docs_stmt = $pdo->prepare("SELECT * FROM product_documents WHERE product_id = ?");
|
|
$docs_stmt->execute([$product_id]);
|
|
$product_documents = $docs_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($product_documents) {
|
|
echo '<ul class="list-group list-group-flush">';
|
|
foreach ($product_documents as $doc) {
|
|
echo '<li class="list-group-item"><a href="uploads/documents/' . htmlspecialchars($doc['file_path']) . '" download><i class="bi bi-file-earmark-arrow-down"></i> ' . htmlspecialchars($doc['file_name']) . '</a></li>';
|
|
}
|
|
echo '</ul>';
|
|
} else {
|
|
echo '<p class="mb-0">' . t('no_documents') . '</p>';
|
|
}
|
|
?>
|
|
</div>
|
|
<div class="tab-pane fade" id="related" role="tabpanel" aria-labelledby="related-tab">
|
|
<div class="row">
|
|
<?php
|
|
$related_sql = "SELECT
|
|
p.*,
|
|
(SELECT file_path FROM product_images WHERE product_id = p.id ORDER BY is_primary DESC, id ASC LIMIT 1) AS image_path
|
|
FROM products p
|
|
JOIN product_relations pr ON p.id = pr.related_product_id
|
|
WHERE pr.product_id = ?";
|
|
$related_products_stmt = $pdo->prepare($related_sql);
|
|
$related_products_stmt->execute([$product_id]);
|
|
$related_products = $related_products_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($related_products) {
|
|
foreach ($related_products as $related_product) {
|
|
$related_image_url = !empty($related_product['image_path'])
|
|
? 'uploads/products/' . htmlspecialchars($related_product['image_path'])
|
|
: 'https://placehold.co/300x300/EEE/31343C?text='.t('no_image_placeholder');
|
|
|
|
echo '<div class="col-md-3 mb-3">';
|
|
echo '<div class="card h-100 product-card shadow-sm">';
|
|
echo '<a href="product.php?id=' . $related_product['id'] . '">';
|
|
echo '<img src="' . $related_image_url . '" class="card-img-top" alt="' . htmlspecialchars($related_product['name']) . '" style="height: 150px; object-fit: cover;">';
|
|
echo '</a>';
|
|
echo '<div class="card-body">';
|
|
echo '<h6 class="card-title"><a href="product.php?id=' . $related_product['id'] . '" class="text-decoration-none text-dark stretched-link">' . htmlspecialchars($related_product['name']) . '</a></h6>';
|
|
echo '</div>';
|
|
echo '</div>';
|
|
echo '</div>';
|
|
}
|
|
} else {
|
|
echo '<p class="mb-0">' . t('no_related_products') . '</p>';
|
|
}
|
|
?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const mainImage = document.getElementById('main-product-image');
|
|
const thumbnails = document.querySelectorAll('.product-thumbnail');
|
|
|
|
thumbnails.forEach(thumbnail => {
|
|
thumbnail.addEventListener('click', function(event) {
|
|
event.preventDefault();
|
|
mainImage.src = this.href;
|
|
|
|
thumbnails.forEach(t => t.classList.remove('active'));
|
|
this.classList.add('active');
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|