Files
web_work/app/Views/layouts/main.php
kirill.khorkov a4092adf2e feat: Add complete reviews system with star ratings
 New Features:
- Reviews system with 1-5 star ratings
- User can add, edit, and delete their own reviews
- One review per product per user (DB constraint)
- Automatic average rating calculation
- Review count tracking
- Interactive star selection UI
- AJAX-powered review submission
- Responsive design for all devices

🗄️ Database:
- New 'reviews' table with full structure
- Added 'rating' and 'review_count' fields to products
- PostgreSQL triggers for automatic rating updates
- Database functions for rating calculations
- Indexes for performance optimization

📦 Backend (PHP):
- Review model with 15+ methods
- ReviewController with 5 actions
- Updated Product model to include ratings
- Updated ProductController to load reviews
- 5 new API endpoints

🎨 Frontend:
- Reviews list component (_reviews_list.php)
- Review form component (_review_form.php)
- Reviews sechow page
- Star ratings in catalog view
- Interactive JavaScript (200+ lines)
- Adaptive styles (400+ lines)

🔒 Security:
- Server-side authorization checks
- XSS protection (htmlspecialchars)
- SQL injection protection (PDO prepared)
- Input validation (client + server)
- Access control for review editing

📝 Modified Files:
- app/Models/Product.php - added rating fields to queries
- app/Controllers/ProductController.php - loads reviews
- app/Views/products/show.php - reviews section
- app/Views/products/catalog.php - star ratings
- config/routes.php - review endpoints
- public/style_for_cite.less - rating styles

🆕 New Files:
- app/Models/Review.php
- app/Controllers/ReviewController.php
- app/Views/products/_reviews_list.php
- app/Views/products/_review_form.php
2026-01-06 17:04:09 +03:00

140 lines
7.2 KiB
PHP

<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AETERNA - <?= $title ?? 'Мебель и Интерьер' ?></title>
<link rel="stylesheet/less" type="text/css" href="/style_for_cite.less">
<script src="https://cdn.jsdelivr.net/npm/less"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
.user-profile-dropdown { position: relative; display: inline-block; }
.user-profile-toggle { display: flex; align-items: center; gap: 10px; cursor: pointer; padding: 8px 12px; border-radius: 4px; transition: all 0.3s ease; }
.user-profile-toggle:hover { background-color: rgba(0, 0, 0, 0.05); }
.user-avatar { width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, #617365 0%, #453227 100%); color: white; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 16px; }
.user-info { display: flex; flex-direction: column; }
.user-email { font-size: 12px; color: #666; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-status { font-size: 10px; padding: 2px 8px; border-radius: 12px; text-transform: uppercase; font-weight: bold; text-align: center; margin-top: 2px; }
.user-status.admin { background-color: #617365; color: white; }
.user-status.user { background-color: #28a745; color: white; }
.user-profile-menu { display: none; position: absolute; top: 100%; right: 0; background: white; min-width: 280px; border-radius: 8px; box-shadow: 0 5px 20px rgba(0,0,0,0.15); z-index: 1000; padding-top: 10px; border: 1px solid #e0e0e0; }
.user-profile-dropdown:hover .user-profile-menu { display: block; }
.user-profile-header { padding: 15px; background: #f8f9fa; border-bottom: 1px solid #e0e0e0; }
.user-profile-name { font-weight: bold; margin-bottom: 5px; color: #333; display: flex; align-items: center; gap: 8px; }
.user-profile-details { font-size: 12px; color: #666; }
.user-profile-links { list-style: none; padding: 10px 0; margin: 0; }
.user-profile-links a { display: flex; align-items: center; gap: 10px; padding: 10px 15px; color: #333; text-decoration: none; transition: all 0.3s ease; border-left: 3px solid transparent; }
.user-profile-links a:hover { background-color: #f5f5f5; border-left-color: #453227; color: #453227; }
.logout-link { color: #dc3545 !important; }
.logout-link:hover { background-color: #ffe6e6 !important; border-left-color: #dc3545 !important; }
.cart-icon { position: relative; margin-right: 15px; }
.cart-count { position: absolute; top: -8px; right: -8px; background: #dc3545; color: white; border-radius: 50%; width: 18px; height: 18px; font-size: 11px; display: flex; align-items: center; justify-content: center; }
.notification { position: fixed; top: 20px; right: 20px; padding: 15px 20px; background: #28a745; color: white; border-radius: 4px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); z-index: 10000; transform: translateX(150%); transition: transform 0.3s ease; max-width: 300px; }
.notification.show { transform: translateX(0); }
.notification.error { background: #dc3545; }
</style>
</head>
<body>
<div id="notification" class="notification"></div>
<?php
// Загружаем категории для header
$categoryModel = new \App\Models\Category();
$headerCategories = $categoryModel->getActive();
?>
<?= \App\Core\View::partial('header', [
'user' => $user ?? null,
'isLoggedIn' => $isLoggedIn ?? \App\Core\View::isAuthenticated(),
'isAdmin' => $isAdmin ?? \App\Core\View::isAdmin(),
'categories' => $headerCategories ?? []
]) ?>
<main>
<?= $content ?>
</main>
<?= \App\Core\View::partial('footer') ?>
<script>
function showNotification(message, type) {
type = type || 'success';
var notification = $('#notification');
notification.text(message);
notification.removeClass('success error').addClass(type + ' show');
setTimeout(function() { notification.removeClass('show'); }, 3000);
}
// Выпадающий список категорий
(function() {
function initCatalogDropdown() {
var catalogDropdown = document.getElementById('catalogDropdown');
var catalogMenu = document.getElementById('catalogMenu');
if (!catalogDropdown || !catalogMenu) {
return;
}
// Обработчик клика на выпадающий список
catalogDropdown.addEventListener('click', function(e) {
e.stopPropagation();
var isActive = this.classList.contains('active');
if (isActive) {
this.classList.remove('active');
catalogMenu.style.display = 'none';
} else {
this.classList.add('active');
catalogMenu.style.display = 'block';
}
});
// Закрытие при клике вне меню
document.addEventListener('click', function(e) {
if (catalogDropdown && !catalogDropdown.contains(e.target)) {
catalogDropdown.classList.remove('active');
if (catalogMenu) {
catalogMenu.style.display = 'none';
}
}
});
// Закрытие при клике на ссылку в меню (через делегирование)
if (catalogMenu) {
catalogMenu.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
catalogDropdown.classList.remove('active');
catalogMenu.style.display = 'none';
}
});
}
}
// Инициализация при загрузке DOM
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initCatalogDropdown);
} else {
initCatalogDropdown();
}
// Резервная инициализация при полной загрузке страницы
window.addEventListener('load', function() {
var catalogDropdown = document.getElementById('catalogDropdown');
if (catalogDropdown && !catalogDropdown.hasAttribute('data-initialized')) {
catalogDropdown.setAttribute('data-initialized', 'true');
initCatalogDropdown();
}
});
})();
$(document).ready(function() {
$.get('/cart/count', function(response) {
if (response.success) {
$('.cart-count').text(response.cart_count);
}
});
});
</script>
</body>
</html>