format('d-m-Y');
} catch (Exception $e) {
return '-';
}
}
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_POST['csrf_token']) || !verifyCSRF($_POST['csrf_token'])) {
$error = 'Invalid CSRF token.';
} else if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'add':
case 'edit':
try {
$title = sanitizeInput($_POST['title']);
$description = $_POST['description']; // Rich text content
$location = sanitizeInput($_POST['location']);
$campaign_date = sanitizeInput($_POST['campaign_date']);
$target_amount = floatval($_POST['target_amount']);
$raised_amount = floatval($_POST['raised_amount'] ?? 0);
$status = sanitizeInput($_POST['status']);
// Validate inputs
if (empty($title) || empty($description) || empty($location) || empty($campaign_date) || $target_amount <= 0) {
throw new Exception('All required fields must be filled, and the target amount must be positive.');
}
if ($raised_amount < 0) {
throw new Exception('Raised amount cannot be negative.');
}
if ($raised_amount > $target_amount) {
throw new Exception('Raised amount cannot exceed the target amount.');
}
// Validate campaign_date
if (!DateTime::createFromFormat('Y-m-d', $campaign_date)) {
throw new Exception('Invalid campaign date. Please select a valid date.');
}
// Sanitize description (allow basic HTML tags)
$description = strip_tags($description, '
- ');
// Handle image upload
$image = '';
if (!empty($_FILES['image']['name']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$upload_dir = '../img/campaigns/';
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0755, true);
chmod($upload_dir, 0755);
}
$file_ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
if (in_array($file_ext, $allowed_ext)) {
if ($_FILES['image']['size'] <= 5 * 1024 * 1024) { // 5MB limit
$image = uniqid('campaign_') . '.' . $file_ext;
if (!move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $image)) {
throw new Exception('Error uploading image.');
}
chmod($upload_dir . $image, 0644);
} else {
throw new Exception('Image size must not exceed 5MB.');
}
} else {
throw new Exception('Invalid file type. Only JPG, JPEG, PNG, or GIF files are allowed.');
}
}
if ($_POST['action'] === 'add') {
$stmt = $db->prepare("
INSERT INTO campaigns (title, description, image, location, campaign_date, target_amount, raised_amount, status, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
");
$stmt->execute([$title, $description, $image, $location, $campaign_date, $target_amount, $raised_amount, $status]);
$message = 'Campaign added successfully!';
} else {
// Keep existing image if no new image uploaded
if (empty($image)) {
$stmt = $db->prepare("SELECT image FROM campaigns WHERE id = ?");
$stmt->execute([$id]);
$existing = $stmt->fetch();
$image = $existing['image'] ?? '';
}
$stmt = $db->prepare("
UPDATE campaigns SET title = ?, description = ?, image = ?, location = ?, campaign_date = ?, target_amount = ?, raised_amount = ?, status = ?, updated_at = NOW()
WHERE id = ?
");
$stmt->execute([$title, $description, $image, $location, $campaign_date, $target_amount, $raised_amount, $status, $id]);
$message = 'Campaign updated successfully!';
}
$action = 'list';
} catch (Exception $e) {
$error = 'Error: ' . htmlspecialchars($e->getMessage());
}
break;
case 'delete':
try {
$delete_id = $_POST['id'];
// Get image filename before deleting
$stmt = $db->prepare("SELECT image FROM campaigns WHERE id = ?");
$stmt->execute([$delete_id]);
$campaign = $stmt->fetch();
// Delete campaign
$stmt = $db->prepare("DELETE FROM campaigns WHERE id = ?");
$stmt->execute([$delete_id]);
// Delete image if it exists
if (!empty($campaign['image']) && file_exists('../img/campaigns/' . $campaign['image'])) {
unlink('../img/campaigns/' . $campaign['image']);
}
$message = 'Campaign deleted successfully!';
} catch (Exception $e) {
$error = 'Error deleting: ' . htmlspecialchars($e->getMessage());
}
break;
}
}
}
// Get campaign data for editing
$campaign_data = null;
if ($action === 'edit' && $id > 0) {
$stmt = $db->prepare("SELECT * FROM campaigns WHERE id = ?");
$stmt->execute([$id]);
$campaign_data = $stmt->fetch();
if (!$campaign_data) {
$error = 'Campaign not found!';
$action = 'list';
}
}
// Get campaign list
$campaign_list = [];
if ($action === 'list') {
$stmt = $db->prepare("SELECT * FROM campaigns ORDER BY created_at DESC");
$stmt->execute();
$campaign_list = $stmt->fetchAll();
}
include 'includes/header.php';
?>
| Image |
Title |
Location |
Date |
Target |
Raised Amount |
Status |
Actions |
|
|
|
|
₹ |
₹ |
|
|