<?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 pagelistModel{
	function Run(&$atts, &$refObj, &$fields){
		global $_vars,$product_class,$blog_class;
		$attlist = "listitem=,listsize=,total=,pernum=,custom=,tpl=";
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE); 
		
		
		if($_GET['main_page']==FILENAME_BLOG){
			if(!$total){
				$total=$GLOBALS['listCount'];
				$pernum=$blog_class->get_pagelist_pernum();
			}
		}else{
			if(!$total){
				$total=$GLOBALS['listCount'];
				$pernum=$product_class->get_pagelist_pernum();

			}
		}
		
		$parameter=array('total'=>$total,'listsize'=>$listsize,'listitem'=>$listitem,'pernum'=>$pernum,"info"=>true);
		if($_GET['main_page']==FILENAME_BLOG){
			$pagelist = $blog_class->get_archives_list_page($_GET,$parameter);
		}else{
			$pagelist = $product_class->get_products_list_page($_GET,$parameter);
		}
		
		if(!$tpl){$tpl="propage.tpl";}
		if($_GET['main_page']==FILENAME_BLOG){$tpl="blogpage.tpl";}
		if(defined("TPL_MODULES_RESET")){
			$html=file_get_contents(TPL_MODULES_RESET."tpl/".$tpl);
			$GLOBALS["tplfile"][]=str_replace(TEMPLATE_TPL,"",TPL_MODULES_RESET."tpl/".$tpl);
		}else{
			$html=file_get_contents(TPL_MODULES."tpl/".$tpl);
			$GLOBALS["tplfile"][]=str_replace(TEMPLATE_TPL,"",TPL_MODULES."tpl/".$tpl);
		}
		
		
		if($custom){
			$content=$this->get_pagelist_tpl($html,$custom);
			foreach($pagelist['info'] as $a => $b){
				$content=str_replace("[field:page_".$a."/]",$b,$content);
			}
			$result.=$content;
			unset($atts['custom']);
			return $result;
		}
		$listitem=array_filter(explode(",",$listitem));
		
        if(count($listitem)>0){
			$keys=['list','prev','next','first','last','active'];
			$continue=false;
			foreach($listitem as $k => $v){
				foreach($keys as $a => $b){
					if($v==$b){$continue=true;}
				}
			}
			if($continue==true){
				foreach($listitem as $k => $v){
					if($v=="list"){
						$tpls=$this->get_pagelist_tpl($html,$v);
						foreach($pagelist[$v] as $a => $b){
							if(array_key_exists("status",$b)){
								$content=$this->get_pagelist_tpl($html,"active");
							}else{
								$content=$tpls;
							}
							$str=str_replace("[field:page_link/]",$b["link"],$content);
							$str=str_replace("[field:page_name/]",$b["text"],$str);
							$result.=$str;
						}
					}
					else{
						$content=$this->get_pagelist_tpl($html,$v);
						$str=str_replace("[field:page_link/]",$pagelist[$v]["link"],$content);
						$str=str_replace("[field:page_name/]",$pagelist[$v]["text"],$str);
						foreach($pagelist['info'] as $a => $b){
							$str=str_replace("[field:page_".$a."/]",$b,$str);
						}
						$result.=$str;
					}
				}
			}
		}

		
		
		return $result;
    }
    private function get_pagelist_tpl($html,$name){
		$start=strpos($html,"{type:".$name."}")+strlen("{type:".$name."}");
		$end=strpos($html,"{/type:".$name."}");
		$length=$end-$start;
		if($length<0){
			return false;
		}
		$content=substr($html,$start,$length);
		return $content;
    }
}
  
  
?>