<?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\model;
use \temmoku\db;
class category{
   	public function module()
    {
    	$moduleDB=db::select('*')->from(jab."module")->where(" close = 0")->query();
		return $moduleDB;
    }


    public function del($cid)
    {
    	$this->category_level();
    	db::delete(jab."category")->where("cid= '$cid' ")->query();
    }
    public function editpost()
    {
    	extract($_POST);
    	$data=[
	    	'name'=>$name,
	    	'upcid'=>(int)$fup,
	    	'type'=>$type,
	    	'cmid'=>$mid,
	    	'dir_name'=>$dir_name,
	    	'seo_name'=>$seo_name,
	    	'seo_keywords'=>$seo_keywords,
	    	'seo_description'=>$seo_description,
	    	'content'=>$content,
	    	'setting'=>serialize($setting)
    	];
    	$this->category_level();
    	db::update(jab."category")->cols($data)->where("cid='$cid'")->query();
    	
    }
    public function postadd($data)
    {
    	db::insert(jab."category")->cols($data)->query();
    	$this->category_level();
    }
    
    public function alllist($cid="0"){
    	static $allDB=[];
		$level='';
		$query=db::select('*')->from(jab."category")->where("upcid = $cid")->orderByDESC(['reorder'])->query();
	    if($query){
	        foreach($query AS $key=>$row){
	        	extract(db::select('count(*) AS NUM')->from(jab."article")->where("cid= '$row[cid]' ")->row());
	        	$row['num']=$NUM;
	        	if($row['cmid']){
	        		$m=db::select('name')->from(jab."module")->where("id= '$row[cmid]' ")->row();
	        		$row['modulenbname']=$m['name'];
	        	}else{
	        		$row['modulenbname']="文章模型";
	        	}
	        	if($row['upcid']!=0){
		        	$level="&nbsp;&nbsp;&nbsp;|";
			        for($i=0;$i<$row['level'];$i++){
			        	$level.="&nbsp;&nbsp;&nbsp;";
			        }
		        	$level.="|--";
		        	$row['levels']=$level;
		        	$allDB[] = $row;
		        	$this->alllist($row['cid']);
		        }else{
		        	$row['levels']=$level;
		        	$allDB[] = $row;
		        	$this->alllist($row['cid']);
		        }
			}
	    }
	    return $allDB; 
	}

    
    public function category_level($level=0,$cid=0){
		db::update(jab."category")->cols(array('level'=>$level+1))->where("upcid='$cid'")->query();
		$categoryDB=db::select('*')->from(jab."category")->where("upcid='$cid'")->query();
		foreach($categoryDB AS $row){
			$this->category_level($row['level'],$row['cid']);
		}
	}
	
}