WebOrbiton
v2.0.0.1

Publisium

48 lines · 2.0 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. $db = Database::site();
  6. $isAdmin = Auth::isAdministrative();
  7. ​
  8. if ($isAdmin) {
  9. $topArticles = $db->query(
  10. 'SELECT title, views_count, status FROM articles WHERE deleted_at IS NULL ORDER BY views_count DESC LIMIT 10'
  11. )->fetchAll();
  12. $totalViews = (int) $db->query('SELECT COALESCE(SUM(views_count), 0) AS total FROM articles WHERE deleted_at IS NULL')->fetch()['total'];
  13. } else {
  14. $statement = $db->prepare(
  15. 'SELECT title, views_count, status FROM articles WHERE author_id = :id AND deleted_at IS NULL ORDER BY views_count DESC LIMIT 10'
  16. );
  17. $statement->execute(['id' => $currentUser['id']]);
  18. $topArticles = $statement->fetchAll();
  19. ​
  20. $totalStatement = $db->prepare('SELECT COALESCE(SUM(views_count), 0) AS total FROM articles WHERE author_id = :id AND deleted_at IS NULL');
  21. $totalStatement->execute(['id' => $currentUser['id']]);
  22. $totalViews = (int) $totalStatement->fetch()['total'];
  23. }
  24. ​
  25. ?>
  26. <h1 class="dash-title"><?= Icons::icon("stats", "icon icon-lg") ?>Statistics</h1>
  27. ​
  28. <div class="dash-cards">
  29. <div class="dash-card"><?= Icons::icon('eye', 'icon icon-lg') ?><div class="dash-card-value"><?= number_format($totalViews) ?></div><div class="dash-card-label">Total views</div></div>
  30. </div>
  31. ​
  32. <h2 class="dash-subtitle"><?= Icons::icon('star', 'icon icon-sm') ?>Most read</h2>
  33. <table class="dash-table">
  34. <thead><tr><th><?= Icons::icon('heading', 'icon icon-sm') ?>Title</th><th><?= Icons::icon('eye', 'icon icon-sm') ?>Views</th><th><?= Icons::icon('flag', 'icon icon-sm') ?>Status</th></tr></thead>
  35. <tbody>
  36. <?php foreach ($topArticles as $article): ?>
  37. <tr>
  38. <td><?= htmlspecialchars($article['title']) ?></td>
  39. <td><?= number_format((int) $article['views_count']) ?></td>
  40. <td><span class="status-pill status-<?= htmlspecialchars($article['status']) ?>"><?= htmlspecialchars(dashStatusLabel((string) $article['status'])) ?></span></td>
  41. </tr>
  42. <?php endforeach; ?>
  43. <?php if (empty($topArticles)): ?>
  44. <tr><td colspan="3">No views yet. Check back once your articles are live.</td></tr>
  45. <?php endif; ?>
  46. </tbody>
  47. </table>
  48. ​