cms/admin/media.php

356 lines
12 KiB
PHP

<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
$message = '';
$error = '';
$db = getDB();
// Obsługa uploadu
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$result = uploadFile($_FILES['file']);
if ($result['success']) {
$message = 'Plik został przesłany pomyślnie';
logActivity('media_uploaded', 'media', $result['id']);
} else {
$error = $result['message'];
}
}
// Obsługa usuwania
if (isset($_POST['delete_media'])) {
$mediaId = $_POST['media_id'];
// Pobierz informacje o pliku
$stmt = $db->prepare("SELECT * FROM media WHERE id = ?");
$stmt->execute([$mediaId]);
$media = $stmt->fetch();
if ($media) {
// Usuń plik z dysku
if (file_exists($media['file_path'])) {
unlink($media['file_path']);
}
// Usuń z bazy
$stmt = $db->prepare("DELETE FROM media WHERE id = ?");
if ($stmt->execute([$mediaId])) {
$message = 'Plik został usunięty';
logActivity('media_deleted', 'media', $mediaId);
}
}
}
// Pobierz wszystkie media
$stmt = $db->query("
SELECT m.*, u.username as uploaded_by_name
FROM media m
LEFT JOIN users u ON m.uploaded_by = u.id
ORDER BY m.created_at DESC
");
$mediaFiles = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zarządzanie mediami - Panel CMS</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #f5f5f5;
}
.header {
background: #2c3e50;
color: white;
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
}
.sidebar {
position: fixed;
left: 0;
top: 60px;
width: 250px;
background: white;
height: calc(100vh - 60px);
border-right: 1px solid #e0e0e0;
overflow-y: auto;
}
.sidebar nav a {
display: block;
padding: 15px 20px;
color: #333;
text-decoration: none;
border-left: 3px solid transparent;
}
.sidebar nav a:hover, .sidebar nav a.active {
background: #f0f4ff;
border-left-color: #667eea;
color: #667eea;
}
.main-content {
margin-left: 250px;
padding: 30px;
}
.btn {
display: inline-block;
padding: 10px 20px;
background: #667eea;
color: white;
text-decoration: none;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.btn:hover { background: #5568d3; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-danger { background: #e74c3c; }
.btn-danger:hover { background: #c0392b; }
.card {
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
margin-bottom: 20px;
}
.card-header {
padding: 20px;
border-bottom: 1px solid #e0e0e0;
}
.card-body { padding: 20px; }
.upload-area {
border: 2px dashed #667eea;
border-radius: 8px;
padding: 40px;
text-align: center;
background: #f8f9ff;
cursor: pointer;
transition: all 0.3s;
}
.upload-area:hover {
background: #f0f4ff;
border-color: #5568d3;
}
.upload-area input[type="file"] {
display: none;
}
.media-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
margin-top: 20px;
}
.media-item {
background: white;
border: 1px solid #e0e0e0;
border-radius: 8px;
overflow: hidden;
transition: transform 0.2s;
}
.media-item:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.media-thumbnail {
width: 100%;
height: 150px;
object-fit: cover;
background: #f5f5f5;
}
.media-info {
padding: 12px;
}
.media-info h4 {
font-size: 14px;
margin-bottom: 8px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.media-info small {
display: block;
color: #666;
font-size: 12px;
margin-bottom: 4px;
}
.media-actions {
display: flex;
gap: 5px;
margin-top: 10px;
}
.message {
padding: 12px 20px;
background: #d4edda;
color: #155724;
border-radius: 5px;
margin-bottom: 20px;
}
.error {
padding: 12px 20px;
background: #f8d7da;
color: #721c24;
border-radius: 5px;
margin-bottom: 20px;
}
.file-icon {
display: flex;
align-items: center;
justify-content: center;
height: 150px;
background: #f5f5f5;
font-size: 48px;
}
</style>
</head>
<body>
<div class="header">
<h1>Panel CMS</h1>
<a href="index.php" class="btn btn-sm">← Powrót</a>
</div>
<div class="sidebar">
<nav>
<a href="index.php">📊 Dashboard</a>
<a href="pages.php">📄 Strony</a>
<a href="media.php" class="active">🖼️ Media</a>
<a href="settings.php">⚙️ Ustawienia</a>
</nav>
</div>
<div class="main-content">
<h1 style="margin-bottom: 30px;">Zarządzanie mediami</h1>
<?php if ($message): ?>
<div class="message"><?php echo escape($message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="error"><?php echo escape($error); ?></div>
<?php endif; ?>
<!-- Upload -->
<div class="card">
<div class="card-header">
<h2>Prześlij nowy plik</h2>
</div>
<div class="card-body">
<form method="POST" enctype="multipart/form-data" id="uploadForm">
<div class="upload-area" onclick="document.getElementById('fileInput').click()">
<div style="font-size: 48px; margin-bottom: 10px;">📁</div>
<p style="color: #666; margin-bottom: 10px;">
Kliknij lub przeciągnij pliki tutaj
</p>
<small style="color: #999;">
Dozwolone typy: JPG, PNG, GIF (max 5MB)
</small>
<input type="file" id="fileInput" name="file" accept="image/*"
onchange="document.getElementById('uploadForm').submit()">
</div>
</form>
</div>
</div>
<!-- Biblioteka mediów -->
<div class="card">
<div class="card-header">
<h2>Biblioteka mediów (<?php echo count($mediaFiles); ?> plików)</h2>
</div>
<div class="card-body">
<?php if (empty($mediaFiles)): ?>
<p style="color: #999; text-align: center; padding: 40px;">
Brak plików w bibliotece. Prześlij pierwszy plik powyżej.
</p>
<?php else: ?>
<div class="media-grid">
<?php foreach ($mediaFiles as $media): ?>
<div class="media-item">
<?php if (strpos($media['mime_type'], 'image/') === 0): ?>
<img src="<?php echo UPLOAD_URL . escape($media['filename']); ?>"
alt="<?php echo escape($media['alt_text'] ?? $media['original_name']); ?>"
class="media-thumbnail">
<?php else: ?>
<div class="file-icon">📄</div>
<?php endif; ?>
<div class="media-info">
<h4 title="<?php echo escape($media['original_name']); ?>">
<?php echo escape($media['original_name']); ?>
</h4>
<small>
<?php echo round($media['file_size'] / 1024, 2); ?> KB
</small>
<small>
<?php echo formatDate($media['created_at'], 'd.m.Y'); ?>
</small>
<small>
przez <?php echo escape($media['uploaded_by_name']); ?>
</small>
<div class="media-actions">
<button class="btn btn-sm"
onclick="copyToClipboard('<?php echo UPLOAD_URL . escape($media['filename']); ?>')"
title="Kopiuj URL">
📋
</button>
<form method="POST" style="display: inline;"
onsubmit="return confirm('Czy na pewno chcesz usunąć ten plik?')">
<input type="hidden" name="delete_media" value="1">
<input type="hidden" name="media_id" value="<?php echo $media['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger" title="Usuń">
🗑️
</button>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<script>
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
alert('URL skopiowany do schowka: ' + text);
}).catch(err => {
console.error('Błąd kopiowania: ', err);
});
}
</script>
</body>
</html>