<?php
// 采集规则模型
class SpiderRuleModel extends Model {
	public function save() {
		$info = $_POST['info'];
		$info['rid'] = intval($info['rid']);
		if (!$info['rid']) {
			$info['rid'] = $this -> getId('rid');
			$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 ($info['basetype'] == 2) {
			if (!strstr($info['baseurl'], '://')) {
				$this -> error = L('SPIDER_BASE_URL_ERROR', 'spider');
				return false;
			}
		} else $info['baseurl'] = '';
		$info['passed'] = intval($info['passed']);
		$info['hitsmin'] = intval($info['hitsmin']);
		$info['hitsmax'] = intval($info['hitsmax']);
		$info['isdesc'] = intval($info['isdesc']);
		$info['unrepeat'] = intval($info['unrepeat']);
		$info['pic1_default'] = intval($info['pic1_default']);
		$info['sim_browser'] = intval($info['sim_browser']);
		$this -> create($info);
		$cacheName = 'rule_' . $info['rid'];
		if ($isAdd) {
			$rs = $this -> add();
			$cache = array('set' => $this -> find($info['rid']));
		} else {
			$rs = $this -> update();
			$cache = Cache :: read($cacheName, 'rule', 0, 'spider');
			$cache['set'] = $this -> find($info['rid']);
		}
		Cache :: write($cacheName, $cache, 'rule', 'spider');
		if ($rs) $rs = $info['rid'];
		return $rs;
	}
	// 删除
	public function del() {
		$selids = R('selids');
		if (!empty($selids)) {
			$where = strstr($selids, ',')?"rid IN ($selids)":"rid=$selids";
			if (!$this -> where($where) -> delete()) {
				$this -> error = L('OPERATE_FAIL');
				$rs = false;
			} else {
				$selids = explode(',', $selids);
				foreach($selids as $rid) {
					Cache :: del('rule_' . $rid, 'rule', 'spider');
				}
				$rs = true;
			}
		} else {
			$this -> error = L('NOPARAM');
			$rs = false;
		}
		return $rs;
	}
}
