<?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 news;
use news\init;
class route extends init{
	private $data=[];
	private $html='html';
	private $url="";
	private $url_rules_selected="";
	private $special="";
	
	public function make_url($data,$module=MODULE){
		$this->data=$data;
		C('HTML') ? $this->html=C('HTML') : $this->html='.html';
		self::$domain ? $this->url=HTTP.self::$domain : $this->url=WEBURL.'/'.strtolower($module);
		C(self::$dir.'|setting') ? $this->url_rules_selected=C(self::$dir.'|setting.url_rules') :  $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= $this->url."/?c=articles&a=articles&aid={$this->data[aid]}{$page}";
				}else{
					$url= $this->url."/?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= $this->url."/articles/articles/aid/{$this->data['aid']}{$page}".$this->html;
				}else{
					$url= $this->url."/category/category/cid/{$this->data[cid]}{$page}".$this->html;
				}
				return $url;
		   	break;
		   	
		   	default:
			   	$dir=C(self::$dir.'|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 $this->url."/{$dir}/articles_{$this->data['aid']}{$page}".$this->html;
				}else{
					return $this->url."/{$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= $this->url."/?c=special&a=".$this->data['special']."&sid={$this->data[sid]}&id={$this->data[id]}{$page}";
				}else{
					$url= $this->url."/?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= $this->url."/special/".$this->data['special']."/sid/{$this->data['sid']}/id/{$this->data['id']}{$page}".$this->html;
				}else{
					$url= $this->url."/special/".$this->data['special']."/sid/{$this->data['sid']}{$page}".$this->html;
				}
				
				return $url;
		   	break;
		   	
		   	default:
		   		$dir='';
		   		if($this->data['sid']){
		   			$category_dir_name=C(self::$dir.'|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(self::$dir.'|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 $this->url."/special/{$dir}/index{$page}".$this->html;
		}
	}
	
	public function route(){
		defined('is_home') || define('is_home','home');
		//加载模块配置
		$dir=APP_PATH.self::$dir.'/conf';
	    Load_conf($dir);
	    if(!C(self::$dir.'|setting.webstate')){
	    	(new \temmoku\controller)->err(C(self::$dir.'|setting.way_close'));
	    }
	   	 //给模块一个网址定义
	    self::$domain ? $url=HTTP.self::$domain : $url=WEBURL.'/'.MODULE;
	    define('M_WEBURL',$url); 
		define('M_jab',self::$table); 
		//不需要匹配
		if(C(self::$dir.'|setting.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(self::$dir.'|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(self::$dir.'|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],C(self::$dir.'|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(self::$dir.'|HOME_ROUTE')[$paths[0]];
			}else{
				define('CONTROLLER','category'); 
				define('METHOD','category'); 
				isset($test_path['1']) && $_GET['page']=$test_path['1'];
				$_GET['cid']=C(self::$dir.'|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']='';
		}
	}
}
?>
