<?php
class TemplateController  extends Easy_Controller_Action {
	public function Init(){
		$this->_IO = new Easy_Filesystem();
	}
	public function indexAction(){
		if ($this->_Request->getParam('path')){
			$path = base64_decode($this->_Request->getParam('path'));
		}else{
			$path = ROOT.'templates';
		}
		$filter = array('public', '.svn', 'member');
		$this->_View->files = $this->_IO->scandir($path, $filter);
	}
	public function modifyAction(){
		$this->_View->filename = base64_decode($this->_Request->getParams('file'));
		if ($this->_Request->isPost()){
			$contents =$this->_Request->getPost('contents');
			copy($this->_View->filename, $this->_View->filename.'.bak');
			$this->_IO->wfile($filename, $contents);
			$this->_View->message = _('保存修改成功!');
			exit($this->_View->render('message'));
		}
		$this->_View->contents = $this->_IO->file_get_content($this->_View->filename);
	}
	public function downAction(){
		$this->_View->filename = base64_decode($this->_Request->getParams('file'));
		$this->_IO->attachment($this->_View->filename);
	}
	public function viewAction(){
		$path = ROOT.'templates';
		$filter = array('public', '.svn', 'member');
		$data = array();
		$array = array();
		foreach($this->_IO->scandir($path, $filter) as $files){
			if (is_dir($files)){
				foreach ($this->_IO->scandir($files) as $file){
					if (basename($file) == 'demo.jpg'){
						$array['picture'] = $file;
					}elseif (basename($file) =='info.ini'){
						$array['ini'] = parse_ini_file($file);
					}
				}
				$data[basename($files)] = $array;
			}
		}
		$this->_View->template = $this->config['template'];
		$this->_View->data = $data;
	}
	public function setAction(){
		$skin = $this->_Request->getParam('name');
		if ($skin){
			$filename = ROOT.'Config/config.inc.php';
			$contents = file_get_contents($filename);
			$contents = preg_replace('/\$config\[\'template\'\]+\s+\=+\s+(.+?);/is', '$config[\'template\'] = \''.$skin.'\';', $contents);
			//copy($this->_View->filename, basename($this->_View->filename).'.bak.php');
			$this->_IO->wfile($filename, $contents);
			$vodcms = new VodcmsModel();
			$vodcms->clean();
			$this->_View->message = _('风格设置成功!');
			exit($this->_View->render('message'));
		}
	}
}