WebOrbiton
v1.1.0.0

StocketBase

228 lines · 11.4 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/auth.php';
  8. require_once __DIR__ . '/includes/csrf.php';
  9. require_once __DIR__ . '/includes/activity-log.php';
  10. require_once __DIR__ . '/includes/site-front.php';
  11. ​
  12. Auth::boot();
  13. Auth::requireRoleAtLeast(Auth::ROLE_STORE_OWNER);
  14. ​
  15. $currentUser = Auth::user();
  16. $currentRole = Auth::role();
  17. $db = Database::site();
  18. ​
  19. $flashMessage = null;
  20. $flashType = 'success';
  21. ​
  22. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  23. if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
  24. $flashMessage = 'Your session expired. Please try again.';
  25. $flashType = 'error';
  26. } else {
  27. $adsError = null;
  28. require __DIR__ . '/dashboard-actions/manage-ads.php';
  29. if ($adsError !== null) {
  30. $flashMessage = $adsError;
  31. $flashType = 'error';
  32. } else {
  33. ActivityLog::record('ads.' . (string) ($_POST['action'] ?? ''));
  34. $flashMessage = 'Changes saved.';
  35. }
  36. }
  37. }
  38. ​
  39. $editId = (int) ($_GET['edit'] ?? 0) ?: null;
  40. $editingAd = null;
  41. if ($editId !== null) {
  42. $statement = $db->prepare('SELECT * FROM ads WHERE id = :id LIMIT 1');
  43. $statement->execute(['id' => $editId]);
  44. $editingAd = $statement->fetch() ?: null;
  45. }
  46. ​
  47. $ads = $db->query('SELECT * FROM ads ORDER BY created_at DESC')->fetchAll();
  48. ​
  49. SiteFront::resetSettings();
  50. $adsConsentEnabled = (SiteFront::settings()['consent_manager_enabled'] ?? '0') === '1';
  51. $adsBannerCategories = SiteFront::bannerConsentCategories();
  52. $adTypeLabels = ['static_text' => 'Text', 'static_image' => 'Image', 'script' => 'Ad network code'];
  53. $adConsentLabels = [
  54. 'always' => 'Always',
  55. 'analytics' => 'After consent: Analytics',
  56. 'marketing' => 'After consent: Marketing',
  57. 'preferences' => 'After consent: Preferences',
  58. ];
  59. $editingConsentRun = $editingAd !== null ? SiteFront::adConsentRun($editingAd) : 'always';
  60. ​
  61. $dashActivePage = 'ads';
  62. $dashPageTitle = 'Ads';
  63. ​
  64. require __DIR__ . '/includes/dash-header.php';
  65. ​
  66. ?>
  67. ​
  68. <?php if ($flashMessage !== null): ?>
  69. <div class="dash-flash dash-flash-<?= htmlspecialchars($flashType) ?>"><?= Icons::icon($flashType === 'error' ? 'x' : 'check', 'icon icon-sm') ?><?= htmlspecialchars($flashMessage) ?></div>
  70. <?php endif; ?>
  71. ​
  72. <div class="dash-header-row">
  73. <h1 class="dash-title"><?= Icons::icon("ads", "icon icon-lg") ?>Ads</h1>
  74. <button type="button" class="dash-btn dash-btn-primary" id="ad-new-btn"><?= Icons::icon('plus', 'icon icon-sm') ?>New ad</button>
  75. </div>
  76. ​
  77. <table class="dash-table">
  78. <thead>
  79. <tr>
  80. <th><?= Icons::icon('heading', 'icon icon-sm') ?>Name</th>
  81. <th><?= Icons::icon('ads', 'icon icon-sm') ?>Type</th>
  82. <th><?= Icons::icon('flag', 'icon icon-sm') ?>Placement</th>
  83. <th><?= Icons::icon('shield', 'icon icon-sm') ?>Shows</th>
  84. <th><?= Icons::icon('eye', 'icon icon-sm') ?>Active</th>
  85. <th></th>
  86. </tr>
  87. </thead>
  88. <tbody>
  89. <?php foreach ($ads as $ad): ?>
  90. <tr>
  91. <td><?= htmlspecialchars($ad['name']) ?></td>
  92. <td><?= htmlspecialchars($adTypeLabels[$ad['type']] ?? $ad['type']) ?></td>
  93. <td><?= htmlspecialchars(SiteFront::AD_PLACEMENTS[$ad['placement']] ?? $ad['placement']) ?></td>
  94. <td><?= htmlspecialchars($adsConsentEnabled ? $adConsentLabels[SiteFront::adConsentRun($ad)] : 'Always (cookie consent is off)') ?></td>
  95. <td><?= $ad['is_active'] ? 'Yes' : 'No' ?></td>
  96. <td class="dash-table-actions">
  97. <button type="button" class="dash-btn-small js-edit-ad"
  98. data-id="<?= (int) $ad['id'] ?>"
  99. data-name="<?= htmlspecialchars($ad['name'], ENT_QUOTES) ?>"
  100. data-type="<?= htmlspecialchars($ad['type'], ENT_QUOTES) ?>"
  101. data-placement="<?= htmlspecialchars($ad['placement'], ENT_QUOTES) ?>"
  102. data-content="<?= htmlspecialchars($ad['content'], ENT_QUOTES) ?>"
  103. data-link-url="<?= htmlspecialchars((string) $ad['link_url'], ENT_QUOTES) ?>"
  104. data-consent-run="<?= htmlspecialchars(SiteFront::adConsentRun($ad), ENT_QUOTES) ?>"
  105. data-is-active="<?= (int) $ad['is_active'] ?>"><?= Icons::icon('edit', 'icon icon-sm') ?>Edit</button>
  106. <form method="post" style="display:inline;" onsubmit="return confirm('Delete this ad? It will disappear from your store.');">
  107. <?= Csrf::field() ?>
  108. <input type="hidden" name="action" value="delete_ad">
  109. <input type="hidden" name="ad_id" value="<?= (int) $ad['id'] ?>">
  110. <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
  111. </form>
  112. </td>
  113. </tr>
  114. <?php endforeach; ?>
  115. <?php if (empty($ads)): ?>
  116. <tr>
  117. <td colspan="6">No ads yet. Use them to promote a sale or show a banner.</td>
  118. </tr>
  119. <?php endif; ?>
  120. </tbody>
  121. </table>
  122. ​
  123. <div id="ad-form-wrapper" class="sidebar-box" style="max-width:560px;margin-top:20px;<?= $editingAd ? '' : 'display:none;' ?>">
  124. <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>
  125. <form method="post" id="ad-form">
  126. <?= Csrf::field() ?>
  127. <input type="hidden" name="action" value="save_ad">
  128. <input type="hidden" name="ad_id" id="ad_id_input" value="<?= $editingAd ? (int) $editingAd['id'] : '' ?>">
  129. ​
  130. <label><?= Icons::icon('heading', 'icon icon-sm') ?>Name</label>
  131. <input type="text" name="name" id="ad_name_input" value="<?= htmlspecialchars($editingAd['name'] ?? '') ?>" required>
  132. ​
  133. <label><?= Icons::icon('ads', 'icon icon-sm') ?>Type</label>
  134. <select name="type" id="ad_type_select">
  135. <option value="static_text" <?= ($editingAd['type'] ?? '') === 'static_text' ? 'selected' : '' ?>>Text</option>
  136. <option value="static_image" <?= ($editingAd['type'] ?? '') === 'static_image' ? 'selected' : '' ?>>Image</option>
  137. <option value="script" <?= ($editingAd['type'] ?? '') === 'script' ? 'selected' : '' ?>>Code from an ad network (HTML or JS)</option>
  138. </select>
  139. ​
  140. <label><?= Icons::icon('flag', 'icon icon-sm') ?>Where it shows up</label>
  141. <select name="placement" id="ad_placement_input" required>
  142. <?php foreach (SiteFront::AD_PLACEMENTS as $placementKey => $placementLabel): ?>
  143. <option value="<?= htmlspecialchars($placementKey, ENT_QUOTES) ?>" <?= ($editingAd['placement'] ?? 'header') === $placementKey ? 'selected' : '' ?>><?= htmlspecialchars($placementLabel) ?></option>
  144. <?php endforeach; ?>
  145. </select>
  146. ​
  147. <label id="content_label"><?= Icons::icon('quote', 'icon icon-sm') ?>Content (text, an image, or ad code)</label>
  148. <textarea name="content" id="ad_content_input" rows="6" class="be-code"><?= htmlspecialchars($editingAd['content'] ?? '') ?></textarea>
  149. ​
  150. <label><?= Icons::icon('external', 'icon icon-sm') ?>Link (optional, not used for ad code)</label>
  151. <input type="text" name="link_url" id="ad_link_url_input" value="<?= htmlspecialchars($editingAd['link_url'] ?? '') ?>">
  152. ​
  153. <label><?= Icons::icon('shield', 'icon icon-sm') ?>When should it show?</label>
  154. <select name="consent_run" id="ad_consent_run_select">
  155. <option value="always" <?= $editingConsentRun === 'always' ? 'selected' : '' ?>>Always, no consent needed</option>
  156. <?php foreach (SiteFront::CONSENT_CATEGORIES as $consentCategoryKey): ?>
  157. <option value="<?= htmlspecialchars($consentCategoryKey) ?>" <?= $editingConsentRun === $consentCategoryKey ? 'selected' : '' ?>>After the visitor allows <?= htmlspecialchars(ucfirst($consentCategoryKey)) ?><?= in_array($consentCategoryKey, $adsBannerCategories, true) ? '' : ' (not shown under Manage, only "Accept" turns it on)' ?></option>
  158. <?php endforeach; ?>
  159. </select>
  160. <p style="font-size:12px;color:var(--muted);margin:6px 0 0;"><?= $adsConsentEnabled ? 'Ads from ad networks usually use cookies, so pick Marketing for those. Until the visitor agrees, the ad simply doesn\'t appear.' : 'Cookie consent is turned off in Analytics, so every ad shows right away. This choice starts working when you turn it on.' ?></p>
  161. ​
  162. <label><input type="checkbox" name="is_active" id="ad_is_active_input" <?= ($editingAd['is_active'] ?? 1) ? 'checked' : '' ?>> Show it on the store</label>
  163. ​
  164. <div class="publish-actions">
  165. <button type="submit" class="dash-btn dash-btn-primary"><?= Icons::icon('check', 'icon icon-sm') ?>Save ad</button>
  166. <button type="button" class="dash-btn" id="ad-cancel-btn"><?= Icons::icon('x', 'icon icon-sm') ?>Cancel</button>
  167. </div>
  168. </form>
  169. </div>
  170. ​
  171. <script>
  172. (function() {
  173. var wrapper = document.getElementById('ad-form-wrapper');
  174. var formTitle = document.getElementById('ad-form-title');
  175. var form = document.getElementById('ad-form');
  176. ​
  177. function resetForm() {
  178. form.reset();
  179. document.getElementById('ad_id_input').value = '';
  180. document.getElementById('ad_placement_input').value = 'header';
  181. document.getElementById('ad_is_active_input').checked = true;
  182. document.getElementById('ad_consent_run_select').value = 'always';
  183. formTitle.textContent = 'New ad';
  184. }
  185. ​
  186. document.getElementById('ad_type_select').addEventListener('change', function() {
  187. var consentSelect = document.getElementById('ad_consent_run_select');
  188. if (this.value === 'script' && consentSelect.value === 'always') {
  189. consentSelect.value = 'marketing';
  190. }
  191. });
  192. ​
  193. function showForm() {
  194. wrapper.style.display = '';
  195. wrapper.scrollIntoView({
  196. behavior: 'smooth',
  197. block: 'nearest'
  198. });
  199. }
  200. ​
  201. document.getElementById('ad-new-btn').addEventListener('click', function() {
  202. resetForm();
  203. showForm();
  204. });
  205. ​
  206. document.getElementById('ad-cancel-btn').addEventListener('click', function() {
  207. wrapper.style.display = 'none';
  208. resetForm();
  209. });
  210. ​
  211. document.querySelectorAll('.js-edit-ad').forEach(function(button) {
  212. button.addEventListener('click', function() {
  213. document.getElementById('ad_id_input').value = this.dataset.id;
  214. document.getElementById('ad_name_input').value = this.dataset.name;
  215. document.getElementById('ad_type_select').value = this.dataset.type;
  216. document.getElementById('ad_placement_input').value = this.dataset.placement;
  217. document.getElementById('ad_content_input').value = this.dataset.content;
  218. document.getElementById('ad_link_url_input').value = this.dataset.linkUrl;
  219. document.getElementById('ad_is_active_input').checked = this.dataset.isActive === '1';
  220. document.getElementById('ad_consent_run_select').value = this.dataset.consentRun || 'always';
  221. formTitle.textContent = 'Edit ad';
  222. showForm();
  223. });
  224. });
  225. })();
  226. </script>
  227. ​
  228. <?php require __DIR__ . '/includes/dash-footer.php'; ?>