<?php
class MovieController extends Easy_Controller_Action {
	public function Init(){
		$this->_Movie = new MovieModel();
		$this->_Category = new CategoryModel();
		$this->_Group = new GroupModel();
	}
	public function IndexAction(){
		$Player = new  PlayerModel();
		$Server = new ServerModel();
		$keyword = urldecode($this->_Request->getParams('keyword'));
		$this->_View->movie = $this->_Movie->Search($this->_Request->getParams());
		$this->_View->printpage = $this->_Movie->printPage;
		$this->_View->_Movie = $this->_Movie;
		$this->_View->Group = $this->_Group;
		$this->_View->GroupOption = $this->_Group->CreateOption($this->_Request->getParams('group'));
		$this->_View->CategoryOption = $this->_Category->CreateOption(0, $this->_Request->getParams('category'));
		$this->_View->AreaOption = $this->_Movie->AreaOption($this->_Request->getParams('area'));
		$this->_View->PlayerOption = $this->_Movie->PlayerOption($this->_Request->getParams('player'));
		$this->_View->ServerOption = $this->_Movie->ServerOption($this->_Request->getParams('sid'));
	}
	public function AddAction(){
		$cfg = new  Easy_Config();
		$Player = new  PlayerModel();
		$Server = new ServerModel();
		$smil = new SmilModel();
		$cfg->set(ROOT.'Config/movie.ini');
		include(ROOT."fckeditor/fckeditor.php") ;
		$sBasePath = $_SERVER['PHP_SELF'] ;
		$sBasePath = $this->config['basedir'].'fckeditor/';
		$this->_View->FCKeditor = new FCKeditor('content') ;
		$this->_View->FCKeditor->BasePath	= $sBasePath ;
		$this->_View->FCKeditor->Value		= _('暂无介绍');
		$this->_View->moviecfg = $cfg->data;
		$this->_View->smil = $smil->getAll(array('locked'=>0));
		if ($this->_Request->isPost()){
			if ($this->_Movie->Add($this->_Request->getPost())){
				$this->_View->message = _('添加成功!');
				exit($this->_View->render('message'));
			}else{
				$this->_View->modify = $this->_Request->getPost();
				$this->_View->message = $this->_Movie->error;
				if (is_array($this->_View->modify['category'])){
					$this->_View->cate = $this->_Category->getAll('id in ('.implode(',', $this->_View->modify['category']).')');
				}else{
					$this->_View->cate = $this->_Category->getAll(array('id'=> $this->_View->modify['category']));
				}
				if (is_array($this->_View->modify['player'])){
					$this->_View->player = $Player->getAll("tag in ('".implode("','", $this->_View->modify['player'])."')");
				}else{
					$this->_View->player = $Player->getAll("tag='".$this->_View->modify['player']."'");
				}
				if (is_array($this->_View->modify['sid'])){
					$this->_View->sid = $Server->getList('sid in ('.implode(',', $this->_View->modify['sid']).')');
				}else{
					$this->_View->sid = $Server->getList('sid='.$this->_View->modify['sid']);
					$this->_Request->setCookie('sid', $this->_View->sid);
				}
			}
		}else{
			$this->_View->modify['language'] = explode(',', $cfg->language);
			$this->_View->modify['recmd']  = explode(',', $cfg->recmd);
			$this->_View->modify['category'] = $cfg->category;
			$this->_View->modify['look'] = $cfg->look;
			$this->_View->modify['down'] = $cfg->down;
			$this->_View->modify['area'] = $cfg->area;
			$this->_View->modify['player'] = $cfg->player;
			$this->_View->modify['sid'] = (int)$this->_Request->getCookie('sid');
			$this->_View->modify['serialise'] = 0;
		}
		$this->_View->CategoryOption = $this->_Category->CreateOptions(0, $this->_View->modify['category']);
		$this->_View->AreaOption = $this->_Movie->AreaOption($this->_View->modify['area']);
		$this->_View->PlayerOption = $this->_Movie->PlayerOption($this->_View->modify['player']);
		$this->_View->langCheckbox = $this->_Movie->getLanguage( $this->_View->modify['language'] );
		$this->_View->recmdCheckbox = $this->_Movie->getRecmdOption($this->_View->modify['recmd']);
		$this->_View->ServerOption = $this->_Movie->ServerOption($this->_View->modify['sid']);
		$this->_View->GroupOption = $this->_Group->CreateOption($cfg->usergroup);
	}
	public function setAction(){
		$cfg = new  Easy_Config();
		$IO = new Easy_Filesystem();
		$cfg->set(ROOT.'Config/movie.ini');
		$this->_View->CategoryOption = $this->_Category->CreateOptions(0, $cfg->category);
		$this->_View->AreaOption = $this->_Movie->AreaOption($cfg->area);
		$this->_View->PlayerOption = $this->_Movie->PlayerOption($cfg->player);
		$this->_View->langCheckbox = $this->_Movie->getLanguage( explode(',', $cfg->language) );
		$this->_View->recmdCheckbox = $this->_Movie->getRecmdOption(explode(',', $cfg->recmd));
		$this->_View->moviecfg = $cfg->data;
		if ($this->_Request->isPost()){
			$content =  $IO->file_get_content(ROOT.'Config/movie.ini');
			foreach ($this->_Request->getPost() as $key=>$value){
				if (is_array($value)){
					$value = implode(',', $value);
				}
				if (is_numeric($value)){
				 	$content = preg_replace('/'.$key.'+\s+=+\s+(.+?);/is', "$key = $value;", $content);
				}else{
					$content = preg_replace('/'.$key.'+\s+=+\s+(.+?);/is', "$key = \"$value\";", $content);
				}
			}
			$IO->wFile(ROOT.'Config/movie.ini' , $content);
			$this->_View->message = _('保存成功!');
			echo $this->_View->render('message');
			exit;
		}
		
	}
	public function modifyAction(){
		$cfg = new  Easy_Config();
		$Player = new  PlayerModel();
		$Server = new ServerModel();
		$smil = new SmilModel();
		$this->_View->smil = $smil->getAll(array('locked'=>0));
		$cfg->set(ROOT.'Config/movie.ini');
		$this->_View->moviecfg = $cfg->data;
		if ($this->_Request->getParam('do') == 'locked'){
			$this->_Movie->locked($this->_Request->getParam('id'));
			header('location:'.$_SERVER['HTTP_REFERER']);
			exit;
		}elseif ($this->_Request->isPost()){
			if ($this->_Movie->add($this->_Request->getPost(), 'modify')===false){
				$this->_View->message = $this->_Movie->error;
			}else{
				$this->_View->message = _('修改成功!');
				
			}
			exit($this->_View->render('message'));
		}
		if ($this->_Request->getParam('movid')){
			$this->_View->modify = $this->_Movie->getRow((int)$this->_Request->getParam('movid'));
			$this->_View->AreaOption = $this->_Movie->AreaOption($this->_View->modify['area']);
			$this->_View->PlayerOption = $this->_Movie->PlayerOption($this->_View->modify['player']);
			$this->_View->langCheckbox = $this->_Movie->getLanguage( explode(',', $this->_View->modify['language']) );
			$this->_View->recmdCheckbox = $this->_Movie->getRecmdOption($this->_View->modify['recmd']);
			$this->_View->ServerOption = $this->_Movie->ServerOption($this->_View->modify['sid']);
			$this->_View->GroupOption = $this->_Group->CreateOption($this->_View->modify['lookgid']);
			include(ROOT."fckeditor/fckeditor.php") ;
			$sBasePath = $_SERVER['PHP_SELF'] ;
			$sBasePath = $this->config['basedir'].'fckeditor/';
			$this->_View->FCKeditor = new FCKeditor('content') ;
			$this->_View->FCKeditor->BasePath	= $sBasePath ;
			$this->_View->FCKeditor->Value		= stripslashes($this->_View->modify['content']);
			if ($cfg->cate == 2){
				if (count($this->_View->modify['category'])>0){
					$this->_View->cate = $this->_Category->getAll('id in ('.implode(',', $this->_View->modify['category']).')');
				}else{
					$this->_View->cate = $this->_Category->getAll(array('id'=> (int)$this->_View->modify['category']));
				}
			}else{
				if (is_array($this->_View->modify['category'])){
					$this->_View->modify['category'] = end($this->_View->modify['category']);
				}
			}
			$this->_View->CategoryOption = $this->_Category->CreateOptions(0, $this->_View->modify['category']);
			
			if ($cfg->play == 2){	//是否开启了多播放器选择
				if (is_array($this->_View->modify['player']) === false){
					$this->_View->modify['player'] = explode(",", $this->_View->modify['player']);
				}
				if (count($this->_View->modify['player'])>0){
					$this->_View->player = $Player->getAll("tag in ('".implode("','", $this->_View->modify['player'])."')");
				}else{
					$this->_View->player = $Player->getAll("tag='".$this->_View->modify['player']."'");
				}
			}
			if ($cfg->server == 2){
				if (is_array($this->_View->modify['sid']) === false){
					$this->_View->modify['sid'] = explode(",", $this->_View->modify['sid']);
				}
				if (count($this->_View->modify['sid'])>0){
					$this->_View->sid = $Server->getList('sid in ('.implode(',', $this->_View->modify['sid']).')');
				}else{
					$this->_View->sid = $Server->getList('sid='.$this->_View->modify['sid']);
				}
			}
			//print_r($this->_View->modify['category']);
		}
	}
	public function deleteAction(){
		$id = (int)$this->_Request->getParam('movid');
		$this->_Movie->delete($id);
		$this->_View->message = _('删除成功!');
		exit($this->_View->render('message'));
	}
	/*
	批量操作修改
	*/
	public function  postAction(){
		$movid = $this->_Request->getPost('movid');
		if (count($movid)<1){
		$this->_View->message = _('请选择记录!');
			exit($this->_View->render('message'));
		}
		if (strtolower($license['version']) == 'free' || $_SERVER['HTTP_HOST'] == '127.0.0.1'){
			if ($this->_Request->getPost('num')>10){
				$this->_View->message = _('非商业版无法此项功能!');
				exit($this->_View->render('message'));
			}
		}
		if ($this->_Request->getPost('do')){	//提交修改
			$select = $this->_Request->getPost('select');
			$movid = $this->_Request->getPost('movid');
			switch ($select){
				case 'delmovie':	//删除影片
					foreach ($movid as $id){
						$this->_Movie->delete($id);
					}
					$this->_View->message = _('删除成功!');
				break;
				case 'locked':		//隐藏影片
					$this->_Movie->lock($movid, 1);
					$this->_View->message = _('隐藏成功!');
				break;
				case 'unlock':		//解锁影片
					$this->_Movie->lock($movid, 0);
					$this->_View->message = _('解锁成功!');
				break;
			}
			$this->_View->jumpurl = $_SERVER['HTTP_REFERER'];
			exit($this->_View->render('message'));
		}elseif($this->_Request->getParam('act') == 'succeed'){
			$movid = $this->_Request->getPost('movid');
			if (count($movid)>0){
				$where = 'movid in ('.implode(',', $movid).')';
			}
			$sid = (int)$this->_Request->getPost('sid');
			$player = $this->_Request->getPost('player');
			$area = $this->_Request->getPost('area');
			$lookgid = (int)$this->_Request->getPost('lookgid');
			$downgid = (int)$this->_Request->getPost('downgid');
			$point = (int)$this->_Request->getPost('point');
			$category = (int)$this->_Request->getPost('category');
			$data = null;
			if ($sid){
				$data['sid'] = $sid;
			}
			if ($player){
				$data['player'] = $player;
			}
			if ($area){
				$data['area'] = $area;
			}
			if ($lookgid != 'no'){
				$data['lookgid'] = $lookgid;
			}
			if ($downgid != 'no'){
				$data['downgid'] = $downgid;
			}
			if ($point){
				$data['point'] = $point;
			}
			if ($category){
				$data['category'] = $category;
			}
			if ($data){
				$this->_Movie->update($data, $where);
				unset($data);
			}
			if ($category){
				$this->_Movie->initsCategory();
			}
			$uri = $this->_Request->getPost('uri');
			if ($uri){
				$this->_View->jumpurl = base64_decode($uri);
			}
			$this->_View->message = _('批量编辑成功!');
			exit($this->_View->render('message'));
		}
		$this->_View->movid = $this->_Request->getPost('movid');
		$this->_View->AreaOption = $this->_Movie->AreaOption(array(1));
		$this->_View->PlayerOption = $this->_Movie->PlayerOption(array(0));
		$this->_View->langCheckbox = $this->_Movie->getLanguage(array(0));
		$this->_View->recmdCheckbox = $this->_Movie->getRecmdOption(array(0));
		$this->_View->ServerOption = $this->_Movie->ServerOption(array(0));
		$this->_View->CategoryOption = $this->_Category->CreateOptions(0, 0);
		$this->_View->GroupOption = $this->_Group->CreateOption(0);
		
	}
}?>