black-sheep-bluff/app/Controllers/Api/AdController.php
2026-09-18 06:46:56 +02:00

65 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
/** API-Controller (Ad) - 1:1 aus api.php. */
final class AdController
{
public function fake_ads(): void
{
$pdo = Database::pdo();
$now = time();
$authUserId = $_SESSION['user_id'] ?? null;
// Öffentlich: aktive Fake-Ads fürs Polling im Frontend
$rows = $pdo->query('SELECT id, image_url, target_url FROM gtl_fake_ads WHERE is_active=1 ORDER BY id DESC')->fetchAll();
respond(['ok' => true, 'ads' => $rows]);
}
public function fake_ads_all(): void
{
$pdo = Database::pdo();
$now = time();
$authUserId = $_SESSION['user_id'] ?? null;
RoomService::requireAdminApi($pdo, $authUserId);
$rows = $pdo->query('SELECT id, image_url, target_url, is_active, created_at FROM gtl_fake_ads ORDER BY id DESC')->fetchAll();
respond(['ok' => true, 'ads' => $rows]);
}
public function fake_ad_create(): void
{
$pdo = Database::pdo();
$now = time();
$authUserId = $_SESSION['user_id'] ?? null;
RoomService::requireAdminApi($pdo, $authUserId);
$body = input();
$img = substr(trim((string) ($body['image_url'] ?? '')), 0, 500);
$target= substr(trim((string) ($body['target_url'] ?? '')), 0, 500);
if ($img === '') respond(['error' => 'Bild-URL erforderlich.'], 400);
$pdo->prepare('INSERT INTO gtl_fake_ads (image_url, target_url, is_active, created_at) VALUES (?,?,1,?)')
->execute([$img, $target !== '' ? $target : null, $now]);
respond(['ok' => true, 'id' => (int) $pdo->lastInsertId()]);
}
public function fake_ad_toggle(): void
{
$pdo = Database::pdo();
$now = time();
$authUserId = $_SESSION['user_id'] ?? null;
RoomService::requireAdminApi($pdo, $authUserId);
$body = input();
$id = (int) ($body['id'] ?? 0);
$pdo->prepare('UPDATE gtl_fake_ads SET is_active = 1 - is_active WHERE id = ?')->execute([$id]);
respond(['ok' => true]);
}
public function fake_ad_delete(): void
{
$pdo = Database::pdo();
$now = time();
$authUserId = $_SESSION['user_id'] ?? null;
RoomService::requireAdminApi($pdo, $authUserId);
$body = input();
$id = (int) ($body['id'] ?? 0);
$pdo->prepare('DELETE FROM gtl_fake_ads WHERE id = ?')->execute([$id]);
respond(['ok' => true]);
}
}