Viewing File: D:\INETPUB\VHOSTS\officespacebangalore.com\httpdocs\admin\property-edit.php

<?php
require_once __DIR__ . '/../config.php';
admin_required();

$pdo = db();
$id = (int)($_GET['id'] ?? 0);

if (isset($_GET['delete'])) {
    check_csrf_token((string)($_GET['csrf'] ?? ''));
    $did = (int)$_GET['delete'];

    $stmt = $pdo->prepare('DELETE FROM property WHERE pid=:id');
    $stmt->execute(['id'=>$did]);

    flash('success', $stmt->rowCount() ? 'Property deleted successfully.' : 'Property was not found.');
    go('admin/properties.php');
}

$defaults = [
    'title'=>'',
    'pcontent'=>'',
    'type'=>'foffice',
    'bhk'=>'',
    'stype'=>'rent',
    'bedroom'=>0,
    'bathroom'=>0,
    'balcony'=>0,
    'kitchen'=>0,
    'hall'=>0,
    'floor'=>'',
    'size'=>0,
    'price'=>0,
    'location'=>'',
    'city'=>'',
    'state'=>'',
    'feature'=>'',
    'pimage'=>'',
    'pimage1'=>'',
    'pimage2'=>'',
    'pimage3'=>'',
    'pimage4'=>'',
    'uid'=>0,
    'status'=>'available',
    'mapimage'=>'',
    'topmapimage'=>'',
    'groundmapimage'=>'',
    'totalfloor'=>'',
    'isFeatured'=>0
];

$p = null;
if ($id > 0) {
    $stmt = $pdo->prepare('SELECT * FROM property WHERE pid=:id LIMIT 1');
    $stmt->execute(['id'=>$id]);
    $p = $stmt->fetch();

    if (!$p) {
        flash('error','Property not found.');
        go('admin/properties.php');
    }
}
$p = array_merge($defaults, $p ?: []);
$formError = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        check_csrf();

        $data = [];
        foreach ($defaults as $key => $default) {
            if (in_array($key, ['pimage','pimage1','pimage2','pimage3','pimage4'], true)) continue;
            $data[$key] = trim((string)($_POST[$key] ?? $default));
        }

        $data['isFeatured'] = isset($_POST['isFeatured']) ? 1 : 0;
        $data['uid'] = (int)$data['uid'];

        foreach (['bedroom','bathroom','balcony','kitchen','hall','size','price'] as $n) {
            $data[$n] = (int)$data[$n];
        }

        $imageFields = ['pimage','pimage1','pimage2','pimage3','pimage4','mapimage','topmapimage','groundmapimage'];
        foreach ($imageFields as $field) {
            $uploaded = upload_image($field . '_upload');
            $data[$field] = $uploaded ?: (string)($p[$field] ?? '');
        }

        if ($id > 0) {
            $set = [];
            foreach ($data as $key => $value) {
                $set[] = "`{$key}` = :{$key}";
            }
            $data['where_id'] = $id;
            $stmt = $pdo->prepare(
                'UPDATE property SET ' . implode(',', $set) . ' WHERE pid=:where_id'
            );
            $stmt->execute($data);
            flash('success','Property updated successfully.');
        } else {
            $columns = array_keys($data);
            $params = array_map(static fn($key) => ':' . $key, $columns);
            $stmt = $pdo->prepare(
                'INSERT INTO property (`' . implode('`,`',$columns) . '`) VALUES (' . implode(',',$params) . ')'
            );
            $stmt->execute($data);
            flash('success','Property created successfully.');
        }

        go('admin/properties.php');
    } catch (Throwable $e) {
        $formError = 'Unable to save this property. Please check the values and database connection.';
    }
}

$title = $id > 0 ? 'Edit property' : 'Add property';
require __DIR__ . '/_header.php';
?>
<?php if ($formError): ?><div class="admin-flash error"><?=e($formError)?></div><?php endif; ?>

<form class="admin-form" method="post" enctype="multipart/form-data">
<input type="hidden" name="csrf" value="<?=e(csrf())?>">

