<?php
// 地区模块控制器
class AreaAction extends BaseAction {
	protected function _initializeMore() {
		$this -> obj = M('area');
	}
	public function defaults() {
		$page = intval(R('page'));
		$pid = intval(R('pid'));
		$pageSize = 20;
		$this -> assign('data', $this -> obj -> getList($page, $pageSize, 'pid=' . $pid, 'orderid ASC, areaid ASC'));
		$this -> assign('pages', $this -> obj -> pages());
		$this -> assign('jmpurl', '?' . del_query_string('page'));
		$this -> assign('orderurl', '?' . del_query_string('order|field'));
		$this -> assign('order', strtoupper(R('order'))=='DESC'?'ASC':'DESC');
		$this -> assign('area', Cache :: read('area'));
		$this -> assign('pid', $pid);
		$this -> display('area');
	}
	public function add() {
		if ($this -> isSubmit) {
			$msg = $this -> obj -> save()?L('OPERATE_SUCCESS'):L('OPERATE_FAIL');
			$this -> showMsg($msg, '?c=area&a=add&pid=' . intval($_POST['info']['pid']));
		}
		$this -> assign('data', array('pid' => intval(R('pid')), 'areaid' => 0));
		$this -> display('area');
	}
	public function edit() {
		if ($this -> isSubmit) {
			if($this -> obj -> save())$this -> showMsg(L('OPERATE_SUCCESS'), $this -> forward);
			else $this -> showMsg($this -> obj -> error, $this -> referer);
		}
		$areaid = intval(R('areaid'));
		$this -> assign('data', $this -> obj -> find($areaid));
		$this -> display('area');
	}
	public function order() {
		$selarray = explode(',', $_POST['selids']);
		foreach($selarray as $areaid) {
			$this -> obj -> where('areaid=' . $areaid) -> create(array('orderid' => $_POST['orderid'][$areaid])) -> update();
		}
		$this -> showMsg(L('OPERATE_SUCCESS'), $_SERVER['HTTP_REFERER']);
	}
	public function check() {
		$sign = R('sign');
		$areaid = intval(R('areaid'));
		if ($sign) {
			$where = array('sign' => $sign);
			if ($areaid) $where[] = "areaid!=$areaid";
			if ($this -> obj -> where($where) -> find()) exit('1');
		}
	}
	// 删除
	public function del() {
		if($this -> obj -> del()) $this -> showMsg(L('OPERATE_SUCCESS'), $this -> referer);
		else $this -> showMsg($this -> obj -> error);
	}
	// 析构函数
	public function __destruct() {
		$this -> obj -> cache();
	}
}
