<?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 plugin\friend_link\admin\controller;
use temmoku\controller;
use temmoku\db;
class category extends controller{
	
	public function alllist(){
		$data=db::select('*')->from(P_jab."category")->orderByDESC(['reorder'])->query();
		foreach($data as $row){
			extract(db::select('COUNT(*) AS NUM')->from(P_jab."friend")->where('cid='.$row['cid'])->row());
			$row['num']=$NUM;
			$f_data[]=$row;
		}
		$this->assign('data', $f_data);
		$this->display(tpl('category/alllist'));
	}
	public function add(){
		if('post'===$_POST['step']){
			if(!$_POST['name']){
				$this->err('分类名称不可以为空');
			}
			$arr=[
				'name'=>$_POST['name'],
				'reorder'=>(int)$_POST['reorder']
			];
			db::insert(P_jab."category")->cols($arr)->query();
			$this->success('添加成功');
		}
		$this->display(tpl('category/add'));
	}
	
	public function edit(){
		
		if('post'===$_POST['step']){
			if(!$_POST['cid']){
				$this->err('缺少必要参数');
			}
			if(!$_POST['name']){
				$this->err('分类名称不可以为空');
			}
			$arr=[
				'name'=>$_POST['name'],
				'reorder'=>(int)$_POST['reorder']
			];
			db::update(P_jab."category")->cols($arr)->where('cid='.$_POST['cid'])->query();
			$this->Success('修改成功');
		}
		$data=db::select('*')->from(P_jab."category")->where('cid='.$_GET['cid'])->row();
		$this->assign('data', $data);
		$this->display(tpl('category/add'));
	}
	public function del(){
		if(!$_GET['cid']){
			$this->err('缺少必要参数');
		}
		extract(db::select('COUNT(*) AS NUM')->from(P_jab."friend")->where('cid='.$_GET['cid'])->row());
		if($NUM){
			$this->err('请清空分类下的友情链接，在继续删除');
		}
		db::delete(P_jab."category")->where('cid='.$_GET['cid'])->query();
		$this->success('删除成功');
	}
}
?>
