<?php 
class Web_config_mod  extends HC_Controller
{
	private $web_temp;
	public function __construct()
	{
		parent::__construct();
		global $config;
		if(isset($config['sys_css'])){
			$this->web_temp['sys_code'] = $config['sys_css'];
			$this->web_temp['sys_css'] = $config['sys_css'];
		}
		if(empty($this -> db)){
			$this -> load_db();
		}
	}

	public function get_sys_mail()
	{
		$sql_sys = " SELECT sys_code ,sys_value FROM ".$this -> db -> prefix."sys_config WHERE sys_type  = 'mail'  ORDER BY sys_id  ";
		$arr = $this -> db -> select_info( $sql_sys );
		
		$re_arr = $this -> mail_key_val( $arr );
		return $re_arr;
	}
	
	private function mail_key_val( $arr = array() )
	{
		foreach((array)$arr as $akey => $aval)
		{
			$re_arr[$aval['sys_code']] = trim($aval['sys_value']);
		}
		return $re_arr;
	}

	public function get_sys_config()
	{
		$sql_sys = "SELECT sys_code ,sys_value FROM ".$this -> db -> prefix."sys_config WHERE sys_type  = 'web'  ORDER BY sys_id";
		$arr = $this -> db -> select_Info( $sql_sys );
		//
		//var_dump($arr);
		
		return $this -> assemble_sys_config( $arr );
	}
	
	public function assemble_sys_config( $arr = array() )
	{
		$re_arr = array();
		
		//$re_arr[$arr['sys_code']] = json_decode($arr['sys_value']);
		//$re_arr[$arr['sys_code']] = $arr['sys_value'];
		
		//var_dump($this->web_temp);
		//echo "fdafds";
		
		foreach( $arr as $key => $val)
		{
			if($val['sys_code']=='temp'){
				//$temp_val = json_decode($info_temp["sys_value"]); 
				$re_arr[$val['sys_code']] = json_decode($val['sys_value']);
			}else{
				$re_arr[$val['sys_code']] = $val['sys_value'];
			}
			
			
		}
		if(isset($this->web_temp['sys_code'])){
			//echo $this->web_temp['sys_code'];
			$re_arr["temp"]->dir =	$this->web_temp['sys_code'];
			$re_arr["temp"]->style	=	$this->web_temp['sys_css'];
			//$re_arr["temp"] = $val['sys_value'];
		}
		//var_dump($re_arr);
		
		if($re_arr['web_close']==1){
			exit('<center>'.$re_arr['web_close_val'].'</center>');
		}
		
		
		return $re_arr; 
	}
	
	public function get_web_menu($type='web')
	{
		$sql = " SELECT web_id, web_name, web_url, web_place, web_targetval FROM ".$this -> db -> prefix."web_menu WHERE web_enabled = 1 GROUP BY web_id ORDER BY web_list LIMIT 60 ";
		
		$info = $this -> db -> select_info($sql);
		if($type=='m'){
			if (isset($info[0])) {
				$webmenuRe = array();
				foreach ($info as $row) {
					if (strstr($row["web_url"], 'http://')) {
						$webmenuRe[$row["web_place"]][]['relurl'] = '<a href="' . $row["web_url"] . '" target="' . $row["web_targetval"] . '" >' . $row["web_name"] . '</a>';
					} else {
						$webmenuRe[$row["web_place"]][]['relurl'] = '<a href="' . BASE_WEBURL .'/m'. $row["web_url"] . '" target="' . $row["web_targetval"] . '" >' . $row["web_name"] . '</a>';
					}
				}
			}
		}else {
			if (isset($info[0])) {
				$webmenuRe = array();
				foreach ($info as $row) {
					if (strstr($row["web_url"], 'http://')) {
						$webmenuRe[$row["web_place"]][]['relurl'] = '<a href="' . $row["web_url"] . '" target="' . $row["web_targetval"] . '" >' . $row["web_name"] . '</a>';
					} else {
						$webmenuRe[$row["web_place"]][]['relurl'] = '<a href="' . BASE_WEBURL . $row["web_url"] . '" target="' . $row["web_targetval"] . '" >' . $row["web_name"] . '</a>';
					}
				}
			}
		}
 
		return $webmenuRe;
	}
	
