<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class linkModel{
	
	function Run(&$atts, &$refObj, &$fields){
		$attlist = "name=,parameter=";
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE);
		$array=array_filter(explode(",",$name));
		
		$parameter=$this->getParameter($parameter);
	
		if(count($array)>1){
			foreach($array as $k => $v){
				$link[$k]['link']=$this->getLink($v,$parameter);
				$link[$k]['title']=constant($v."_TITLE");
			}
			return $link;
		}else{
			return $this->getLink($name,$parameter);
		}
		
	}
	private function getLink($name,$parameter){
		if($name=="FILENAME_BACK"){
			$link=zen_back_link(true);
		}else{
			if(strstr($parameter,"+")){
				$parameter=explode("+",$parameter);
				$str="";
				foreach($parameter as $k => $v){
					if(strstr($v,"$")){
						$n=eval('return '. $v . ';');
						$str.=$n;
					}else{$str.=$v;}
				}
				$parameter=$str;
			}
			$file_name=constant($name);
			if($name=="FILENAME_INDEX"){$file_name="index";}
			$link=str_replace("&amp;","&",zen_href_link($file_name,$parameter,'SSL'));
		}
		return $link;
	}
	private function getParameter($parameter){
		if($parameter=="auto"){
			$parameter=zen_get_all_get_params();
			return $parameter;
		}
		foreach($_GET as $k => $v){
			$parameter=str_replace("__".$k."__",$v,$parameter);
		}
		$parameter=str_replace("[","",$parameter);
		$parameter=str_replace("]","",$parameter);
		$parameter=array_filter(explode(",",$parameter));
		$str="";
		foreach($parameter as $k => $v){
			if(strstr($v,"=>")){
				$v=explode("=>",$v);
				if($k>=1){
					$str.="&".$v[0]."=".$v[1];
				}else{
					$str.=$v[0]."=".$v[1];
				}
				
			}else{
				echo "The parameter is incorrect!!!";
			}
		}
		return $str;
	}
}
?>