<?
class leave_word extends action
{
	function dlist()
	{
			$this->init_tpl();
			$limit			=		10;
			$page   			=  (int)$_GET['page']?(int)$_GET['page']:1;
			$this_page     =  ($page-1)*$limit;	
			
			$sql				=		"SELECT * FROM {$this->db->db_pre}leave_word  WHERE 1=1";
			$count_sql		=		"SELECT COUNT(leave_word_id) as count FROM {$this->db->db_pre}leave_word WHERE 1=1";	
	
			if(isset($_GET['state'])&&is_numeric($_GET['state'])&&$_GET['state']>=0){
				$sql			.=		" AND state	=".$_GET['state'];
				$count_sql	.=		" AND state =".$_GET['state'];
			}
			if($name	=	$_GET['name']){
				$sql			.=		" AND name LIKE '%".$this->db->like_escape($name)."%'";
				$count_sql	.=		" AND name LIKE '%".$this->db->like_escape($name)."%'";
			}
			if($email	=	$_GET['email']){
				$sql					.=		" AND email LIKE '%".$this->db->like_escape($email)."%'";
				$count_sql			.=		" AND email LIKE '%".$this->db->like_escape($email)."%'";
			}
			if($type		=	(int)$_GET['type']){
				$sql					.=		" AND type =".$type;
				$count_sql			.=		" AND type =".$type;
			}
		
			$sql	.=" ORDER BY time DESC  LIMIT $this_page,$limit ";
			if($leave_word_res		=	 $this->db->getRecords($sql)){
				foreach($leave_word_res as $key=>$val){
					$leave_word_res[$key]['attachment_link']				=	dir_to_url($val['attachment']);
					$leave_word_res[$key]['attachment_name']				=	basename($val['attachment']);
				}
			}

			$this->tpl->out['leave_word_res']	=		$leave_word_res;	
			$count			=		$this->db->getNumber($count_sql);
			$this->tpl->out['page_arr']	=		pagination_arr($page,$count,$limit,10,'page');
	
			$leave_word_setting		=		storage_dencode($this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='leave_word_setting'"));
			foreach($leave_word_setting['type'] as $key=> $val){
				$this->tpl->out['leave_word_type'][$key]			=	$val['name'];
			}
			$this->tpl->display("leave_word.dlist.html");
	}

	function change_state()
	{
		if($this->is_ajax){
			$leave_word_id		=		(int)$_GET['leave_word_id'];
			$state				=		(int)$_GET['state'];
		}
		$result					=		$this->db->updateRecord("{$this->db->db_pre}leave_word", " leave_word_id='".$leave_word_id."'",array('state'=>$state));
		if($result){
			echo json_return('ok');exit;
		}else{
			echo json_return('no');exit;
		}
	}

	function setting()	
	{
			$this->init_tpl();
			
			$leave_word_setting		=		storage_dencode($this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='leave_word_setting'"));
			$this->tpl->out['leave_word_setting']		=	$leave_word_setting;	
			$this->tpl->display("leave_word.setting.html");
	}
	
	function save_setting()
	{
		$mail_to_admin			=		(int)$_POST['mail_to_admin'];
		$is_attachment			=		(int)$_POST['is_attachment'];
		$type_name				=		$_POST['type_name'];
		$type_return_tpl		=		$_POST['type_return_tpl'];
		$is_close				=		(int)$_POST['is_close'];

		$type_arr				=		array();
		if($type_name){
			foreach($type_name as $key=>$val){
				if(!$val){
					continue;
				}
				$type_arr[$key]	=	array('name'=>$val,'return_tpl'=>$type_return_tpl[$key]);
			}
		}

		$leave_word_setting	=		array(
			'mail_to_admin'	=>		$mail_to_admin,
			'is_attachment'	=>		$is_attachment,
			'type'				=>		$type_arr,
			'is_close'			=>		$is_close,
		);
		global $_ADMIN_LANG;
		$leave_word_setting	=		storage_encode($leave_word_setting); 	
		if($this->db->updateRecord("{$this->db->db_pre}site_config"," name='leave_word_setting'",array('value'=>$leave_word_setting))){
			$this->system_message($_ADMIN_LANG['successfully_change_settings'],'success',array($_ADMIN_LANG['continue_modification']=>'index.php?act=leave_word.setting',$_ADMIN_LANG['go_to_message_list']=>'index.php?act=leave_word.dlist&state=0'));
				exit;		
		}else{
				$this->system_message($_ADMIN_LANG['modification_failure'],'error',array($_ADMIN_LANG['return']=>'javascript:history.go(-1);'));
				exit;		
		}
	}
		
	function del_leave_word()	
	{
		if($this->is_ajax){
			$leave_word_id		=		(int)$_GET['leave_word_id'];			
		}
		$attachment				=		$this->db->getString("SELECT attachment FROM {$this->db->db_pre}leave_word WHERE leave_word_id	=$leave_word_id");
		if($attachment){
			@unlink(UPLOAD_DIR.$attachment);
		}
		$result			=		$this->db->deleteRecord("{$this->db->db_pre}leave_word "," leave_word_id=$leave_word_id");
		if($result){
			echo json_return('ok');exit;
		}else{
			echo json_return('no');exit;
		}
	}
}
