| Server IP : 72.60.21.38 / Your IP : 216.73.216.164 Web Server : LiteSpeed System : Linux uk-fast-web1372.main-hosting.eu 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64 User : u390967363 ( 390967363) PHP Version : 8.2.30 Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail MySQL : OFF | cURL : ON | WGET : ON | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/u390967363/domains/aibenproperties.com/public_html/app/ |
Upload File : |
<?php
require 'includes/header.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';
$companyId = getCurrentCompanyId();
if (!$companyId) {
die("Access denied: No company context.");
}
$id = $_GET['id'] ?? null;
if (!$id) {
header("Location: estates.php");
exit;
}
// Fetch existing estate
$stmt = $pdo->prepare("SELECT * FROM estates WHERE id = ? AND company_id = ?");
$stmt->execute([$id, $companyId]);
$estate = $stmt->fetch();
if (!$estate) {
die("Estate not found or access denied.");
}
$success = "";
$error = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name']);
$location = trim($_POST['location']);
$description = trim($_POST['description']);
$status = $_POST['status'];
// File Upload Logic
$imagePath = $estate['image'];
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
$uploadDir = 'uploads/estates/';
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
$fileName = time() . '_' . basename($_FILES['image']['name']);
$targetFile = $uploadDir . $fileName;
if (move_uploaded_file($_FILES['image']['tmp_name'], $targetFile)) {
$imagePath = $targetFile;
}
}
try {
$stmt = $pdo->prepare("UPDATE estates SET name = ?, location = ?, description = ?, status = ?, image = ? WHERE id = ? AND company_id = ?");
$stmt->execute([$name, $location, $description, $status, $imagePath, $id, $companyId]);
$success = "Estate project updated successfully!";
// Refresh data
$estate['name'] = $name;
$estate['location'] = $location;
$estate['description'] = $description;
$estate['status'] = $status;
$estate['image'] = $imagePath;
} catch (Exception $e) {
$error = "Error updating estate: " . $e->getMessage();
}
}
// Handle Phase Addition
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_phase') {
$phaseName = trim($_POST['phase_name']);
$phaseDesc = trim($_POST['phase_description']);
if (!empty($phaseName)) {
try {
$stmt = $pdo->prepare("INSERT INTO phases (estate_id, name, description) VALUES (?, ?, ?)");
$stmt->execute([$id, $phaseName, $phaseDesc]);
$success = "Phase added successfully!";
} catch (Exception $e) {
$error = "Error adding phase: " . $e->getMessage();
}
}
}
// Handle Phase Deletion
if (isset($_GET['delete_phase'])) {
$phaseId = $_GET['delete_phase'];
try {
// Verify phase belongs to this estate (and implicitly company via estate logic)
$stmt = $pdo->prepare("DELETE FROM phases WHERE id = ? AND estate_id = ?");
$stmt->execute([$phaseId, $id]);
$success = "Phase deleted successfully!";
} catch (Exception $e) {
$error = "Error deleting phase: " . $e->getMessage(); // Likely constraint violation if properties exist
}
}
// Fetch Phases
$phasesStmt = $pdo->prepare("SELECT * FROM phases WHERE estate_id = ? ORDER BY created_at ASC");
$phasesStmt->execute([$id]);
$phases = $phasesStmt->fetchAll();
?>
<div class="container-fluid px-4 py-4">
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h1 class="h3 fw-bold text-primary mb-1">Edit Estate: <?= htmlspecialchars($estate['name']) ?></h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb mb-0 text-muted small">
<li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
<li class="breadcrumb-item"><a href="estates.php">Estates</a></li>
<li class="breadcrumb-item active">Edit</li>
</ol>
</nav>
</div>
</div>
<div class="card border-0 shadow-sm">
<div class="card-body p-4">
<?php if ($success): ?>
<div class="alert alert-success"><?= $success ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data">
<div class="row g-3">
<div class="col-md-12">
<label class="form-label fw-bold">Project Name <span class="text-danger">*</span></label>
<input type="text" name="name" class="form-control" required value="<?= htmlspecialchars($estate['name']) ?>">
</div>
<div class="col-md-12">
<label class="form-label fw-bold">Location <span class="text-danger">*</span></label>
<input type="text" name="location" class="form-control" required value="<?= htmlspecialchars($estate['location']) ?>">
</div>
<div class="col-md-12">
<label class="form-label fw-bold">Description</label>
<textarea name="description" class="form-control" rows="4"><?= htmlspecialchars($estate['description']) ?></textarea>
</div>
<div class="col-md-6">
<label class="form-label fw-bold">Project Status</label>
<select name="status" class="form-select">
<option value="active" <?= $estate['status'] == 'active' ? 'selected' : '' ?>>Active (Selling)</option>
<option value="inactive" <?= $estate['status'] == 'inactive' ? 'selected' : '' ?>>Inactive (Developing)</option>
<option value="sold_out" <?= $estate['status'] == 'sold_out' ? 'selected' : '' ?>>Sold Out</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label fw-bold">Project Image</label>
<input type="file" name="image" class="form-control" accept="image/*">
<?php if (!empty($estate['image'])): ?>
<div class="mt-2">
<img src="<?= htmlspecialchars($estate['image']) ?>" alt="Current Image" class="img-thumbnail" style="height: 100px;">
<div class="form-text">Current image. Upload new to replace.</div>
</div>
<?php endif; ?>
</div>
<div class="col-12 mt-4 d-flex justify-content-end gap-2">
<a href="estates.php" class="btn btn-light">Cancel</a>
<button type="submit" class="btn btn-primary px-4">Update Project</button>
</div>
</div>
</form>
</div>
</div>
<!-- Phases Management -->
<div class="card border-0 shadow-sm mt-4">
<div class="card-header bg-white py-3">
<h5 class="mb-0 fw-bold">Project Phases</h5>
</div>
<div class="card-body p-4">
<div class="table-responsive mb-4">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th>Phase Name</th>
<th>Description</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($phases)): ?>
<tr><td colspan="4" class="text-center text-muted">No phases added yet.</td></tr>
<?php else: ?>
<?php foreach($phases as $phase): ?>
<tr>
<td class="fw-bold"><?= htmlspecialchars($phase['name']) ?></td>
<td><?= htmlspecialchars($phase['description']) ?></td>
<td><?= date('M d, Y', strtotime($phase['created_at'])) ?></td>
<td>
<a href="?id=<?= $id ?>&delete_phase=<?= $phase['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure? Properties linked to this phase might be affected.');">
<i class="fa-solid fa-trash"></i>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<h6 class="fw-bold text-primary mb-3">Add New Phase</h6>
<form method="POST" class="row g-3">
<input type="hidden" name="action" value="add_phase">
<div class="col-md-5">
<input type="text" name="phase_name" class="form-control" placeholder="Phase Name (e.g. Phase 1, North Wing)" required>
</div>
<div class="col-md-5">
<input type="text" name="phase_description" class="form-control" placeholder="Description (Optional)">
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-success w-100"><i class="fa-solid fa-plus"></i> Add</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php require 'includes/footer.php'; ?>