<?php
// 友情链接模块控制器
class LinkAction extends BaseAction {
	protected function _initializeMore() {
		$this -> obj = M('link');
	}
	public function defaults() {
		$page = intval(R('page'));
		$pageSize = 20;
		$this -> assign('data', $this -> obj -> getList($page, $pageSize, '', array('orderid ASC', 'elite DESC', 'linktype DESC', 'id DESC')));
		$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('link');
	}
	public function add() {
		if ($this -> isSubmit) {
			$msg = $this -> obj -> save()?L('OPERATE_SUCCESS'):L('OPERATE_FAIL');
			$this -> showMsg($msg, '?c=link&a=add&catid=' . intval($_POST['info']['catid']));
		}
		$this -> assign('data', array('id' => 0, 'passed' => 1, 'catid' => intval(R('catid'))));
		$this -> display('link');
	}
	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('link');
	}
	// 审核
	public function check() {
		if ($this -> obj -> setFields(array('passed' => 1))) $this -> showMsg(L('OPERATE_SUCCESS'), $this -> referer);
		else $this -> showMsg($this -> error);
	}
	// 推荐(取消推荐)
	public function elite() {
		if ($this -> obj -> setFields(array('elite' => intval(R('elite'))))) $this -> showMsg(L('OPERATE_SUCCESS'), $this -> referer);
		else $this -> showMsg($this -> error);
	}
	public function order() {
		$selarray = explode(',', R('selids'));
		if (is_array($selarray)) {
			$orderid = R('orderid');
			$num = 0;
			foreach($selarray as $id) {
				$tmp = ($orderid[$id] < 1 or $orderid[$id] > 255)?255:intval($orderid[$id]);
				if ($this -> obj -> setFields(array('orderid' => $tmp), 'id', $id)) $num++;
			}
			if ($num) $this -> showMsg(L('OPERATE_SUCCESS'), $this -> referer);
			else $this -> showMsg($this -> error);
		} 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();
	}
}
