<?php
/*
秀影6.0影片管理模型
QQ:24498936
*/
class MovieModel extends DBModel {
	protected $_name1;
	protected $_name2;
	protected $_name3;
	protected $_name4;
	protected $_name5;
	protected $_name6;
	public function Init(){
	}
	/*
	影片的地区option
	$param string 默认或者选中的地区名
	*/
	public function AreaOption($default=0){
		$this->_XML->set(ROOT.'Config/movie.ini', 'area');
		$data = '';
		foreach ($this->_XML->data as $area){
			if ($default == $area){
				$data.= '<option value="'.$area.'" selected="selected" >&nbsp;'.$area.'</option>';
			}else{
				$data.= '<option value="'.$area.'" >&nbsp;'.$area.'</option>';
			}
		}
		return $data;
	}
	/*
	生成播放器option菜单
	@param array 为默认播放器数组
	*/
	public function PlayerOption($default=0){
		$player = new PlayerModel();
		$all = $player->getAll();
		$data = '';
		foreach ($all as $row){
			if ($default == $row['tag']){
				$data.= '<option value="'.$row['tag'].'" selected="selected" >&nbsp;'.$row['name'].'</option>';
			}else{
				$data.= '<option value="'.$row['tag'].'" >&nbsp;'.$row['name'].'</option>';
			}
		}
		unset($all);
		return $data;
	}
	/*
	生成语言checkbox菜单
	@param array 为默认的参数数组
	*/
	public function getLanguage($default = array()){
		$this->_XML->set(ROOT.'Config/movie.ini', 'language');
		$data = '';
		foreach ($this->_XML->data as $lng){
			if (in_array($lng, $default)){
				$data.= '&nbsp;<input type="checkbox" class="checkbox" name="language[]" value="'.$lng.'" checked="checked" >&nbsp;'.$lng;
			}else{
				$data.= '&nbsp;<input type="checkbox" class="checkbox" name="language[]" value="'.$lng.'" >&nbsp;'.$lng.'';
			}
		}
		return $data;
	}
	/*
	影片服务器的option
	@param array 选中服务器的值
	*/
	public function ServerOption($id=0){
		$server = new ServerModel();
		$data = '';
		$all = $server->getAll('locked=0');
		foreach ($all as $row){
			if ($id == $row['self']['sid']){
				$data.= '<option value="'.$row['self']['sid'].'" selected="selected" >'.$row['self']['sid'].'号服务器|'.$row['self']['name'].'|'.$row['self']['helixserver'].'</option>';
			}else{
				$data.= '<option value="'.$row['self']['sid'].'" >'.$row['self']['sid'].'号服务器|'.$row['self']['name'].'|'.$row['self']['helixserver'].'</option>';
			}
			if (is_array($row['nodes'])){
				foreach ($row['nodes'] as $rs){
					if ($id == $rs['sid']){
						$data.= '<option value="'.$rs['sid'].'" selected="selected" >'.$rs['sid'].'号服务器|'.$rs['name'].'|'.$rs['helixserver'].'</option>';
					}else{
						$data.= '<option value="'.$rs['sid'].'" >&nbsp;&nbsp;'.$rs['sid'].'号服务器|'.$rs['name'].'|'.$rs['helixserver'].'</option>';
					}
				}
			}
		}
		unset($all);
		return $data;
	}
	/*
	推荐位置option生成
	*/
	public function getRecmdOption($id = array()){
		$data = '';
		$this->_XML->set(ROOT.'Config/movie.ini', 'recommend');
		foreach ($this->_XML->data as $val=>$text){
			if (in_array($val, $id)){
				$data.= '&nbsp;<input type="checkbox" class="checkbox" value="'.$val.'" checked="checked" name="recmd[]" >&nbsp;'.$text;
			}else{
				$data.= '&nbsp;<input type="checkbox" class="checkbox" value="'.$val.'" name="recmd[]" >&nbsp;'.$text;
			}
		}
		return $data;
	}
	/*
	@param array $POST 
	*/
	public function add($POST, $action='add'){
		$this->error = null;
		if (empty($POST['title'])){
			$this->error[] = _('请填写影片名称!');
		}
		if (empty($POST['category'])){
			$this->error[] = _('请选择影片分类!');
		}
		if (empty($POST['player'])){
			$this->error[] = _('请选择影片播放器!');
		}
		if (empty($POST['sid'])){
			$this->error[] = _('请选择影片所在服务器!');
		}
		if (empty($POST['url'])){
			$this->error[] = _('请选择设定影片的路径!');
		}
		if (empty($POST['content'])){
			$this->error[] = _('请填写影片的介绍!');
		}
		if (is_array($this->error)){
			return false;
		}
		if (is_array($POST['category']) === false){
			$POST['category'] = explode(',', $POST['category']);
		}
		if (is_array($POST['player']) === true){
			$POST['player'] = implode(',', $POST['player']);
		}
		if (is_array($POST['sid']) === true){
			$POST['sid'] = implode(',', $POST['sid']);
		}
		if (is_array($POST['language']) === true){
			$POST['language'] = implode(',', $POST['language']);
		}
		if ($POST['urltype'] == 2){
			$POST['url'] = explode("\n", $POST['url']);
		}
		$POST['addtime'] = time();
		$POST['modifytime'] = time();
		$POST['author'] = 'admin';
		$recommend = $POST['recmd'];
		$category = $POST['category'];
		if (is_array($POST['category'])){
			$POST['category'] = implode(',', $POST['category']);
		}
		$param = $POST['param'];
		$url = $POST['url'];
		$sort = $POST['sort'];
		//print_r($sort);
		$alt = $POST['alt'];
		$urlid = $POST['urlid'];
		$content = $POST['content'];
		$POST['urlcount'] = count($url);
		$POST['letter'] = $this->getLetter($POST['title']);
		unset($POST['recmd'], $POST['url'],
		 $POST['sort'], $POST['alt'],$POST['param'], $POST['ext'], 
		 $POST['endnum'], $POST['startnum'], $POST['urlname'], 
		 $POST['urlid'], $POST['urltype']);
		Easy_Cache::clear(); //清理缓存
		if ($action == 'add'){	//修改模式
			$this->_DB->insert(VODCMS_MOVIE, $POST);
			$movid = $this->_DB->lastInsertId();
			if ($movid){
				$this->addCategory($movid, $category);
				$this->AddRecmd($movid, $recommend);
				$this->AddUrl($movid, $url, $alt, $sort, $param);
				//$this->setFile($movid, $file);
				//$this->setContent($movid, $content);
			}
			$this->setMidTable($movid);	//更新中间表
			return $movid;
		 }elseif ($action == 'modify'){
		 	$movid = (int)$POST['movid'];
		 	$this->_DB->update(VODCMS_MOVIE, $POST, 'movid='.(int)$POST['movid']);
		 	$this->addCategory($movid, $category);
		 	$this->AddRecmd($movid, $recommend);
		 	$this->modifyUrl($movid, $url, $alt, $sort, $urlid, $param);
		 	//$this->setFile($movid, $file);
			//$this->setContent($movid, $content);
			$this->setMidTable($movid); //更新中间表
		 	return true;
		 }
	}
	/*
	public function setFile($movid, $file){
		if ($movid && is_array($file)){
			$this->_DB->delete(VODCMS_FILE, 'movid='.$movid);
			$this->_DB->insert(VODCMS_FILE, array('movid'=>$movid, 'otherpic' => $file['otherpic'], 'pic'=> $file['pic'], 'bt'=> $file['bt']));
			//print_r(array_keys($file));
			//echo $this->_DB->sql;
			return true;
		}else{
			$this->error = _('参数有误!');
			return false;
		}
	}*/
	public function  AddCategory($movid, $category){
		if (is_array($category) === false){
			$category = explode(',', $category);
		}
		$this->_DB->delete(VODCMS_MCLASSID, 'movid='.$movid);
		foreach ($category as $val){
		 	$this->_DB->insert(VODCMS_MCLASSID, array('category'=>$val, 'movid'=> $movid) );
		}
	}
	/*
	获取分类信息
	*/
	public function getCategory($movid){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MCLASSID)->where('movid='.(int)$movid)->toString();
		$data = $this->_DB->fetAll($sql);
		$array = array();
		foreach ($data as $row){
			$array[] = $row['category'];
		}
		unset($data);
		return $array;
	}
	/*
	修改影片路径
	*/
	function modifyurl($movid, $url, $alt ,$sort, $urlid, $param=null){
		$this->_DB->delete(VODCMS_URL, 'movid='.$movid);
		for($i=0; $i<count($url); $i++){
			$select = $this->_DB->select();
			$sql = $select->from(VODCMS_URL)->where('urlid='.(int)$urlid[$i])->toString();
			//echo $sql;
			if ($this->_DB->fetRow($sql)){
				$this->_DB->update(VODCMS_URL,
					array(
					'movid'=>$movid, 
					'url'=> $url[$i], 
					'title'=> $alt[$i], 
					'sort'=> $sort[$i], 
					'params'=> $param[$i]),
					array('urlid' => $urlid[$i])
				);
				//echo $this->_DB->sql;
			}else{
				$this->_DB->insert(VODCMS_URL, array('movid'=>$movid, 'url'=> $url[$i], 'title'=> $alt[$i], 'sort'=> $sort[$i], 
				'params'=> $param[$i]));
				//echo $this->_DB->sql;
			}
		}
	}
	/*
	添加影片路径
	*/
	public function AddUrl($movid, $url, $alt , $sort,  $param=null){
		
		for($i=0; $i<count($url); $i++){
			$this->_DB->insert(VODCMS_URL, 
			array('movid'=>$movid, 'url'=> $url[$i], 'title'=> $alt[$i], 'sort'=> $sort[$i], 'params'=> $param[$i]));
		}
	}
	/*
	根据影片编号获取影片所有URL
	*/
	public function getUrl($movid){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_URL)->where('movid='.$movid)->order('sort,urlid ASC')->toString();
		//echo $sql;
		return $this->_DB->FetAll($sql);
		
	}
	/*
	添加影片推荐
	*/
	public function AddRecmd($movid, $recommend){
		if (is_array($recommend) === false){
			$recommend = explode(',', $recommend);
		}
		$this->_DB->delete(VODCMS_RECMD, 'movid='.$movid);
		foreach ($recommend as $val){
		 	$this->_DB->insert(VODCMS_RECMD, array('recmd'=>$val, 'movid'=> $movid) );
		}
	}
	/*
	添加影片介绍
	public function setContent($movid, $content){
		if ($movid && $content){
			$select = $this->_DB->select();
			$sql = $select->from($this->_name6)->where('movid='.(int)$movid)->toString();
			$row = $this->_DB->fetRow($sql);
			if ($row['movid']){
				$this->_DB->update($this->_name6, array('content'=> $content), array('movid'=> (int)$movid));
			}else{
				$this->_DB->insert($this->_name6, array('content' => $content, 'movid' => (int)$movid));
			}
			//echo $this->_DB->sql;
			unset($sql, $row);
			return true;
		}else{
			$this->error = _('参数传递有误!');
			return false;
		}
	}
	*/
	/*
	获取用户推荐数据
	返回数组
	*/
	public function getRecmd($movid){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_RECMD)->where('movid='.$movid)->order('movid DESC')->toString();
		$data = $this->_DB->fetAll($sql);
		//echo $sql;
		$array = array();
		foreach ($data as $row){
			$array[] = $row['recmd'];
		}
		unset($data);
		return $array;
	}
	//*/*/
	public function getAll($where=null){
		unset($where['controller'], $where['action']);
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MOVIE)->where($where)->order('movid DESC')->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printPage = $this->_Page->PrintPage();
		//echo $sql;
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*
	search
	影片搜索
	*/
	public function Search($POST, $limitpage=true, $pagesize=0){
		$where = '1=1';
		$cate = new  CategoryModel();
		$POST['keyword'] = urldecode($POST['keyword']);
		$POST['area'] = urldecode($POST['area']);
		if ($POST['locked'] !='all'){
			$where.= ' AND locked='.(int)$POST['locked'];
		}
		if ($POST['area']){
			$where.= ' AND area=\''.$POST['area'].'\'';
		}
		if ($POST['group'] != 'all' && empty($POST['group'])===false){
			$where.= ' AND lookgid='.(int)$POST['group'];
		}
		if ($POST['category']){
			$category = $cate->getChilds($POST['category']);
			$str = implode(',', array_map('intval', $category));
			$where .= ' AND movid in (select movid from '.VODCMS_MCLASSID.' WHERE category in ('.$str.'))';
		}
		if ($POST['keyword'] && $POST['type']){
			if ($POST['checkbox'] == 1){
				$where.=  " AND `".$POST['type']."` LIKE '".urldecode($POST['keyword'])."%'";
			}else{
				$where.= " AND ".$POST['type']." LIKE '%".urldecode($POST['keyword'])."%'" ;
			}
		}
		if ($POST['sid']){
			$where.= ' AND FIND_IN_SET('.intval($POST['sid']).', `sid`)';
		}
		if ($POST['player']){
			$where.= ' AND FIND_IN_SET(\''.(int)$POST['player'].'\', `player`)';
		}
		$select = $this->_DB->select();
		$select->from(VODCMS_MOVIE, '*');
		$select->where($where);
		$select->order('movid DESC');
		if ($pagesize){
			$select->limit($pagesize);
		}
		$sql = $select->toString();
		if ($limitpage===true){
			$total = $this->_DB->getCount($sql);
			$this->_Page->set($total);
			$this->printPage = $this->_Page->PrintPage();
			return $this->_DB->fetAll($sql.$this->_Page->limit());
		}else{
			return $this->_DB->fetAll($sql);
		}
	}
	/*
	return one row
	获取一行记录。只接受询问ID参数
	*/
	public function getRow($id){
		$select = $this->_DB->select();
		$select->from(VODCMS_MOVIE.' AS a' , 'a.*');
		$select->where('a.movid='.(int)$id);
		$select->order('a.movid DESC');
		$sql= $select->toString();
		$data = $this->_DB->fetRow($sql);
		$data['category'] = $this->getCategory($id);
		$data['recmd'] = $this->getRecmd($id);
		$data['url'] = $this->getUrl($id);
		//print_r($data['recmd']);
		if (stripos(',', $data['sid']) !==false){
			$data['sid'] = @explode(',', $data['sid']);
		}
		if (stripos(',', $data['player']) !==false){
			$data['player'] = @explode(',', $data['player']);
		}
		return $data;
	}
	/*
	根据分类ID传回格式化后的分类名称
	*/
	public function formatCategory($category){
		if ($category){
			if(is_array($category) == false){
				$category = explode(',', $category);
			}
			$where = '';
			foreach ($category as $row){
				if($row)
				$where.= $where ? ' OR id= '.(int)$row : ' id = '. (int)$row;
			}
			$cate = new CategoryModel();
			$data = $cate->getAll('('.$where.')');
			foreach ($data as $row){
				$link.= '<a href="'.INSTALLDIR.'movie/index/category/'.$row['id'].'/">'.$row['category'].'</a>&nbsp;';
			}
			unset($data);
			return $link;
		}
	}
	/*
	根据传入ID隐藏影片信息
	*/
	public function locked($id){
		return $this->_DB->exec('UPDATE '.VODCMS_MOVIE.' SET locked=NOT(locked) WHERE movid='.(int)$id);
	}
	public function  lock($id, $lock){
		if (is_array($id)){
			$id = implode(',', array_map('intval', $id));
		}
		$this->_DB->exec('UPDATE '.VODCMS_MOVIE.' SET locked=NOT(locked) WHERE movid in ('.$id.')');
		//echo $sql;
	}
	/*删除影片以及相关信息*/
	public function delete($id){
		if (is_numeric($id)){
			$where = 'movid='. intval($id);
		}else{
			$where = array('mkey'=> $id);
		}
		$IO = new Easy_Filesystem();
		$select = $this->_DB->select();
		$select->from(VODCMS_MOVIE)->where($where)->limit(1);
		$sql = $select->toString();
		$row = $this->_DB->fetRow($sql);
		if ($row['movid']){
			if ($row['pic']){
				$IO->delete($row['pic']);
			}
			if ($row['otherpic']){
				$IO->delete($row['otherpic']);
			}
			if ($row['bt']){
				$IO->delete($row['bt']);
			}
			$this->_DB->delete(VODCMS_MOVIE, $where);
			$this->_DB->delete(VODCMS_MCLASSID, 'movid='.(int)$row['movid']);
			$this->_DB->delete(VODCMS_URL, 'movid='.(int)$row['movid']);
			return true;
		}else{
			$this->error = _('影片未找到!');
			return false;
		}
		
	}
	/*
	根据条件检测是否存在条件记录成功返回记录ID否则返回0
	*/
	public function exists($where){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MOVIE)->where($where)->limit(1)->toString();
		$row = $this->_DB->fetRow($sql);
		if ($row['movid']){
			return $row['movid'];
		}else{
			return 0;
		}
	}
	/*初始化影片分类资料*/
	public function initCategory(){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MOVIE,'movid')->toString();
		foreach ($this->_DB->fetAll($sql) as $row){
			$select = $this->_DB->select();
			$sql = $select->from(VODCMS_MCLASSID,'category')->where(array('movid'=> $row['movid']))->toString();
			$result = $this->_DB->fetAll($sql);
			foreach ($result as $rs){
				$category[] = $rs['category'];
			}
			if (is_array($category)){
				$sql = 'UPDATE '.VODCMS_MOVIE.' SET 
				category=\''.implode(',', $category).'\' WHERE movid='.(int)$row['movid'];
				$this->_DB->exec($sql);
			}
			unset($category, $sql);
		}
	}
	/*
	根据传如字符返回首字符拼音
	*/
	public function getchr($str){
		$ret="";
		for($i=0;$i<strlen($str);$i++){
			$p=ord(substr($str,$i,1));
			if($p>160){
				$q=ord(substr($str,++$i,1));
				$p=$p*256+$q-65536;
			}
			$ret.=$p;
		}
		return $ret;
	}
	function getLetter($char)   
	{       
		$tmp = 65536 + $this->getchr($char);                
		if($tmp>=45217 && $tmp<=45252) {
		$getpychar= "A";
		}elseif($tmp>=45253 && $tmp<=45760) {
		$getpychar= "B";
		}elseif($tmp>=45761 && $tmp<=46317) {
		$getpychar= "C";
		}elseif($tmp>=46318 && $tmp<=46825) {
		$getpychar= "D";
		}elseif($tmp>=46826 && $tmp<=47009) { 
		$getpychar= "E";
		}elseif($tmp>=47010 && $tmp<=47296) { 
		$getpychar= "F";
		}elseif($tmp>=47297 && $tmp<=47613) { 
		$getpychar= "G";
		}elseif($tmp>=47614 && $tmp<=48118) {
		$getpychar= "H";
		}elseif($tmp>=48119 && $tmp<=49061) {
		$getpychar= "J";
		}elseif($tmp>=49062 && $tmp<=49323) { 
		$getpychar= "K";
		}elseif($tmp>=49324 && $tmp<=49895) { 
		$getpychar= "L";
		}elseif($tmp>=49896 && $tmp<=50370) { 
		$getpychar= "M";
		}elseif($tmp>=50371 && $tmp<=50613) { 
		$getpychar= "N";
		}elseif($tmp>=50614 && $tmp<=50621) { 
		$getpychar= "O";
		}elseif($tmp>=50622 && $tmp<=50905) {
		$getpychar= "P";
		}elseif($tmp>=50906 && $tmp<=51386) { 
		$getpychar= "Q";
		}elseif($tmp>=51387 && $tmp<=51445) { 
		$getpychar= "R";
		}elseif($tmp>=51446 && $tmp<=52217) { 
		$getpychar= "S";
		}elseif($tmp>=52218 && $tmp<=52697) { 
		$getpychar= "T";
		}elseif($tmp>=52698 && $tmp<=52979) { 
		$getpychar= "W";
		}elseif($tmp>=52980 && $tmp<=53640) { 
		$getpychar= "X";
		}elseif($tmp>=53689 && $tmp<=54480) { 
		$getpychar= "Y";
		}elseif($tmp>=54481 && $tmp<=62289) {
		$getpychar= "Z";
		}else{//如果不是中文，则不处理
		$getpychar=$char;
		}
		return $getpychar;
	}
	/*更新影片表字段信息*/
	public function  update($data, $where=null){
		$this->_DB->update(VODCMS_MOVIE, $data, $where);
		return true;
	}
	/*初始化影片分类信息.将影片表中的category字段值更新到临时表mclassid中*/
	public function initsCategory(){
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MOVIE)->toString();
		foreach ($this->_DB->fetAll($sql) as $row){
			$category = explode(',', $row['category']);
			$this->AddCategory($row['movid'], $category);
		}
		return true;
	}
	/*
	初始化影片信息
	1。更新影片分类表中的临时字段影片数
	2、更新影片表中的影片集数临时字段
	*/
	public function initmovie(){
		$category = new CategoryModel();
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_CATEGORY)->where('locked=0')->toString();
		foreach ($this->_DB->fetAll($sql) as $row){
			@set_time_limit(999);
			$id = implode(',' , $category->getChilds($row['id']));
			if (!$id){
				$id = $row['id'];
			}
			$select = $this->_DB->select();
			$select->from(VODCMS_MOVIE.' AS a', 'a.movid');
			$select->join(VODCMS_MCLASSID.' AS b','a.movid=b.movid','b.category');
			$select->where('a.locked=0');
			$select->where('b.category in ('.$id.')');
			$sql = $select->toString();
			$total = $this->_DB->getCount($sql);
			$this->_DB->update(VODCMS_CATEGORY, 
			array('moviecount'=>$total), array('id'=> $row['id']));
		}
		$select = $this->_DB->select();
		$sql = $select->from(VODCMS_MOVIE,'movid')->toString();
		foreach ($this->_DB->fetAll($sql) as $row){
			$select = $this->_DB->select();
			$select->from(VODCMS_URL);
			$sql = $select->where('movid='.$row['movid'])->toString();
			//echo $sql;
			$total = $this->_DB->getCount($sql);
			$this->_DB->update(VODCMS_MOVIE, 
			array('urlcount'=>$total), array('movid'=> $row['movid']));
		}
		unset($sql);
	}
	public function getMovid($POST=null){
		$category = new CategoryModel();
		if ($POST['category']){
			$where.= ' b.category in ('.implode(',', $category->getChilds($POST['category'])).')';	
		}
		$POST['pagesize'] = $POST['pagesize'] ? $POST['pagesize'] : 1;
		$select = $this->_DB->select();
		$select->from(VODCMS_MOVIE.' AS a', 'a.movid');
		$select->join(VODCMS_MCLASSID.' AS b', 'a.movid=b.movid',null);
		$select->where($where);
		$select->order('a.modifytime DESC');
		if($POST['max']){
			$select->limit($POST['max']);
		}else{
			$select->limit(($POST['pagesize']-1)*20, 20);
		}
		$sql = $select->toString();
		return $this->_DB->fetAll($sql);
	}
	//*更新中间表信息到*/
	public function setMidTable($movid = 0){
		/*if (is_numeric($movid) && $movid>0){
			$where = ' WHERE movid='.$movid;
		}
		$sql = 'SELECT movid,lookgid,downgid,locked,addtime,modifytime,click FROM '.VODCMS_MOVIE.$where;
		$Rows = $this->_DB->fetAll($sql);
		foreach($Rows as $row){
			$this->_DB->update(VODCMS_MCLASSID, $row, array('movid' => $row['movid']));
			$this->_DB->update(VODCMS_RECMD, $row, array('movid' => $row['movid']));
		}
		unset($Rows, $row, $sql);*/
	}
}?>