<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace home;
class route{
	private $data=[];
	private $html='html';
	private $url_rules_selected="";
	private $special="";
	
	public function make_url($data){
		$this->data=$data;
		C('HTML') ? $this->html=C('HTML') : $this->html='.html';
		$this->url_rules_selected=C('URL_RULES');
		if($data['special']){
			return $this->special();
		}else{
			return $this->articles();
		}
	}
	
	private function articles(){
		switch($this->url_rules_selected){
			case  1:
		    	!isset($this->data['page']) && $this->data['page']=1;
				if($this->data['page']>1){
					$page="&page={$data['page']}";
				}
				if($this->data['aid']){
					$url= WEBURL."/?c=articles&a=articles&aid={$this->data[aid]}{$page}";
				}else{
					$url= WEBURL."/?c=category&a=category&cid={$this->data[cid]}{$page}";
				}
				return $url;
		   	break;
			case  2:
		    	!isset($this->data['page']) && $this->data['page']=1;
				$page="/page/{$this->data['page']}";
				
				if($this->data['aid']){
					$url= WEBURL."/articles/articles/aid/{$this->data['aid']}{$page}".$this->html;
				}else{
					$url= WEBURL."/category/category/cid/{$this->data[cid]}{$page}".$this->html;
				}
				return $url;
		   	break;
		   	
		   	default:
			   	$dir=C('home|CATEGORY_SETTING')[$this->data['cid']]['dir_name'];
				!isset($this->data['page']) && $this->data['page']=1;
				if($this->data['page']>1){
					$page="_{$this->data['page']}";
				}
				if(!$dir){
					$dir='category/'.$this->data['cid'];
				}
				if($this->data['aid']){
					return WEBURL."/{$dir}/articles_{$this->data['aid']}{$page}".$this->html;
				}else{
					return WEBURL."/{$dir}/index{$page}".$this->html;
				}
		}
	}
	
	private function special(){
		
		switch($this->url_rules_selected){
			case  1:
		    	!isset($this->data['page']) && $this->data['page']=1;
				if($this->data['page']>1){
					$page=$this->url."&page={$data['page']}";
				}
				if($this->data['id']){
					$url= WEBURL."/?c=special&a=".$this->data['special']."&sid={$this->data[sid]}&id={$this->data[id]}{$page}";
				}else{
					$url= WEBURL."/?c=special&a=".$this->data['special']."&sid={$this->data[sid]}{$page}";
				}
				return $url;
		   	break;
			case  2:
		    	!isset($this->data['page']) && $this->data['page']=1;
				$page="/page/{$this->data['page']}";
				if($this->data['id']){
					$url= WEBURL."/special/".$this->data['special']."/sid/{$this->data['sid']}/id/{$this->data['id']}{$page}".$this->html;
				}else{
					$url= WEBURL."/special/".$this->data['special']."/sid/{$this->data['sid']}{$page}".$this->html;
				}
				
				return $url;
		   	break;
		   	
		   	default:
		   		$dir='';
		   		if($this->data['sid']){
		   			$category_dir_name=C('home|Special_category_setting')[$this->data['sid']]['dir_name'];
		   			if(!$category_dir_name){
		   				$category_dir_name='category_'.$this->data['sid'];
		   			}
		   			$dir .=$category_dir_name;
		   		}
		   		if($this->data['id']){
		   			$content_dir_name =C('home|Special_list_setting')[$this->data['id']]['dir_name'];
		   			if(!$content_dir_name){
		   				$content_dir_name='content_'.$this->data['id'];
		   			}
		   			$dir .="/".$content_dir_name;
		   		}
		   		
				!isset($this->data['page']) && $this->data['page']=1;
				if($this->data['page']>1){
					$page="_{$this->data['page']}";
				}
				return WEBURL."/special/{$dir}/index{$page}".$this->html;
		}
	}
	
