home/skymarketplace/public_html/controllers/HeroImageController.php 0000644 00000003736 15003700700 0022001 0 ustar 00 model = new HeroModel($database->getConnection());
}
public function uploadHeroImage(array $files): void
{
if (!isset($files['hero_image'])) {
$this->jsonResponse(false, 'No image uploaded.');
return;
}
$image = $files['hero_image'];
$imageName = time() . '_' . basename($image['name']);
$targetPath = $this->uploadDir . $imageName;
if (!move_uploaded_file($image['tmp_name'], $targetPath)) {
$this->jsonResponse(false, 'Image upload failed.');
return;
}
if ($this->model->insertHeroImage($imageName)) {
$this->jsonResponse(true, 'Hero image uploaded successfully.');
} else {
$this->jsonResponse(false, 'Database error.');
}
}
public function deleteHeroImage(array $data): void
{
$id = $data['delete_hero_id'] ?? null;
$image = $data['delete_hero_image'] ?? null;
if (!$id || !$image) {
$this->jsonResponse(false, 'Missing data for deletion.');
return;
}
$filePath = $this->uploadDir . $image;
if (file_exists($filePath)) {
unlink($filePath);
}
if ($this->model->deleteHeroImage((int)$id)) {
$this->jsonResponse(true, 'Hero image deleted successfully.');
} else {
$this->jsonResponse(false, 'Database error.');
}
}
private function jsonResponse(bool $success, string $message): void
{
echo json_encode([
'status' => $success ? 'success' : 'error',
'message' => $message
]);
}
}