<?php
/*
秀影EasyFramework框架
qq:24498936
msn:jdzcn_net@hotmail.com
website:http://www.vodcms.com
系统后台管理菜单控制模型
*/
class ModuleModel extends DBModel {
	protected $_name = VODCMS_MODULE;
	public function Init(){
		
	}
	public function addModule($array){
		if (is_array($array)){
			$select = $this->_DB->select();
			$select->from($this->_name,'sort')->where('parentid='.$array['parentid'])->order('sort DESC')->limit(1);
			$sql = $select->toString();
			$data = $this->_DB->fetRow($sql);
			if (!$array['sort']){
				$array['sort'] = $data['sort']+1;
			}
			unset($data, $sql);
			if ($this->Exists($array['name']) ===true){
				$this->error = _('菜单名已经存在!请更换!');
				return false;
			}else{
				return $this->_DB->insert($this->_name, $array);
			}	
		}else{
			$this->error = _('程序内部错误:调用方法参数错误!');
			return false;
		}
	}
	/*
	列出全部的管理菜单
	$where 为可选项.接受参数为SQl条件字符或者数组
	return array();
	*/
	public function listModule($where = null){
		$select = $this->_DB->select();
		if ($where){
			return $this->_DB->fetAll($select->from($this->_name)->where($where)->order('sort,id'));
		}else{
			return $this->_DB->fetAll($select->from($this->_name)->order('sort,id'));
		}
	}
	/*
	$name 检测菜单名是否已经存在!
	return bool
	*/
	public function Exists($name){
		if ($this->_DB->fetRow($this->_DB->select()->from($this->_name)->where("`name`= '$name'")->limit(1))){
			return true;	//存在
		}else{
			return false;
		}
	}
	public function CreateOption($id=0, $selected=0){
		$select = $this->_DB->select();
		$sql = $select->from($this->_name)->where('parentid='.$id)->order('sort,id');
		//echo $sql;
		$data = $this->_DB->fetAll($sql);
		unset($option);
		if (is_array($data))
		foreach($data as $row){
			$option.= '<option value="'.$row['id'].'" '.($row['id'] == $selected) ? 'selected' : ''.'>'.$row['name'].'</option>'.$this->CreateOption($row['id']);
		}
		return $option;
	}
	public function getAllModule(){
		$select = $this->_DB->select();
		$sql = $select->from($this->_name)->order('sort ASC');
		//echo $sql;
		$result = $this->_DB->fetAll($sql);
		foreach($result as $val){
			if($val['parentid'] == 0){
				$data[$val['id']]['self'] = $val;
			}else{
				$data[$val['parentid']]['nodes'][] = $val;
			}
		}
		unset($result);
		return $data;
	}
	public function getRightModule(){
		$module = $this->_Response->getParam('controller');
		$select = $this->_DB->select();
		$select->from($this->_name.' AS a','DISTINCT a.parentid');
		$select->join($this->_name.' AS b', 'a.parentid=b.parentid', 'b.*');
		$select->where("b.islink = 'true' AND a.url like '".$module."%'");
		$sql = $select->toString();
		//echo $sql;
		return $this->_DB->fetAll($sql);
	}
	/*获取一行指定条件的数据记录*/
	public function getModule($where = null){
		if (is_null($where)===false){
			$sql = $this->_DB->select()->from($this->_name)->where($where)->limit(1);
			return $this->_DB->fetRow($sql);
		}
	}
	/*
	修改模块信息
	@param array
	*/
	public function modifyModule($data, $where){
		if (is_array($data)){
			if (empty($data['name'])){
				$this->error = _('模块名不能为空!');
				return false;
			}elseif ($data['id'] == $data['parentid']){
				$this->error = _('不能选择自身为父分类!');
				return false;
			}else{
				return $this->_DB->update($this->_name, $data, $where);
			}
		}else{
			$this->error = _('第一个传入参数必须为数组!');
			return false;
		}
	}
	/*删除一条模块信息*/
	public function delete($id){
		$row = $this->getModule('parentid='.$id);
		if ($row['id']){
			$this->error = _('必须删除子模块后才能删除父模块!');
			return false;
		}else{
			return $this->_DB->delete($this->_name, 'id='.(int)$id);
		}
	}
	public function sort($sort){
		$i=0;
		foreach($sort as $id){
			$i++;
			$this->_DB->update($this->_name, array('sort'=> $i), 'id='.(int)$id);
		}
	}
}