<?php
class ad extends My_Controller {
	function ad(){
		parent::My_Controller();
		$this->lang->load("admin/ad",$this->config->item('language'));
		$this->load->model("admin/ad_model","ad");
		$this->load->model("admin/channel_model","channel");
		$this->load->model("public_model","public_model");
		$this->tpl_path	=	$this->config->item('tpl_admin_path')."main_frame";
		$this->tpl_dir	=	$this->config->item('tpl_admin_path');
	}
	
	function index($page_no=0){
		$this->load->library('pagination');
		$config				=	$this->config->item("pager_settings");
		$config['base_url'] = base_url.URI_MODEL."admin/ad/index";
		$config['total_rows'] = $this->ad->getAdlist()->num_rows();
		//$config['uri_segment'] = 3;
		if(isset($_POST['showlines'])){
			$config['per_page']	=	$_POST['showlines'];
		}
		$this->page['adobj']	=	$this->ad->getAdlist($page_no,$config['per_page'])->result_object();
		$i=0;
		foreach($this->page['adobj'] as $adobj){
				if($adobj->ad_locality!=0){
					$data[$i]=$this->channel->getchannel($adobj->ad_locality);
				}
				$i++;
		}
		$this->page['channel']	=	@$data;
		$this->page['advmun']	=	$this->ad->getAdmun($page_no,$config['per_page']);
		$this->pagination->initialize($config);
		$this->page['pagerbar']	=	$this->pagination->create_links();
		$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/index";
		$this->page['title']		=	$this->lang->line('new');
		$this->load->vars($this->page);
		$this->load->view($this->tpl_path);
	}
	
	function addposition(){
		if($_POST){
			$data=array(
						'ad_name'=>$this->get_post('ad_name'),
						'ad_msg'=>$this->get_post('ad_msg'),
						'ad_locality'=>$this->get_post('ad_locality'),
						'ad_price'=>$this->get_post('ad_price'),
						'ad_model'=>$this->get_post('ad_model'),
						'ad_width'=>$this->get_post('ad_width'),
						'ad_hight'=>$this->get_post('ad_hight'),
						'ad_issue'=>$this->get_post('ad_issue'),
						'ad_created'=>date('Y-m-d H:i:s'),
				  );
				 // $file=$this->upload->data();
				  if($this->ad->addposition($data)){
				  	$this->page['report_msg']	=	$this->lang->line("success");
					$this->index();
				  }
				  $this->ad->newcode(); 
		}else{
	
		$this->page['channel']=$this->channel->getchannellist()->result_array();
		$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/add";
		$this->page['title']		=	$this->lang->line('ad_add');
		$this->load->vars($this->page);
		$this->load->view($this->tpl_path);
		}
	}	
	
	function editposition($id=0){
		if($_POST){
			$data=array(
						'ad_name'=>$this->get_post('ad_name'),
						'ad_msg'=>$this->get_post('ad_msg'),
						'ad_locality'=>$this->get_post('ad_locality'),
						'ad_price'=>$this->get_post('ad_price'),
						'ad_model'=>$this->get_post('ad_model'),
						'ad_width'=>$this->get_post('ad_width'),
						'ad_hight'=>$this->get_post('ad_hight'),
						'ad_issue'=>$this->get_post('ad_issue'),
						'ad_position'=>$this->get_post('ad_position'),
						'ad_updated'=>date('Y-m-d H:i:s'),
				  );
				  
			
			// $file=$this->upload->data();
			
			if($this->ad->editposition($data)){
				$this->page['report_msg']	=	$this->lang->line("success");
			} 
			$this->ad->newcode(); 
			$id=$this->get_post('ad_position');
			$this->index();
		}else{
		$this->page['channel']=$this->channel->getchannellist()->result_array();
		$this->page['position']= $this->ad->getone($id)->row_array();
		$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/edit";
		$this->page['title']		=	$this->lang->line('new');
		$this->load->vars($this->page);
		$this->load->view($this->tpl_path);
		}
	}
	
