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

	function tc_table(){

	}
	function new_id(){
		return $this->dest_id;
	}
	function post2table( $mytablename, $property_id ){
		global $xoopsDB,$myts;

		$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 ;
		
		// hp flag is set if there are no records which has flag of recommends
		$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'" ;

		$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
		);
		
		if( is_null($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'" ;
		}
		$ret = $xoopsDB->queryF( $sql );

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