Exemplo n.º 1
0
function edit($s) {	
	$_SESSION['last_house_id'] = (isset($_SESSION['last_house_id'])) ? $_SESSION['last_house_id'] : -1;
	$id = intval($_REQUEST['id']);
	$house = new House();
	$house->getFull($id);
	//загрузка дома	
	if (empty($_POST) && $house->id>0) {		
		if (isset($_SESSION["admin"])) $_SESSION['last_house_id'] = $id; 	
		if ($id!=$_SESSION['last_house_id'] && !$_SESSION['user_id'] && !$_SESSION["admin"]) {
			header("Location: /index.html");
			exit();
		}		
		if (isset($_SESSION['user_id']) && $house->user_id != $_SESSION['user_id'] && !$_SESSION["admin"] 
			&& $id!=$_SESSION['last_house_id']) {
			header("Location: /index.html");
			exit();
		}		
		$block_html = Html::pageHouseEdit($house);
		$s->assign("YANDEX_KEY",YANDEX_KEY);
		
		$s->assign("city_id",$house->city_id);
		$s->assign("city",$house->city);
		$s->assign("street_id",$house->street_id);
		$s->assign("street",$house->street);
		if (isset($_SESSION['admin'])) $s->assign("is_admin",$_SESSION['admin']);		
		$s->assign("block_html",$block_html);
		$s->display("house_edit.tpl");
	}
	//обновление дома
	elseif (isset($_POST) && $_REQUEST['id']>0) {
		$house = new House();		
		$errors = $house->checkForm($_REQUEST);
		$id = intval($_REQUEST['id']);
		if ($id!=$_SESSION['last_house_id'] && !$_SESSION['user_id'] && !$_SESSION["admin"]) {
			header("Location: /index.html");
			exit();
		}
		if (!isset($_SESSION['user_id'])) {
			$house->getFull($id);
		}
		else {
			$house->getFull($id,"h.user_id='{$_SESSION['user_id']}'");
			if ($house->user_id != $_SESSION['user_id'] && !$_SESSION["admin"] && $id!=$_SESSION['last_house_id']) {
				header("Location: /index.html");
				exit();
			}
		}				
		if (!isset($errors['is_error']) && $house->id > 0) {						
			$house->update($_POST);
			$photo_house_path = $house->getPhotoPath();
			if (!is_dir($photo_house_path)) {			
				mkdir($photo_house_path,0777);
				chmod($photo_house_path,0777);						
			}
			//добавляем новые фото
			if (isset($_POST['photo_house']) && is_array($_POST['photo_house'])) {
				foreach ($_POST['photo_house'] as $fname) {
					$fname = clearTextData($fname);
					if ($fname!='') $house->addPhoto($fname,$_POST);
				}
			}
			//редактируем существующие фото
			if (isset($_POST['photo_house_exist']) && is_array($_POST['photo_house_exist'])) {
				foreach ($_POST['photo_house_exist'] as $fname) {
					$fname = clearTextData($fname);
					if ($fname!='') $house->editPhoto($fname,$_POST);
				}
			}			
			header("Location: /house.html?action=view&id=".$house->id);
			exit();
		}
		else {
			echo "error";
			$id = intval($_REQUEST['id']);			
			$house->getFull($id);				
			$block_html = Html::pageHouseEdit($house,$errors);
			$s->assign("YANDEX_KEY",YANDEX_KEY);
			$s->assign("is_admin",$_SESSION['admin']);			
			$s->assign("block_html",$block_html);
			$s->display("house_edit.tpl");
		}
	}
	else {
		echo "error";
	}
}