211 lines
8.5 KiB
PHP
211 lines
8.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/init_client.php';
|
|
require_once __DIR__.'/../includes/header.php';
|
|
|
|
$praticien_id = (int)($_GET['praticien_id'] ?? 0);
|
|
if ($praticien_id <= 0) { echo "<div class='container py-5'>ID invalide.</div>"; exit; }
|
|
|
|
if (empty($_SESSION['csrf_token'])) $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
|
$CSRF = $_SESSION['csrf_token'];
|
|
|
|
function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
|
|
function percent_from_5($n){ $n=max(0,min(5,(float)$n)); return (int)round($n/5*100); }
|
|
function pseudo_from($prenom,$nom){
|
|
$p = trim((string)$prenom); $n = trim((string)$nom);
|
|
$p = $p!=='' ? mb_convert_case($p, MB_CASE_TITLE, 'UTF-8') : 'Client';
|
|
$i = $n!=='' ? mb_strtoupper(mb_substr($nom,0,1,'UTF-8'),'UTF-8') : '';
|
|
return $i ? ($p.'.'.$i) : $p;
|
|
}
|
|
|
|
// Titre + stats
|
|
$pp = $pdo->prepare("SELECT titre, prenom, nom FROM praticien WHERE id=?");
|
|
$pp->execute([$praticien_id]);
|
|
$pr = $pp->fetch(PDO::FETCH_ASSOC);
|
|
if (!$pr) { echo "<div class='container py-5'>".$translations['praticien_introuvable']."</div>"; exit; }
|
|
|
|
$avg = $pdo->prepare("SELECT ROUND(AVG(note),1) AS m, COUNT(*) AS c FROM avis_praticien WHERE praticien_id=? AND statut='publie'");
|
|
$avg->execute([$praticien_id]); $stats = $avg->fetch(PDO::FETCH_ASSOC) ?: ['m'=>null,'c'=>0];
|
|
|
|
// Form POST
|
|
$flash = null;
|
|
if ($_SERVER['REQUEST_METHOD']==='POST' && ($_POST['do'] ?? '')==='add') {
|
|
if (!hash_equals($_SESSION['csrf_token'] ?? '', $_POST['csrf'] ?? '')) {
|
|
$flash = ['type'=>'danger','msg'=>$translations['avis_form_err']];
|
|
} elseif ($_SESSION['role'] !== 'client') {
|
|
$flash = ['type'=>'danger','msg'=>$translations['non_autorise'] ?? 'Non autorisé'];
|
|
} else {
|
|
$note = (int)($_POST['note'] ?? 0);
|
|
$titre = trim((string)($_POST['titre'] ?? ''));
|
|
$commentaire = trim((string)($_POST['commentaire'] ?? ''));
|
|
$visit_date = trim((string)($_POST['visit_date'] ?? ''));
|
|
|
|
$ok = $note>=1 && $note<=5 && mb_strlen($commentaire,'UTF-8')>=10;
|
|
$date_ok = true;
|
|
if ($visit_date !== '') {
|
|
$ts = strtotime($visit_date.' 00:00:00');
|
|
$date_ok = $ts && $ts <= time()+86400; // pas dans le futur
|
|
}
|
|
if ($ok && $date_ok) {
|
|
$st = $pdo->prepare("INSERT INTO avis_praticien (praticien_id, client_id, note, titre, commentaire, visit_date, statut)
|
|
VALUES (?, ?, ?, ?, ?, ?, 'publie')");
|
|
$st->execute([$praticien_id, (int)$_SESSION['reference_id'], $note,
|
|
($titre!==''?$titre:null), ($commentaire!==''?$commentaire:null),
|
|
($visit_date!==''?$visit_date:null)]);
|
|
$flash = ['type'=>'success','msg'=>$translations['avis_form_ok']];
|
|
// refresh stats
|
|
$avg->execute([$praticien_id]); $stats = $avg->fetch(PDO::FETCH_ASSOC) ?: ['m'=>null,'c'=>0];
|
|
} else {
|
|
$flash = ['type'=>'danger','msg'=>$translations['avis_form_err']];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pagination
|
|
$perPage = 10;
|
|
$page = max(1, (int)($_GET['page'] ?? 1));
|
|
$offset = ($page - 1) * $perPage;
|
|
|
|
$cnt = $pdo->prepare("SELECT COUNT(*) FROM avis_praticien WHERE praticien_id = :pid AND statut = 'publie'");
|
|
$cnt->execute([':pid' => $praticien_id]);
|
|
$total = (int)$cnt->fetchColumn();
|
|
$pages = max(1, (int)ceil($total / $perPage));
|
|
|
|
$sql = "SELECT a.note, a.titre, a.commentaire, a.visit_date, a.created_at,
|
|
c.prenom, c.nom
|
|
FROM avis_praticien a
|
|
LEFT JOIN client c ON c.id = a.client_id
|
|
WHERE a.praticien_id = :pid AND a.statut = 'publie'
|
|
ORDER BY a.created_at DESC
|
|
LIMIT :o, :l";
|
|
|
|
$ls = $pdo->prepare($sql);
|
|
$ls->bindValue(':pid', $praticien_id, PDO::PARAM_INT);
|
|
$ls->bindValue(':o', (int)$offset, PDO::PARAM_INT);
|
|
$ls->bindValue(':l', (int)$perPage, PDO::PARAM_INT);
|
|
$ls->execute();
|
|
|
|
$avis = $ls->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// ID depuis l'URL (on passe bien ?praticien_id=...)
|
|
$praticien_id = (int)($_GET['praticien_id'] ?? 0);
|
|
if ($praticien_id <= 0) { echo "<div class='container py-5'>ID invalide.</div>"; exit; }
|
|
|
|
// Charger le praticien
|
|
$pp = $pdo->prepare("SELECT titre, prenom, nom FROM praticien WHERE id = :id");
|
|
$pp->execute([':id' => $praticien_id]);
|
|
$pr = $pp->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$pr) {
|
|
echo "<div class='container py-5'>".$translations['praticien_introuvable']."</div>";
|
|
exit;
|
|
}
|
|
|
|
// Définir $name AVANT de l'utiliser
|
|
$name = trim(
|
|
|
|
($pr['prenom'] ?? '')
|
|
. ' '
|
|
. ($pr['nom'] ?? '')
|
|
);
|
|
|
|
?>
|
|
<div class="main1">
|
|
<div class="container py-5">
|
|
<div class="shadow p-4 rounded bg-light">
|
|
<h2 class="mb-2"><?= h($translations['avis_titre_page']) ?> — <?= h($name) ?></h2>
|
|
|
|
<div class="d-flex align-items-center gap-3 mb-3">
|
|
<?php $m=(float)($stats['m'] ?? 0); $c=(int)($stats['c'] ?? 0); $w=percent_from_5($m); ?>
|
|
<div class="rating" aria-label="Note">
|
|
<div class="stars-base">★★★★★</div>
|
|
<div class="stars-fill" style="width:<?= $w ?>%">★★★★★</div>
|
|
</div>
|
|
<div class="text-muted"><?= $m ? number_format($m,1,',',' ') : '—' ?>/5 • <?= $c.' '.h($translations['avis_total']) ?></div>
|
|
</div>
|
|
|
|
<?php if ($flash): ?>
|
|
<div class="alert alert-<?= h($flash['type']) ?>"><?= h($flash['msg']) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($_SESSION['role']==='client'): ?>
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-body">
|
|
<h5 class="mb-3"><?= h($translations['avis_form_titre']) ?></h5>
|
|
<form method="post" class="row g-3">
|
|
<div class="col-12 col-md-4">
|
|
<label class="form-label"><?= h($translations['avis_form_note']) ?></label>
|
|
<select name="note" class="form-select" required>
|
|
<?php for($i=5;$i>=1;$i--): ?>
|
|
<option value="<?= $i ?>"> <?= $i ?> ★</option>
|
|
<?php endfor; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-12 col-md-4">
|
|
<label class="form-label"><?= h($translations['avis_form_date_visite']) ?></label>
|
|
<input type="date" name="visit_date" class="form-control">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label"><?= h($translations['avis_form_titre_lbl']) ?></label>
|
|
<input type="text" name="titre" class="form-control" maxlength="150">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label"><?= h($translations['avis_form_commentaire']) ?></label>
|
|
<textarea name="commentaire" class="form-control" rows="4" minlength="10" required></textarea>
|
|
</div>
|
|
<div class="col-12">
|
|
<input type="hidden" name="csrf" value="<?= h($CSRF) ?>">
|
|
<input type="hidden" name="do" value="add">
|
|
<button class="btn btn-primary"><?= h($translations['avis_form_envoyer']) ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!$avis): ?>
|
|
<div class="text-muted"><?= h($translations['avis_aucun']) ?></div>
|
|
<?php else: foreach ($avis as $a): $pw=percent_from_5((float)$a['note']); ?>
|
|
<div class="card shadow-sm mb-3">
|
|
<div class="card-body">
|
|
<div class="d-flex justify-content-between align-items-start mb-1">
|
|
<div>
|
|
<div class="rating mb-1">
|
|
<div class="stars-base">★★★★★</div>
|
|
<div class="stars-fill" style="width:<?= $pw ?>%">★★★★★</div>
|
|
</div>
|
|
<div class="small text-muted">
|
|
<strong><?= h(pseudo_from($a['prenom'] ?? '', $a['nom'] ?? '')) ?></strong>
|
|
<?php if(!empty($a['visit_date'])): ?> • <?= date('d/m/Y', strtotime($a['visit_date'])) ?><?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<div class="small text-muted"><?= date('d/m/Y', strtotime($a['created_at'])) ?></div>
|
|
</div>
|
|
<?php if(!empty($a['titre'])): ?><div class="fw-semibold mb-1"><?= h($a['titre']) ?></div><?php endif; ?>
|
|
<div><?= nl2br(h($a['commentaire'] ?? '')) ?></div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; endif; ?>
|
|
|
|
<?php if ($pages>1): ?>
|
|
<nav class="mt-3">
|
|
<?php for ($i=1;$i<=$pages;$i++):
|
|
$href = '/public/client/avis.php?praticien_id='.$praticien_id.'&page='.$i;
|
|
$cls = $i===$page ? 'btn btn-sm btn-primary me-1' : 'btn btn-sm btn-outline-primary me-1';
|
|
?>
|
|
<a class="<?= $cls ?>" href="<?= h($href) ?>"><?= $i ?></a>
|
|
<?php endfor; ?>
|
|
</nav>
|
|
<?php endif; ?>
|
|
|
|
<div class="mt-4">
|
|
<a class="btn btn-outline-secondary" href="/public/client/detail_praticien.php?id=<?= (int)$praticien_id ?>">⬅️ <?= $translations['retour_avis_profil'] ?></a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<footer class="site-footer">
|
|
<div class="container">
|
|
<p><?= $translations['copyr'] ?></p>
|
|
</div>
|
|
</footer>
|