<section class="admin-panel">
<div class="panel-head">
<div><h2><?=$id?'Edit':'Create'?> property</h2><p class="muted">Fields map directly to your supplied <code>property</code> table.</p></div>
<a class="outline-btn" href="<?=url('admin/properties.php')?>">Cancel</a>
</div>

<div class="form-grid">
<label class="full">Title<input name="title" value="<?=e($p['title'])?>" required></label>

<label>Property type
<select name="type">
<?php foreach(property_types() as $k=>$v): ?>
<option value="<?=e($k)?>" <?=$p['type']===$k?'selected':''?>><?=e($v)?></option>
<?php endforeach; ?>
</select>
</label>

<label>Purpose
<select name="stype">
<option value="rent" <?=$p['stype']==='rent'?'selected':''?>>Rent</option>
<option value="sale" <?=$p['stype']==='sale'?'selected':''?>>Sale</option>
</select>
</label>

<label>City<input name="city" value="<?=e($p['city'])?>" required></label>
<label>State<input name="state" value="<?=e($p['state'])?>" required></label>
<label class="full">Location<input name="location" value="<?=e($p['location'])?>" required></label>

<label>Price<input type="number" min="0" name="price" value="<?=e($p['price'])?>"></label>
<label>Size (sqft)<input type="number" min="0" name="size" value="<?=e($p['size'])?>"></label>
<label>BHK<input name="bhk" value="<?=e($p['bhk'])?>"></label>
<label>Bedrooms<input type="number" min="0" name="bedroom" value="<?=e($p['bedroom'])?>"></label>
<label>Bathrooms<input type="number" min="0" name="bathroom" value="<?=e($p['bathroom'])?>"></label>
<label>Balcony<input type="number" min="0" name="balcony" value="<?=e($p['balcony'])?>"></label>
<label>Kitchen<input type="number" min="0" name="kitchen" value="<?=e($p['kitchen'])?>"></label>
<label>Hall<input type="number" min="0" name="hall" value="<?=e($p['hall'])?>"></label>
<label>Floor<input name="floor" value="<?=e($p['floor'])?>"></label>
<label>Total floors<input name="totalfloor" value="<?=e($p['totalfloor'])?>"></label>
<label>User / Agent UID<input type="number" min="0" name="uid" value="<?=e($p['uid'])?>"></label>
<label>Status<input name="status" value="<?=e($p['status'])?>"></label>

<label class="check-field">
<input type="checkbox" name="isFeatured" value="1" <?=$p['isFeatured']?'checked':''?>>
<span>Mark as featured</span>
</label>

<label class="full">Description<textarea name="pcontent" rows="10"><?=e($p['pcontent'])?></textarea></label>
<label class="full">Features / amenities<textarea name="feature" rows="10"><?=e($p['feature'])?></textarea></label>
</div>
</section>

<section class="admin-panel">
<div class="panel-head">
<div><h2>Property images</h2><p class="muted">Upload JPG, PNG or WebP files up to 8 MB. Existing database filenames are preserved until replaced.</p></div>
</div>

<div class="image-form-grid">
<?php foreach(['pimage','pimage1','pimage2','pimage3','pimage4','mapimage','topmapimage','groundmapimage'] as $field): ?>
<label>
<span><?=e($field)?></span>
<?php if (!empty($p[$field])): ?>
<img src="<?=e(img((string)$p[$field]))?>" alt="" loading="lazy">
<small>Current: <?=e($p[$field])?></small>
<?php else: ?>
<div class="image-empty">No image</div>
<?php endif; ?>
<input type="file" name="<?=e($field)?>_upload" accept="image/jpeg,image/png,image/webp,image/gif">
</label>
<?php endforeach; ?>
</div>
</section>

<div class="form-actions">
<a class="outline-btn" href="<?=url('admin/properties.php')?>">Cancel</a>
<button class="admin-btn" type="submit"><?=$id?'Save changes':'Create property'?> →</button>
</div>
</form>

<?php require __DIR__ . '/_footer.php'; ?>
Back to Directory File Manager