<?php
/*
*/
class SmilModel extends DBModel{
	public function Init(){

	}
	public function Add($POST){
		if (empty($POST['title'])){
			$this->error = _('请填写广告名称!');
			return false;
		}
		if ($_FILES['filename']['tmp_name']){
			$IO = new Easy_Filesystem();
			$POST['filename'] = $IO->setFile($_FILES['filename']);
		}
		$this->_DB->insert(VODCMS_SMIL, $POST);
		return $this->_DB->lastInsertID();
	}
	public function modify($POST){
		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['filename']['tmp_name']){
			$IO = new Easy_Filesystem();
			$POST['filename'] = $IO->setFile($_FILES['filename']);
		}
		$id = $POST['id'];
		unset($POST['id']);
		systemlog::set('编辑了编号为'.$id.'的视频插播记录');
		return $this->_DB->update(VODCMS_SMIL, $POST, array('id'=> $id));
	}
	public function getAll($where = null){
		$select = $this->_DB->select();
		$select->from(VODCMS_SMIL);
		$select->where($where);
		$select->order('id DESC');
		$sql = $select->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($where = null){
		$select = $this->_DB->select();
		$select->from(VODCMS_SMIL);
		$select->where($where);
		$sql = $select->toString();
		return $this->_DB->fetrow($sql);
	}
	/*设置视频广告插播的状态*/
	public function locked($id){
		if ($id){
			$this->_DB->update(VODCMS_SMIL, 'locked = NOT(locked)', 'id='.(int)$id);
			return true;
		}else{
			$this->error = _('请传入需要隐藏或者启用记录的编号');
			return false;
		}
	}
	/*删除记录*/
	public function delete($id){
		if ($id){
			$row = $this->getRow(array('id'=>$id));
			if ($row['filename']){
				$IO = new  Easy_Filesystem();
				$IO->delete(ROOT.$row['filename']);
			}
			systemlog::set('删除了编号为'.$id.'的视频插播记录');
			return $this->_DB->Delete(VODCMS_SMIL, array('id'=>$id));
		}else{
			$this->error = _('请传入需要隐藏或者启用记录的编号');
			return false;
		}
	}
	/*播放器广告播放*/
	public function getPlay($id){
		if($id){
			$row = $this->getRow(array('id'=>$id));
			switch ($row['type']){
				case 'picture':
					$contents = '<a href="'.$row['url.'].'" target="_blank"><img src="'.$row['filename.'].'" width="'.$row['width.'].'" height="'.$row['height.'].'" /></a>';
				break;
				case 'video':
					$ext = strrchr($row['video'], '.');
					if (in_array($ext, array('.rmvb', '.rm')) == false){
					$contents = '<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="WindowsMediaPlayer1" width="100%" height="96%">
								<param name="URL" value="'.$row['video'].'">
								<param name="rate" value="1">
								<param name="balance" value="0">
								<param name="currentPosition" value="0">
								<param name="defaultFrame" value>
								<param name="playCount" value="1">
								<param name="autoStart" value="-1">
								<param name="currentMarker" value="0">
								<param name="invokeURLs" value="-1">
								<param name="baseURL" value>
								<param name="volume" value="50">
								<param name="mute" value="0">
								<param name="uiMode" value="none">
								<param name="stretchToFit" value="0">
								<param name="windowlessVideo" value="0">
								<param name="enabled" value="-1">
								<param name="enableContextMenu" value="-1">
								<param name="fullScreen" value="0">
								<param name="SAMIStyle" value>
								<param name="SAMILang" value>
								<param name="SAMIFilename" value>
								<param name="captioningID" value>
								<param name="enableErrorDialogs" value="0">
							</object>';
					}else{
						$contents = '<object id="RVOCX" classid="CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="100%" height="96%">
						<param name="SRC" value="'.$row['video'].'">
						<PARAM NAME="AUTOSTART" VALUE="-1">
						<param name="CONTROLS" value="ImageWindow">
						<param name="CONSOLE" value="cons">
						<PARAM NAME="MAINTAINASPECT" VALUE="1">
						<embed src="'.$row['video'].'" type="audio/x-pn-realaudio-plugin" MAINTAINASPECT=1 AUTOSTART=true width="550" height="400" controls="ImageWindow" console="cons">
						</embed> </object>';
					}
				break;
				case 'swf':
					$contents = '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="obj1" 
					codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" 
					border="0" width="100%" height="100%">
								<param name="movie" value="'.$row['filename'].'">
								<param name="quality" value="High">
								<embed src="'.$row['filename'].'" pluginspage="http://www.macromedia.com/go/getflashplayer" 
								type="application/x-shockwave-flash" name="obj1" width="500" height="500"></object>';
				break;
				case 'flv':
					$contents = '<object type="application/x-shockwave-flash" width="100%" height="96%" 
									wmode="transparent data="{basedir}images/flvplayer.swf?autoStart=true&file='.$row['video'].'">
									<param name="movie" value="{basedir}images/flvplayer.swf?autoStart=true&file='.$row['video'].'" />
									<param name="wmode" value="transparent" />
								</object>';
				break;
				case 'code':
					$contents = $row['code'];
				break;
				default:
					echo '错误的广告类型'.$row['type'];
				break;
				
			}
			$javascript = '<script language="javascript">
			function showTime(t){
				$("#showTime").val("广告剩余时间:"+t);
				if (t>1){
					setTimeout(function(){showTime(t-1)}, 1000);
					
				}
			}
			setTimeout(function(){top.location.href="'.$_SERVER['REQUEST_URI'].'showin/true/"}, '.$row['end'].'000);
			showTime('.$row['end'].');
			</script>';
			return '<div id="bill">'.$contents.'</div><div align="center" style="margin-top:0px;"><input type="submit" value="广告剩余时间" id="showTime" name="showTime" /></div>'.$javascript;
		}
	}
}
?>