<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Flash_model extends Model
{
	function Flash_model()
	{
		parent::Model();
	}
	function add_record($f_path)
	{
		$data=array(
			'title'=>$this->input->post("title"),
			'seqorder'=>$this->input->post("seqorder"),
			'catalog_id'=>$this->input->post("catalog_id"),
			'hplink'=>$this->input->post("hplink")
		);
		if($f_path['file_path'])
		{
			$data['pic_path']=$f_path['file_path'];
		}
		else
		{
			$data['pic_path']=$this->input->post("pic_path");
		}
		$this->db->insert("shop_flash",$data);
		return TRUE;
	}
	
	function get_records($catalog_id)
	{
		$this->db->order_by('seqorder','asc');
		$this->db->order_by('id','desc');
		return $this->db->get_where("shop_flash",array('catalog_id' => $catalog_id));
	}
	
	function get_record($id)
	{
		$query=$this->db->get_where("shop_flash",array('id'=>$id));
		if($query->num_rows()>0)
		{
			return $query->row_array();
		}
	}
	
	function sort_record()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		foreach($rd_id as $row)
		{
			$sort_value=$this->input->post("sort".$row)?(int)$this->input->post("sort".$row):0;
			$this->db->update("shop_flash",array('seqorder'=>$sort_value),array('id'=>$row));
		}
		return TRUE;
	}
	
	function save_record($f_path)
	{
		$rd_id=$this->input->post("rd_id");
		$data=array(
			'title'=>$this->input->post("title"),
			'seqorder'=>$this->input->post("seqorder"),
			'hplink'=>$this->input->post("hplink")
		);
		if($f_path['file_path'])
		{
			$data['pic_path']=$f_path['file_path'];
		}
		else
		{
			$data['pic_path']=$this->input->post("pic_path");
		}
		$this->db->update("shop_flash",$data,array('id'=>$rd_id));
		return TRUE;
	}
	
	function del_record()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$this->db->where_in("id",$rd_id);
		$this->db->delete("shop_flash");
		if($this->db->affected_rows())
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	
	function ajax_toggle()
	{
		$rd_id=$this->input->post('rd_id');
		$query=$this->db->get_where("shop_flash",array('id'=>$rd_id));
		$rs=$query->row_array();
		$data=$rs['is_trash']?0:1;
		$this->db->update('shop_flash',array('is_trash'=>$data),array('id'=>$rd_id));
		return TRUE;
	}
}
?>