WebOrbiton
v2.0.0.1

Publisium

77 lines · 3.4 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. $db = Database::site();
  6. $action = $_POST['action'] ?? '';
  7. ​
  8. if ($action === 'save_ad') {
  9. $adId = (int) ($_POST['ad_id'] ?? 0) ?: null;
  10. $name = trim((string) ($_POST['name'] ?? ''));
  11. $type = in_array($_POST['type'] ?? '', ['static_text', 'static_image', 'script'], true) ? $_POST['type'] : 'static_text';
  12. $placement = array_key_exists($_POST['placement'] ?? '', SiteFront::AD_PLACEMENTS) ? $_POST['placement'] : 'header';
  13. $content = (string) ($_POST['content'] ?? '');
  14. $linkUrl = trim((string) ($_POST['link_url'] ?? ''));
  15. $isActive = isset($_POST['is_active']) ? 1 : 0;
  16. $hideForSubscribers = isset($_POST['hide_for_subscribers']) ? 1 : 0;
  17. $audience = array_key_exists((string) ($_POST['audience'] ?? ''), SiteFront::AD_AUDIENCES) ? (string) $_POST['audience'] : 'all';
  18. if ($audience === 'paid') {
  19. $hideForSubscribers = 0;
  20. }
  21. $consentCategory = array_key_exists((string) ($_POST['consent_category'] ?? ''), SiteFront::AD_CONSENTS) ? (string) $_POST['consent_category'] : 'none';
  22. $excludedIds = static fn(string $field): array => array_values(array_unique(array_filter(array_map('intval', is_array($_POST[$field] ?? null) ? $_POST[$field] : []))));
  23. $excludedItems = json_encode([
  24. 'home' => isset($_POST['exclude_home']),
  25. 'articles' => $excludedIds('excluded_articles'),
  26. 'pages' => $excludedIds('excluded_pages'),
  27. ]);
  28. ​
  29. if ($name === '' || $content === '') {
  30. return;
  31. }
  32. ​
  33. if ($adId !== null) {
  34. $statement = $db->prepare(
  35. 'UPDATE ads SET name = :name, type = :type, placement = :placement, content = :content, link_url = :link_url, is_active = :is_active, hide_for_subscribers = :hide_for_subscribers, audience = :audience, excluded_items = :excluded_items, consent_category = :consent_category WHERE id = :id'
  36. );
  37. $statement->execute([
  38. 'name' => $name,
  39. 'type' => $type,
  40. 'placement' => $placement,
  41. 'content' => $content,
  42. 'link_url' => $linkUrl !== '' ? $linkUrl : null,
  43. 'is_active' => $isActive,
  44. 'hide_for_subscribers' => $hideForSubscribers,
  45. 'audience' => $audience,
  46. 'excluded_items' => $excludedItems,
  47. 'consent_category' => $consentCategory,
  48. 'id' => $adId,
  49. ]);
  50. } else {
  51. $statement = $db->prepare(
  52. 'INSERT INTO ads (name, type, placement, content, link_url, is_active, hide_for_subscribers, audience, excluded_items, consent_category, created_by) VALUES (:name, :type, :placement, :content, :link_url, :is_active, :hide_for_subscribers, :audience, :excluded_items, :consent_category, :created_by)'
  53. );
  54. $statement->execute([
  55. 'name' => $name,
  56. 'type' => $type,
  57. 'placement' => $placement,
  58. 'content' => $content,
  59. 'link_url' => $linkUrl !== '' ? $linkUrl : null,
  60. 'is_active' => $isActive,
  61. 'hide_for_subscribers' => $hideForSubscribers,
  62. 'audience' => $audience,
  63. 'excluded_items' => $excludedItems,
  64. 'consent_category' => $consentCategory,
  65. 'created_by' => $currentUser['id'],
  66. ]);
  67. }
  68. }
  69. ​
  70. if ($action === 'delete_ad') {
  71. $adId = (int) ($_POST['ad_id'] ?? 0);
  72. if ($adId > 0) {
  73. $statement = $db->prepare('DELETE FROM ads WHERE id = :id');
  74. $statement->execute(['id' => $adId]);
  75. }
  76. }
  77. ​