<?php
// 后台会员组控制器
class RoleAction extends BaseAction {
	public function defaults() {
		$this -> assign('data', M('role') -> getList());
		$this -> display('role');
	}
	public function add() {
		if ($this -> isSubmit) {
			$roleObj = M('role');
			$roleObj -> create(R('info', 'post'));
			$roleObj -> roleid = $roleObj -> getId('roleid');
			if ($roleObj -> add()) {
				$this -> showMsg(L('OPERATE_SUCCESS'));
			} else {
				$msg = $roleObj -> error ? $roleObj -> error : L('OPERATE_FAIL');
				$this -> showMsg($msg);
			}
		}
		$data = array('roleid'=>0,'name'=>'','description'=>'','uncheck'=>1);
		$this -> assign('data', $data);
		$this -> display('role');
	}
	public function edit() {
		$roleObj = M('role');
		if ($this -> isSubmit) {
			if ($roleObj -> create(R('info', 'post')) -> update()) {
				$this -> showMsg(L('OPERATE_SUCCESS'), $this -> forward);
			} else {
				$msg = $roleObj -> error ? $roleObj -> error : L('OPERATE_FAIL');
				$this -> showMsg($msg);
			}
		}
		$this -> assign('data', $roleObj -> find(intval(R('roleid'))));
		$this -> display('role');
	}
	public function del() {
		$roleObj = M('role');
		$msg = $roleObj -> delete(intval(R('roleid'))) ? L('OPERATE_SUCCESS') : L('OPERATE_FAIL');
		$this -> showMsg($msg);
	}
	public function order() {
		$roleObj = M('role');
		$selarray = explode(',', R('selids'));
		foreach($selarray as $roleid) {
			$roleObj -> where("roleid=$roleid") -> create(array('orderid' => $_POST['orderid'][$roleid])) -> update();
		}
		$this -> showMsg(L('OPERATE_SUCCESS'), $_SERVER['HTTP_REFERER']);
	}
	// 析构函数
	public function __destruct() {
		M('role') -> cache();
	}
}
