Example #1
0
function add($s) {	
	if (empty($_POST)) {		
		$tenement = new Tenement();			 						
		$flat = new Flat();
		if (isset($_COOKIE['contacts'])) {			
			$errors[FLAT]['val']['contacts'] = $_COOKIE['contacts'];
		}
		else {
			$errors = NULL;	
		}							
		$block_html = Html::pageFlatAdd($tenement,$flat,$errors);
		$s->assign("block_html",$block_html);
		$s->assign("YANDEX_KEY",YANDEX_KEY);
		$s->assign("LAT_CENTER_REGION",LAT_CENTER_REGION);
		$s->assign("LON_CENTER_REGION",LON_CENTER_REGION);				
		$s->display("flat_add.tpl");
	}
	else {
		$tenement = new Tenement();		
		if ($_REQUEST['city_id']>1) {
			Tenement::$_properties['street']['required']=null;
			Tenement::$_properties['street_id']['required']=null;	
			Tenement::$_properties['number']['required']=null;
		}		
		$errors_tenement = (!isset($_REQUEST['id'])||!$_REQUEST['id']) ? Tenement::checkForm($_REQUEST) : array();		
		$flat = new Flat();
		//Продажа
		$_POST['flat__price'] = intval(numStrToClearStr($_POST['flat__price']));
		if ($_POST['type_deal']==SALE && $_POST['flat__price']<20000) {
			$_POST['flat__price'] *= 1000;
			$_REQUEST['flat__price'] = $_POST['flat__price']; 
		}
		if ($_POST['type_deal']==RENT && $_POST['flat__price']>=100000) {
			$_POST['type_deal']= SALE;
		}
		
		if ($_POST['type_deal']==RENT) {
			Flat::$_properties['price']['min_val']=1000;
		}
		$errors_flat = Flat::checkForm($_REQUEST,'flat__');						
		$errors = array(TENEMENT=>$errors_tenement,FLAT=>$errors_flat);		
		require_once './libs/securimage/securimage.php';
		$securimage = new Securimage();
		if ($securimage->check($_POST['captcha_code']) == false) {
			$errors['captcha']['is_error'] = 1;
		}		
		if (!isset($errors[TENEMENT]['is_error'])&&!isset($errors[FLAT]['is_error'])&&!isset($errors['captcha']['is_error'])) {
			//Дом уже есть в бд
			if (isset($_POST['id']) && $_POST['id']>0) {				
				$tenement_id = intval($_POST['id']);
				$tenement->find($tenement_id);
				if (!$tenement->id) {
					echo "Дом не найден";
					exit();
				}
			}
			//Добавляем дом в бд
			else {				
				$tenement->add($_POST);
			}			
			//Квартира уже есть в бд
			if (isset($_POST['flat_id'])) {
				echo "exist:".$flat_id = intval($_POST['flat_id']);
				$flat->getFull($flat_id);
				$flat_id = $flat->id;				
			}
			//Добавляем кв в бд
			else {
				$data = $_POST;
				$data['flat__tenement_id'] = $tenement->id;
				$data['flat__status'] = $data['type_deal']==SALE ? REALTY_STATUS_NEW : REALTY_STATUS_RENT_NEW;
				if ($tenement_id>0 && isset($_SESSION['user_id'])) {
					if ($data['flat__status']==REALTY_STATUS_NEW) {
						$data['flat__status'] = REALTY_STATUS_SALE;
					}
					else {
						$data['flat__status'] = REALTY_STATUS_RENT;
					}					
				}
				$data['flat__description'] = clearTextData($data['flat__description'],10000);
				$data['flat__contacts'] = clearTextData($data['flat__contacts'],1000); 
				$flat->add($data,'flat__');
			}
			setcookie('contacts',stripslashes($_POST['flat__contacts']));
			$_SESSION['last_flat_id'] = $flat->id;					
			$photo_tenement_path = $tenement->getPhotoPath();
			if (!is_dir($photo_tenement_path)) {
				mkdir($photo_tenement_path,0777,true);				
			}
			if (isset($_POST['photo_tenement']) && is_array($_POST['photo_tenement'])) {
				foreach ($_POST['photo_tenement'] as $fname) {
					$fname = clearTextData($fname);
					$tenement->addPhoto($fname,$_POST);
				}
			}
			$photo_flat_path = $flat->getPhotoPath();
			if (!is_dir($photo_flat_path)) {			
				mkdir($photo_flat_path,0777);						
			}
			if (isset($_POST['photo_flat']) && is_array($_POST['photo_flat'])) {
				foreach ($_POST['photo_flat'] as $fname) {
					$fname = clearTextData($fname);
					$flat->addPhoto($fname,$_POST);
				}
			}
			if ($flat->id > 0) header("Location: /flat.html?action=view&id=".$flat->id);			
			exit();
		}
		else {
			//echo "Error:".print_r($errors);
			$block_html = Html::pageFlatAdd($tenement,$flat,$errors);			
			$s->assign("YANDEX_KEY",YANDEX_KEY);
			if (isset($_SESSION['admin'])) $s->assign("is_admin",$_SESSION['admin']);
			if (isset($_REQUEST['id'])) $s->assign("tenement_id",intval($_REQUEST['id']));			
			$s->assign("city_id",intval($_REQUEST['city_id']));
			$s->assign("street_id",intval($_REQUEST['street_id']));
			$s->assign("lon",clearTextData($_REQUEST['lon']));
			$s->assign("lat",clearTextData($_REQUEST['lat']));
			$s->assign("is_error",1);
			$s->assign("block_html",$block_html);
			$s->display("flat_add.tpl");
		}
	}	
}