<?php
// µ¥ÍøÒ³Ä£¿é¿ØÖÆÆ÷
class OnepageAction extends BaseAction {
	protected function _initializeMore() {
		$this -> obj = M('onepage');
		$this -> assign('config', C('', 0, 'onepage'));
	}
	public function defaults() {
		if (R('catid')) {
			$this -> action = 'category';
			$this -> category();
			exit;
		}
		$page = intval(R('page'));
		$block = intval(R('block'));
		$pageSize = 20;
		$where = array('catid' => 0, 'block' => $block);
		$this -> assign('data', $this -> obj -> getList($page, $pageSize, $where));
		$this -> assign('pages', $this -> obj -> pages());
		$this -> assign('block', $block);
		$this -> assign('orderurl', '?' . del_query_string('order|field'));
		$this -> assign('order', strtoupper(R('order')) == 'DESC'?'ASC':'DESC');
		$this -> display('onepage');
	}
	public function add() {
		if ($this -> isSubmit) {
			$rs = $this -> obj -> save();
			if ($rs) {
				$msg = L('OPERATE_SUCCESS') . '<iframe width=1 height=1 border=0 frameborder=0 src="index.php?c=html&a=update_by_id&m=onepage&id=' . $this -> loginInfo['userid'] . '&hash=' . $this -> loginInfo['hash'] . '&selids=' . $rs . '"></iframe>';
			} else $msg = L('OPERATE_FAIL');
			$this -> showMsg($msg, $this -> referer);
		}
		$this -> assign('data', array('id' => 0, 'block' => intval(R('block'))));
		$this -> assign('onepagetpl', $this -> obj -> listTpl());
		$this -> display('onepage');
	}
	public function edit() {
		if ($this -> isSubmit) {
			$rs = $this -> obj -> save();
			if ($rs) {
				$msg = L('OPERATE_SUCCESS') . '<iframe width=1 height=1 border=0 frameborder=0 src="index.php?c=html&a=update_by_id&m=onepage&id=' . $this -> loginInfo['userid'] . '&hash=' . $this -> loginInfo['hash'] . '&selids=' . $rs . '"></iframe>';
			} else $msg = L('OPERATE_FAIL');
			$this -> showMsg($msg, $this -> forward);
		}
		$id = intval(R('id'));
		$this -> assign('data', $this -> obj -> find($id));
		$attachmentObj = M('attachment', '', 'onepage');
		$attachments = $attachmentObj -> where(array('isattach' => 1, 'module' => 'onepage', 'contentid' => $id)) -> select();
		$cnt = count($attachments);
		if ($cnt < 6) {
			for($i = $cnt; $i < 6; $i++) {
				$attachments[$i] = '';
			}
		} else {
			$attachments[$cnt] = '';
			$attachments[$cnt + 1] = '';
		}
		$this -> assign('onepagetpl', $this -> obj -> listTpl());
		$this -> assign('attachments', $attachments);
		$this -> display('onepage');
	}
	public function manage() {
		$page = intval(R('page'));
		$pageSize = 20;
		$this -> assign('data', $this -> obj -> getList($page, $pageSize, 'catid!=0'));
		$this -> assign('pages', $this -> obj -> pages());
		$this -> assign('orderurl', '?' . del_query_string('order|field'));
		$this -> assign('order', strtoupper(R('order')) == 'DESC'?'ASC':'DESC');
		$this -> display('onepage');
	}
	public function category() {
		$catid = intval(R('catid'));
		if (!$catid) $this -> showMsg(L('NOPARAM'), $this -> referer);
		$category = Cache :: read('category', $catid);
		if (empty($category) or $category['type'] != 1) $this -> showMsg(L('NOPARAM'), $this -> referer);
		if ($this -> isSubmit) {
			if ($this -> obj -> save($category)) {
				$msg = L('OPERATE_SUCCESS') . '<iframe width=1 height=1 border=0 frameborder=0 src="index.php?c=html&a=category&id=' . $this -> loginInfo['userid'] . '&hash=' . $this -> loginInfo['hash'] . '&catids=' . $catid . '"></iframe>';
			} else $msg = L('OPERATE_FAIL');
			$this -> showMsg($msg, $this -> referer);
		}
		$data = $this -> obj -> where('catid=' . $catid) -> find();
		if (empty($data)) {
			$data = array('id' => 0, 'name' => '', 'title' => '', 'seotitle' => '', 'keywords' => '', 'description' => '', 'variable' => '', 'content' => '', 'tpl' => '', 'catid' => $catid);
		}
		$data['category'] = $category['name'];
		$this -> assign('data', $data);
		$this -> assign('onepagetpl', $this -> obj -> listTpl());
		$this -> display('onepage');
	}
	// É¾³ý
	public function del() {
		if ($this -> obj -> del()) $this -> showMsg(L('OPERATE_SUCCESS'), $this -> referer);
		else $this -> showMsg($this -> error);
	}
}
