View_helpers.php

PHP wanda 2 Vues Taille : 9.50 KB Date : Jan 15, 26 @ 6:39 PM
  1. 1<?php
  2. 2/**
  3. 3 * --------------------------------------------------------------------------
  4. 4 * PROJET - Helpers de Vue (Fonctions d'affichage)
  5. 5 * --------------------------------------------------------------------------
  6. 6 *
  7. 7 * Fichier : theme/default/partials/view_helpers.php
  8. 8 * Rôle : Contient les fonctions PHP nécessaires uniquement pour l'affichage
  9. 9 * (ex: rendu récursif des commentaires, comptage, nettoyage HTML).
  10. 10 *
  11. 11 * @function count_comments_total() Compte les commentaires + réponses.
  12. 12 * @function render_comment_node() Génère le HTML d'un commentaire et ses enfants.
  13. 13 *
  14. 14 * @included_by theme/default/view.php
  15. 15 */
  16. 16?>
  17. 17
  18. 18// Total count including replies
  19. 19if (!function_exists('count_comments_total')) {
  20. 20 function count_comments_total($list): int {
  21. 21 if (!is_array($list) || !$list) return 0;
  22. 22 if (!isset($list[0]['children'])) return (int)count($list);
  23. 23 $sum = 0;
  24. 24 foreach ($list as $n) {
  25. 25 $sum++;
  26. 26 if (!empty($n['children']) && is_array($n['children'])) {
  27. 27 $sum += count_comments_total($n['children']);
  28. 28 }
  29. 29 }
  30. 30 return $sum;
  31. 31 }
  32. 32}
  33. 33
  34. 34// Descendants counter
  35. 35if (!function_exists('count_descendants')) {
  36. 36 function count_descendants(array $node): int {
  37. 37 $sum = 0;
  38. 38 if (!empty($node['children']) && is_array($node['children'])) {
  39. 39 foreach ($node['children'] as $ch) {
  40. 40 $sum++;
  41. 41 $sum += count_descendants($ch);
  42. 42 }
  43. 43 }
  44. 44 return $sum;
  45. 45 }
  46. 46}
  47. 47
  48. 48// Safe body HTML
  49. 49if (!function_exists('comment_body_html_safe')) {
  50. 50 function comment_body_html_safe(array $c): string {
  51. 51 // Supposons que render_comment_html est défini ailleurs (functions.php global)
  52. 52 return isset($c['body_html'])
  53. 53 ? (string)$c['body_html']
  54. 54 : (function_exists('render_comment_html') ? render_comment_html((string)($c['body'] ?? '')) : htmlspecialchars($c['body'] ?? ''));
  55. 55 }
  56. 56}
  57. 57
  58. 58// Render Comment Node (Recursive)
  59. 59if (!function_exists('render_comment_node')) {
  60. 60 function render_comment_node(
  61. 61 array $c,
  62. 62 bool $can_comment,
  63. 63 string $commentsActionUrl,
  64. 64 string $csrf,
  65. 65 callable $loginNext,
  66. 66 int $depth = 0,
  67. 67 int $maxDepth = 3
  68. 68 ) {
  69. 69 global $baseurl, $mod_rewrite, $lang; // Récupération des globales nécessaires
  70. 70 $___base = rtrim((string)($baseurl ?? ''), '/') . '/';
  71. 71 $___mr = ((string)($mod_rewrite ?? '1') === '1');
  72. 72
  73. 73 $id = (int)($c['id'] ?? 0);
  74. 74 $username = (string)($c['username'] ?? 'Invité');
  75. 75 $ts = isset($c['created_at']) ? strtotime((string)$c['created_at']) : 0;
  76. 76 $ago = $ts ? ('il y a ' . (function_exists('conTime') ? conTime($ts) : date('Y-m-d', $ts))) : '';
  77. 77 $body_html = comment_body_html_safe($c);
  78. 78 $can_delete = !empty($c['can_delete']);
  79. 79 $children = is_array($c['children'] ?? null) ? $c['children'] : [];
  80. 80 $initial = strtoupper(mb_substr($username !== '' ? $username : 'G', 0, 1, 'UTF-8'));
  81. 81 $hasKids = !empty($children);
  82. 82 $clamped = ($depth >= $maxDepth) && $hasKids;
  83. 83 $threadId = 'thread-' . $id;
  84. 84 $descCnt = $clamped ? count_descendants($c) : 0;
  85. 85 $depthStyle = '--d:' . (int)$depth . ';';
  86. 86
  87. 87 // Inclure le template d'un commentaire unique
  88. 88 // Pour éviter de casser la récursion avec des includes, on garde le HTML ici ou on fait un template très simple.
  89. 89 // Pour simplifier ta vie maintenant, on garde le HTML généré ici, c'est le plus robuste pour la récursion.
  90. 90 ?>
  91. 91 <li id="c-<?php echo $id; ?>" class="list-group-item bg-body comment-item depth-<?php echo (int)$depth; ?>" style="<?php echo $depthStyle; ?>">
  92. 92 <div class="d-flex gap-3">
  93. 93 <div class="rounded-circle d-flex align-items-center justify-content-center flex-shrink-0 comment-avatar">
  94. 94 <span class="fw-bold"><?php echo htmlspecialchars($initial, ENT_QUOTES, 'UTF-8'); ?></span>
  95. 95 </div>
  96. 96 <div class="flex-grow-1 comment-main">
  97. 97 <div class="d-flex align-items-center justify-content-between mb-1">
  98. 98 <div class="d-flex align-items-center gap-2 flex-wrap">
  99. 99 <span class="fw-semibold">
  100. 100 <?php
  101. 101 $cu = trim($username);
  102. 102 if ($cu !== '' && strcasecmp($cu, 'Invité') !== 0) {
  103. 103 $cu_href = $___mr ? ($___base . 'user/' . rawurlencode($cu)) : ($___base . 'user.php?user=' . rawurlencode($cu));
  104. 104 echo '<a href="' . htmlspecialchars($cu_href, ENT_QUOTES, 'UTF-8') . '" class="text-decoration-none">' . htmlspecialchars($cu, ENT_QUOTES, 'UTF-8') . '</a>';
  105. 105 } else {
  106. 106 echo htmlspecialchars($cu ?: 'Invité', ENT_QUOTES, 'UTF-8');
  107. 107 }
  108. 108 ?>
  109. 109 </span>
  110. 110 <?php if ($ago): ?><span class="text-muted small"><?php echo htmlspecialchars($ago, ENT_QUOTES, 'UTF-8'); ?></span><?php endif; ?>
  111. 111 <a href="#c-<?php echo $id; ?>" class="comment-permalink ms-1 text-decoration-none">#</a>
  112. 112 </div>
  113. 113 <div class="d-flex align-items-center gap-2">
  114. 114 <?php if ($can_comment): ?>
  115. 115 <button type="button" class="btn btn-link btn-sm p-0 comment-reply" data-target="#reply-form-<?php echo $id; ?>">
  116. 116 <i class="bi bi-reply"></i> <?php echo htmlspecialchars($lang['reply'] ?? 'Répondre', ENT_QUOTES, 'UTF-8'); ?>
  117. 117 </button>
  118. 118 <?php endif; ?>
  119. 119 <?php if ($can_delete): ?>
  120. 120 <form method="post" action="<?php echo htmlspecialchars($commentsActionUrl, ENT_QUOTES, 'UTF-8'); ?>" class="d-inline delete-comment-form">
  121. 121 <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8'); ?>">
  122. 122 <input type="hidden" name="action" value="delete_comment">
  123. 123 <input type="hidden" name="comment_id" value="<?php echo $id; ?>">
  124. 124 <button type="submit" class="btn btn-sm btn-outline-danger border-0" title="Supprimer"><i class="bi bi-trash"></i></button>
  125. 125 </form>
  126. 126 <?php endif; ?>
  127. 127 </div>
  128. 128 </div>
  129. 129 <div class="comment-body lh-base"><?php echo $body_html; ?></div>
  130. 130
  131. 131 <?php if ($can_comment): ?>
  132. 132 <div id="reply-form-<?php echo $id; ?>" class="mt-2 d-none">
  133. 133 <form method="post" action="<?php echo htmlspecialchars($commentsActionUrl, ENT_QUOTES, 'UTF-8'); ?>">
  134. 134 <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8'); ?>">
  135. 135 <input type="hidden" name="action" value="add_comment">
  136. 136 <input type="hidden" name="parent_id" value="<?php echo $id; ?>">
  137. 137 <div class="mb-2">
  138. 138 <textarea class="form-control" name="comment_body" rows="3" required placeholder="Réponse..."></textarea>
  139. 139 </div>
  140. 140 <div class="d-flex justify-content-end gap-2">
  141. 141 <button type="button" class="btn btn-outline-secondary btn-sm reply-cancel" data-target="#reply-form-<?php echo $id; ?>">Annuler</button>
  142. 142 <button type="submit" class="btn btn-primary btn-sm">Répondre</button>
  143. 143 </div>
  144. 144 </form>
  145. 145 </div>
  146. 146 <?php endif; ?>
  147. 147
  148. 148 <?php if (!empty($children)): ?>
  149. 149 <?php if ($clamped): ?>
  150. 150 <div class="mt-2">
  151. 151 <button class="btn btn-outline-secondary btn-sm comment-expand" data-target="#<?php echo $threadId; ?>">
  152. 152 <i class="bi bi-chevron-down"></i> Afficher la suite
  153. 153 </button>
  154. 154 </div>
  155. 155 <ul id="<?php echo $threadId; ?>" class="list-group list-group-flush mt-2 d-none" style="--pd: <?php echo (int)$depth; ?>;">
  156. 156 <?php foreach ($children as $ch) { render_comment_node($ch, $can_comment, $commentsActionUrl, $csrf, $loginNext, $depth + 1, $maxDepth); } ?>
  157. 157 </ul>
  158. 158 <?php else: ?>
  159. 159 <ul class="list-group list-group-flush mt-2" style="--pd: <?php echo (int)$depth; ?>;">
  160. 160 <?php foreach ($children as $ch) { render_comment_node($ch, $can_comment, $commentsActionUrl, $csrf, $loginNext, $depth + 1, $maxDepth); } ?>
  161. 161 </ul>
  162. 162 <?php endif; ?>
  163. 163 <?php endif; ?>
  164. 164 </div>
  165. 165 </div>
  166. 166 </li>
  167. 167 <?php
  168. 168 }
  169. 169}
  170. 170?>
Commentaires 0
Connectez-vous pour publier un commentaire.
  • Aucun commentaire pour l’instant. Soyez le premier.
Connectez-vous pour publier un commentaire. Connexion ou inscription