WebOrbiton
v1.0.0.0

StocketBase

124 lines · 6.4 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. $db = Database::site();
  11. $isAdmin = Auth::hasRoleAtLeast(Auth::ROLE_STORE_OWNER);
  12. ​
  13. $statement = $isAdmin
  14. ? $db->query('SELECT p.id, p.title, p.trashed_status, p.deleted_at, t.display_name FROM products p JOIN team_accounts t ON t.id = p.created_by WHERE p.deleted_at IS NOT NULL ORDER BY p.deleted_at DESC')
  15. : $db->prepare('SELECT p.id, p.title, p.trashed_status, p.deleted_at, t.display_name FROM products p JOIN team_accounts t ON t.id = p.created_by WHERE p.deleted_at IS NOT NULL AND p.created_by = :id ORDER BY p.deleted_at DESC');
  16. ​
  17. if (!$isAdmin) {
  18. $statement->execute(['id' => $currentUser['id']]);
  19. }
  20. ​
  21. $trashed = $statement->fetchAll();
  22. ​
  23. $trashedFileCounts = [];
  24. if (!empty($trashed)) {
  25. $trashedIds = array_map(static fn(array $row): int => (int) $row['id'], $trashed);
  26. $placeholders = implode(',', array_fill(0, count($trashedIds), '?'));
  27. try {
  28. $countStatement = $db->prepare("SELECT product_id, COUNT(*) AS files FROM product_files WHERE product_id IN ({$placeholders}) GROUP BY product_id");
  29. $countStatement->execute($trashedIds);
  30. foreach ($countStatement->fetchAll() as $countRow) {
  31. $trashedFileCounts[(int) $countRow['product_id']] = (int) $countRow['files'];
  32. }
  33. } catch (PDOException $exception) {
  34. $trashedFileCounts = [];
  35. }
  36. }
  37. $trashHasFiles = array_sum($trashedFileCounts) > 0;
  38. ​
  39. $renderFileDialog = static function (string $dialogId, string $action, ?int $productId, string $question, int $fileCount): string {
  40. return '<dialog class="dash-dialog" id="' . htmlspecialchars($dialogId) . '">'
  41. . '<form method="post">'
  42. . Csrf::field()
  43. . '<input type="hidden" name="action" value="' . htmlspecialchars($action) . '">'
  44. . ($productId !== null ? '<input type="hidden" name="product_id" value="' . $productId . '">' : '')
  45. . '<h2 class="dash-dialog-title">' . Icons::icon('download', 'icon icon-sm') . htmlspecialchars($question) . '</h2>'
  46. . '<p class="dash-dialog-text">This will permanently delete the product' . ($productId === null ? 's' : '') . '. '
  47. . 'Attached downloadable files: ' . $fileCount . '.</p>'
  48. . '<p class="dash-dialog-text"><strong>Do you also want to remove access to the files?</strong></p>'
  49. . '<ul class="dash-dialog-list">'
  50. . '<li><strong>Keep files</strong> — customers who already bought the product can still download them. The files stay under Files.</li>'
  51. . '<li><strong>Delete files</strong> — the files are erased and every customer loses access. This cannot be undone.</li>'
  52. . '</ul>'
  53. . '<div class="dash-dialog-actions">'
  54. . '<button type="button" class="dash-btn" onclick="this.closest(\'dialog\').close();">Cancel</button>'
  55. . '<button type="submit" name="file_action" value="keep" class="dash-btn dash-btn-primary">Delete product, keep files</button>'
  56. . '<button type="submit" name="file_action" value="delete" class="dash-btn dash-btn-danger">Delete product and files</button>'
  57. . '</div>'
  58. . '</form>'
  59. . '</dialog>';
  60. };
  61. ​
  62. ?>
  63. <div class="dash-header-row">
  64. <h1 class="dash-title"><?= Icons::icon('trash', 'icon icon-lg') ?>Trash</h1>
  65. <a href="dashboard.php?view=products" class="dash-btn"><?= Icons::icon('grid', 'icon icon-sm') ?>Back to products</a>
  66. <?php if (!empty($trashed)): ?>
  67. <?php if ($trashHasFiles): ?>
  68. <button type="button" class="dash-btn dash-btn-danger" onclick="document.getElementById('trash-dialog-all').showModal();"><?= Icons::icon('trash', 'icon icon-sm') ?>Empty trash</button>
  69. <?= $renderFileDialog('trash-dialog-all', 'empty_trash', null, 'Empty the trash?', array_sum($trashedFileCounts)) ?>
  70. <?php else: ?>
  71. <form method="post" style="display:inline;" onsubmit="return confirm('Permanently delete everything in the trash? This cannot be undone.');">
  72. <?= Csrf::field() ?>
  73. <input type="hidden" name="action" value="empty_trash">
  74. <button type="submit" class="dash-btn dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Empty trash</button>
  75. </form>
  76. <?php endif; ?>
  77. <?php endif; ?>
  78. </div>
  79. ​
  80. <table class="dash-table">
  81. <thead>
  82. <tr>
  83. <th><?= Icons::icon('heading', 'icon icon-sm') ?>Title</th>
  84. <th><?= Icons::icon('user', 'icon icon-sm') ?>Created by</th>
  85. <th><?= Icons::icon('flag', 'icon icon-sm') ?>Was</th>
  86. <th><?= Icons::icon('clock', 'icon icon-sm') ?>Deleted</th>
  87. <th></th>
  88. </tr>
  89. </thead>
  90. <tbody>
  91. <?php foreach ($trashed as $product): ?>
  92. <tr>
  93. <td><?= htmlspecialchars($product['title']) ?><?php if (($trashedFileCounts[(int) $product['id']] ?? 0) > 0): ?> <span class="status-pill status-scheduled"><?= (int) $trashedFileCounts[(int) $product['id']] ?> file<?= $trashedFileCounts[(int) $product['id']] === 1 ? '' : 's' ?></span><?php endif; ?></td>
  94. <td><?= htmlspecialchars($product['display_name']) ?></td>
  95. <td><?= htmlspecialchars((string) ($product['trashed_status'] ?? 'draft')) ?></td>
  96. <td><?= htmlspecialchars($product['deleted_at']) ?></td>
  97. <td class="dash-table-actions">
  98. <form method="post" style="display:inline;">
  99. <?= Csrf::field() ?>
  100. <input type="hidden" name="action" value="restore_product">
  101. <input type="hidden" name="product_id" value="<?= (int) $product['id'] ?>">
  102. <button type="submit" class="dash-btn-small dash-btn-success"><?= Icons::icon('refresh', 'icon icon-sm') ?>Restore</button>
  103. </form>
  104. <?php $productFileCount = $trashedFileCounts[(int) $product['id']] ?? 0; ?>
  105. <?php if ($productFileCount > 0): ?>
  106. <button type="button" class="dash-btn-small dash-btn-danger" onclick="document.getElementById('trash-dialog-<?= (int) $product['id'] ?>').showModal();"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete forever</button>
  107. <?= $renderFileDialog('trash-dialog-' . (int) $product['id'], 'purge_product', (int) $product['id'], 'Delete "' . $product['title'] . '" forever?', $productFileCount) ?>
  108. <?php else: ?>
  109. <form method="post" style="display:inline;" onsubmit="return confirm('Permanently delete this product? This cannot be undone.');">
  110. <?= Csrf::field() ?>
  111. <input type="hidden" name="action" value="purge_product">
  112. <input type="hidden" name="product_id" value="<?= (int) $product['id'] ?>">
  113. <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete forever</button>
  114. </form>
  115. <?php endif; ?>
  116. </td>
  117. </tr>
  118. <?php endforeach; ?>
  119. <?php if (empty($trashed)): ?>
  120. <tr><td colspan="5">The trash is empty.</td></tr>
  121. <?php endif; ?>
  122. </tbody>
  123. </table>
  124. ​