v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- if (count(get_included_files()) === 1) {
- http_response_code(403);
- exit;
- }
-
- require_once __DIR__ . '/../includes/currency.php';
-
- $db = Database::site();
-
- $isAdmin = Auth::isAdministrative();
-
- $statement = $isAdmin
- ? $db->query('SELECT p.*, t.display_name, c.name AS category_name FROM products p LEFT JOIN team_accounts t ON t.id = p.created_by LEFT JOIN categories c ON c.id = p.category_id WHERE p.deleted_at IS NULL ORDER BY p.updated_at DESC')
- : $db->prepare('SELECT p.*, t.display_name, c.name AS category_name FROM products p LEFT JOIN team_accounts t ON t.id = p.created_by LEFT JOIN categories c ON c.id = p.category_id WHERE p.created_by = :id AND p.deleted_at IS NULL ORDER BY p.updated_at DESC');
-
- if (!$isAdmin) {
- $statement->execute(['id' => $currentUser['id']]);
- }
- $products = $statement->fetchAll();
-
- $trashStatement = $isAdmin
- ? $db->query('SELECT COUNT(*) FROM products WHERE deleted_at IS NOT NULL')
- : $db->prepare('SELECT COUNT(*) FROM products WHERE deleted_at IS NOT NULL AND created_by = :id');
- if (!$isAdmin) {
- $trashStatement->execute(['id' => $currentUser['id']]);
- }
- $trashCount = (int) $trashStatement->fetchColumn();
-
- $storeCurrency = SiteFront::settings()['store_currency'] ?? 'USD';
-
- ?>
- <div class="dash-header-row">
- <h1 class="dash-title"><?= Icons::icon("grid", "icon icon-lg") ?>Products</h1>
- <a href="dashboard.php?view=trash" class="dash-btn"><?= Icons::icon('trash', 'icon icon-sm') ?>Trash<?= $trashCount > 0 ? ' (' . $trashCount . ')' : '' ?></a>
- <a href="dashboard.php?view=product-edit" class="dash-btn dash-btn-primary"><?= Icons::icon('plus', 'icon icon-sm') ?>New product</a>
- </div>
-
- <table class="dash-table">
- <thead>
- <tr>
- <th><?= Icons::icon('heading', 'icon icon-sm') ?>Title</th>
- <th><?= Icons::icon('categories', 'icon icon-sm') ?>Category</th>
- <th><?= Icons::icon('dollar', 'icon icon-sm') ?>Price</th>
- <th><?= Icons::icon('flag', 'icon icon-sm') ?>Status</th>
- <th><?= Icons::icon('clock', 'icon icon-sm') ?>Updated</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($products as $product): ?>
- <tr>
- <td><a href="dashboard.php?view=product-edit&id=<?= (int) $product['id'] ?>"><?= htmlspecialchars($product['title']) ?></a></td>
- <td><?= htmlspecialchars($product['category_name'] ?? 'No category') ?></td>
- <td>
- <?php if (!empty($product['price_cents'])): ?>
- <?= htmlspecialchars(Currency::format((int) $product['price_cents'], (string) ($product['currency'] ?: $storeCurrency))) ?><?= $product['billing_interval'] && $product['billing_interval'] !== 'one_time' ? '/' . htmlspecialchars((string) $product['billing_interval']) : '' ?>
- <?php else: ?>
- Free
- <?php endif; ?>
- </td>
- <td><span class="status-pill status-<?= htmlspecialchars($product['status']) ?>"><?= htmlspecialchars(StatusLabel::get((string) $product['status'])) ?></span></td>
- <td><?= htmlspecialchars($product['updated_at']) ?></td>
- <td class="dash-table-actions">
-
- <?php if ($isAdmin && $product['status'] === 'pending_review'): ?>
- <form method="post" style="display:inline;">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="moderate_product">
- <input type="hidden" name="product_id" value="<?= (int) $product['id'] ?>">
- <input type="hidden" name="decision" value="approve">
- <button type="submit" class="dash-btn-small dash-btn-success"><?= Icons::icon('check', 'icon icon-sm') ?>Approve</button>
- </form>
- <button type="button" class="dash-btn-small dash-btn-danger js-reject-btn" data-product-id="<?= (int) $product['id'] ?>"><?= Icons::icon('x', 'icon icon-sm') ?>Send back</button>
- <?php endif; ?>
-
- <form method="post" style="display:inline;" onsubmit="return confirm('Move this product to the trash? You can bring it back later.');">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="delete_product">
- <input type="hidden" name="product_id" value="<?= (int) $product['id'] ?>">
- <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
- </form>
- <?php if ($product['status'] === 'published' && !empty($product['slug'])): ?>
- <a href="<?= htmlspecialchars(SiteFront::productUrl($product['slug'])) ?>" class="dash-btn-small" target="_blank" rel="noopener noreferrer"><?= Icons::icon('eye', 'icon icon-sm') ?>View</a>
- <?php else: ?>
- <a href="product.php?preview=<?= (int) $product['id'] ?>" class="dash-btn-small" target="_blank" rel="noopener noreferrer"><?= Icons::icon('eye', 'icon icon-sm') ?>Preview</a>
- <?php endif; ?>
- </td>
- </tr>
-
- <?php if ($isAdmin && $product['status'] === 'pending_review'): ?>
- <tr class="js-reject-row" id="reject-row-<?= (int) $product['id'] ?>" style="display:none;">
- <td colspan="6">
- <form method="post">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="moderate_product">
- <input type="hidden" name="product_id" value="<?= (int) $product['id'] ?>">
- <input type="hidden" name="decision" value="reject">
- <input type="text" name="rejection_reason" placeholder="What should be fixed?" class="dash-input-inline">
- <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('check', 'icon icon-sm') ?>Send back</button>
- </form>
- </td>
- </tr>
- <?php endif; ?>
-
- <?php endforeach; ?>
- <?php if (empty($products)): ?>
- <tr><td colspan="6">No products yet. Click "New product" to add your first one.</td></tr>
- <?php endif; ?>
- </tbody>
- </table>
-