<?php
/**
 * 模板管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class TemplateController extends BaseController {
    public function index() {
		
		$module='Mobile';
		$indexlist=getmodtpl('shops_index_','Takeout','',$module);
		$list = array();
		foreach($indexlist as $k=>$v){
			$list[$k][$v] = $v;
			$list[$k]['img'] = APP_PATH.$module.'/View/'.get_default_theme($module).'/'.$pathdir.'Takeout'.'/'.str_replace('shops_index_',"",strstr($v,'.',true)).'.jpg';
		}

		$this->assign('list',$list);
        $temparray=strtoarray(C('TEMPLATE_PATH'));
        $mod=I('get.mod','Home');
        $act=I('get.act');
        $this->assign('defaulttpl',get_default_theme($mod,NOW_TIME));
        if($act=='changetpl'){
            $postdata=I('get.');
			if($postdata['tpl'] == "default"){
				$config['LIST_TPL']= 'shops_index_list';
			}
			if($postdata['tpl'] == "blue"){
				$config['LIST_TPL']= 'shops_index_blue';
			}
			if($postdata['tpl'] == "green"){
				$config['LIST_TPL']= 'shops_index_green';
			}
			if($postdata['tpl'] == "yellow"){
				$config['LIST_TPL']= 'shops_index_yellow';
			}
			
			if ($config && is_array($config)) {
				$vi=array();
				$this->model = D('Config');
				foreach ($config as $name => $value) {
					$map = array('name' => $name);
					if(is_array($value)){
						foreach($value as $key=>$val){
							$vi[]=implode(":",$val);
						}$value = implode(",",$vi);
					}
					$this->model->where($map)->setField('value', $value);
				}
				S('DB_CONFIG_DATA', null);
			}
            $this->success('更新完成', U('index'));
        }else{
		$dir = new \Common\Vendor\Dir;
        $dirlist[$mod]=array_slice($dir->getList(APP_PATH.$mod.'/View/'),2);
		if($mod == "Takeout"){
			$dirlisttpl = array(
				'default'=>array('name'=>'default模板','version'=>'1.0','author'=>'汉潮','url'=>'http://www.hanchao9999.com','thumb'=>'thumb_default.jpg','big'=>'big_default.jpg'),
				'blue'=>array('name'=>'blue模板','version'=>'1.0','author'=>'汉潮','url'=>'http://www.hanchao9999.com','thumb'=>'thumb_blue.jpg','big'=>'big_blue.jpg'),
				'green'=>array('name'=>'green模板','version'=>'1.0','author'=>'汉潮','url'=>'http://www.hanchao9999.com','thumb'=>'thumb_green.jpg','big'=>'big_green.jpg'),
				'yellow'=>array('name'=>'yellow模板','version'=>'1.0','author'=>'汉潮','url'=>'http://www.hanchao9999.com','thumb'=>'thumb_yellow.jpg','big'=>'big_yellow.jpg')
			);
		}
        if(isset($dirlist[$mod])){            
        foreach($dirlist[$mod] as $val){
            if(str_exists($val,'html') || in_array($val,array('.git','.svn')))continue;
            $dirlisttpl[$val]=$this->gettplconfig($mod,$val);
        }}

        $this->assign('dirlisttpl',$dirlisttpl);
        $this->assign('_list', $temparray);
        $this->assign('model', $mod);
        $this->display();
        }
    }
    
    public function setting(){
        $mod=I('mod','Home');
        $tpl=I('tpl');
        if(empty($tpl))$this->error('请先选择模板再设置');
        if(IS_POST){
            $postdata=I('post.');
            $data=array();
            if(!empty($postdata['name']))$data['name']=$postdata['name'];
            if(!empty($postdata['version']))$data['version']=$postdata['version'];
            if(!empty($postdata['author']))$data['author']=$postdata['author'];
            if(!empty($postdata['url']))$data['url']=$postdata['url'];
            if(!empty($postdata['thumb']))$data['thumb']=$postdata['thumb'];
            if(!empty($postdata['big']))$data['big']=$postdata['big'];
            if(!empty($postdata['color_name'])){
                foreach($postdata['color_name'] as $k=>$v){
                    if(!empty($v) && !empty($postdata['color_value'][$k])){
                        $data['color'][$v]=$postdata['color_value'][$k];
                        if(!empty($postdata['color_temp'][$k])){
                             $data['colortemp'][$v]=$postdata['color_temp'][$k];
                        }
                    }
                }
                
                
            }
            if($data){
                $basepath=substr(dirname(__FILE__),0,-29);
                $configfile=$basepath.'/Application/'.$mod.'/View/'.$tpl.'/config.php';
                arr2file($configfile,$data);
                $this->success('设计成功');
            }else{
                $this->error('设计失败');
            }
        }else{
             
            $info=$this->gettplconfig($mod,$tpl);
            $this->assign($info);
            $this->display();
        }
    }
    private function gettplconfig($module,$tpl){
        $basepath=substr(dirname(__FILE__),0,-29);
        $tplconfigurl=$basepath.'/Application/'.$module.'/View/'.$tpl.'/config.php';
        $config=array('name'=>$tpl.'模板','version'=>'1.0','author'=>'汉潮','url'=>'http://www.hanchao9999.com','thumb'=>'thumb.jpg','big'=>'big.jpg','color'=>array());
        if(file_exists($tplconfigurl)){$tpl_config = require_once $tplconfigurl;if($tpl_config)$config=array_merge($config,$tpl_config);}
        return $config;        
    }
}

?>