<?php
// 广告模块控制器
class AdAction extends BaseAction {
	protected function _initializeMore() {
		$this -> obj = M('ad');
		//$this -> assign('config', C('', 0, 'ad'));
	}
	public function defaults() {
		$page = intval(R('page'));
		$pageSize = 20;
		if (R('field')) {
			$order = R('order')?strtoupper(R('order')):'DESC';
			$order = R('field') . ' ' . $order;
		} else $order = '';
		$this -> assign('data', $this -> obj -> getList($page, $pageSize, '', $order));
		$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 -> display('ad');
	}
	public function add() {
		if ($this -> isSubmit) {
			$msg = $this -> obj -> save()?L('OPERATE_SUCCESS'):L('OPERATE_FAIL');
			$this -> showMsg($msg, '?c=ad&a=add&catid=' . intval($_POST['info']['catid']));
		}
		$this -> assign('data', array('id' => 0, 'adtype' => 'code', 'usetype' => 1));
		$this -> display('ad');
	}
	public function edit() {
		if ($this -> isSubmit) {
			$msg = $this -> obj -> save()?L('OPERATE_SUCCESS'):L('OPERATE_FAIL');
			$this -> showMsg($msg, $this -> forward);
		}
		$id = intval(R('id'));
		$this -> assign('data', $this -> obj -> find($id));
		$this -> display('ad');
	}
	public function check() {
		$name = R('name');
		$id = R('id');
		if ($name) {
			$where = array('name' => $name);
			if ($id > 0) $where[] = "id!=$id";
			if ($this -> obj -> where($where) -> count()) exit('1');
		}
	}
	public function show() {
		if (R('name')) {
			$this -> assign('name', R('name'));
			$this -> display('ad');
		} else $this -> showMsg(L('NOPARAM'));
	}
	// 删除
	public function del() {
		if($this -> obj -> del()) $this -> showMsg(L('OPERATE_SUCCESS'), $this -> referer);
		else $this -> showMsg($this -> error);
	}
	// 析构函数
	public function __destruct() {
		$this -> obj -> cache();
	}
}
