<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Ad extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	var $master_act = 'ad';
	var $info_prefix = 'ad_';
	function __construct()
	{
		parent::__construct();	
		$this -> load_lang('adminsys/'.$this -> master_act);
		$this -> view -> assign('master_act', $this -> master_act);
		$this -> view -> assign('info_prefix',	$this -> info_prefix);
		$this -> load('admin');
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper');     //加载辅助类
		$this -> load_model('sort'); //加载分类模型
	}


	function info_list(  )
	{
		$keyword 	= $this -> input -> req["keyword"];
		$list_val   = $this -> input -> req["list_val"];
		$list_type  = $this -> input -> req["list_type"];

		$table_arr['where_add'] = '';

		if($keyword!='')
		{ 	
			$table_arr['where_add'] .= " AND ( ad_name LIKE  '%".$keyword."%' )";  
		}
		

		$table_arr['order_by']  = 'ad_id DESC';
		if($list_val!='' and $list_type =='DESC' )
		{
			$table_arr['order_by'] = $list_val ."  DESC ";
		}elseif($list_val!='' and $list_type =='ASC')
		{
			$table_arr['order_by'] = $list_val." ASC";
		}else{
			$table_arr['order_by'] =  'ad_id DESC';
		}

		$this -> load('page',$this->lang['page']);

		//列表字段显示
		$show_arr = array('ad_id' => array('th'=>1,'td'=>'id'),'ad_name'=> array('th'=>0,'td'=>''),'ad_media_type'=> array('th'=>1,'td'=>'arr'),'ad_url'=> array('th'=>1,'td'=>''),'ad_start_time'=>array('th'=>1,'td'=>'time'),'ad_end_time'=>array('th'=>1,'td'=>'time'),'ad_enabled'=>array('th'=>1,'td'=>'arr') );
		
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type ,  $this->lang['field_list'] );

		$table_arr['table']		= $this -> master_act;
 	
		$table_arr['prefix'] 	= $this -> db -> prefix;
		$table_arr['field_arr'] = array_keys($show_arr);
		$table_arr['def_where'] = ' 1 ';
		$table_arr['group_by']  = '';
		$table_arr['limit_val'] = $this -> page ->get_limit_val();
		
		$re_sql   = $this -> helper -> get_list_sql( $table_arr );
		//var_dump($re_sql);
		$info 	  = $this -> db -> select_info( $re_sql['sql_list'] );
		
 
		$key_arr['ad_media_type'] 	= $this->lang['ad_media_type_arr'];
		$key_arr['ad_enabled'] 		= $this->lang['enabled_arr'];
		
		$opt = array( 'edit'=> $this->lang['edit'], 'delete'=> $this->lang['delete']);
		
		$td_rows  = $this -> helper -> get_td_list( $info , $show_arr ,$key_arr ,$this -> master_act, $opt );
		
		$count 	  = $this -> db -> select_one( $re_sql['sql_count'] );

		$page_str = $this -> page ->get_admin_page( $count["CNUM"] );
		
		$this -> view -> assign('keyword',		$keyword);

		$this -> view -> assign('list_val',		$list_val);
		$this -> view -> assign('list_type',	$list_type);
		
		$this -> view -> assign('colspan_val',	count($show_arr)+1);
		$this -> view -> assign('field_show',	$field_show);
		$this -> view -> assign('page_str',		$page_str);
		$this -> view -> assign('td_rows',		$td_rows);


		$this -> view -> display('item_list.tpl');
		
	}




	function delete( )
	{
		$info_id   = $this -> input ->  get["id"];
		$insert_ip = " DELETE FROM `".$this -> db -> prefix.$this -> master_act."` WHERE `ad_id` in ( $info_id ) ";

		$this -> db -> query_sql($insert_ip);
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function show( )
	{
		$info_id = $this -> input ->  get["id"];
		$upshow = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET ad_school = 1  WHERE `ad_id` in ( $info_id ) ";

		$this -> db -> query_sql($upshow);
		$this -> view ->  assign('info_title',$_LANG['bd_showsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function hide( )
	{
		//$indel = implode(',', $delArray);
		$info_id = $this -> input ->  get["id"];
		
		$up_hide = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET ad_school = 0  WHERE  `ad_id` in ( $info_id ) ";
		$this -> db -> query_sql( $up_hide );
		
		$this -> view ->  assign('info_title',$_LANG['bd_hidesusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	private function get_ad_position()
	{
		$sql_query = " SELECT adp_id,adp_name  FROM  ".$this -> db -> prefix."ad_position  WHERE 1 ORDER BY `adp_id` DESC ";
		return $this -> db -> select_info($sql_query);
	}


	
	function info()
	{
		$act_type = $this -> input -> get["act_type"];
		
		$ap_info  = $this -> get_ad_position();
		$this -> view -> assign('ap_info',$ap_info);
		
		if($act_type=='edit'){
			
			$id_val = $this -> input ->  get["id"];

			$sql_query = " SELECT *  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE ad_id = '".$id_val."' ORDER BY `ad_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			$resume_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'ad_resume' ,$info['ad_resume'],'Basic');//加载文本编辑器
			$this -> view -> assign( 'resume_ckeditorLoad', $resume_ckeditorLoad);
			
			$this -> view -> assign('txt_title',  $this->lang['edit_title']);
			$this -> view -> assign('edit_submit', $this->lang['edit']);
			$this -> view -> assign('info',$info);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('sort_select_str',$sort_select_str);
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=edit");
			
			
		}else{

			$resume_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'ad_resume' ,'','Basic');//加载文本编辑器
			$this -> view -> assign( 'resume_ckeditorLoad', $resume_ckeditorLoad);

			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=add");
			
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('txt_title',$this->lang['add_title']);
			$this -> view -> assign('edit_submit', $this-> lang['add']);
			
		}

		$this -> view ->  display('ad_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$ad_id 			= $this -> input ->  funpost("ad_id");
		$ad_name 		= $this -> input ->  funpost("ad_name");
		$ad_media_type 	= $this -> input ->  funpost("ad_media_type");
		$adp_id 		= $this -> input ->  funpost("adp_id");
		$ad_start_time 	= strtotime($this -> input ->  funpost("ad_start_time"));
		$ad_end_time 	= strtotime($this -> input ->  funpost("ad_end_time"));
		$ad_url 		= $this -> input ->  funpost("ad_url");
		
		$ad_desc 		= $this -> input ->  funpost("ad_desc");

		$ad_enabled 	= $this -> input ->  funpost("ad_enabled");
 
		$act_type 		= $this -> input ->  req["act_type"];

		if($act_type=='edit')
		{

		
			if($ad_name !='' and  $ad_media_type !='' and $ad_id !='')
			{
	
				 $update = "UPDATE `".$this -> db -> prefix.$this -> master_act."` SET  ad_name = '".$ad_name."' , ad_media_type = '".$ad_media_type."' , adp_id = '".$adp_id."', ad_url = '".$ad_url."', ad_start_time = '".$ad_start_time."', ad_end_time = '".$ad_end_time."', ad_desc = '".$ad_desc."' , ad_enabled = '".$ad_enabled."' WHERE  ad_id = '".$ad_id."' ";
				
				$this -> db -> query_sql( $update );
				
				$this -> view ->  assign('info_title',$this->lang['edit_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
				$this -> view ->  display('suggestion.tpl');
	
		
			}else{
			
				$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info&id=".$ad_id);
				$this -> view ->  display('suggestion.tpl');	
			}
		

		}else{
			
				if($ad_name !='' and  $ad_media_type !='')
				{
					$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE ad_name = '".$ad_name."' ORDER BY `ad_id` ";
					$info = $this -> db -> select_one($sql_query);


					if( $info["LNUM"] < 1 )
					{
						
						$insert_sql =" INSERT INTO `".$this -> db -> prefix.$this -> master_act."` (
						`ad_name` ,`ad_media_type` , `adp_id`  , `ad_url`, `ad_start_time` , `ad_end_time`, `ad_desc`,`ad_enabled` ) 
						VALUES ( '$ad_name','$ad_media_type','$adp_id','$ad_url','$ad_start_time','$ad_end_time','$ad_desc','$ad_enabled')";
						
						$this -> db -> query_sql($insert_sql);
						$this -> view ->  assign('info_title',$this->lang['add_success']);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
						$this -> view ->  display('suggestion.tpl');	
					}else{
						
						$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info&act_type=add");
						$this -> view ->  display('suggestion.tpl');	
					}
					
				}else{
					
					$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
					$this -> view ->  assign('URL_TIME',URL_TIME);
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info&act_type=add");
					$this -> view ->  display('suggestion.tpl');
				}
		}
		
	}

/*	

	function flash_list()
	{
		$read_arr = $this -> read_flash_xml();
		//$this -> read_flash_xml();

		$this -> view ->  assign('level_count',count($read_arr['level']));
		$this -> view ->  assign('level',$read_arr['level']);
		$this -> view ->  assign('play_time',$read_arr['playtime']);
		
		$this -> view ->  display('flash_list.tpl');
		
	}
	
	function add_flash()
	{
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=ad&act=add_infoflash");
		$this -> view -> display('flashadd.tpl');
	
	}
	
	function delete_flash( )
	{
		//var_dump( $delArray );
		$itemval = $this -> input ->  get["item"];
		$linkval = $this -> input ->  get["link"];
		
		$item_urlval = base64_decode($itemval);
		$link_urlval = base64_decode($linkval);
		
		$read_arr = $this->read_flash_xml(); //$read_arr['level']
		//$this -> read_flash_xml();
		
		$addstr = '';
		foreach($read_arr['level'] as $k=>$v)
		{
			if($v["item_url"] == $item_urlval and  $v["link_url"]==$link_urlval)
			{
			
			}else{
				$addstr = $addstr."<item item_url=\"".$v["item_url"]."\" link=\"".$v["link_url"]."\" />\n";
			}
		}
		
		$arrval["itemstr"] = $addstr ;
		
		$this -> helper -> build_xml( $arrval , $contenthtml ='<?xml version="1.0" encoding="utf-8"?>
				 <bcaster autoPlayTime="10">
				 {itemstr}				
				</bcaster>', $file_name = ROOT_PATH.'/xml/bcastr.xml');
				
		$this -> view ->  assign('info_title',$_LANG['flash_delsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=ad&act=flash_list");
		$this -> view ->  display('suggestion.tpl');	
		
	}
	
	function info_flash()
	{
		$itemval = $this -> input ->  get["item"];
		$linkval = $this -> input ->  get["link"];
		
		$item_urlval = base64_decode($itemval);
		$link_urlval = base64_decode($linkval);
		
		$this -> view -> assign('item_url',$item_urlval);
		$this -> view -> assign('link_url',$link_urlval);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=ad&act=editflash");
		$this -> view -> display('flashedit.tpl');

	}

	function edit_flash()
	{
		$item_urlval	= $this -> input ->  funpost("item_url");
		$link_urlval 	= $this -> input ->  funpost("link_url");
		$olditem_urlval	= $this -> input ->  funpost("olditem_url");
		$oldlink_urlval = $this -> input ->  funpost("oldlink_url");
		
		$addstr = "";
		
		$read_arr = $this->read_flash_xml(); //$read_arr['level']
		//$this -> read_flash_xml();
		
		foreach($read_arr['level'] as $k=>$v)
		{
			if($v["item_url"] == $olditem_urlval and  $v["link_url"]==$oldlink_urlval)
			{
				if(BASE_WEBURL <> '')
				{
					if(strstr($item_urlval,BASE_WEBURL))
					{
						$addstr = $addstr."<item item_url=\"".$item_urlval."\" link=\"".$link_urlval."\" />\n";
					}else{
						$addstr = $addstr."<item item_url=\"".BASE_WEBURL.$item_urlval."\" link=\"".$link_urlval."\" />\n";
					}
				}else{
				
					$addstr = $addstr."<item item_url=\"".BASE_WEBURL.$item_urlval."\" link=\"".$link_urlval."\" />\n";
				}
				
				
			}else{
				$addstr = $addstr."<item item_url=\"".$v["item_url"]."\" link=\"".$v["link_url"]."\" />\n";
			}
			
			
		}
		//echo $addstr;
		$arrval["itemstr"] = $addstr ;
		
		$this -> helper -> build_xml( $arrval , $contenthtml ='<?xml version="1.0" encoding="utf-8"?>
				 <bcaster autoPlayTime="10">
				 {itemstr}				
				</bcaster>', $file_name = ROOT_PATH.'/xml/bcastr.xml');
				
		$this -> view ->  assign('info_title',$_LANG['flash_editsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=ad&act=flash_list");
		$this -> view ->  display('suggestion.tpl');	
	
	}
	
	function add_info_flash()
	{
		$item_urlval	= $this -> input ->  funpost("item_url");
		$link_urlval 	= $this -> input ->  funpost("link_url");
		
		$addstr = "<item item_url=\"".$item_urlval."\" link=\"".$link_urlval."\" />\n";
		
		$read_arr = $this->read_flash_xml();
		
		///var_dump($this -> level);
		
		//$itemstr = '';  //["item_url"]=> string(13) "img/01.jpg" ["link_url"]
		
		foreach($read_arr['level'] as $k=>$v)
		{
			$addstr = $addstr."<item item_url=\"".$v["item_url"]."\" link=\"".$v["link_url"]."\" />\n";
		}
		//echo $addstr;
		$arrval["itemstr"] =$addstr ;
		
		$this -> helper -> build_xml( $arrval , $contenthtml ='<?xml version="1.0" encoding="utf-8"?>
				 <bcaster autoPlayTime="10">
				 {itemstr}				
				</bcaster>', $file_name = ROOT_PATH.'/xml/bcastr.xml');
				
		$this -> view ->  assign('info_title',$_LANG['flash_addsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=ad&act=flash_list");
		$this -> view ->  display('suggestion.tpl');	
		
	}
	
	function flash_xml_str()
	{
	 	 $read_arr = $this->read_flash_xml();
		 
		 return $str = '<?xml version="1.0" encoding="utf-8"?>
				 <bcaster autoPlayTime="'.$read_arr['playtime']["autoplaytime"].'">
				 {itemstr}				
				</bcaster>';
	}
	
	function read_flash_xml()
	{
		$re_arr = array();
		$file = ROOT_PATH.'/xml/bcastr.xml';
		
		$xml_parser = xml_parser_create();
		
		if (!($fp = fopen($file, "r")))
		{
			die("could not open XML input");
		}
		
		$data = fread($fp, filesize($file));
		fclose($fp);
		xml_parse_into_struct($xml_parser, $data, $vals, $index);
		xml_parser_free($xml_parser);
		
		$key = 0 ;
		foreach ($vals as  $xml_elem)
		{
			 if($xml_elem['type'] == 'complete')
			 {
				//var_dump($xml_elem['attributes']);
				if(array_key_exists('attributes',$xml_elem))
				{
				   	$re_arr['level'][$key]["item_url"] = $xml_elem["attributes"]["ITEM_URL"];
					$re_arr['level'][$key]["item_urlcode"] = base64_encode($xml_elem["attributes"]["ITEM_URL"]);
				 	$re_arr['level'][$key]["link_url"] = $xml_elem["attributes"]["LINK"];
					$re_arr['level'][$key]["link_urlcode"] = base64_encode($xml_elem["attributes"]["LINK"]);
				  	$key++;
				 } 
			  }
			  
			 if ($xml_elem['type'] == 'open')
			 {
				//var_dump($xml_elem['attributes']);
				if (array_key_exists('attributes',$xml_elem))
				{
				   	$re_arr['autoplaytime'] = $xml_elem["attributes"]["AUTOPLAYTIME"];
					//$this -> playtime["autoplaytime"] = $xml_elem["attributes"]["AUTOPLAYTIME"];
				 } 
			  }

		}
		
		return $re_arr;
		
	}
	
	function show_flash()
	{
		//预查看首页广告
		$this -> view ->  display('show_flash.tpl');
	}
		
	*/

		
}