| Current Path : /home/seto/testhive.pm/admin/ |
| Current File : /home/seto/testhive.pm/admin/campaigns.php |
<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/redis.php';
require_once __DIR__ . '/_layout.php';
// Handle status toggle
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_id'])) {
$id = (int)$_POST['toggle_id'];
db()->prepare(
"UPDATE campaigns SET status = CASE WHEN status='active' THEN 'paused' ELSE 'active' END WHERE id=?"
)->execute([$id]);
cache_del('campaigns:active');
header('Location: /admin/campaigns.php'); exit;
}
// Handle delete
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
$id = (int)$_POST['delete_id'];
db()->prepare("DELETE FROM page_assignments WHERE campaign_id=?")->execute([$id]);
db()->prepare("DELETE FROM impressions_daily WHERE link_id IN (SELECT id FROM links WHERE campaign_id=?)")->execute([$id]);
db()->prepare("DELETE FROM links WHERE campaign_id=?")->execute([$id]);
db()->prepare("DELETE FROM campaigns WHERE id=?")->execute([$id]);
cache_del('campaigns:active');
header('Location: /admin/campaigns.php'); exit;
}
$campaigns = db()->query(
"SELECT c.*,
(SELECT COUNT(*) FROM links WHERE campaign_id=c.id) as link_count,
(SELECT COUNT(*) FROM page_assignments WHERE campaign_id=c.id) as assign_count
FROM campaigns c ORDER BY c.created_at DESC"
)->fetchAll();
layout_head('Campaigns');
?>
<div class="mb-3">
<a href="/admin/campaign-edit.php" class="btn btn-primary btn-sm">+ Новая campaign</a>
</div>
<div class="card shadow-sm">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th>Название</th>
<th>Target URL</th>
<th>Status</th>
<th class="text-end">Impressions</th>
<th class="text-end">Links</th>
<th class="text-end">Assigned</th>
<th>Weight</th>
<th>Coverage</th>
<th>Rotation</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($campaigns as $c): ?>
<tr>
<td><?= htmlspecialchars($c['name']) ?></td>
<td class="text-truncate" style="max-width:160px">
<a href="<?= htmlspecialchars($c['target_url']) ?>" target="_blank" rel="noopener">
<?= htmlspecialchars($c['target_url']) ?>
</a>
</td>
<td>
<form method="post" class="d-inline">
<input type="hidden" name="toggle_id" value="<?= $c['id'] ?>">
<?php if ($c['status'] === 'active'): ?>
<button class="badge bg-success border-0" style="cursor:pointer" title="Нажмите для паузы">Active</button>
<?php else: ?>
<button class="badge bg-warning text-dark border-0" style="cursor:pointer" title="Нажмите для запуска">Paused</button>
<?php endif ?>
</form>
</td>
<td class="text-end"><?= number_format((int)$c['total_impressions']) ?></td>
<td class="text-end"><?= (int)$c['link_count'] ?></td>
<td class="text-end">
<a href="/admin/assignments.php?campaign_id=<?= $c['id'] ?>"><?= (int)$c['assign_count'] ?></a>
</td>
<td><?= htmlspecialchars((string)($c['weight'] ?? 1)) ?></td>
<td><?= htmlspecialchars((string)($c['coverage_percent'] ?? 50)) ?>%</td>
<td><?= (int)($c['rotation_days'] ?? 14) ?>d</td>
<td class="text-end">
<a href="/admin/campaign-edit.php?id=<?= $c['id'] ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
<form method="post" class="d-inline" onsubmit="return confirm('Удалить campaign и все её assignments?')">
<input type="hidden" name="delete_id" value="<?= $c['id'] ?>">
<button class="btn btn-sm btn-outline-danger">Del</button>
</form>
</td>
</tr>
<?php endforeach ?>
<?php if (empty($campaigns)): ?>
<tr><td colspan="10" class="text-center text-muted py-3">Campaigns не найдены</td></tr>
<?php endif ?>
</tbody>
</table>
</div>
<?php layout_foot() ?>