<?php

namespace Home\Controller;
use Think\Controller;
/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class PlistController extends Controller {
    
 //生成游戏渠道plist文件
    public function create_plist($game_id=0,$promote_id=0,$marking="",$url=""){
        $xml = new \DOMDocument();
        $xml->load('./Uploads/Plist/testdemo.Plist');
        $online = $xml->getElementsByTagName('dict');//查找节点
        $asd=$online->item(1)->getElementsByTagName('string');//第二个节点下所有string
        foreach ($asd as $key=>$value) {
            switch ($value->textContent) {
                case 'ipa_url':
                if(preg_match("/Uploads/", $url)){
                  $value->nodeValue="http://".$_SERVER['HTTP_HOST'].ltrim($url,".");//"https://iosdemo.vlcms.com/app/MCHSecretary.ipa";//替换xml对应的值    
                }else{
                $value->nodeValue=$url;
                }
                    break;
                case 'icon':
                $value->nodeValue="http://".$_SERVER["HTTP_HOST"].get_cover(get_game_icon_id($game_id),'path');;                
                    break;
                 case 'com.dell':
                $value->nodeValue=$marking;
                    break;
                case '1.0.0':
                $value->nodeValue=game_version($game_id);
                    break;
                case 'mchdemo':
                $value->nodeValue=get_ios_game_name($game_id);
                    break;

            }
            if($promote_id==0){
            $xml->save("./Uploads/SourcePlist/$game_id.Plist");
            }else{
            $pname=$game_id."-".$promote_id;
            $xml->save("./Uploads/GamePlist/$pname.Plist");
            }
        }
        if($promote_id==0){
          return "./Uploads/SourcePlist/$game_id.Plist";
        }else{
          return "./Uploads/GamePlist/$pname.Plist";
        }


    }


     //生成App plist文件
    public function create_plist_app($version="",$promote_id=0,$marking="",$url=""){
        $xml = new \DOMDocument();
        $xml->load('./Uploads/Plist/testdemo.Plist');
        $online = $xml->getElementsByTagName('dict');//查找节点
        $asd=$online->item(1)->getElementsByTagName('string');//第二个节点下所有string
        foreach ($asd as $key=>$value) {
            switch ($value->textContent) {
                case 'ipa_url':
                if(preg_match("/Uploads/", $url)){
                  $value->nodeValue="http://".$_SERVER['HTTP_HOST'].ltrim($url,".");//"https://iosdemo.vlcms.com/app/MCHSecretary.ipa";//替换xml对应的值    
                }else{
                $value->nodeValue=$url;
                }
                    break;
                case 'icon':
                $value->nodeValue="http://".$_SERVER["HTTP_HOST"].get_cover(C('APP_ICON'),'path');;                
                    break;
                 case 'com.dell':
                $value->nodeValue=$marking;
                    break;
                case '1.0.0':
                $value->nodeValue=$version;
                    break;
                case 'mchdemo':
                $value->nodeValue="折扣APP";
                    break;

            }
            $pname=$promote_id;
               if($promote_id==0){
                      $xml->save("./Uploads/SourcePlist/$pname.Plist");

                }else{
                      $xml->save("./Uploads/AppPlist/$pname.Plist");
                }
        }
         if($promote_id==0){
            return "./Uploads/SourcePlist/$pname.Plist";
        }else{
           return "./Uploads/AppPlist/$pname.Plist";
        }
    }

	/**
	 * 重写 create_plist 方法。方便 开放平台 调用
	 * @param int $game
	 * @param int $promote_id
	 * @param string $marking
	 * @param string $url
	 * author: xmy 280564871@qq.com
	 */
	public function createPlist($game = 0, $promote_id = 0, $marking = "", $url = "")
	{
		$xml = new \DOMDocument();
		$xml->load('./Uploads/Plist/testdemo.Plist');
		$online = $xml->getElementsByTagName('dict');//查找节点
		$asd = $online->item(1)->getElementsByTagName('string');//第二个节点下所有string
		foreach ($asd as $key => $value) {
			switch ($value->textContent) {
				case 'ipa_url':
					if (preg_match("/Uploads/", $url)) {
						$value->nodeValue = "http://" . $_SERVER['HTTP_HOST'] . ltrim($url, ".");//"https://iosdemo.vlcms.com/app/MCHSecretary.ipa";//替换xml对应的值
					} else {
						$value->nodeValue = $url;
					}
					break;
				case 'icon':
					$value->nodeValue = "http://" . $_SERVER["HTTP_HOST"] . get_cover($game['icon'], 'path');;
					break;
				case 'com.dell':
					$value->nodeValue = $marking;
					break;
				case '1.0.0':
					$value->nodeValue = $game['sdk_version'];
					break;
				case 'mchdemo':
					$value->nodeValue = substr($game['game_name'], 0, strpos($game, "("));
					break;

			}
			if ($promote_id == 0) {
				$xml->save("./Uploads/SourcePlist/{$game['id']}.Plist");
			} else {
				$pname = $game['id'] . "-" . $promote_id;
				$xml->save("./Uploads/GamePlist/$pname.Plist");
			}
		}
	}
}