<?
class channel extends action
{
	function dlist()
	{
		$this->init_tpl();
		$channel_arr								=	$this->db->getRecords(" SELECT * FROM {$this->db->db_pre}channel ");
		foreach($channel_arr as $key=>$val){
			if(!$val['go_to_link']){
				$channel_arr[$key]['go_to_link']	=	'/channel.php?channel_id='.$val['channel_id'];	
			}
		}
		$this->tpl->out['channel_arr']		=	$channel_arr;
		$this->tpl->display("channel.dlist.html");
	}

	function addchannel()
	{
		$this->init_tpl();
		create_html_editor('content',$this->tpl->out['channel_res']['content'],$this->tpl->tpl);
		$this->tpl->display("channel.addchannel.html");
	}

	function save_addchannel()
	{
		global $_ADMIN_LANG;
		if(!($insert_arr	=	$this->check_submit_channel())){
			$this->system_message($_ADMIN_LANG['adding_category_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;
		}
		$insert_arr['type']	=	'custom';
		$result		=		$this->db->addRecord("{$this->db->db_pre}channel",$insert_arr);	
		if($result){
			$this->system_message($_ADMIN_LANG['adding_channel_success'],'success',array($_ADMIN_LANG['back_to_channel_page']=>'index.php?act=channel.dlist'));
			exit;
		}else{
			$this->system_message($_ADMIN_LANG['adding_channel_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;
		}		

				
	}


	function updatechannel()
	{
		$this->init_tpl();
		$channel_id	=	(int)$_GET['channel_id'];	
		$this->tpl->out['channel_res']					=	$this->db->getRecord("SELECT * FROM {$this->db->db_pre}channel WHERE channel_id={$channel_id}");	
		
		create_html_editor('content',$this->tpl->out['channel_res']['content'],$this->tpl->tpl);
		
		$this->tpl->out['channel_res']['menu_arr']	=	$this->split_menu($this->tpl->out['channel_res']['menu']);
		$this->tpl->display("channel.updatechannel.html");
	}

	function save_updatechannel()
	{
		global $_ADMIN_LANG;
		$channel_id	=	(int)$_GET['channel_id'];		
		if(!($update_arr	=	$this->check_submit_channel())){
			$this->system_message($_ADMIN_LANG['channel_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;
		}	
		$result		=		$this->db->updateRecord("{$this->db->db_pre}channel"," channel_id=$channel_id",$update_arr);	
		if($result){
			$this->system_message($_ADMIN_LANG['channel_modification_success'],'success',array($_ADMIN_LANG['back_to_channel_page']=>'index.php?act=channel.dlist'));
			exit;
		}else{
			$this->system_message($_ADMIN_LANG['channel_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;
		}

	}

	function check_submit_channel()
	{
		$channel_name	=	$_POST['channel_name'];				
		if(!$channel_name){
			return false;
		}
		
		$menu_name		=	$_POST['menu_name'];
		$menu_url		=	$_POST['menu_url'];
		$menu				=	'';
		foreach($menu_name as $key=>$val){
			if((!$val)||$menu_url[$key]=='http://'||(!$menu_url[$key])){
				continue;
			}
			$menu[]		=	array(
						'name'	=>	trim($val),
						'url'		=>	trim($menu_url[$key]),			
					);
		}
		$menu	=	serialize($menu);
		$sql_data_arr	=	array(
			'channel_name'	=>	$channel_name,		
			'sort_order'	=>	$_POST['sort_order'],
			'menu'			=>	$menu,	
			'is_open'		=>	(int)$_POST['is_open'],
			'content'		=>	$_POST['content'],
			'seo_desc'		=>	$_POST['seo_desc'],
			'seo_title'		=>	$_POST['seo_title'],
			'seo_keywords'	=>	$_POST['seo_keywords'],
			'go_to_link'	=>	$_POST['go_to_link'],
		);	
		return $sql_data_arr;	
	}

	function split_menu($menu_str)
	{
			$menu_str	=	unserialize($menu_str);
			return $menu_str;
	}


	function handle_is_open()
	{
		$channel_id	=	(int)$_GET['channel_id'];
		$is_open		=	(int)$_GET['is_open'];
		$result		=	$this->db->updateRecord("{$this->db->db_pre}channel"," channel_id=$channel_id", array('is_open'=>$is_open));
		if($result){
			echo json_return('ok');
		}	
	}

	function del_channel_ajax()
	{
		$channel_id	=	(int)$_GET['channel_id'];
		$result		=	$this->db->deleteRecord("{$this->db->db_pre}channel"," channel_id=$channel_id AND type='custom'");
		if($result){
			echo json_return('ok');
		}
	}

}
