<?php
class admin extends Checklogin{

	public function init(){
		template('list','admin/guestbook');
	}
	
	public function setting(){
		$guestbook_arr=base::load_cache('cache_set_guestbook','_guestbook');
		assign("guestbook",$guestbook_arr);
		template("setting","admin/guestbook");
	}
	
	public function audit(){
		$id=isset($_GET['id'])?intval($_GET['id']):0;
		$state=isset($_GET['state'])?intval($_GET['state']):0;
		$this->mysql->db_update('guestbook','`state`='.$state,'`id`='.$id);
		showmsg(C('update_success'),'-1');
	}
	
	public function reply(){
		$id=isset($_GET['id'])?intval($_GET['id']):0;
		$rs=$this->mysql->get_one('select * from '.DB_PRE.'guestbook where `id`='.$id);
		$rp=$this->mysql->get_one('select * from '.DB_PRE.'guestbook_reply where `gid`='.$rs['id']);
		
		assign('rs',$rs);
		assign('rp',$rp);
		template('reply','admin/guestbook');
	}
	
	public function replysave(){
		$id=isset($_POST['id'])?intval($_POST['id']):0;
		$reply_content=safe_html($_POST['reply_content']);
		$replytime=datetime();
		
		if(empty($reply_content)){
			showmsg(C('material_not_complete'),'-1');
		}
		
		$rs=$this->mysql->get_one('select * from '.DB_PRE.'guestbook_reply where `gid`='.$id);
		if(!empty($rs['reply_content'])){
			$this->mysql->db_update('guestbook_reply','`reply_content`='.$reply_content,'`gid`='.$id);
		}else{
			$sql="insert into ".DB_PRE."guestbook_reply(gid,reply_content,replytime) values('{$id}','{$reply_content}','{$replytime}')";
			$this->mysql->query($sql);
			$this->mysql->db_update('guestbook','`state`=1','`id`='.$id);
		}

		showmsg(C('update_success'),'-1');
	}
	
	public function delete(){
		if(isset($_POST['id'])){
			foreach($_POST['id'] as $id){
				$this->mysql->db_delete('guestbook','`id`='.$id);
				$this->mysql->db_delete('guestbook_reply','`gid`='.$id);
			}
		}elseif(isset($_GET['id'])){
			$id=$_GET['id'];
			$this->mysql->db_delete('guestbook','`id`='.$id);
			$this->mysql->db_delete('guestbook_reply','`gid`='.$id);
		}else{
			showmsg(C('error'),'-1');
		}
		showmsg(C('delete_success'),'-1');
	}
}

?>