|
Server IP : 217.21.85.138 / Your IP : 216.73.216.103 Web Server : LiteSpeed System : Linux in-mum-web906.main-hosting.eu 4.18.0-553.37.1.lve.el8.x86_64 #1 SMP Mon Feb 10 22:45:17 UTC 2025 x86_64 User : u915722082 ( 915722082) PHP Version : 7.4.33 Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail MySQL : OFF | cURL : ON | WGET : ON | Perl : OFF | Python : OFF Directory (0755) : /home/u915722082/.nvm/../public_html/lohri/user/ |
| [ Home ] | [ C0mmand ] | [ Upload File ] |
|---|
<?php
// user/get_reviews.php - UPDATED to show user info properly
header('Content-Type: application/json');
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/../config/config.php';
try {
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4",
DB_USER,
DB_PASS,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pid = isset($_POST['pid']) ? (int)$_POST['pid'] : 0;
$variant_title = isset($_POST['variant_title']) ? trim($_POST['variant_title']) : '';
$variant_attributes = isset($_POST['variant_attributes']) ? json_decode($_POST['variant_attributes'], true) : [];
if ($pid > 0) {
// Get reviews with user info - FIXED to use the stored user_name
$sql = "
SELECT
COALESCE(pr.user_name, 'Anonymous') as user_name,
pr.user_email,
pr.user_id,
pr.rating,
pr.review_title,
pr.review_text,
pr.is_verified_purchase,
pr.created_at,
pr.variant_title,
pr.variant_attributes
FROM product_reviews pr
WHERE pr.pid = ?
AND (pr.variant_title = ? OR pr.variant_title IS NULL OR pr.variant_title = '')
ORDER BY pr.created_at DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$pid, $variant_title]);
$reviews = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Filter by variant attributes if specified
if (!empty($variant_attributes)) {
$filteredReviews = [];
foreach ($reviews as $review) {
if (empty($review['variant_attributes'])) {
$filteredReviews[] = $review;
} else {
$reviewAttributes = json_decode($review['variant_attributes'], true);
if ($reviewAttributes && array_intersect_assoc($variant_attributes, $reviewAttributes)) {
$filteredReviews[] = $review;
}
}
}
$reviews = $filteredReviews;
}
// Calculate review statistics
$total_reviews = count($reviews);
$rating_sum = array_sum(array_column($reviews, 'rating'));
$average_rating = $total_reviews > 0 ? $rating_sum / $total_reviews : 0;
// Rating breakdown
$rating_breakdown = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0];
foreach ($reviews as $review) {
$rating_breakdown[(int)$review['rating']]++;
}
// Check if current user can review this variant
$current_user_id = $_SESSION['user_id'] ?? null;
$can_review = false;
$user_has_reviewed = false;
if ($current_user_id) {
// Check if user already reviewed this variant
$checkSql = "SELECT id FROM product_reviews WHERE pid = ? AND user_id = ? AND (variant_title = ? OR variant_title IS NULL OR variant_title = '')";
$stmt = $pdo->prepare($checkSql);
$stmt->execute([$pid, $current_user_id, $variant_title]);
$user_has_reviewed = (bool)$stmt->fetch();
$can_review = !$user_has_reviewed;
}
echo json_encode([
'success' => true,
'reviews' => $reviews,
'stats' => [
'total_reviews' => $total_reviews,
'average_rating' => $average_rating,
'rating_breakdown' => $rating_breakdown
],
'user_info' => [
'logged_in' => !empty($current_user_id),
'can_review' => $can_review,
'has_reviewed' => $user_has_reviewed
]
]);
} else {
echo json_encode(['success' => false, 'message' => 'Invalid product ID']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
}
?>