36716-vm/edit-portfolio.php
2025-12-07 05:00:42 +00:00

178 lines
7.1 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'stripe/config.php';
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'coach') {
header('Location: login.php');
exit();
}
$coach_id = $_SESSION['user_id'];
$pdo = db();
// Fetch coach's current portfolio data
$stmt = $pdo->prepare("SELECT bio, specialties FROM coaches WHERE id = ?");
$stmt->execute([$coach_id]);
$coach = $stmt->fetch();
$bio = $coach['bio'] ?? '';
$specialties = $coach['specialties'] ?? '';
// Handle form submission for bio and specialties
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) {
$bio = trim($_POST['bio']);
$specialties = trim($_POST['specialties']);
$stmt = $pdo->prepare("UPDATE coaches SET bio = ?, specialties = ? WHERE id = ?");
$stmt->execute([$bio, $specialties, $coach_id]);
header('Location: edit-portfolio.php?success=1');
exit();
}
// Handle media upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload_media'])) {
if (isset($_FILES['media']) && $_FILES['media']['error'] == 0) {
$caption = trim($_POST['caption']);
$allowed = ['jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif'];
$filename = $_FILES['media']['name'];
$filetype = $_FILES['media']['type'];
$filesize = $_FILES['media']['size'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!array_key_exists($ext, $allowed)) {
die("Error: Please select a valid file format.");
}
$maxsize = 5 * 1024 * 1024;
if ($filesize > $maxsize) {
die("Error: File size is larger than the allowed limit.");
}
if (in_array($filetype, $allowed)) {
$new_filename = uniqid() . '.' . $ext;
$filepath = 'uploads/portfolio/' . $new_filename;
$error_message = '';
if (move_uploaded_file($_FILES['media']['tmp_name'], $filepath)) {
$stmt = $pdo->prepare("INSERT INTO coach_portfolio_items (coach_id, item_type, url, caption) VALUES (?, 'image', ?, ?)");
$stmt->execute([$coach_id, $filepath, $caption]);
header('Location: edit-portfolio.php?success=2');
exit();
} else {
$error_message = 'Error: There was a problem uploading your file. Please try again.';
} }
}
}
// Handle media deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_media'])) {
$item_id = $_POST['delete_item_id'];
// First, get the file path to delete the file
$stmt = $pdo->prepare("SELECT url FROM coach_portfolio_items WHERE id = ? AND coach_id = ?");
$stmt->execute([$item_id, $coach_id]);
$item = $stmt->fetch();
if ($item) {
// Delete file from server
if (file_exists($item['url'])) {
unlink($item['url']);
}
// Delete from database
$stmt = $pdo->prepare("DELETE FROM coach_portfolio_items WHERE id = ?");
$stmt->execute([$item_id]);
header('Location: edit-portfolio.php?success=3');
exit();
}
}
// Fetch all portfolio items for the coach
$stmt = $pdo->prepare("SELECT * FROM coach_portfolio_items WHERE coach_id = ? ORDER BY created_at DESC");
$stmt->execute([$coach_id]);
$portfolio_items = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Portfolio</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<?php include 'includes/header.php'; ?>
<div class="container mt-5">
<h2>Edit Portfolio</h2>
<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success">Portfolio updated successfully.</div>
<?php endif; ?>
<?php if (!empty($error_message)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<h5 class="card-title">Profile Information</h5>
<form method="POST" action="edit-portfolio.php">
<div class="form-group">
<label for="bio">Biography</label>
<textarea class="form-control" id="bio" name="bio" rows="5"><?php echo htmlspecialchars($bio); ?></textarea>
</div>
<div class="form-group">
<label for="specialties">Specialties</label>
<input type="text" class="form-control" id="specialties" name="specialties" value="<?php echo htmlspecialchars($specialties); ?>" placeholder="e.g., Nutrition, Strength Training, Yoga">
</div>
<button type="submit" name="update_profile" class="btn btn-primary">Save Changes</button>
</form>
</div>
</div>
<div class="card mt-4">
<div class="card-body">
<h5 class="card-title">Portfolio Media</h5>
<form method="POST" action="edit-portfolio.php" enctype="multipart/form-data">
<div class="form-group">
<label for="media">Upload Image</label>
<input type="file" class="form-control-file" id="media" name="media" accept="image/*">
</div>
<div class="form-group">
<label for="caption">Caption</label>
<input type="text" class="form-control" id="caption" name="caption" placeholder="Optional caption">
</div>
<button type="submit" name="upload_media" class="btn btn-secondary">Upload Media</button>
</form>
<hr>
<div class="row">
<?php foreach ($portfolio_items as $item): ?>
<div class="col-md-4 mb-3">
<div class="card">
<img src="<?php echo htmlspecialchars($item['url']); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($item['caption']); ?>">
<div class="card-body">
<p class="card-text"><?php echo htmlspecialchars($item['caption']); ?></p>
<form method="POST" action="edit-portfolio.php" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="delete_item_id" value="<?php echo $item['id']; ?>">
<button type="submit" name="delete_media" class="btn btn-danger btn-sm">Delete</button>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>