157 lines
7.9 KiB
PHP
157 lines
7.9 KiB
PHP
<?php
|
||
require_once 'db/config.php';
|
||
require_once 'config.php';
|
||
require_once 'includes/telegram_api.php';
|
||
|
||
$message = '';
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'add_public') {
|
||
$title = $_POST['title'] ?? '';
|
||
$username = $_POST['username'] ?? '';
|
||
$telegrams_id = $_POST['telegrams_id'] ?? null;
|
||
|
||
if (!empty($title) && !empty($telegrams_id)) {
|
||
$subscribers = getSubscriberCount($telegrams_id, TELEGRAM_BOT_TOKEN);
|
||
if ($subscribers === null) {
|
||
$subscribers = 0;
|
||
$message = '<div class="alert alert-warning">Не удалось получить количество подписчиков. Канал добавлен со значением 0.</div>';
|
||
}
|
||
|
||
try {
|
||
$pdo = db();
|
||
$stmt = $pdo->prepare("INSERT INTO publics (title, username, telegrams_id, subscribers) VALUES (?, ?, ?, ?)");
|
||
$stmt->execute([$title, $username, $telegrams_id, $subscribers]);
|
||
$message .= '<div class="alert alert-success">Канал успешно добавлен!</div>';
|
||
} catch (PDOException $e) {
|
||
$message = '<div class="alert alert-danger">Ошибка: ' . $e->getMessage() . '</div>';
|
||
}
|
||
} else {
|
||
$message = '<div class="alert alert-danger">Название и ID канала обязательны.</div>';
|
||
}
|
||
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'refresh_public') {
|
||
$id = $_POST['id'] ?? null;
|
||
if ($id) {
|
||
try {
|
||
$pdo = db();
|
||
$stmt = $pdo->prepare("SELECT telegrams_id FROM publics WHERE id = ?");
|
||
$stmt->execute([$id]);
|
||
$public = $stmt->fetch();
|
||
|
||
if ($public) {
|
||
$telegrams_id = $public['telegrams_id'];
|
||
$subscribers = getSubscriberCount($telegrams_id, TELEGRAM_BOT_TOKEN);
|
||
|
||
if ($subscribers !== null) {
|
||
$updateStmt = $pdo->prepare("UPDATE publics SET subscribers = ? WHERE id = ?");
|
||
$updateStmt->execute([$subscribers, $id]);
|
||
$message = '<div class="alert alert-success">Количество подписчиков обновлено!</div>';
|
||
} else {
|
||
$message = '<div class="alert alert-warning">Не удалось обновить количество подписчиков. API Telegram не ответило.</div>';
|
||
}
|
||
} else {
|
||
$message = '<div class="alert alert-danger">Канал не найден.</div>';
|
||
}
|
||
} catch (PDOException $e) {
|
||
$message = '<div class="alert alert-danger">Ошибка: ' . $e->getMessage() . '</div>';
|
||
}
|
||
}
|
||
}
|
||
|
||
try {
|
||
$pdo = db();
|
||
$stmt = $pdo->query("SELECT id, title, username, subscribers, telegrams_id FROM publics ORDER BY created_at DESC");
|
||
$publics = $stmt->fetchAll();
|
||
} catch (PDOException $e) {
|
||
die("Ошибка загрузки каналов: " . $e->getMessage());
|
||
}
|
||
?>
|
||
|
||
<header class="mb-4 d-flex justify-content-between align-items-center">
|
||
<h1 class="h3">Управление пабликами</h1>
|
||
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPublicModal">
|
||
<i class="bi bi-plus-circle"></i> Добавить паблик
|
||
</button>
|
||
</header>
|
||
|
||
<?php echo $message; ?>
|
||
|
||
<div class="card">
|
||
<div class="card-header">
|
||
<h5 class="card-title mb-0">Список ваших каналов</h5>
|
||
</div>
|
||
<div class="card-body">
|
||
<div class="table-responsive">
|
||
<table class="table table-dark table-striped table-hover">
|
||
<thead>
|
||
<tr>
|
||
<th>ID</th>
|
||
<th>Название</th>
|
||
<th>Username</th>
|
||
<th>Telegram ID</th>
|
||
<th>Подписчики</th>
|
||
<th>Действия</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php if (empty($publics)): ?>
|
||
<tr>
|
||
<td colspan="6" class="text-center">Вы еще не добавили ни одного канала.</td>
|
||
</tr>
|
||
<?php else: ?>
|
||
<?php foreach ($publics as $public): ?>
|
||
<tr>
|
||
<td><?php echo htmlspecialchars($public['id']); ?></td>
|
||
<td><?php echo htmlspecialchars($public['title']); ?></td>
|
||
<td><?php echo $public['username'] ? '@' . htmlspecialchars($public['username']) : 'N/A'; ?></td>
|
||
<td><?php echo htmlspecialchars($public['telegrams_id']); ?></td>
|
||
<td><?php echo htmlspecialchars($public['subscribers']); ?></td>
|
||
<td>
|
||
<form action="index.php?page=publics" method="POST" class="d-inline-block">
|
||
<input type="hidden" name="action" value="refresh_public">
|
||
<input type="hidden" name="id" value="<?php echo $public['id']; ?>">
|
||
<button type="submit" class="btn btn-sm btn-outline-info" title="Обновить подписчиков">
|
||
<i class="bi bi-arrow-repeat"></i>
|
||
</button>
|
||
</form>
|
||
<button class="btn btn-sm btn-outline-light" title="Редактировать"><i class="bi bi-pencil"></i></button>
|
||
<button class="btn btn-sm btn-outline-danger" title="Удалить"><i class="bi bi-trash"></i></button>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Modal -->
|
||
<div class="modal fade" id="addPublicModal" tabindex="-1" aria-labelledby="addPublicModalLabel" aria-hidden="true">
|
||
<div class="modal-dialog">
|
||
<div class="modal-content bg-dark text-white">
|
||
<div class="modal-header">
|
||
<h5 class="modal-title" id="addPublicModalLabel">Добавить новый паблик</h5>
|
||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||
</div>
|
||
<div class="modal-body">
|
||
<form action="index.php?page=publics" method="POST">
|
||
<input type="hidden" name="action" value="add_public">
|
||
<div class="mb-3">
|
||
<label for="title" class="form-label">Название канала</label>
|
||
<input type="text" class="form-control" id="title" name="title" required>
|
||
</div>
|
||
<div class="mb-3">
|
||
<label for="username" class="form-label">Username канала (без @)</label>
|
||
<input type="text" class="form-control" id="username" name="username">
|
||
</div>
|
||
<div class="mb-3">
|
||
<label for="telegrams_id" class="form-label">Telegram ID канала</label>
|
||
<input type="text" class="form-control" id="telegrams_id" name="telegrams_id" required>
|
||
<div class="form-text">Для публичных каналов - @username, для приватных - ID (например, -100123456789).</div>
|
||
</div>
|
||
<button type="submit" class="btn btn-primary">Добавить</button>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|