<?php
// +----------------------------------------------------------------------
// | WBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.w3note.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 网菠萝果
// +----------------------------------------------------------------------
// $Id$
class NodeAction extends CommonAction {
	public function index(){
		$listsarr=$this->lists();
		$lists=$listsarr['lists'];
		foreach ($lists as $k => $v) {
			$lists[$k]['class']= M('Node')->where(array('id' =>$v['pid']))->getField('title');
		}	
		$this->assign('page', $listsarr['page']);
        $this->assign('Nodelist', $lists);
        $this->display();
	   }
	public function lists($where='',$pagesize=12,$order='id desc') {
		$Modulename = $this->getActionName();
		$M = M($Modulename);
		$modleid = $this->getmodleid($Modulename);//获取模型ID		
        import("ORG.Util.Page");
		$count = $M->where($where)->count(); //计算总数
		$p = new Page($count, $pagesize);
		$lists = $M->where($where)->limit($p->firstRow . ',' . $p->listRows)->order('ord asc,id desc')->select();
		
		$p->setConfig('header', '条');
		$p->setConfig('prev', "<");
		$p->setConfig('next', '>');
		$p->setConfig('first', '<<');
		$p->setConfig('last', '>>');
		$this->assign('tpl', $tpl = "index");//模板标识，用于判断是什么类型的模板以加载相应的JS和CSS
		$listsarr = array('lists'=> $lists,'page'=>$p->show());
		return $listsarr;
	}
	public function add(){
			$this->addata('Node');
		}
	

	public function insert(){
		$nodelist=M('Node')->field('id,title')->select();
		$this->assign("nodelist", $nodelist);
		$this->display();
	}
	public function edit() {
           $Node = M("Node");      
			 if(!$id) $this->error("编辑项不存在或已删除!");			                 
				  $this->assign('vo', $Node->getById($_GET['id'])); 
				  $this->assign("nodelist", $Node->field('id,title')->select());   
                  $this->display();     
                     	  
             }                
 
    public function update() {				
			$this->getupdate();
       }
	public function act(){
		$this->action();
	}
	public function setaccess(){
		if(!$_GET['role_id']) $this->error("该角色不存在或已被删除!");
	    $role_id =$_GET['role_id'];
		$node_ids = M('Access')->where(array('role_id'=>$role_id))->getField('node_id',true);
		$Nodelist = M('Node')->order('id desc')->select(); 	
		foreach ($Nodelist as $key => $value) {
			$Nodelist[$key]['checked']=in_array($value['id'],$node_ids) ? 1 :0;
			}	
        $this->assign('Nodelist', $Nodelist);
		$this->assign('role_id', $role_id);
       $this->display();
    }
	public function saveaccess(){ 
	      if(!$_POST['role_id']) $this->error("该角色不存在或已被删除!");
	      $role_id = $_POST['role_id']; 
		  $node_ids = $_POST['id']; 
		   //echo  $role_id;
		   //print_r($node_id);
		   $Access=M('Access');
		  // $condition['role_id']=$role_id;
		   $delets=$Access->where(array('role_id'=>$role_id))->delete(); //$delets是返回被删除的记录数
	
	      foreach($node_ids as $v){
				$data['role_id'] = $role_id;
				$data['node_id'] = $v;
				$vo = M('Node')->getById($v);
				$data['pid']=$vo['pid'];
				$data['level']=$vo['level'];
				$result[]=$Access->add($data);
			}
			$this->message($result);
			
    }  
}
?>