<?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\home\Controller;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\lib\pages;
class category extends controller
{

    public function category()
    {
    	!isset($_GET['cid']) && $this->err('缺少必要参数');
    	!isset($_GET['page']) && $_GET['page']=1;
    	$cache_dir=RUNTIME_PATH."/cache/".MODULE.'/'.DEVICE."/category/{$_GET['cid']}_{$_GET[page]}.txt";
    	if(C(MODULE.'|setting.category_cache_time') && is_file($cache_dir) && time()-@filemtime($cache_dir) < C(MODULE.'|setting.category_cache_time')*3600){
    		echo get_cache($cache_dir);
    		exit;
    	}
    	$data=db::select('*')->from(M_jab.'category')->where('cid='.$_GET['cid'])->row();
   		$data['setting']=unserialize($data['setting']);
   		if($data['setting']['tpl']['category']){
   			$tpl=$data['setting']['tpl']['category'];
   		}else{
   			if('2'==$data['type']){
   				$tpl="single";
   			}else{
   				if($data['cmid']){
	   				$tpl="category_".$data['cmid'];
	   			}else{
	   				$tpl="category";
	   			}
   			}
   		}
    	if('1'==$data['type']){
    		//为1是大分类
			//获取所有一级栏目
			
    		$getalllist=$this->get_all_list();
    	}elseif('2'==$data['type']){
    		//为2是单页
    		$data['content']=htmlspecialchars_decode($data['content']);
    	}else{
    		//为0是列表
    		$NUM=$data['setting']['rows'] ? $data['setting']['rows'] :20;
    		$getlist=db::select('*')->calcFoundRows()->from(M_jab.'article')->where("cid = $_GET[cid] AND state=1")->orderByDESC(array('aid'))->setPaging($NUM)->page($_GET['page'])->query();
    		$getlistNUM=db::row(' SELECT FOUND_ROWS() AS allNUM');
			
    		$Get_Page=(new Pages)->Pages('','','',$NUM,$_GET['page'],$getlistNUM['allNUM']);
    		$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return U(['cid'=>$_GET['cid'],'page'=>$matches[1]]);}, $Get_Page );
    	}
		
		//不是pc设备才去获取
		if(DEVICE !='pc'){
			if($data[type]==1){
				$menu_more_list=db::select('*')->from(M_jab.'category')->where("upcid=$data[cid]")->query();
			}else{
				$menu_more_list=db::select('*')->from(M_jab.'category')->where("upcid=$data[upcid]")->query();
			}
			//网站名称,或者是栏目名称
			if($data['type']==0 ){
				if($_GET[json]==true){
					$this->assign('json',true);
					$_getlist=$getlist;
					unset($data,$getlist,$Get_Page);//防止泄露敏感信息
					foreach($_getlist as $row){
						$_row[url]=U(['aid'=>$row['aid'],'cid'=>$row['cid']]);
						$_row[title]=$row[title];
						$_row[posttime]=date('Y-m-d H:i:s',$row[posttime]);
						$getlist[]=$_row;
					}
				}
				$page=ceil($getlistNUM['allNUM']/$NUM);
				$this->assign('page',$page);
			}
			$this->assign('webname',$data['name']);
		}
		
		
		
		
    	//title标签
    	$title=empty($data['seo_name']) ? $data['name'] :$data['seo_name'];
    	$TITLE=$title.'_'.C('WEBNAME');
    	//关键字标签
    	$KEYWORDS=
    	//描述标签
    	$DESCRIPTION=empty($data['seo_description']) ? C('WEB_SEO_DESCRIPTION') :$data['seo_description'];
  
    	//面包屑
    	$Crumbs = preg_replace_callback('/cid=([0-9]+)/',function ($matches) {return U(['cid'=>$matches[1]],MODULE);}, C(MODULE.'|WN_CRUMBS.'.$data['cid']) );
    	$this->assign('menu_more_list',$menu_more_list);
    	$this->assign('WEBNAME',$TITLE);
    	$this->assign('KEYWORDS',$KEYWORDS);
    	$this->assign('DESCRIPTION',$DESCRIPTION);
    	$this->assign('latest_news',$latest_news);
    	$this->assign('Crumbs',$Crumbs);
    	$this->assign('data',$data);
    	$this->assign('getlist',$getlist);
    	$this->assign('Get_Page',$Get_Page);
    	$this->assign('getalllist',$getalllist);
		
		
		
        $this->display(tpl($tpl));
        
        if(C(MODULE.'|setting.category_cache_time')){
			write_file($cache_dir,ob_get_contents());
		}
    }

    private function get_all_list(){
    	$data=db::select('*')->from(M_jab.'category')->where("upcid = $_GET[cid]")->orderByDESC(array('cid'))->query();
    	foreach($data as $row){
    		if(!$row['type']){
    			$row['article']=db::select('*')->from(M_jab.'article')->where("cid = $row[cid]")->orderByDESC(array('aid'))->setPaging('10')->page('1')->query();
    		}
    		$_data[]=$row;
    	}
    	return $_data;
    }
}
?>