<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Flash extends Controller
{
	function Flash()
	{
		parent::Controller();
		check_is_login();
		$this->load->database();
		$this->load->model("shop_admin/Flash_model");
		$this->load->library('form_validation');
	}
	function index($catalog_id=0)
	{
		$data = array(
			'query'	=> $this->Flash_model->get_records($catalog_id),
			'catalog_id' => $catalog_id
		);
		$this->load->view(TPL_FOLDER."flash_list",$data);
	}
	
	function sort_record()
	{
		$this->form_validation->set_rules("rd_id","排序项","required");
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg=validation_errors();
			echo_msg($tx_msg);
		}

		if($this->Flash_model->sort_record())
		{
			
			$this->_make_flash_img_cache();
			$tx_msg="<li>记录排序成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录排序失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function add_record()
	{
		$this->form_validation->set_rules("title","标题","trim|required");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		$up_img=do_upload(array('up_path'=>UP_IMAGES_PATH,'form_name'=>"pic",'suffix'=>UP_IMAGES_EXT));
		if(!$up_img['status'])
		{
			$tx_msg.=$up_img['upload_errors'];
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		if($this->Flash_model->add_record($up_img))
		{
			
			$this->_make_flash_img_cache();
			$tx_msg="<li>记录添加成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录添加失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function add_catalog_record()
	{
		$this->form_validation->set_rules("cat_name","名称","trim|required");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		if($this->Flash_model->add_catalog_record())
		{
			
			$tx_msg="<li>记录添加成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录添加失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function edit_record()
	{
		$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->Flash_model->get_record($rd_id);
		$data=array(
			'url'=>$get['url'],
			'edit_data'=>$edit_data
		);
		$this->load->view(TPL_FOLDER."flash_edit",$data);
	}
	
	function save_record()
	{
		$this->form_validation->set_rules("title","标题","trim|required");
		$this->form_validation->set_rules("rd_id","记录ID","trim|integer");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		$up_img=do_upload(array('up_path'=>UP_IMAGES_PATH,'form_name'=>"pic",'suffix'=>UP_IMAGES_EXT));
		if(!$up_img['status'])
		{
			$tx_msg.=$up_img['upload_errors'];
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		if($this->Flash_model->save_record($up_img))
		{
			
			$this->_make_flash_img_cache();
			$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->Flash_model->ajax_toggle())
		{
			
			$this->_make_flash_img_cache();
			echo "1";
		}
		else
		{
			echo "0";
		}
	}

	function del_record()
	{
		$this->form_validation->set_rules("rd_id","删除的记录","required");
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg=validation_errors();
			echo_msg($tx_msg);
		}
		if($this->Flash_model->del_record())
		{
			$this->_make_flash_img_cache();
			$tx_msg="<li>记录删除成功.</li>";
			echo_msg($tx_msg,'','yes');
		}
		else
		{
			$tx_msg="<li>记录删除失败.</li>";
			echo_msg($tx_msg);
		}
	}
	
	function _make_flash_img_cache()
	{
		$this->db->select('title,hplink,pic_path');
		$this->db->where('is_trash',0);
		$this->db->order_by('seqorder','ASC');
		$query = $this->db->get('shop_flash');
		if($query->num_rows() > 0)
		{
			$arr = $query->result_array();
			$str = '$config["flash_img"] = ';
			$str .= var_export($arr,true);
			$str .= ';';
		}
		else
		{
			$str = '$config["flash_img"] = array();';
		}
		$this->load->helper('file');
		$string = read_file(APPPATH.'config/flash_img_config'.EXT);
		$reg = '/\$config\[\"flash_img\"\].+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$string = str_replace($out[0][0],$str,$string);
		}
		write_file(APPPATH.'config/flash_img_config'.EXT, $string);
	}
}
?>