<?php
// 后台会员组控制器
class GroupAction extends BaseAction {
	public function defaults() {
		$groupObj = M('group');
		$array = $groupObj -> getList('', 0, '', array('orderid', 'groupid'));
		$data = array();
		if (is_array($array)) {
			foreach($array as $k => $v) {
				$v['membernum'] = $groupObj -> table('member') -> where('groupid=' . $v['groupid']) -> count('*');
				$data[] = $v;
			}
		}
		$this -> assign('data', $data);
		$this -> display('group');
	}
	public function add() {
		if ($this -> isSubmit) {
			$groupObj = M('group');
			$groupObj -> create(R('info', 'post'));
			$groupObj -> groupid = $groupObj -> getId('groupid');
			if ($groupObj -> add()) {
				$this -> showMsg(L('OPERATE_SUCCESS'));
			} else {
				$msg = $groupObj -> error ? $groupObj -> error : L('OPERATE_FAIL');
				$this -> showMsg($msg);
			}
		}
		$data = array('groupid'=>0,'name'=>'','description'=>'','allowpost'=>1,'allowsearch'=>1,'maxmessage'=>100);
		$this -> assign('data', $data);
		$this -> display('group');
	}
	public function edit() {
		$groupObj = M('group');
		if ($this -> isSubmit) {
			if ($groupObj -> create(R('info', 'post')) -> update()) {
				$this -> showMsg(L('OPERATE_SUCCESS'), $this -> forward);
			} else {
				$msg = $groupObj -> error ? $groupObj -> error : L('OPERATE_FAIL');
				$this -> showMsg($msg);
			}
		}
		$this -> assign('data', $groupObj -> find(intval(R('groupid'))));
		$this -> display('group');
	}
	public function del() {
		$groupObj = M('group');
		$msg = $groupObj -> delete(intval(R('groupid'))) ? L('OPERATE_SUCCESS') : L('OPERATE_FAIL');
		$this -> showMsg($msg);
	}
	public function order() {
		$groupObj = M('group');
		$selarray = explode(',', $_POST['selids']);
		foreach($selarray as $groupid) {
			$groupObj -> where("groupid=$groupid") -> create(array('orderid' => $_POST['orderid'][$groupid])) -> update();
		}
		$this -> showMsg(L('OPERATE_SUCCESS'), $_SERVER['HTTP_REFERER']);
	}
	// 析构函数
	public function __destruct() {
		M('group') -> cache();
	}
}
