<?php 
/*
　* @$Id
　* @
　* @Ver: 2.0
　* @Create: 2009-11-18 8:10
　* @Modify: 
　* @Author: CPWEB http://www.cpweb.net.cn
　* @Copyright (C) 2008 www.cpweb.net.cn. All Rights Reserved.
　* @License CPWEB is free software and use is subject to license terms
　*/
class MenusAction extends GlobalAction 
{
	function _initialize()
	{
		parent::_initialize();
		$this->model='Menus';
	}
	public function index()
	{
		$this->cpweb_list();
		$this->display();
	}
	public function add()
	{
		$tree	=	json_encode($this->_makeTree(0));
		$this->assign('category_json',$tree);
		$this->display();
	}
	public function edit()
	{
		$this->cpweb_read();		
		$tree	=	json_encode($this->_makeTree(0));
		$this->assign('category_json',$tree);  
		$this->display();
	}
	public function submit(){
		$act=$_REQUEST['act'];
		$id=$_REQUEST['id'];
		if($act=='delete'&&!is_array($id)){//单个删除时才检测栏目的删除权限
			$S=M('Menu')->find($id);
			if (!$id||!$S) $this->error(L('CPWEB_SELECT_NOT_EXIST'));//检测删除的内容是否存在
		}
		$this->_subAction();
	}
	public function getmenu()
	{
		$id=$_REQUEST["id"];
		$map['menu_id']=$id;
		$counter=0;
		$optionsInfo='{';
		$Menu=M('Menu')->where($map)->findall();
		foreach($Menu AS $key => $value){
			if($counter>0){
				$optionsInfo=$optionsInfo.",";
			}
			$optionsInfo=$optionsInfo."'";
			$optionsInfo=$optionsInfo.$value["id"];
			$optionsInfo=$optionsInfo."':'";
			$optionsInfo=$optionsInfo.$value["name"];
			$optionsInfo=$optionsInfo."'";
			$counter++; 
		}
		$optionsInfo=$optionsInfo.'}';
		print($optionsInfo);
	}
	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法	//新增方法
   	
	//获取LI列表
	function getCategoryList($pid='0') {
		$list	=	$this->_makeLiTree($pid);
		return $list;
	}
	function _makeLiTree($pid) {
		
		if( $c = D('Category')->where("pid='$pid'")->findAll() ){
			
			$list	.=	'<ul>';
			foreach($c as $p){
				@extract($p);
				
				$ptitle	=	"<span id='category_".$id."' title='".$title."'><a href='javascript:void(0)' onclick=\"edit('".$id."')\">".$title."</a></span>";
				$title	=	'['.$id.'] '.$ptitle;

				$list	.=	'
					<li id="li_'.$id.'">
					<span style="float:right;">
						<a href="javascript:void(0)" onclick="edit(\''.$id.'\')">修改</a>
						<a href="javascript:void(0)" onclick="del(\''.$id.'\')">删除</a>
					</span> '.$title.'
					</li>
					<hr style="height:1px;color:#ccc" />';
				
				$list	.=	$this->_makeLiTree($id);
			
			}
			$list	.=	'</ul>';
			
		}
		return $list;
	}
	
	
	//添加菜单分类
	function adds() {
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ("Menus");
		$menuss=$model->create ();
		if (false === $menuss) {
			$this->error ( $model->getError () );
		}
		//保存当前数据对象
		$menuss['pid']	=	$this->_digCate($_POST);
		$menuss['cate_tree']	=	$this->_digCateTree($_POST);
		$list=$model->add ($menuss);
		if ($list!==false) { //保存成功
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('添加分类成功!');
		} else {
			//失败提示
			$this->error ('分类添加失败!');
		}
	}
	function edits() {
		$model	= D('Menus');
		if(false === $vo = $model->create()) {
			$this->error($model->getError());
		}
		$vo['pid']	=	$_POST['id'];
		$vo['cate_tree']	=	$this->_digCateTree($_POST);
		$id = intval($_POST['id']);
		if($model->where("id=$id")->save($vo)) {
			//成功提示
			$this->assign("jumpUrl",$_POST['returnList']);
			$this->success('更新成功!');
		}else {
			//错误提示
			$this->error("更新失败! ".$model->getError());
			return;
		}
	}
   		
   	//删除分类
   	function delCategory() {
   			$id	=	intval($_POST['id']);
			if(D('Category')->where('id='.$id)->delete()){
				$this->assign("jumpUrl", __URL__.'/index');
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
	
   		}
   	//解析分类
	function _digCate($array) {
		foreach($array as $k=>$v){
			$nk	=	str_replace('category','',$k);
			if(is_numeric($nk) && !empty($v)){
				$cates[$nk]	=	intval($v);
			}
		}
		$pid	=	is_array($cates)?end($cates):0;
		unset($cates);
		return intval($pid);
	}
	//解析分类树
	function _digCateTree($array) {
		foreach($array as $k=>$v){
			$nk	=	str_replace('category','',$k);
			if(is_numeric($nk) && !empty($v)){
				$cates[$nk]	=	intval($v);
			}
		}
		if(is_array($cates)){
			return implode(',',$cates);
		}else{
			return intval($cates);
		}
	}
	//生成分类Tree
	function _makeTree($pid) {
		$c = D('Menus')->where("pid='$pid'")->findAll();
		if( !empty($c) ){
			foreach($c as $v){
				$cTree[t]	=	$v['title'];
				$cTree[a]	=	$v['id'];
				$cTree[d]	=	$this->_makeTree($v['id']);
				$cTrees[]	=	$cTree;
			}
		}
		
		return $cTrees;
	}
}
?>