WebOrbiton
v1.0.0.3

StocketBase

100 lines · 4.0 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. if (count(get_included_files()) === 1) {
  6. http_response_code(403);
  7. exit;
  8. }
  9. ​
  10. if (!Auth::hasRoleAtLeast(Auth::ROLE_STORE_MANAGER)) {
  11. echo '<p>You don\'t have access to reviews.</p>';
  12. return;
  13. }
  14. ​
  15. $usersDb = Database::users();
  16. $db = Database::site();
  17. ​
  18. $statusFilter = in_array($_GET['status'] ?? '', ['visible', 'hidden', 'flagged'], true) ? $_GET['status'] : 'visible';
  19. ​
  20. $statement = $usersDb->prepare(
  21. 'SELECT r.*, u.display_name, u.email FROM product_reviews r LEFT JOIN user_accounts u ON u.id = r.user_account_id
  22. WHERE r.status = :status ORDER BY r.created_at DESC LIMIT 200'
  23. );
  24. $statement->execute(['status' => $statusFilter]);
  25. $reviews = $statement->fetchAll();
  26. $flaggedCount = (int) $usersDb->query("SELECT COUNT(*) FROM product_reviews WHERE status = 'flagged'")->fetchColumn();
  27. ​
  28. $productIds = array_values(array_unique(array_map(static fn(array $review) => (int) $review['product_id'], $reviews)));
  29. $productTitles = [];
  30. if (!empty($productIds)) {
  31. $placeholders = implode(',', array_fill(0, count($productIds), '?'));
  32. $productStatement = $db->prepare("SELECT id, title FROM products WHERE id IN ($placeholders)");
  33. $productStatement->execute($productIds);
  34. foreach ($productStatement->fetchAll() as $row) {
  35. $productTitles[(int) $row['id']] = $row['title'];
  36. }
  37. }
  38. ​
  39. ?>
  40. <div class="dash-header-row">
  41. <h1 class="dash-title"><?= Icons::icon("star", "icon icon-lg") ?>Reviews</h1>
  42. </div>
  43. ​
  44. <div class="settings-tabs">
  45. <a href="dashboard.php?view=reviews&status=visible" class="<?= $statusFilter === 'visible' ? 'active' : '' ?>">Visible</a>
  46. <a href="dashboard.php?view=reviews&status=hidden" class="<?= $statusFilter === 'hidden' ? 'active' : '' ?>">Hidden</a>
  47. <?php if ($flaggedCount > 0): ?>
  48. <a href="dashboard.php?view=reviews&status=flagged" class="<?= $statusFilter === 'flagged' ? 'active' : '' ?>">Reported (<?= $flaggedCount ?>)</a>
  49. <?php endif; ?>
  50. </div>
  51. ​
  52. <table class="dash-table">
  53. <thead>
  54. <tr>
  55. <th><?= Icons::icon('grid', 'icon icon-sm') ?>Product</th>
  56. <th><?= Icons::icon('user', 'icon icon-sm') ?>Customer</th>
  57. <th><?= Icons::icon('star', 'icon icon-sm') ?>Rating</th>
  58. <th><?= Icons::icon('message', 'icon icon-sm') ?>Review</th>
  59. <th><?= Icons::icon('clock', 'icon icon-sm') ?>Posted</th>
  60. <th></th>
  61. </tr>
  62. </thead>
  63. <tbody>
  64. <?php foreach ($reviews as $review): ?>
  65. <tr>
  66. <td><?= htmlspecialchars($productTitles[(int) $review['product_id']] ?? ('#' . (int) $review['product_id'])) ?></td>
  67. <td><?= htmlspecialchars((string) ($review['display_name'] ?? $review['email'] ?? 'Anonymous')) ?></td>
  68. <td><?= $review['rating'] ? str_repeat('★', (int) $review['rating']) . str_repeat('☆', 5 - (int) $review['rating']) : 'No rating' ?></td>
  69. <td><?= htmlspecialchars(mb_strimwidth((string) $review['body'], 0, 140, '…')) ?></td>
  70. <td><?= htmlspecialchars($review['created_at']) ?></td>
  71. <td class="dash-table-actions">
  72. <?php if ($statusFilter !== 'visible'): ?>
  73. <form method="post" style="display:inline;">
  74. <?= Csrf::field() ?>
  75. <input type="hidden" name="action" value="moderate_review">
  76. <input type="hidden" name="tab" value="<?= htmlspecialchars($statusFilter, ENT_QUOTES) ?>">
  77. <input type="hidden" name="review_id" value="<?= (int) $review['id'] ?>">
  78. <input type="hidden" name="decision" value="show">
  79. <button type="submit" class="dash-btn-small dash-btn-success"><?= Icons::icon('check', 'icon icon-sm') ?>Approve</button>
  80. </form>
  81. <?php endif; ?>
  82. <?php if ($statusFilter !== 'hidden'): ?>
  83. <form method="post" style="display:inline;">
  84. <?= Csrf::field() ?>
  85. <input type="hidden" name="action" value="moderate_review">
  86. <input type="hidden" name="tab" value="<?= htmlspecialchars($statusFilter, ENT_QUOTES) ?>">
  87. <input type="hidden" name="review_id" value="<?= (int) $review['id'] ?>">
  88. <input type="hidden" name="decision" value="hide">
  89. <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('x', 'icon icon-sm') ?>Hide</button>
  90. </form>
  91. <?php endif; ?>
  92. </td>
  93. </tr>
  94. <?php endforeach; ?>
  95. <?php if (empty($reviews)): ?>
  96. <tr><td colspan="6">Nothing here right now.</td></tr>
  97. <?php endif; ?>
  98. </tbody>
  99. </table>
  100. ​