<?php
class SoftModel extends DBModel {
	public function  Init(){
		
	}
	public function Add($POST){
		if(!$POST['softname']){
			$this->error = _('请填写软件名称!');
			return false;
		}
		if (!$POST['filename']){
			$this->error = _('请填写软件下载地址!');
			return false;
		}
		if ($_FILES['picture']['tmp_name']){
			$IO = new Easy_Filesystem();
			$POST['picture'] = $IO->setFile($_FILES['picture']);
		}
		$POST['addtime'] = time();
		$POST['admin'] = $this->_Response->getCookie('admin');
		return  $this->_DB->insert(VODCMS_SOFT, $POST);
	}
	public function getAll($where=null){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_SOFT)->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.$this->_Page->limit());
	}
	public function getRow($id){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_SOFT)->where(array('id'=>$id))->order('id DESC')->toString();
		return $this->_DB->fetRow($sql);
	}
	public function modify($POST){
		if(!$POST['softname']){
			$this->error = _('请填写软件名称!');
			return false;
		}
		if (!$POST['filename']){
			$this->error = _('请填写软件下载地址!');
			return false;
		}
		if ($POST['delfile']){
			$filename = strrev(base64_decode($POST['delfile']));
			if (is_file(ROOT.$filename)){
				$IO = new Easy_Filesystem();
				$IO->delete(ROOT.$filename);
				$POST['picture'] = '';
			}
		}
		if ($_FILES['picture']['tmp_name']){
			$IO = new Easy_Filesystem();
			$POST['picture'] = $IO->setFile($_FILES['picture']);
		}
		unset($POST['delfile']);
		$POST['admin'] = $this->_Response->getCookie('admin');
		return  $this->_DB->update(VODCMS_SOFT, $POST, array('id' => (int)$POST['id']));
	}
	public function delete($id){
		$this->_DB->delete(VODCMS_SOFT, array('id' => (int)$id));
	}

}