v2.0.0.0
Publisium
- <?php
-
- declare(strict_types=1);
-
- $db = Database::site();
- $action = $_POST['action'] ?? '';
-
- if ($action === 'save_ad') {
- $adId = (int) ($_POST['ad_id'] ?? 0) ?: null;
- $name = trim((string) ($_POST['name'] ?? ''));
- $type = in_array($_POST['type'] ?? '', ['static_text', 'static_image', 'script'], true) ? $_POST['type'] : 'static_text';
- $placement = array_key_exists($_POST['placement'] ?? '', SiteFront::AD_PLACEMENTS) ? $_POST['placement'] : 'header';
- $content = (string) ($_POST['content'] ?? '');
- $linkUrl = trim((string) ($_POST['link_url'] ?? ''));
- $isActive = isset($_POST['is_active']) ? 1 : 0;
- $hideForSubscribers = isset($_POST['hide_for_subscribers']) ? 1 : 0;
- $audience = array_key_exists((string) ($_POST['audience'] ?? ''), SiteFront::AD_AUDIENCES) ? (string) $_POST['audience'] : 'all';
- if ($audience === 'paid') {
- $hideForSubscribers = 0;
- }
- $consentCategory = array_key_exists((string) ($_POST['consent_category'] ?? ''), SiteFront::AD_CONSENTS) ? (string) $_POST['consent_category'] : 'none';
- $excludedIds = static fn(string $field): array => array_values(array_unique(array_filter(array_map('intval', is_array($_POST[$field] ?? null) ? $_POST[$field] : []))));
- $excludedItems = json_encode([
- 'home' => isset($_POST['exclude_home']),
- 'articles' => $excludedIds('excluded_articles'),
- 'pages' => $excludedIds('excluded_pages'),
- ]);
-
- if ($name === '' || $content === '') {
- return;
- }
-
- if ($adId !== null) {
- $statement = $db->prepare(
- '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'
- );
- $statement->execute([
- 'name' => $name,
- 'type' => $type,
- 'placement' => $placement,
- 'content' => $content,
- 'link_url' => $linkUrl !== '' ? $linkUrl : null,
- 'is_active' => $isActive,
- 'hide_for_subscribers' => $hideForSubscribers,
- 'audience' => $audience,
- 'excluded_items' => $excludedItems,
- 'consent_category' => $consentCategory,
- 'id' => $adId,
- ]);
- } else {
- $statement = $db->prepare(
- '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)'
- );
- $statement->execute([
- 'name' => $name,
- 'type' => $type,
- 'placement' => $placement,
- 'content' => $content,
- 'link_url' => $linkUrl !== '' ? $linkUrl : null,
- 'is_active' => $isActive,
- 'hide_for_subscribers' => $hideForSubscribers,
- 'audience' => $audience,
- 'excluded_items' => $excludedItems,
- 'consent_category' => $consentCategory,
- 'created_by' => $currentUser['id'],
- ]);
- }
- }
-
- if ($action === 'delete_ad') {
- $adId = (int) ($_POST['ad_id'] ?? 0);
- if ($adId > 0) {
- $statement = $db->prepare('DELETE FROM ads WHERE id = :id');
- $statement->execute(['id' => $adId]);
- }
- }
-