<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email_template extends Controller
{
	function Email_template()
	{
		parent::Controller();
		check_is_login();
		$this->load->model("shop_admin/Email_template_model");
		$this->load->library('form_validation');
		$this->load->database();
	}

	function index()
	{
		$data = array(
				'query'		=> $this->db->get('shop_email_template')
		);
		$this->load->view(TPL_FOLDER."email_template",$data);
	}
	
	function edit_record()
	{
		$this->load->model("Fck_model");
		$rd_id=$this->uri->segment(4,0);
		parse_str($_SERVER['QUERY_STRING'],$get);
		if(!$rd_id) 
		{
			$tx_msg="<li>ID参数有误.</li>";
			echo_msg($tx_msg);
		}
		$edit_data=$this->Email_template_model->get_record($rd_id);
		$data=array(
			'content'=> $this->Fck_model->fckeditor("content",$edit_data['content']),
			'edit_data'=>$edit_data,
			'url'=>$get['url']
		);
		$this->load->view(TPL_FOLDER."email_template_edit",$data);
	}
	
	function save_record()
	{
		$this->form_validation->set_rules("title","邮件标题","required");
		$this->form_validation->set_rules("content","模板内容","required");
		$this->form_validation->set_rules("rd_id","记录ID","trim|integer");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		if($this->Email_template_model->save_record())
		{
			
			$tx_msg="<li>记录修改成功.</li>";
			echo_msg($tx_msg,$this->input->post('url'),'yes');
		}
		else
		{
			$tx_msg="<li>记录修改失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function ajax_toggle()
	{
		$this->form_validation->set_rules("rd_id","0","trim|required");
		if($this->form_validation->run()==FALSE)
		{
			die("0");
		}
		if($this->Email_template_model->ajax_toggle())
		{
			
			echo "1";
		}
		else
		{
			echo "0";
		}
	}
}
?>