Beispiel #1
0
function edit($s) {	
	$_SESSION['last_flat_id'] = (isset($_SESSION['last_flat_id'])) ? $_SESSION['last_flat_id'] : -1;	
	if (empty($_POST)) {
		$id = intval($_REQUEST['id']);
		if (isset($_SESSION["admin"])) $_SESSION['last_flat_id'] = $id; 	
		if ($id!=$_SESSION['last_flat_id'] && !$_SESSION['user_id'] && !$_SESSION["admin"]) {
			header("Location: /index.html");
			exit();
		}		
		$flat = new Flat();
		$flat->getFull($id);
		if (isset($_SESSION['user_id']) && $flat->user_id != $_SESSION['user_id'] && !$_SESSION["admin"] 
			&& $id!=$_SESSION['last_flat_id']) {
			header("Location: /index.html");
			exit();
		}		
		$block_html = Html::pageFlatEdit($flat);
		$s->assign("YANDEX_KEY",YANDEX_KEY);	
		$s->assign("block_html",$block_html);
		$s->display("flat_edit.tpl");
	}
	else {
		$flat = new Flat();		
		$id = intval($_REQUEST['id']);
		if ($id!=$_SESSION['last_flat_id'] && !$_SESSION['user_id'] && !$_SESSION["admin"]) {
			header("Location: /index.html");
			exit();
		}
		if (!isset($_SESSION['user_id'])) {
			$flat->getFull($id);
		}
		else {
			$flat->getFull($id,"f.user_id='{$_SESSION['user_id']}'");
			if ($flat->user_id != $_SESSION['user_id'] && !$_SESSION["admin"] && $id!=$_SESSION['last_flat_id']) {
				header("Location: /index.html");
				exit();
			}
			else {
				//$_SESSION['last_flat_id'] = $flat->user_id; 
			}
		}
		$_REQUEST['price'] = intval(numStrToClearStr($_REQUEST['price']));
				
		if (in_array($flat->status,array(REALTY_STATUS_RENT,REALTY_STATUS_RENT_APPLY,REALTY_STATUS_RENT_NEW))) {
			Flat::$_properties['price']['min_val']=1000;
		}
		$errors = $flat->checkForm($_REQUEST);		
		if (!isset($errors['is_error']) && $flat->id > 0) {						
			$flat->update($_POST);
			$photo_flat_path = $flat->getPhotoPath();
			if (!is_dir($photo_flat_path)) {			
				mkdir($photo_flat_path,0777);
				chmod($photo_flat_path,0777);						
			}
			//добавляем новые фото
			if (isset($_POST['photo_flat']) && is_array($_POST['photo_flat'])) {
				foreach ($_POST['photo_flat'] as $fname) {
					$fname = clearTextData($fname);
					if ($fname!='') $flat->addPhoto($fname,$_POST);
				}
			}
			//редактируем существующие фото
			if (isset($_POST['photo_flat_exist']) && is_array($_POST['photo_flat_exist'])) {
				foreach ($_POST['photo_flat_exist'] as $fname) {
					$fname = clearTextData($fname);
					if ($fname!='') $flat->editPhoto($fname,$_POST);
				}
			}			
			header("Location: /flat.html?action=view&id=".$flat->id);
			exit();
		}
		else {
			//echo "error";
			//print_r($errors);
			$id = intval($_REQUEST['id']);			
			$flat->getFull($id);				
			$block_html = Html::pageFlatEdit($flat,$errors);
			$s->assign("YANDEX_KEY",YANDEX_KEY);
			if (isset($_SESSION['admin'])) $s->assign("is_admin",$_SESSION['admin']);			
			$s->assign("block_html",$block_html);
			$s->display("flat_add.tpl");
		}
	}
}