<?php
namespace Admin\Controller;
use Think\Controller;

class CateController extends Controller {
	function listCategory($id = null) {
		$cates = M('categories');
		if (!isset($id)) {
			//$arr=$cates->table(C('DB_PREFIX').'categories c , '.C('DB_PREFIX').'archives a')->where('a.category=c.id and c.userid='.$_SESSION['userid'])->group('c.id')->field('c.*,count("a.category") as sum')->select();
			$arr = $cates->where('userid=' . $_SESSION['userid'])->select();
		} else {
			$arr = $cates->where('id=' . $id . ' AND userid=' . $_SESSION['userid'])->find();
		}

		return $arr;
	}
	public function listSelectedTags($aid) {
		$TagRelationship = M('tags_relationships');
		$arr             = $TagRelationship->where('aid=' . $aid)->field('tid')->select();
		$string          = '';
		for ($i = 0; $i < sizeof($arr); $i++) {
			$string .= ($arr[$i]['tid'] . ',');
		}
		return $string;
	}
	public function listTag($id = null) {
		$cates = M('tags');
		if (null == $id) {
			$arr = $cates->where('userid=' . $_SESSION['userid'])->select();
		} else {
			$arr = $cates->where('id=' . $id . ' AND userid=' . $_SESSION['userid'])->find();
		}

		return $arr;
	}
	public function addCategory() {
		$data['catename']  = $_POST['new_category'];
		$data['catealias'] = $_POST['category_alias'];
		$data['catetext']  = $_POST['category_text'];
		$data['parentid']  = $_POST['parentid'];
		$data['userid']    = $_SESSION['userid'];
		$cates             = M('Categories');
		if ($cates->data($data)->add() != 0) {
			$this->success('添加成功');
		} else {
			$this->error('添加失败');
		}

	}

	public function addTag() {
		$data['tag']      = $_POST['new_tag'];
		$data['tagalias'] = $_POST['tag_alias'];
		$data['userid']   = $_SESSION['userid'];
		$tag              = M('tags');
		if ($tag->data($data)->add() != 0) {
			$this->success('添加成功');
		} else {
			$this->error('添加失败');
		}

	}

	public function delCategory() {

		$category = M('categories');
		if ($category->where('id=' . $_GET['id'] . ' AND userid=' . $_SESSION['userid'])->delete() != 0) {
			$this->success('成功删除');
		} else {
			$this->error('删除失败');
		}

	}
	public function delTag() {
		$tag = M('tags');
		if ($tag->where('id=' . $_GET['id'] . ' AND userid=' . $_SESSION['userid'])->delete() != 0) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}

	}
	public function editCategory() {
		$cates             = M('categories');
		$data['catename']  = $_POST['category'];
		$data['catetext']  = $_POST['category_text'];
		$data['catealias'] = $_POST['category_alias'];
		$data['parentid']  = $_POST['parentid'];
		if ($cates->where('id=' . $_GET['id'] . ' AND userid=' . $_SESSION['userid'])->data($data)->save() != 0) {
			$this->success('成功修改');
			$this->redirect('Index/items');

		} else {
			$this->error('修改错误');
		}

	}
	public function editTag() {
		$tags             = M('tags');
		$data['tag']      = $_POST['tag'];
		$data['tagalias'] = $_POST['tag_alias'];
		if ($tags->where('id=' . $_GET['id'] . ' AND userid=' . $_SESSION['userid'])->data($data)->save() != 0) {
			$this->success('成功修改');
			$this->redirect('Index/items');

		} else {
			$this->error('修改错误');
		}

	}

}