<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------

// 轮播
class FlashAction extends CommonAction{
	public function index()
	{
		$flash_style = D("SysConf")->where("name='FLASH_STYLE'")->getField("val");
		$style_list = D("SysConf")->where("name='FLASH_STYLE'")->getField("val_arr");
		$style_list = explode(",",$style_list);
		$this->assign("style_list",$style_list);
		$this->assign("flash_style",$flash_style);
		$this->assign("flash_width",D("SysConf")->where("name='FLASH_WIDTH'")->getField("val"));
		$this->assign("flash_height",D("SysConf")->where("name='FLASH_HEIGHT'")->getField("val"));
		
		$flash_list = M("Flash")->order("id")->findAll();
		$this->assign("flash_list",$flash_list);

		$this->display();
	}
	
	public function update()
	{
		$uploaded_list = $this->uploadFile(0,'adv');
		$ids = $_REQUEST['flash_id'];
		$names = $_REQUEST['name'];
		$urls = $_REQUEST['url'];
		$upload_files = $_FILES['img'];
		$uploaded_list_idx = 0;
		
		foreach($ids as $k=>$v)
		{
			$data = array();
			$data['id'] = $ids[$k];
			$data['name'] = $names[$k];
			$data['url'] = $urls[$k];
			if($upload_files['name'][$k]!='')
			{
				if($data['id']!=0)
				{
					$ori_img = M("Flash")->where("id=".$data['id'])->getField("img");
					@unlink($this->getRealPath().$ori_img);
				}				
				$data['img'] = $uploaded_list[$uploaded_list_idx]['recpath'].$uploaded_list[$uploaded_list_idx]['savename'];
				$uploaded_list_idx++;				
			}
			if($data['id']==0)
				M("Flash")->add($data);
			else 
				M("Flash")->save($data);
		}
		
		D("SysConf")->where("name='FLASH_STYLE'")->setField("val",$_REQUEST['flash_style']);
		D("SysConf")->where("name='FLASH_WIDTH'")->setField("val",$_REQUEST['flash_width']);
		D("SysConf")->where("name='FLASH_HEIGHT'")->setField("val",$_REQUEST['flash_height']);
		
		$this->outputData();
		$this->success(L("EDIT_SUCCESS"));
	}
	
	public function delFlash()
	{
		$id = intval($_REQUEST['id']);
		$flash_data = M("Flash")->getById($id);
		@unlink($this->getRealPath().$flash_data['img']);
		M("Flash")->where("id=".$id)->delete();
		$this->outputData();
	}
	
	private function outputData()
	{
		$flash_list = M("Flash")->order("id")->findAll();
		$js_str = $this->getDataStr($flash_list,"js");
		$xml_str = $this->getDataStr($flash_list,"xml");
		$style_list = D("SysConf")->where("name='FLASH_STYLE'")->getField("val_arr");
		$style_list = explode(",",$style_list);
		
		foreach($style_list as $style_item)
		{
			$style_path = $this->getRealPath()."/Public/flashdata/".$style_item."/";
			if(file_exists($style_path."data.js"))
			{
				@file_put_contents($style_path."data.js",$js_str);
			}
			if(file_exists($style_path."data.xml"))
			{
				@file_put_contents($style_path."data.xml",$xml_str);
			}			
		}
	}
	
	private function getDataStr($list,$filetype)
	{
		$xml_str = '';
		$js_str = '';
		if($filetype=="xml")
		{
			$xml_str = '<?xml version="1.0" encoding="utf-8"?><bcaster>';
			foreach ($list as $data)
		    {
		    	$img = $data['img']!=''?substr($data['img'],1):'';
		        $xml_str .= '<item item_url="' . $img . '" link="' . $data['url'] . '" />';
		    }
		    $xml_str .= '</bcaster>';
		    return $xml_str;
		}
		if($filetype=='js')
		{
			$jsdata_p1 = '';
		    $jsdata_p2 = array('url' => 'var pics=', 'txt' => 'var texts=', 'link' => 'var links=');
		    $count = 1;
		    $join = '';
		    foreach ($list as $data)
		    {
		    	$img = $data['img']!=''?substr($data['img'],1):'';
		        $jsdata_p1 .= 'imgUrl' . $count . '="' . $img . '";' . "\n";
		        $jsdata_p1 .= 'imgtext' . $count . '="' . $data['name'] . '";' . "\n";
		        $jsdata_p1 .= 'imgLink' . $count . '=escape("' . $data['url'] . '");' . "\n";
		        if ($count != 1)
		        {
		            $join = '+"|"+';
		        }
		        $jsdata_p2['url'] .= $join . 'imgUrl' . $count;
		        $jsdata_p2['txt'] .= $join . 'imgtext' . $count;
		        $jsdata_p2['link'] .= $join . 'imgLink' . $count;
		        ++$count;
		    }
		    $js_str = $jsdata_p1 . "\n" . $jsdata_p2['url'] . ";\n" . $jsdata_p2['link'] . ";\n" . $jsdata_p2['txt'] . ";";		   
	    	return $js_str;
		}
	}
}
?>