<?php
//  ------------------------------------------------------------------------ //
//                XOOPS Cube Realty - Realty Management System               //
//                    Copyright (c) 2011 Bluemoon inc.                       //
//               auther Yoshi Sakai <http://www.bluemooninc.jp/>             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
class model_detail {
	var $dest_id = 0;
	var $record=array();
	var $mytablename;

	function model_detail($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'] ) ;
		$transit = empty( $_POST['takewalk'] ) ? 0 : intval( $_POST['takewalk'] ) ;
		$layout_desc = $myts->addSlashes( $_POST['layout_desc'] ) ;
		$price = empty( $_POST['price'] ) ? 0 : intval( $_POST['price'] ) ;
		$realty_switch = $_SESSION['realty_switch'];
		// 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',transit='$transit',layout_desc='$layout_desc',price='$price',realty_switch='$realty_switch'";

		$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,
			'transit'=> $transit,
			'layout_desc'=> $layout_desc,
			'price'=> $price,
			'realty_switch' => $realty_switch
		);
		
		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;
	}
}
?>