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

<?php
require_once __DIR__.'/../config.php'; admin_required();
$id=(int)($_GET['id']??0);
if(isset($_GET['delete'])){ if(!hash_equals($_SESSION['csrf']??'',$_GET['csrf']??'')){http_response_code(419);exit('Invalid request token.');} $did=(int)$_GET['delete'];$st=db()->prepare('DELETE FROM property WHERE pid=:id');$st->execute(['id'=>$did]);flash('success','Property deleted.');go('admin/properties.php'); }
$pdo=db();$p=null;if($id){$st=$pdo->prepare('SELECT * FROM property WHERE pid=:id');$st->execute(['id'=>$id]);$p=$st->fetch();if(!$p){flash('error','Property 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=array_merge($defaults,$p??[]);
if($_SERVER['REQUEST_METHOD']==='POST'){check_csrf();$data=[];foreach(array_keys($defaults) as $k){if(in_array($k,['pimage','pimage1','pimage2','pimage3','pimage4'],true))continue;$data[$k]=$_POST[$k]??$defaults[$k];}$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];foreach(['pimage','pimage1','pimage2','pimage3','pimage4','mapimage','topmapimage','groundmapimage'] as $f){$up=upload_image($f.'_upload');if($up)$data[$f]=$up;else $data[$f]=$p[$f]??'';}
if($id){$set=[];foreach($data as $k=>$v)$set[]="`$k`=:$k";$data['where_id']=$id;$st=$pdo->prepare('UPDATE property SET '.implode(',',$set).' WHERE pid=:where_id');$st->execute($data);flash('success','Property updated successfully.');go('admin/properties.php');}else{$columns=array_keys($data);$params=array_map(fn($x)=>':'.$x,$columns);$st=$pdo->prepare('INSERT INTO property (`'.implode('`,`',$columns).'`) VALUES ('.implode(',',$params).')');$st->execute($data);flash('success','Property created successfully.');go('admin/properties.php');}}
$title=$id?'Edit property':'Add property'; include __DIR__.'/_header.php';
?>
<form class="admin-form" method="post" enctype="multipart/form-data"><input type="hidden" name="csrf" value="<?=csrf()?>"><section class="admin-panel"><div class="panel-head"><div><h2><?=$id?'Edit':'Create'?> property</h2><p class="muted">All fields map directly to your supplied `property` 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>Location<input name="location" value="<?=e($p['location'])?>" required></label><label>Price<input type="number" name="price" value="<?=e($p['price'])?>"></label><label>Size (sqft)<input type="number" name="size" value="<?=e($p['size'])?>"></label><label>Bedrooms<input type="number" name="bedroom" value="<?=e($p['bedroom'])?>"></label><label>Bathrooms<input type="number" name="bathroom" value="<?=e($p['bathroom'])?>"></label><label>Balcony<input type="number" name="balcony" value="<?=e($p['balcony'])?>"></label><label>Kitchen<input type="number" name="kitchen" value="<?=e($p['kitchen'])?>"></label><label>Hall<input type="number" 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" 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':''?>> Mark as featured</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 new files or keep the existing database filename.</p></div></div><div class="image-form-grid"><?php foreach(['pimage','pimage1','pimage2','pimage3','pimage4','mapimage','topmapimage','groundmapimage'] as $f):?><label><span><?=e($f)?></span><?php if($p[$f]):?><img src="<?=img($p[$f])?>" alt=""><small>Current: <?=e($p[$f])?></small><?php endif;?><input type="file" name="<?=$f?>_upload" accept="image/*"></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