Joywork/admin/articles.php
2026-05-22 21:21:54 +03:00

93 lines
4.6 KiB
PHP

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
if (!$_SESSION['superadmin']) { header("location:/index.php"); die("Доступ запрещён!"); }
require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/header-admin.php");
$articles = Article::getAll();
?>
<div class="main-title">
<a href="/admin/article-edit.php" class="medium-button" style="float: right;">+ Новая статья</a>
<h1>Статьи</h1>
</div>
<div class="users fr__users" style="margin-right: 20px;">
<table class="table" style="width:100%; border-collapse:collapse;">
<thead>
<tr style="background:#f5f5f5; border-bottom:2px solid #ddd;">
<th style="padding:10px 8px; text-align:left; width:60px;">Фото</th>
<th style="padding:10px 8px; text-align:left;">Заголовок</th>
<th style="padding:10px 8px; text-align:left; width:120px;">Дата</th>
<th style="padding:10px 8px; text-align:left; width:220px;">Слаг</th>
<th style="padding:10px 8px; text-align:center; width:90px;">Статус</th>
<th style="padding:10px 8px; text-align:right; width:240px;">Действия</th>
</tr>
</thead>
<tbody>
<?php if (empty($articles)): ?>
<tr>
<td colspan="6" style="padding:30px; text-align:center; color:#999;">Статей пока нет</td>
</tr>
<?php else: ?>
<?php foreach ($articles as $a): ?>
<tr style="border-bottom:1px solid #eee;" class="article-row" data-id="<?= $a['id'] ?>">
<td style="padding:8px;">
<?php if ($a['image']): ?>
<img src="<?= htmlspecialchars($a['image']) ?>" style="width:50px;height:35px;object-fit:cover;border-radius:3px;" alt="">
<?php else: ?>
<div style="width:50px;height:35px;background:#eee;border-radius:3px;"></div>
<?php endif; ?>
</td>
<td style="padding:8px;">
<a href="/admin/article-edit.php?id=<?= $a['id'] ?>" style="color:#333; font-weight:500;">
<?= htmlspecialchars($a['title']) ?>
</a>
</td>
<td style="padding:8px; color:#666; font-size:13px;"><?= htmlspecialchars($a['date_created']) ?></td>
<td style="padding:8px; color:#999; font-size:12px; font-family:monospace;"><?= htmlspecialchars($a['slug']) ?></td>
<td style="padding:8px; text-align:center;">
<?php if ($a['published']): ?>
<span style="background:#e8f5e9;color:#2e7d32;padding:2px 8px;border-radius:10px;font-size:12px;">Опубл.</span>
<?php else: ?>
<span style="background:#fff3e0;color:#e65100;padding:2px 8px;border-radius:10px;font-size:12px;">Черновик</span>
<?php endif; ?>
</td>
<td style="padding:8px; text-align:right; white-space:nowrap;">
<a href="/admin/article-edit.php?id=<?= $a['id'] ?>"
style="display:inline-block; padding:6px 14px; background:#fff; border:1px solid #ccc; color:#333; border-radius:4px; text-decoration:none; font-size:13px; margin-right:8px;">Редактировать</a>
<button class="btn-delete-article" data-id="<?= $a['id'] ?>"
style="display:inline-block; padding:6px 14px; background:#f44336; color:#fff; border:none; border-radius:4px; cursor:pointer; font-size:13px;">Удалить</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<script>
document.querySelectorAll('.btn-delete-article').forEach(function(btn) {
btn.addEventListener('click', function() {
if (!confirm('Удалить статью? Это действие нельзя отменить.')) return;
var id = this.getAttribute('data-id');
fetch('/admin/ajax/deleteArticle.php', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'id=' + id
})
.then(function(r) { return r.json(); })
.then(function(data) {
if (data.success) {
document.querySelector('.article-row[data-id="' + id + '"]').remove();
} else {
alert('Ошибка: ' + data.error);
}
});
});
});
</script>
<?php require_once($_SERVER['DOCUMENT_ROOT'] . "/templates/footer-admin.php"); ?>