<?php
// ÊÕ²ØÄ£ÐÍ
class CollectModel extends Model {
	public function save() {
		$moduleid = intval(R('moduleid'));
		$module = get_module($moduleid);
		$id = intval(R('id'));
		$moduleObj = M($module['name']);
		$data = $moduleObj -> find($id);
		if (empty($data)) {
			$this -> error = L('COLLECT_OBJ_NOT_EXIST');
			return false;
		}
		if ($this -> where(array('userid' => get_cookie('userid'), 'module' => $moduleid, 'id' => $id)) -> find()) {
			$this -> error = L('ALREADY_COLLECT');
			return false;
		}
		$info = array('userid' => get_cookie('userid'), 'catid' => $data['catid'], 'module' => $moduleid, 'id' => $id);
		$rs = $this -> create($info) -> add();
		if ($rs) $moduleObj -> setFields(array('favorite' => 'favorite+1'), 'id', $id);
		return $data['favorite'] + 1;
	}
	// É¾³ý
	public function del() {
		$selids = R('selids');
		if (!empty($selids)) {
			$where = strstr($selids, ',')?"id IN ($selids)":"id=$selids";
			$data = $this -> where($where) -> select();
			if ($this -> where($where) -> delete()) {
				$moduleObj = array();
				foreach($data as $tmp) {
					if (empty($moduleObj[$tmp['module']])) {
						$module = get_module($tmp['module']);
						$moduleObj[$tmp['module']] = M($module['name']);
						$moduleObj -> setFields(array('favorite' => 'favorite-1'), 'id', $tmp['id']);
					}
				}
			} else {
				$this -> error = L('OPERATE_FAIL');
				$rs = false;
			}
		} else {
			$this -> error = L('NOPARAM');
			$rs = false;
		}
		return $rs;
	}
}