	public function route(){
		//加载模块配置
		$dir=APP_PATH.MODULE.'/conf';
	    Load_conf($dir);
		//不需要匹配
		if(C('url_rules')==='2' )return ;
		$paths      =   explode(MODULE_PATHINFO_DEPR,$_SERVER['PATH_INFO']);
		$paths[0]=strtolower($paths[0]);
		//自定目录解析
		if(empty($paths[0])){
			return;
		}
		if($paths[0]=='special'){
			$this->special_route($paths);
		}
		$this->article_route($paths);
	}
	
	private function special_route($paths){
		define('CONTROLLER','special');
		if(count($paths)=='3'){
			define('METHOD','category');
			$this->special_category_route($paths);
		}elseif(count($paths)=='4'){
			define('METHOD','content');
			$this->special_category_route($paths,false);
			$this->special_list_route($paths);
		}
		return;
	}
	
	private function special_category_route($paths,$action=true){
		
		$matches = preg_match('/category_[0-9]+/is',$paths[1]);
		if($matches){
			if($action==true){
				$matches_page = preg_match('/index_[0-9]+/is',$paths[2]);
				if($matches_page){
					$test_page      =   explode('_',$paths[2]);
					$_GET['page']=$test_page[1];
				}
			}
			$test_path      =   explode('_',$paths[1]);
			$_GET['sid']=$test_path[1];
		}else{
			$sid=C('home|SPECIAL_ROUTE.'.$paths[1]);
			if($sid){
				if($action==true){
					$matches_page = preg_match('/index_[0-9]+/is',$paths[2]);
					if($matches_page){
						$test_page      =   explode('_',$paths[2]);
						$_GET['page']=$test_page[1];
					}
				}
				$_GET['sid']=$sid;
			}
		}
	}
	
	private function special_list_route($paths){
		$matches = preg_match('/content_[0-9]+/is',$paths[2]);
		if($matches){
			$matches_page = preg_match('/index_[0-9]+/is',$paths[3]);
			if($matches_page){
				$test_page      =   explode('_',$paths[3]);
				$_GET['page']=$test_page[1];
			}
			$test_path      =   explode('_',$paths[2]);
			$_GET['id']=$test_path[1];
		}else{
			$id=C('home|SPECIAL_LIST_ROUTE.'.$paths[2]);
			if($id){
				if($action==true){
					$matches_page = preg_match('/index_[0-9]+/is',$paths[3]);
					if($matches_page){
						$test_page      =   explode('_',$paths[3]);
						$_GET['page']=$test_page[1];
					}
				}
				$_GET['id']=$id;
			}
		}
	}
	
	private function article_route($paths){
		if( array_key_exists($paths[0],(array)C('home|HOME_ROUTE'))){
			$test_path      =   explode('_',$paths[1]);
			if(in_array('articles',$test_path)){
				define('CONTROLLER','articles'); 
				define('METHOD','articles'); 
				isset($test_path['1']) && $_GET['aid']=$test_path['1'];
				isset($test_path['2']) && $_GET['page']=$test_path['2'];
				$_GET['cid']=C('home|HOME_ROUTE')[$paths[0]];
			}else{
				define('CONTROLLER','category'); 
				define('METHOD','category'); 
				isset($test_path['1']) && $_GET['page']=$test_path['1'];
				$_GET['cid']=C('home|HOME_ROUTE')[$paths[0]];
			}
			//清空PATH_INFO变量
			$_SERVER['PATH_INFO']='';
		}elseif('category'===$paths[0]){
			//默认的目录解析
			$test_path      =   explode('_',$paths[2]);
			if(in_array('articles',$test_path)){
				define('CONTROLLER','articles');
				define('METHOD','articles'); 
				!empty($paths['1']) && $_GET['cid']=$paths['1'];
				!empty($test_path['1']) && $_GET['aid']=$test_path['1'];
				!empty($test_path['2']) && $_GET['page']=$test_path['2'];
			}else{
				define('CONTROLLER','category');
				define('METHOD','category');
				isset($paths['1']) && $_GET['cid']=$paths['1'];
				isset($test_path['1']) && $_GET['page']=$test_path['1'];
			}
			//清空PATH_INFO变量
			$_SERVER['PATH_INFO']='';
		}
	}
}
?>
