v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/auth.php';
- require_once __DIR__ . '/includes/csrf.php';
- require_once __DIR__ . '/includes/activity-log.php';
- require_once __DIR__ . '/includes/site-front.php';
-
- Auth::boot();
- Auth::requireRoleAtLeast(Auth::ROLE_STORE_OWNER);
-
- $currentUser = Auth::user();
- $currentRole = Auth::role();
- $db = Database::site();
-
- $flashMessage = null;
- $flashType = 'success';
-
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
- $flashMessage = 'Your session expired. Please try again.';
- $flashType = 'error';
- } else {
- require __DIR__ . '/dashboard-actions/manage-ads.php';
- ActivityLog::record('ads.' . (string) ($_POST['action'] ?? ''));
- $flashMessage = 'Changes saved.';
- }
- }
-
- $editId = (int) ($_GET['edit'] ?? 0) ?: null;
- $editingAd = null;
- if ($editId !== null) {
- $statement = $db->prepare('SELECT * FROM ads WHERE id = :id LIMIT 1');
- $statement->execute(['id' => $editId]);
- $editingAd = $statement->fetch() ?: null;
- }
-
- $ads = $db->query('SELECT * FROM ads ORDER BY created_at DESC')->fetchAll();
-
- $dashActivePage = 'ads';
- $dashPageTitle = 'Ads';
-
- require __DIR__ . '/includes/dash-header.php';
-
- ?>
-
- <?php if ($flashMessage !== null): ?>
- <div class="dash-flash dash-flash-<?= htmlspecialchars($flashType) ?>"><?= Icons::icon($flashType === 'error' ? 'x' : 'check', 'icon icon-sm') ?><?= htmlspecialchars($flashMessage) ?></div>
- <?php endif; ?>
-
- <div class="dash-header-row">
- <h1 class="dash-title"><?= Icons::icon("ads", "icon icon-lg") ?>Ads</h1>
- <button type="button" class="dash-btn dash-btn-primary" id="ad-new-btn"><?= Icons::icon('plus', 'icon icon-sm') ?>New ad</button>
- </div>
-
- <table class="dash-table">
- <thead>
- <tr>
- <th><?= Icons::icon('heading', 'icon icon-sm') ?>Name</th>
- <th><?= Icons::icon('ads', 'icon icon-sm') ?>Type</th>
- <th><?= Icons::icon('flag', 'icon icon-sm') ?>Placement</th>
- <th><?= Icons::icon('eye', 'icon icon-sm') ?>Active</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($ads as $ad): ?>
- <tr>
- <td><?= htmlspecialchars($ad['name']) ?></td>
- <td><?= htmlspecialchars($ad['type']) ?></td>
- <td><?= htmlspecialchars(SiteFront::AD_PLACEMENTS[$ad['placement']] ?? $ad['placement']) ?></td>
- <td><?= $ad['is_active'] ? 'Yes' : 'No' ?></td>
- <td class="dash-table-actions">
- <button type="button" class="dash-btn-small js-edit-ad"
- data-id="<?= (int) $ad['id'] ?>"
- data-name="<?= htmlspecialchars($ad['name'], ENT_QUOTES) ?>"
- data-type="<?= htmlspecialchars($ad['type'], ENT_QUOTES) ?>"
- data-placement="<?= htmlspecialchars($ad['placement'], ENT_QUOTES) ?>"
- data-content="<?= htmlspecialchars($ad['content'], ENT_QUOTES) ?>"
- data-link-url="<?= htmlspecialchars((string) $ad['link_url'], ENT_QUOTES) ?>"
- data-is-active="<?= (int) $ad['is_active'] ?>"><?= Icons::icon('edit', 'icon icon-sm') ?>Edit</button>
- <form method="post" style="display:inline;" onsubmit="return confirm('Delete this ad? It will disappear from your store.');">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="delete_ad">
- <input type="hidden" name="ad_id" value="<?= (int) $ad['id'] ?>">
- <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- <?php if (empty($ads)): ?>
- <tr>
- <td colspan="5">No ads yet. Use them to promote a sale or show a banner.</td>
- </tr>
- <?php endif; ?>
- </tbody>
- </table>
-
- <div id="ad-form-wrapper" class="sidebar-box" style="max-width:560px;margin-top:20px;<?= $editingAd ? '' : 'display:none;' ?>">
- <h2 class="dash-subtitle" style="margin-top:0;"><?= Icons::icon('megaphone', 'icon icon-sm') ?><span id="ad-form-title"><?= $editingAd ? 'Edit ad' : 'New ad' ?></span></h2>
- <form method="post" id="ad-form">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="save_ad">
- <input type="hidden" name="ad_id" id="ad_id_input" value="<?= $editingAd ? (int) $editingAd['id'] : '' ?>">
-
- <label><?= Icons::icon('heading', 'icon icon-sm') ?>Name</label>
- <input type="text" name="name" id="ad_name_input" value="<?= htmlspecialchars($editingAd['name'] ?? '') ?>" required>
-
- <label><?= Icons::icon('ads', 'icon icon-sm') ?>Type</label>
- <select name="type" id="ad_type_select">
- <option value="static_text" <?= ($editingAd['type'] ?? '') === 'static_text' ? 'selected' : '' ?>>Text</option>
- <option value="static_image" <?= ($editingAd['type'] ?? '') === 'static_image' ? 'selected' : '' ?>>Image</option>
- <option value="script" <?= ($editingAd['type'] ?? '') === 'script' ? 'selected' : '' ?>>Code from an ad network (HTML or JS)</option>
- </select>
-
- <label><?= Icons::icon('flag', 'icon icon-sm') ?>Where it shows up</label>
- <select name="placement" id="ad_placement_input" required>
- <?php foreach (SiteFront::AD_PLACEMENTS as $placementKey => $placementLabel): ?>
- <option value="<?= htmlspecialchars($placementKey, ENT_QUOTES) ?>" <?= ($editingAd['placement'] ?? 'header') === $placementKey ? 'selected' : '' ?>><?= htmlspecialchars($placementLabel) ?></option>
- <?php endforeach; ?>
- </select>
-
- <label id="content_label"><?= Icons::icon('quote', 'icon icon-sm') ?>Content (text, an image, or ad code)</label>
- <textarea name="content" id="ad_content_input" rows="6" class="be-code"><?= htmlspecialchars($editingAd['content'] ?? '') ?></textarea>
-
- <label><?= Icons::icon('external', 'icon icon-sm') ?>Link (optional, not used for ad code)</label>
- <input type="text" name="link_url" id="ad_link_url_input" value="<?= htmlspecialchars($editingAd['link_url'] ?? '') ?>">
-
- <label><input type="checkbox" name="is_active" id="ad_is_active_input" <?= ($editingAd['is_active'] ?? 1) ? 'checked' : '' ?>> Show it on the store</label>
-
- <div class="publish-actions">
- <button type="submit" class="dash-btn dash-btn-primary"><?= Icons::icon('check', 'icon icon-sm') ?>Save ad</button>
- <button type="button" class="dash-btn" id="ad-cancel-btn"><?= Icons::icon('x', 'icon icon-sm') ?>Cancel</button>
- </div>
- </form>
- </div>
-
- <script>
- (function() {
- var wrapper = document.getElementById('ad-form-wrapper');
- var formTitle = document.getElementById('ad-form-title');
- var form = document.getElementById('ad-form');
-
- function resetForm() {
- form.reset();
- document.getElementById('ad_id_input').value = '';
- document.getElementById('ad_placement_input').value = 'header';
- document.getElementById('ad_is_active_input').checked = true;
- formTitle.textContent = 'New ad';
- }
-
- function showForm() {
- wrapper.style.display = '';
- wrapper.scrollIntoView({
- behavior: 'smooth',
- block: 'nearest'
- });
- }
-
- document.getElementById('ad-new-btn').addEventListener('click', function() {
- resetForm();
- showForm();
- });
-
- document.getElementById('ad-cancel-btn').addEventListener('click', function() {
- wrapper.style.display = 'none';
- resetForm();
- });
-
- document.querySelectorAll('.js-edit-ad').forEach(function(button) {
- button.addEventListener('click', function() {
- document.getElementById('ad_id_input').value = this.dataset.id;
- document.getElementById('ad_name_input').value = this.dataset.name;
- document.getElementById('ad_type_select').value = this.dataset.type;
- document.getElementById('ad_placement_input').value = this.dataset.placement;
- document.getElementById('ad_content_input').value = this.dataset.content;
- document.getElementById('ad_link_url_input').value = this.dataset.linkUrl;
- document.getElementById('ad_is_active_input').checked = this.dataset.isActive === '1';
- formTitle.textContent = 'Edit ad';
- showForm();
- });
- });
- })();
- </script>
-
- <?php require __DIR__ . '/includes/dash-footer.php'; ?>