<?php
/*
秀影广告系统模型
秀影6.0
2008-11-1
*/
class BillModel extends DBModel{
	public function Init(){
		$this->_IO = new Easy_Filesystem(ROOT);
	}
	public function Add($POST){
		$POST['admin'] = $this->_Response->getCookie('admin');
		if (empty($POST['title'])){
			$this->error = _('请填写广告的说明');
			return false;
		}
		if ($_FILES['file']['tmp_name']){
			$POST['filename'] = $this->_IO->setFile($_FILES['file']);
			if($this->_IO->error){
				$this->error = $this->_IO->error;
				return false;
			}
		}
		$this->_DB->insert(VODCMS_BILL, $POST);
		return $this->_DB->lastInsertID();
	}
	public function modify($POST){
		$POST['admin'] = $this->_Response->getCookie('admin');
		if (empty($POST['title'])){
			$this->error = _('请填写广告的说明');
			return false;
		}
		if ($POST['delfile']){
			@unlink(ROOT.strrev(base64_decode($POST['delfile'])));
			$POST['filename'] = '';
		}
		unset($POST['delfile']);
		if ($_FILES['file']['tmp_name']){
			$POST['filename'] = $this->_IO->setFile($_FILES['file']);
			if($this->_IO->error){
				$this->error = $this->_IO->error;
				return false;
			}
		}
		$id = $POST['id'];
		unset($POST['id']);
		return $this->_DB->update(VODCMS_BILL, $POST, array('id'=> $id));
	}
	public function getAll($where = null){
		$select = $this->_DB->select();
		$select->from(VODCMS_BILL);
		$select->where($where);
		$select->order('id DESC');
		$sql = $select->toString();
		$total = $this->_DB->select();
		$this->_Page->set($total);
		$this->printPage = $this->_Page->PrintPage();
		return $this->_DB->fetAll($sql);
	}
	public function getRow($where){
		$select = $this->_DB->select();
		$select->from(VODCMS_BILL);
		$select->where($where);
		$select->order('id DESC');
		$sql = $select->toString();
		return $this->_DB->fetRow($sql);
	}
	public function delete($id){
		$this->_DB->delete(VODCMS_BILL, array('id'=>$id));
	}
	public function locked($id){
		$this->_DB->update(VODCMS_BILL, 'locked =NOT(locked)',  array('id'=>$id));
	}
	public function getByTag($tag){
		if ($tag){
			$config = $GLOBALS['config'];
			$row = $this->getRow(array('locked'=>0,'tag'=> $tag));
			switch($row['type']){
				case 'picture':
					if ($row['filename']){
						$content = 'document.write(\'<a href="'.$row['url'].'" target="_blank"><img src="'.$config['basedir'].$row['filename'].'" width="'.$row['width'].'" height="'.$row['height'].'" border="0"></a>\');';
					}
				break;
				case 'swf':
					if ($row['filename']){
						$content = 'document.write(\'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'.$row['width'].'" height="'.$row['height'].'"><param name="movie" value="'.$config['basedir'].$row['filename'].'" /><param name="quality" value="high" /><embed src="'.$config['basedir'].$row['filename'].'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'.$row['width'].'" height="'.$row['height'].'"></embed></object>\');';
					}
				break;
				case 'text':
					$content = 'document.write(\'<a href="'.$row['url'].'" target="_blank">'.$row['title'].'</a>\')';
				break;
				case 'code':
					$content = $row['code'];
				break;
				case 'html';
					$row['code'] = preg_replace('/<script(.+?)>/is', '', str_replace("'", "\'", $row['code']));
					$row['code'] = preg_replace('/<\/script>/is', '', $row['code']);
					$row['code'] = preg_replace('/([\n\t]+)/is', '', $row['code']);
					$content = 'document.write(\''.$row['code'].'\');';
					//echo $content;
			}
			unset($row);
			return $content;
		}else{
			return false;
		}
	}
}?>