	function addadv($id=0){
		if($_POST){
			$data=array(
					'position_id'=>$this->get_post('position_id'),
					'ad_sname'=>$this->get_post('ad_sname'),
					'ad_advmsg'=>$this->get_post('ad_advmsg'),
					'ad_type'=>$this->get_post('ad_type'),
					'ad_imageurl'=>$this->get_post('ad_imageurl'),
					'ad_imgtitle'=>$this->get_post('ad_imgtitle'),
					'ad_linkurl'=>$this->get_post('ad_linkurl'),
					'flashurl'=>$this->get_post('flashurl'),
					'ad_wmode'=>$this->get_post('ad_wmode'),
					'ad_username'=>$this->get_post('ad_username'),
					'ad_fromdate'=>$this->get_post('ad_fromdate'),
					'ad_todate'=>$this->get_post('ad_todate'),
					'ad_isuse'=>$this->get_post('ad_isuse'),
					'ad_text'=>$this->get_post('ad_text'),
					'ad_code'=>$this->get_post('ad_code'),
				);
			  $config['upload_path'] = $this->config->item('upload_path');
			  $config['allowed_types'] = 'gif|jpg|png';
			  $config['max_size'] = '10000';
			  $config['max_width']  = '1024';
			  $config['max_height']  = '768';
			  $config['encrypt_name'] = TRUE;
              $config['remove_spaces'] = TRUE;
			  $this->load->library('upload', $config);
			  $this->upload->initialize($config);
			  if($_POST['ad_type']=="image"){
			 	  $field_name="ad_imageurl";
			  }else{
			  	  $field_name="flashurl";
			  }
			  
			  if($this->upload->do_upload($field_name)){
					 $file=$this->upload->data();
					 if($_POST['ad_type']=="image"){
						  $data["ad_imageurl"]=$config['upload_path']."/".$file['file_name'];
					  }else{
						  $data["flashurl"]=$config['upload_path']."/".$file['file_name'];
					  }
					//  $this->ad->code($config['upload_path']."/js/".$data['position_id'].".js",$content);
					  if($this->ad->addadv($data)){
					  	$this->page['report_msg']	=	$this->lang->line("success");
						$this->advlist($id,$page_no=0);
					  }
					 $this->ad->newcode(); 
				}else{
				  if($_FILES[$field_name]['name']){
					  $this->page['report_msg']	=$this->upload->display_errors();
					  $this->page['msg_type']	=	"error";
					  $this->page['id']=$id;
						$this->page['ad']=$this->ad->getAdlist()->result_array();
						$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/addadv";
						$this->page['title']		=	$this->lang->line('new');
						$this->load->vars($this->page);
						$this->load->view($this->tpl_path);
				  }else{
					 if($this->ad->addadv($data)){
							$this->page['report_msg']	=	$this->lang->line("success");
							$this->ad->newcode(); 
							$this->advlist($id,$page_no=0);
					 }else{
							$this->page['report_msg']	=	$this->lang->line("add_flink");
							$this->page['msg_type']	=	"error";
					 }
				  }
				}
		
			  //$this->ad->addadv($data);	
			
		}else{
			$this->page['id']=$id;
			$this->page['ad']=$this->ad->getAdlist()->result_array();
			$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/addadv";
			$this->page['title']		=	$this->lang->line('new');
			$this->load->vars($this->page);
			$this->load->view($this->tpl_path);
		}
	}
	
	function advlist($id,$page_no=0){
		$this->load->library('pagination');
		$config				=	$this->config->item("pager_settings");
		$config['base_url'] = base_url.URI_MODEL."admin/ad/advlist";
		$config['total_rows'] = $this->ad->getAdvlist($id)->num_rows();
		//$config['uri_segment'] = 3;
		if(isset($_POST['showlines'])){
			$config['per_page']	=	$_POST['showlines'];
		}
		$this->page['adobj']	=	$this->ad->getAdvlist($id,$page_no,$config['per_page'])->result_object();
		$this->pagination->initialize($config);
		$this->page['adid']=$id;
		$this->page['pagerbar']	=	$this->pagination->create_links();
		$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/advlist";
		$this->page['title']		=	$this->lang->line('new');
		$this->load->vars($this->page);
		$this->load->view($this->tpl_path);
	}
	
