<?php 
class ad_model extends Model {
	function ad_model(){
		parent::Model();
		$this->_tables['ad_orderform']	=	"ad_orderform";
		$this->_tables['ad_position']	=	"ad_position";
	}
	
	function getAdlist($page_no=false,$showlines=false){
		$this->db->order_by($this->_tables['ad_position'].".ad_name DESC");
			if($page_no){
				$this->db->limit($showlines,$page_no);
			}
			return $this->db->get($this->_tables['ad_position']);
	}
	
	function getAdmun($page_no=false,$showlines=false){
		$this->db->order_by($this->_tables['ad_position'].".ad_name DESC");
			if($page_no){
				$this->db->limit($showlines,$page_no);
			}
			$data=$this->db->get($this->_tables['ad_position'])->result_array();
			
			$i=0;
			foreach($data as $data){
				
				$this->db->where("position_id",$data['ad_position']);
				@$data1['admun'][$i]=$this->db->get($this->_tables['ad_orderform'])->num_rows();
				$i++;
			}
			return @$data1['admun'];
	}
	
	function getAdvlist($id,$page_no=false,$showlines=false){
		if($id!=0){
		$this->db->where("position_id",$id);
		}
		$this->db->join($this->_tables['ad_position'],$this->_tables['ad_position'].".ad_position=".$this->_tables['ad_orderform'].".position_id");
		$this->db->order_by($this->_tables['ad_orderform'].".ad_sname DESC");
			if($page_no){
				$this->db->limit($showlines,$page_no);
			}
		return $this->db->get($this->_tables['ad_orderform']);
	}
	
	function addposition($data){
		if($this->db->insert($this->_tables['ad_position'], $data)){
				return true;
			}else{
				return false;
			}
	}
	
	function getone($id){
		$this->db->where("ad_position",$id);
		return $this->db->get($this->_tables['ad_position']);
	}
	
	function editposition($data){
			$id=$data['ad_position'];
			$this->db->where('ad_position',$id);
			return $this->db->update($this->_tables['ad_position'], $data);
	}
	
	function addadv($data){
		
			if($this->db->insert($this->_tables['ad_orderform'], $data)){
				return true;
			}else{
				return false;
			}
	}
	
	function advedit($data){
		$this->db->where('ad_id',$data['ad_id']);
		return $this->db->update($this->_tables['ad_orderform'],$data);
	}
	
	function mun($id){
		$data=$this->db->get_where($this->_tables['ad_orderform'],array('ad_id'=>$id))->row_array();
		return $this->db->update($this->_tables['ad_orderform'],array('ad_advmun'=>$data['ad_advmun']+1),array('ad_id'=>$data['ad_id']));
	}
	
	function getadvone($id){
		$this->db->where("ad_id",$id);
		$this->db->join($this->_tables['ad_position'],$this->_tables['ad_position'].".ad_position=".$this->_tables['ad_orderform'].".position_id");	
		$this->db->order_by($this->_tables['ad_orderform'].".ad_sname DESC");
		return $this->db->get($this->_tables['ad_orderform']);
	}
	
