<?php
// 内容模型
class LinkModel extends Model {
	public function save() {
		$info = $_POST['info'];
		$info['id'] = intval($info['id']);
		if (!$info['id']) {
			$info['id'] = $this -> getId('id');
			$info['addtime'] = TIME;
			$isAdd = true;
		} else $isAdd = false;
		// 名称有效性
		$info['name'] = trim($info['name']);
		if (empty($info['name'])) {
			$this -> error = L('NAME_EMPTY');
			return false;
		}
		if (empty($info['url'])) {
			$this -> error = L('URL_EMPTY');
			return false;
		}
		$info['elite'] = isset($info['elite'])?intval($info['elite']):0;
		$info['passed'] = isset($info['passed'])?intval($info['passed']):0;
		$this -> create($info);
		return $isAdd?$this -> add():$this -> update();
	}
	// 删除
	public function del() {
		$selids = R('selids');
		if (!empty($selids)) {
			$where = strstr($selids, ',')?"id IN ($selids)":"id=$selids";
			if ($this -> where($where) -> delete()) $rs = true;
			else {
				$this -> error = L('OPERATE_FAIL');
				$rs = false;
			}
		} else {
			$this -> error = L('NOPARAM');
			$rs = false;
		}
		return $rs;
	}
	public function cache() {
		$data = $this -> order(array('orderid ASC', 'elite DESC','linktype DESC','id DESC')) -> where('passed=1') -> select();
		Cache :: write('link', $data);
	}
}
