<?php

/**
 * 取得地铁信息 模块 Model
 * ============================================================================
 * ZhunaCMS 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: comment.class.php 36 2011-11-30 11:46:05Z zhangwentao $
 * http://un.zhuna.cn；
 */
class subway extends Model
{

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
//        $this->connect();
    }

    /**
     * 取得城市里的地铁线路
     * @param <int> $cityid     
     */
    public function subway_line($cityid)
    {
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');

        $nofind = '';
     
        $xml_url = $cfg_apiurl . "ditie_json.php?cityid=$cityid";        
        $xml_root = VAR_ROOT . "json/subway/{$cityid}/";        
        $xml_name = "subway_{$cityid}.json";
        $this->M('hotelLeft')->createXML($xml_url, $xml_root, $xml_name, 604800);
        $xmlstring = $xml_root . $xml_name;

        $line_ary = array();
        $line_result = json_decode(file_get_contents($xmlstring), true);
        if ($line_result['retmsg'] == '1') {

            if (count($line_result['reqdata']) > 0) {
                foreach ($line_result['reqdata'] as $k => $basexml) {                    
                    $line_ary[$k]['line_id'] = $basexml['zhuantiid'];
                    $line_ary[$k]['line_title'] = $basexml['title'];              
                    $line_ary[$k]['cityid'] = $basexml['cityid'];
                }
            } else {
                $nofind_comment = '没有此条线路!';
            }
        } else {
            $nofind_comment = '接口读取失败，请重试！';
        }
        $result['nofind'] = $nofind;
        $result['rs'] = $line_ary;
        return $result;
    }
    
    /**
     * 取得某个地铁线路的详细信息
     * @param $cityid 城市id
     * @param $line 地铁线路
     */
    public function getsubwaystation($cityid,$line)
    {
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');

        $nofind = '';
     
        $xml_url = $cfg_apiurl . "ditieinfo_json.php?id=$line";  
        
        $xml_root = VAR_ROOT . "json/subway/{$cityid}/";        
        $xml_name = "{$line}_station.json";
        $this->M('hotelLeft')->createXML($xml_url, $xml_root, $xml_name, 604800);
        $xmlstring = $xml_root . $xml_name;

        $line_ary = array();
        $line_result = json_decode(file_get_contents($xmlstring), true);
        if ($line_result['retmsg'] == '1') {

            if (count($line_result['reqdata']) > 0) {
                foreach ($line_result['reqdata'] as $k => $basexml) {                    
                    $line_ary[$k]['stationid'] = $basexml['id'];
                    $line_ary[$k]['stationname'] = $basexml['title'];              
                }
            } else {
                $nofind_comment = '没有此条线路!';
            }
        } else {
            $nofind_comment = '接口读取失败，请重试！';
        }
        $result['nofind'] = $nofind;
        $result['rs'] = $line_ary;
        return $result;    	
    }

    
    /**
     * 取得某个线路的中文名称
     * @param $cityid
     * @param $line
     */
    public function getsubway($cityid,$line)
    {
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');
     
        $xml_url = $cfg_apiurl . "ditie_json.php?cityid=$cityid";        
        $xml_root = VAR_ROOT . "json/subway/{$cityid}/";        
        $xml_name = "subway_{$cityid}.json";
        $xmlstring = $xml_root . $xml_name;
        
        $title = "";

        $line_result = json_decode(file_get_contents($xmlstring), true);
        if ($line_result['retmsg'] == '1') {

            if (count($line_result['reqdata']) > 0) {
                foreach ($line_result['reqdata'] as $k => $basexml) {                    
                    if($basexml['zhuantiid'] == $line){
                        $title = $basexml['title'];              
                    }
                }
            } else {
                $title = '没有此条线路!';
            }
        } else {
            $title = '接口读取失败，请重试！';
        }
        return $title;
    }    

}