	function newcode(){
		$adobj=$this->db->get($this->_tables['ad_position'])->result_array();
		foreach($adobj as $adv){
			$this->db->where("position_id",$adv['ad_position']);
			$this->db->where("ad_isuse",'1');
			$this->db->join($this->_tables['ad_position'],$this->_tables['ad_position'].".ad_position=".$this->_tables['ad_orderform'].".position_id");
			$advobj=$this->db->get($this->_tables['ad_orderform'])->result_array();
				$i=0;
				@$data="
				var xmlhttp;
				var xmldom;
				function myrequest(){
					if(xmlhttp.readyState==4)
					{
						if(xmlhttp.status==200)
						{
							xmldom=xmlhttp.responseText;
				
						}
					}
				}
				function mychange(j)
				{
				
					if(window.ActiveXObject)
					{
					xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
					}else if(window.XMLHttpRequest){
					xmlhttp=new XMLHttpRequest();
					}else{
				
					}
					
					xmlhttp.open('get','".base_url.URI_MODEL."admin/ad/mun/'+j,true);
					xmlhttp.onreadystatechange=myrequest;
					xmlhttp.send(null);
				}
				var curdate 	= 	new Date();
					
					var timestamp 	= 	Math.floor(curdate.getTime()/1000);
					var Hour 		=	curdate.getYear();
					var Minutes		=	Math.floor(curdate.getMonth())+1;
					if(Minutes<=10){
						Minutes='0'+Minutes;
					}
					var Seconds		=	curdate.getDate();
					var curtimes	=	Hour+'-'+Minutes+'-'+Seconds;
					var action;
					var hh = curdate.getHours(); 
					var mm = curdate.getMinutes(); 
					var ss = curdate.getTime() % 60000; 
					ss = (ss - (ss % 1000)) / 1000; 
					var clock = hh+':';
					if (mm < 10) clock += '0'; 
					clock += mm+':'; 
					if (ss < 10) clock += '0'; 
					clock += ss; 
					clock=timestamp;\r\n";
				foreach($advobj as $advobj){
					@$data.="var todata".$i."		=	".strtotime($advobj['ad_todate']).";\r\n";
					if($advobj['ad_type']=='flash'){
						@$data.="if(clock<todata".$i."){ action=".$i."}\r\n";
						
						@$data.="function adcode".$i."(){
						document.write('<div onclick=mychange(".$advobj['ad_id'].")><object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0 width=".$adv['ad_width']." height=".$adv['ad_hight'].">	<param name=movie value=".base_url.'/'.$advobj['flashurl']." /><param name=quality value=high />	<param name=wmode value=transparent />	<embed src=".base_url.'/'.$advobj['flashurl']." width=".$adv['ad_width']." height=".$adv['ad_hight']." quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash>	</embed>	</object></div>');
						}\r\n";
					}
					
					if($advobj['ad_type']=='image'){
						if(substr($advobj['ad_imageurl'],0,6)=="http:/"){
							$imagePath	=	$advobj['ad_imageurl'];
						}
						else{
							$imagePath	=	base_url.$advobj['ad_imageurl'];	
						}
						@$data.="if(clock<todata".$i."){ action=".$i."}\r\n"; 
						@$data.='function adcode'.$i.'(){
						document.write("<a href='.$advobj['ad_linkurl'].' onclick=mychange('.$advobj['ad_id'].') target=_blank><img src='.$imagePath.' border=0 width='.$adv['ad_width'].' height='.$adv['ad_hight'].' alt='.$advobj['ad_imgtitle'].'></a>");
						}
						';
					}
					
					if($advobj['ad_type']=='text'){
						@$data.="if(clock<todata".$i."){ action=".$i."}\r\n";
						@$data.="function adcode".$i."(){
						document.write('<div onclick=mychange(".$advobj['ad_id'].")>".addslashes(preg_replace('/[\n\r\t]/','',@$advobj['ad_text']))."</div>') 
						}\r\n";
					}
					 
					if($advobj['ad_type']=='code'){
						@$data.="if(clock<todata".$i."){ action=".$i."}\r\n";
						@$data.="function adcode".$i."(){
						document.write('<div onclick=mychange(".$advobj['ad_id'].")>".addslashes(preg_replace('/[\n\r\t]/','',@$advobj['ad_code']))."</div>')
						}\r\n";
					}
					
					$i++;
				}
				@$data.="switch(action){	\r\n";
				
					for($j=0;$j<$i;$j++){
						
						@$data.="case ".$j.":adcode".$j."();\r\n break;\r\n";
						
					}
					@$data.="}\r\n";
			$path=$this->config->item('upload_path')."/js/".$adv['ad_position'].".js";
			$this->load->helper('file');
			write_file($path,@$data);
			}
	}
	
	
	function addelete($id_list){//ɾ����λ
			
				$sql	=	"
			DELETE ad_position,ad_orderform 
			FROM ad_position 
			LEFT JOIN ad_orderform ON ad_orderform.position_id=ad_position.ad_position
			WHERE ad_position.ad_position IN (".implode(",",$id_list).")
		";
		foreach($id_list as $id){
			@unlink($this->config->item('upload_path')."/js/".$id.".js");
		}
		return $this->db->query($sql);
	    }
		
	function adsdelete($id_list){//ɾ����
					$this->db->where_in("ad_id",$id_list);
			return $this->db->delete($this->_tables['ad_orderform']);
	    }
		
		function advdelete($id_list){//��չ��
					$this->db->where_in("position_id",$id_list);
			return $this->db->delete($this->_tables['ad_orderform']);
	    }
}