	function advedit($id=0){
	if($_POST){
			$data=array(
					'ad_id'=>$_POST['ad_id'],
					'position_id'=>$_POST['position_id'],
					'ad_sname'=>$_POST['ad_sname'],
					'ad_advmsg'=>$_POST['ad_advmsg'],
					'ad_type'=>$_POST['ad_type'],
					'ad_imageurl'=>$_POST['ad_imageurl'],
					'ad_imgtitle'=>$_POST['ad_imgtitle'],
					'ad_linkurl'=>$_POST['ad_linkurl'],
					'flashurl'=>$_POST['flashurl'],
					'ad_wmode'=>$_POST['ad_wmode'],
					'ad_username'=>$_POST['ad_username'],
					'ad_fromdate'=>$_POST['ad_fromdate'],
					'ad_todate'=>$_POST['ad_todate'],
					'ad_isuse'=>$_POST['ad_isuse'],
					'ad_text'=>$_POST['ad_text'],
					'ad_code'=>$_POST['ad_code'],
				);
			  $config['upload_path'] = $this->config->item('upload_path');
			  $config['allowed_types'] = 'gif|jpg|png';
			  $config['max_size'] = '10000';
			  $config['max_width']  = '1024';
			  $config['max_height']  = '768';
			  $config['encrypt_name'] = TRUE;
              $config['remove_spaces'] = TRUE;
			  $this->load->library('upload', $config);
			  $this->upload->initialize($config);
			  if($_POST['ad_type']=="image"){
			 	  $field_name="ad_imageurl";
			  }else{
			  	  $field_name="flashurl";
			  }
			  
			  if($this->upload->do_upload($field_name)){
					$file=$this->upload->data();
					 if($_POST['ad_type']=="image"){
						  $data["ad_imageurl"]=$config['upload_path']."/".$file['file_name'];
					  }else{
						  $data["flashurl"]=$config['upload_path']."/".$file['file_name'];
					  }
					 
					  if($this->ad->advedit($data)){
					 	 $this->page['report_msg']	=	$this->lang->line("success");
					  }
					   $this->ad->newcode(); 
				}else{
					if($_FILES[$field_name]['name']){
					  $this->page['report_msg']	=$this->upload->display_errors();
					  $this->page['msg_type']	=	"error";
				  }else{
					 if($this->ad->advedit($data)){
							$this->page['report_msg']	=	$this->lang->line("success");
							$this->ad->newcode(); 
					 }else{
							$this->page['report_msg']	=	$this->lang->line("add_flink");
							$this->page['msg_type']	=	"error";
					 }
				  }
					
				}
		
			  $id=$_POST['ad_id'];	
			
		}
		$this->page['ad']=$this->ad->getAdlist()->result_array();
		$this->page['position']= $this->ad->getadvone($id)->row_array();
		$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/advedit";
		$this->page['title']		=	$this->lang->line('new');
		$this->load->vars($this->page);
		$this->load->view($this->tpl_path);
	}
	
	function publishjs(){
		$this->ad->newcode();
		$this->page['report_msg']	=	$this->lang->line("js_publish_success");
		$this->page['msg_type']		=	"message";
		$this->index();
	}
	
	function code($id=0){
		$this->page['position']= $id;
		$this->page['tpl_path']	=	$this->tpl_dir."admin/ad/code";
		$this->page['title']		=	$this->lang->line('new');
		$this->load->vars($this->page);
		$this->load->view($this->tpl_path);
	}
	
	function addelete($id="",$adv=false){
			if(@$_POST){
			$brand_id	=	$_POST['select_id'];
		}
		else{
			$brand_id[]	=	$id;
		}
		if(!$adv){
				if($this->ad->addelete($brand_id)){
						$this->page['report_msg']	=	$this->lang->line("delete");
						$this->page['msg_type']		=	"message";
				}
			}else{
				if($this->ad->advdelete($brand_id)){
					$this->page['report_msg']	=	$this->lang->line("delete");
					$this->page['msg_type']		=	"message";
				}
			}
		$this->index();
	}
	
	function adsdelete($id=""){
		if(@$_POST){
			$brand_id	=	$_POST['select_id'];
		}
		else{
			$brand_id[]	=	$id;
		}
		if($this->ad->adsdelete($brand_id)){
				$this->page['report_msg']	=	$this->lang->line("delete");
					$this->page['msg_type']		=	"message";
		}

		$this->advlist($this->get_post("id"));
	}
	
	
	
	function showadv($id){
	$position= $this->ad->getadvone($id)->row_array();
		
		echo '<script language="javascript" src="'.base_url.'public/upload/js/'.$id.'.js"></script>';
		
	}
	function mun($id){
		$this->ad->mun($id);
	} 
}