<?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 admin\controller;
use \admin\model\category AS model;
use \admin\home;
use \temmoku\db;
use admin\public_class\article;
class category extends home
{
    public function add()
    {
	    if('post'===$_POST['step']){
	    	if(!$_POST['post']['name']){
		    	$this->err('最少需要输入一个栏目');
		    }else{
		    	if($_POST['post']['cid']){
		   			$cid=$_POST['post']['cid'];
		    		$Return = db::select('*')->from(jab."category")->where("cid='$cid'")->row();
		   			if($Return['type'] !='1' && $Return){
		   				$this->err('栏目或者单篇文章不允许添加下级栏目');
					}
		    	}
			   	$allname=explode(PHP_EOL,$_POST['post']['name']);
				foreach($allname AS $value){
					if(!$value){
						continue;
					}
					$data=[
						'name'=>$value,
						'upcid'=>$_POST['post']['cid'],
						'type'=>$_POST['post']['type'],
						'cmid'=>$_POST['post']['mid']
					];
					(new model)->postadd($data);	
				}
				$ReturnData=(new model)->alllist();
			
				(new \admin\public_class\cache)->category_setting($ReturnData);
				$this->Success('栏目创建成功',FromUrl);
		    }
	    }
	    $moduleDB = (new model)->module();
	    $allcidDB=(new article)->allcidDB('0',jab."category");
	    !isset($_GET['cid']) && $_GET['cid'] = 0;
	    $this->assign('cid', $_GET['cid']);
	    $this->assign('moduleDB', $moduleDB);
	    $this->assign('allcidDB', $allcidDB);
	    $this->display(tpl('category/add'));
    }
    public function edit()
    {
    	if('post'===$_POST['step']){
    		if(!$_POST['cid']){
    			$this->err('缺少必要参数CID');
    		}
    		(new model)->editpost();
    		$ReturnData=(new model)->alllist();
			(new \admin\public_class\cache)->category_setting($ReturnData);
	    	$this->Success('栏目修改成功');
    	}
    	
    	$data=db::select('*')->from(jab."category")->where("cid= '$_GET[cid]' ")->row();
    	
    	$category_dir=TEMPLATE."/home/".C('select_template')."/home/pc/category";
    	$article_dir=TEMPLATE."/home/".C('select_template')."/home/pc/articles";
    	$list_template=$this->Get_tpl($category_dir);
    	$article_template=$this->Get_tpl($article_dir);
    	
    	$this->assign('article_template', $article_template);
    	$this->assign('list_template', $list_template);
    	
    	$moduleDB = (new model)->module();
	    $allcidDB=(new article)->allcidDB('0',jab."category");
    	
    	$this->assign('data', $data);
    	$this->assign('moduleDB', $moduleDB);
    	$this->assign('allcidDB', $allcidDB);
    	$this->display(tpl('category/edit'));
    }
    
    public function del()
    {
    	$cid=$_GET['cid'];
    	$Return = db::select('*')->from(jab."category")->where("cid='$cid'")->row();
    	
    	if(!$Return){
    		$this->err('不存在的栏目');
		}
		$Return = db::select('*')->from(jab."category")->where("upcid='$cid'")->row();
		if($Return){
			$this->err('分类下有栏目，请先删除或转移下级栏目');
		}
		$Return = db::select('*')->from(jab."article")->where("cid='$cid'")->row();
		if($Return){
			$this->err('栏目里有文章存在，请删除后在继续本操作');
		}
		(new model)->del($cid);
		$ReturnData=(new model)->alllist();
		(new \admin\public_class\cache)->Category_setting($ReturnData);
    	$this->Success('栏目删除成功');
    }
    public function index()
    {
    	$Return = (new model)->alllist();
    	$this->assign('Return', $Return);
    	$this->display(tpl('category/index'));
    }
    public function batch_action(){
    	if($_POST['step']==='reorder'){
    		foreach($_POST['reorder'] as $cid=>$reorder){
    			db::update(jab."category")->cols(['reorder'=>$reorder])->where("cid='$cid'")->query();
    		}
    	}
    	$this->success('操作成功');
    }
    
	private function Get_tpl($dir){
		$data=[];
		if ( $handle  =  @opendir ( $dir )) {
			while (( $file  =  readdir ( $handle ))!== false) {
				if($file==='.' || $file==='..'){
					continue;
				}
				$files=$dir."/".$file;
				if(is_file($files)){
					$php=strtolower(strrchr($files,"."));
					if($php=='.php'){
						$data[]=@include_once $files;
					}
				}
			}
			//关闭打开的目录
			closedir ( $handle );
		}
		return $data;
	}
    
}
?>