<?php
class detail_table {
	var $dest_id = 0;
	var $record=array();
	var $mytablename;

	function detail_table($mytablename){
		$this->mytablename = $mytablename;
	}
	function new_id(){
		return $this->dest_id;
	}
	function getByPropertyId($property_id,$tblname=null){
		global $xoopsDB;
		// main query
		if (is_null($tblname))$tblname = $this->mytablename;
		$sql = "SELECT * FROM $tblname WHERE property_id=".$property_id;
		$result = $xoopsDB->query( $sql ) ;
		if( ( $result_array = $xoopsDB->fetchArray( $result ) ) == false ) {
			redirect_header( XOOPS_URL.'/' , 2 , _TC_FILENOTFOUND .$sql) ;
			exit ;
		}
		return $result_array;
	}
	function getById($id){
		global $xoopsDB;
		// main query
		$sql = "SELECT * FROM $this->mytablename WHERE id=".$id;
		$result = $xoopsDB->query( $sql ) ;
		if( ( $result_array = $xoopsDB->fetchArray( $result ) ) == false ) {
			redirect_header( XOOPS_URL.'/' , 2 , _TC_FILENOTFOUND .$sql) ;
			exit ;
		}
		return $result_array;
	}
	function post2table( $mytablename, $id = NULL ){
		global $xoopsDB,$myts;

		$property_id = $myts->addSlashes( $_POST['property_id'] ) ;
		$title4save = $myts->addSlashes( $_POST['title'] ) ;
		$html_header4save = $myts->addSlashes( $_POST['html_header'] ) ;
		$text4save = $myts->addSlashes( $_POST['message'] ) ;
		$visible = empty( $_POST['visible'] ) ? 0 : 1 ;
		$nohtml = empty( $_POST['nohtml'] ) ? 0 : intval( $_POST['nohtml'] ) ;
		$nosmiley = empty( $_POST['nosmiley'] ) ? 0 : 1 ;
		$nobreaks = empty( $_POST['nobreaks'] ) ? 0 : 1 ;
		$nocomments = empty( $_POST['comments'] ) ? 1 : 0 ;
		$submenu = empty( $_POST['submenu'] ) ? 0 : 1 ;
		$recommends = empty( $_POST['recommends'] ) ? 0 : 1 ;
		$address = $myts->addSlashes( $_POST['address'] ) ;
		$railline_name = $myts->addSlashes( $_POST['railline'] ) ;
		$station_name = $myts->addSlashes( $_POST['station'] ) ;
		$twf_station = empty( $_POST['takewalk'] ) ? 0 : intval( $_POST['takewalk'] ) ;
		$layout_desc = $myts->addSlashes( $_POST['layout_desc'] ) ;
		$price = empty( $_POST['price'] ) ? 0 : intval( $_POST['price'] ) ;
		
		// hp flag is set if there are no records which has flag of recommends
		if( is_null($id) && $property_id ){
			$sql = "SELECT id FROM $mytablename WHERE property_id=$property_id";
			$result = $xoopsDB->query( $sql ) ;
			list( $id ) = $xoopsDB->fetchRow( $result ) ;
		}
		$sql_set = "SET property_id='$property_id',title='$title4save',text='$text4save',visible='$visible',nohtml='$nohtml',nosmiley='$nosmiley',nobreaks='$nobreaks',"
		. "nocomments='$nocomments',link='0',submenu='$submenu',recommends='$recommends',html_header='$html_header4save',"
		. "address='$address',railline_name='$railline_name',station_name='$station_name',twf_station='$twf_station',layout_desc='$layout_desc',price='$price'";

		$this->record = array(
			'property_id' => $property_id,
			'title' => $title4save,
			'text' => $text4save,
			'visible' => $visible,
			'nohtml'=> $nohtml,
			'nosmiley' => $nosmiley,
			'nobreaks' => $nobreaks,
			'nocomments'=>$nocomments,
			'link'=>'0',
			'submenu' => $submenu,
			'recommends' => $recommends,
			'html_header' => $html_header4save,
			'address'=> $address,
			'railline_name'=> $railline_name,
			'station_name'=> $station_name,
			'twf_station'=> $twf_station,
			'layout_desc'=> $layout_desc,
			'price'=> $price
		);
		
		if( is_null($id) || $id=='' ) {
			$sql = "INSERT INTO $mytablename $sql_set,created=NOW()";
		} else {
			// not to update last_modified
			if( ! empty( $_POST['dont_update_last_modified'] ) ) $sql_set .= ",last_modified=last_modified" ;

			$sql = "UPDATE $mytablename $sql_set WHERE id='$id'" ;
		}
		//echo $sql; die;
		$ret = $xoopsDB->queryF( $sql );

		if ( $property_id==0 ) $this->dest_id = $xoopsDB->getInsertId() ;
		
		return $ret;
	}
}
?>