<?php
class FlinkModel extends DBModel{
	public function init(){
	
	}
	public function Add($POST){
		if (!$POST['sitename']){
			$this->error[] = _('请填写标题!');
		}
		if (!$POST['description']){
			$this->error[] = _('请填写站点描述!');
		}
		if(!filter_var($POST['url'], FILTER_VALIDATE_URL)){
			$this->error[] = _('请填写一个有效的URL地址!');
		}
		if ($_FILES['logo']['tmp_name']){
			$IO = new Easy_Filesystem();
			$POST['logo'] = $IO->setFile($_FILES['logo']);
		}
		if (count($this->error)>0){
			return false;
		}else{
			return $this->_DB->insert(VODCMS_FLINK, $POST);
		}
		
	}
	public function getAll($where=null){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_FLINK)->where($where)->order('id DESC')->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printpage = $this->_Page->PrintPage();
		return $this->_DB->fetAll($sql);
	}
	public function getRow($id){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_FLINK)->where(array('id'=> (int)$id))->order('id DESC')->toString();
		return $this->_DB->fetRow($sql);
	}
	public function modify($POST){
		if (!$POST['sitename']){
			$this->error[] = _('请填写标题!');
		}
		if (!$POST['description']){
			$this->error[] = _('请填写站点描述!');
		}
		if(!filter_var($POST['url'], FILTER_VALIDATE_URL)){
			$this->error[] = _('请填写一个有效的URL地址!');
		}
		if ($POST['delfile']){
			$filename = strrev(base64_decode($POST['delfile']));
			if (is_file(ROOT.$filename)){
				$IO = new Easy_Filesystem();
				$IO->delete(ROOT.$filename);
				$POST['logo'] = '';
			}
		}
		if ($_FILES['logo']['tmp_name']){
			$IO = new Easy_Filesystem();
			$POST['logo'] = $IO->setFile($_FILES['logo']);
		}
		unset($POST['delfile']);
		if (count($this->error)>0){
			return false;
		}else{
			return $this->_DB->update(VODCMS_FLINK, $POST, array('id'=> $POST['id']));
		}
		
	}
	public function locked($data, $id){
		if (is_array($id)){
			$id = implode(',', array_map('intval', $id));
		}
		return $this->_DB->update(VODCMS_FLINK, $data, 'id in ('.(int)$id.')');
	}
	public function delete($id){
		if (is_array($id)){
			$id = implode(',', array_map('intval', $id));
		}
		return $this->_DB->delete(VODCMS_FLINK,'id in ('.(int)$id.')');
	}
}?>