	public function get_user_menu()
	{
		$sql_query = " SELECT web_id, web_name, web_url, web_place,web_list,web_parent_id FROM  ".$this -> db -> prefix."web_menu  WHERE  web_place = 'user' and  web_enabled = 1 GROUP BY web_id ORDER BY `web_id` LIMIT  0 , 100";

		$info_sm = $this -> db -> select_info($sql_query);
		
		$web_menu = array();
		  foreach ($info_sm as $rowkey => $rowval)
		  {
		   		if($rowval["web_parent_id"] == 0)
				{
					$web_menu[$rowval["web_id"]]['web_id'] 		 	= $rowval["web_id"] ;
					$web_menu[$rowval["web_id"]]['web_name'] 	 	= $rowval["web_name"] ;
					$web_menu[$rowval["web_id"]]['web_parent_id']  	= $rowval["web_parent_id"] ;
					$web_menu[$rowval["web_id"]]['web_place']     	= $rowval["web_place"] ;
					$web_menu[$rowval["web_id"]]['web_url'] 	    = $rowval["web_url"] ;
					$web_menu[$rowval["web_id"]]['web_list'] 	 	= $rowval["web_list"] ;
					$web_menu[$rowval["web_id"]]['web_list'] 	 	= $rowval["web_list"] ;
				}else{
					$web_menu[$rowval["web_parent_id"]]['array'][] = $rowval;	
				}
		 }
		return  $web_menu;
		
	}
	function get_link()
	{

		$sql = " SELECT lin_id, lin_name, lin_url FROM ".$this -> db -> prefix."link WHERE lin_enabled = 1 GROUP BY lin_id ORDER BY lin_list LIMIT 10 ";
		$info = $this -> db -> select_info($sql);
		$linksRe = array();	
		if ($info[0])
		{ 
		   foreach ($info as $row)
		   {
				$linksRe[$row["lin_id"]]["relurl"] = '<a href="'.$row["lin_url"].'" target="_blank">'.$row["lin_name"].'</a>' ; 
		   }
		}
		return $linksRe;
	}


	public function get_sort( $limit = 2000 )
	{
		//var_dump( $this->db );
		$sql_query = " SELECT prs_id, prs_name,prs_parent_id  FROM  ".$this -> db -> prefix."pro_sort  WHERE 1 and prs_enabled = 1 GROUP BY prs_id ORDER BY `prs_list` limit $limit ";
		return $this -> db -> select_info($sql_query);

		/*
		$psInfo = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["prs_parent_id"] == 0)
				{
					$psInfo[$rowval["prs_id"]]['prs_id'] = $rowval["prs_id"] ;
					$psInfo[$rowval["prs_id"]]['prs_name'] = $rowval["prs_name"] ;
					$psInfo[$rowval["prs_id"]]['prs_parent_id'] = $rowval["prs_parent_id"] ;
				}else{
					$psInfo[$rowval["prs_parent_id"]]['array'][] = $rowval;	
				}
				$psInfoval[$rowval["prs_id"]] = $rowval["prs_name"] ;
		}
		
		//$this -> view -> assign('psInfoval',$psInfoval);		//列排表显示使用
		
		//$this -> view -> assign('psInfo',$psInfo);		
		//return $psInfo;
		*/
	}
	

	//无数据查询及处理
	public function select_son_prs_id( $parent_id = 0 )
	{
		$sql_query = " SELECT prs_id, prs_parent_id  FROM  ".$this -> db -> prefix."pro_sort  WHERE 1 and prs_enabled = 1 GROUP BY prs_id ORDER BY prs_parent_id ASC , prs_id ASC , `prs_list` limit 1000 ";

		$sort_info = $this -> db -> select_info($sql_query);
		return $this -> build_son_id_str( $sort_info , $parent_id );
	}
	
	public function build_son_id_str( $sort_array = array(), $check_parent_id = 0 )
	{
		foreach((array)$sort_array as $key => $val)
		{
			$all_array[$val['prs_id']]['prs_id'] 		= $val['prs_id'];
			$all_array[$val['prs_id']]['prs_parent_id'] = $val['prs_parent_id'];
		}
		if($check_parent_id!=0){
			$re_arr[0] = $check_parent_id ;	
		}else{
			$re_arr[0] = 0 ;	
		}
		return $this -> get_son_id( $all_array  , $re_arr ,  $grade_val = 6   );
			
	}
	
	public function get_prs_son_id( $parent_id , $prs_key_val )
	{
		if($parent_id!=0){
			$re_arr[0] = $parent_id ;	
		}else{
			$re_arr[0] = 0 ;	
		}
		
		return $this -> get_son_id( $prs_key_val  , $re_arr ,  $grade_val = 6   );
	}
	
	
	public function get_son_id( $big_arr = array() , $re_arr = array(),  $grade_val = 6  )
	{
		
		if( $grade_val >= 0 ){
			foreach((array) $big_arr as $akey => $aval ){
						if( in_array( $aval['prs_parent_id'] , $re_arr ) ){
							$re_arr[] = $aval['prs_id'];

							unset( $big_arr[$akey] );

							$a = $this -> get_son_id( $big_arr  , $re_arr ,  $grade_val -1  );	

							if(is_array($a)){
								$re_arr = array_merge($re_arr,$a);
							}

						}

			}
			
		}
		
		if($re_arr[0]==0){ unset($re_arr[0]); }

		if(count($re_arr)<1){ return false; }else{	return array_unique($re_arr); }
	}



} 