<?php

/**
 * 前台 团购模块 model
 * ============================================================================
 * ZhunaCMS 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: teams.class.php    2012-01-30 13:49:41Z zhaojianjun $
 * http://union.zhuna.cn；
 */
class teams extends Model
{

	public $hotel_left_model;

	public function _init()
	{
        $this->hotel_left_model = Tmac::model('hotelLeft');		
	}
	
    /**
     * 获得有团购列表的城市
     */
    public function getCitys()
    {
		$string = "http://union.api.zhuna.cn/tuan/api/citys.xml" ;
		//删除昨天的缓存文件
		$yestoday = VAR_ROOT . "xml/teams/".date('Ymd',strtotime('-1 day'));
		if(file_exists($yestoday)){
			@Functions::deldir($yestoday);
		}
        $xml_root = VAR_ROOT . "xml/teams/";
        $xml_name = "team_city.xml";
        //缓存文件
        $this->hotel_left_model->createXML($string, $xml_root, $xml_name, 864000);
        $xmlstring = $xml_root . $xml_name;
        		
		$dom = new DOMDocument();
		$dom->load($xmlstring);
		$tmp = $dom->getElementsByTagName("city");
		$city = array();
		foreach ($tmp as $k=>$teams) {
		    $city[$k]['ename'] = $teams->getAttribute('ename');
		    $city[$k]['id'] = $teams->getAttribute('id');
		    $city[$k]['nums'] = $teams->getAttribute('nums');
		    $city[$k]['city'] = $teams->getAttribute('city');   
		}
		return $city;	
    }  
    
    /**
     * 取某个城市的团购列表
     * @param $cityid
     */
    public function getTeamsList($cityid)
    {
        $string = "http://union.api.zhuna.cn/tuan/api/lists/list_{$cityid}.xml" ;
        $xml_root = VAR_ROOT . "xml/teams/".date('Ymd').'/';
        $xml_name = "team_list_{$cityid}.xml";
        $this->hotel_left_model->createXML($string, $xml_root, $xml_name, 864000);
        $xmlstring = $xml_root . $xml_name;        
        $dom = new DOMDocument();
        $dom->load($xmlstring);
        $tmp = $dom->getElementsByTagName("team");
        $teams = array();
        foreach ($tmp as $k=>$team) {
            $teams[$k]['id'] = $team->getAttribute('id'); 
            $teams[$k]['hotelname'] = $team->getAttribute('hotelname'); 
            $teams[$k]['teamPrice'] = $team->getAttribute('teamPrice'); 
            $teams[$k]['markPrice'] = $team->getAttribute('markPrice'); 
            $teams[$k]['imgrul'] = $team->getAttribute('imgrul'); 
            $teams[$k]['endtime'] = $team->getAttribute('endtime'); 
            $teams[$k]['cname'] = $team->getAttribute('cname'); 
            $teams[$k]['tzone'] = $team->getAttribute('tzone'); 
            $teams[$k]['nowNumber'] = $team->getAttribute('nowNumber');
            if($team->childNodes->length) { 
                foreach($team->childNodes as $i) { 
                    $teams[$k][$i->nodeName] = $i->nodeValue; 
                } 
            } 
            
        }
        return $teams;      	
    }
    
    /**
     * 得到一个具体的团购信息
     */
    public function getTeams($id)
    {
        $string = "http://union.api.zhuna.cn/tuan/api/teams/".date('Ymd',time())."/team_{$id}.xml" ;
        $xml_root = VAR_ROOT . "xml/teams/".date('Ymd').'/';
        $xml_name = "team_info_{$id}.xml";
        $this->hotel_left_model->createXML($string, $xml_root, $xml_name, 864000);
        $xmlstring = $xml_root . $xml_name;          	
        $dom = new DOMDocument();
        $dom->load($string);
        $info = $dom->getElementsByTagName("info");
        $teams = array();
        foreach ($info as $team) {
            $teams['id'] = $team->getAttribute('id'); 
            $teams['hotelname'] = $team->getAttribute('hotelname'); 
            $teams['teamPrice'] = $team->getAttribute('teamPrice'); 
            $teams['markPrice'] = $team->getAttribute('markPrice'); 
            $teams['imgrul'] = $team->getAttribute('imgrul'); 
            $teams['cityid'] = $team->getAttribute('cityid'); 
            $teams['endtime'] = $team->getAttribute('endtime'); 
            $teams['x'] = $team->getAttribute('x'); 
            $teams['y'] = $team->getAttribute('y'); 
            $teams['mapbz'] = $team->getAttribute('mapbz'); 
            $teams['maxNumber'] = $team->getAttribute('maxNumber');
            $teams['minNumber'] = $team->getAttribute('minNumber');
            $teams['nowNumber'] = $team->getAttribute('nowNumber');
            $teams['title'] = $dom->getElementsByTagName("title")->item(0)->nodeValue;
            $teams['detail'] = $dom->getElementsByTagName("detail")->item(0)->nodeValue;
            $teams['notice'] = $dom->getElementsByTagName("notice")->item(0)->nodeValue;
            //相关团购
            $tmp = $dom->getElementsByTagName("other");
            $others = array() ;
            foreach ($tmp as $j => $other)
            {
                $others[$j]['id'] = $other->getAttribute('id');
                $others[$j]['teamPrice'] = $other->getAttribute('teamPrice');
                $others[$j]['marketPrice'] = $other->getAttribute('marketPrice');
                $others[$j]['image'] = $other->getAttribute('image');
                $others[$j]['cityid'] = $other->getAttribute('cityid');
                $others[$j]['nowNumber'] = $other->getAttribute('nowNumber');
                $others[$j]['title'] = $other->nodeValue;
            }
            $teams['other'] = $others;
        }    	
        
        return $teams;
    }
}    
    