<?php

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

    private $db;
    private $city_table;           //商圈
    private $hotel_table;
    private $cbd_table;
    private $ad_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->city_table = DB_PREFIX . 'city';
        $this->hotel_table = DB_PREFIX . 'hotel';
        $this->cbd_table = DB_PREFIX . 'cbd';
        $this->ad_table = DB_PREFIX . 'ad';
    }

    /**
     * 取商业区cbd的
     * @param <int> $cityid
     * @param <int> $limit
     */
    public function cbdList($cityid, $limit)
    {
        $cbd_rs = $this->db->getAll("SELECT * FROM $this->cdb_table WHERE cityid = $cityid ORDER BY hotelnum DESC LIMIT 0, $limit");
        return $cbd_rs;
    }

    public function getCbdInfo($cbd_id)
    {
        $sql = "SELECT * FROM $this->cbd_table WHERE cbd_id = $cbd_id LIMIT 0, 1";
        $cbd_info = $this->db->cacheGetRow($sql, 86400);
        return $cbd_info;
    }

    /**
     * 推荐北京热门酒店
     * @param <type> $cityid
     * @param <type> $limit
     * @param <type> $name
     */
    public function getCityHotHotel($cityid, $limit, $orderby = 'Px_commend')
    {
        $sql = "SELECT hotel_id, hotelname, picture, min_price, cbd_name, cityid, haoping FROM $this->hotel_table ";
        $sql .= "WHERE cityid = $cityid ";
        $sql .= "ORDER BY $orderby DESC ";
        $sql .= "LIMIT 0, $limit";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

    public function getCityHotHotel_ary($cityid, $limit = 5)
    {
        $sql = "SELECT * FROM $this->cbd_table WHERE cityid=$cityid ORDER BY hotelnum DESC LIMIT 0,5";
        $cbd_array = $this->db->cacheGetAll($sql, 86400);
        $rs_array = array();
        foreach ($cbd_array AS $k => $v) {
            $sql = "SELECT hotel_id, hotelname, min_price, cbd_name, cityid, chain_name, chain_id, cbd_id FROM $this->hotel_table ";
            $sql .= "WHERE cityid = $cityid ";
            $sql .= "AND cbd_id = {$v['cbd_id']} ";
            $sql .= " AND min_price > 0 ";
            $sql .= "ORDER BY min_price ASC ";
            $sql .= "LIMIT 0, $limit";
            $rs = $this->db->getAll($sql);
            $rs_array[$k]['cbd_id'] = $v['cbd_id'];
            $rs_array[$k]['cbd_name'] = $v['cbd_name'];
            $rs_array[$k]['hotel_ary'] = $rs;
            $rs_array[$k]['rand'] = rand(1, 3);
            if (count($rs) == 0)
                unset($rs_array[$k]);
        }
//        print_r($rs_array);        
        return $rs_array;
    }
    
    /**
     * 为了解决REQUEST_URI不通用的问题
     */
	function request_uri()
	{
	    if (isset($_SERVER['REQUEST_URI']))
	    {
	        $uri = $_SERVER['REQUEST_URI'];
	    }
	    else
	    {
	        if (isset($_SERVER['argv']))
	        {
	            $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
	        }
	        else
	        {
	            $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
	        }
	    }
	    return $uri;
	}
    

    /**
     * 设置footer里的数据
     */
    public function footer()
    {
        $configcache = Tmac::config('configcache.config', APP_NAME, '.inc.php');
        $uri = $this->request_uri();
        $this_pageurl = 'http://' . $_SERVER['HTTP_HOST'] . $uri;

        $array['web_domain'] = 'http://' . $_SERVER['HTTP_HOST'] . '/';
        $array['cfg_webname'] = $configcache['cfg_webname'];
        $array['cfg_uid'] = $configcache['cfg_uid'];
        $array['cfg_powerby'] = $this->HD($configcache['cfg_powerby']);
        $array['this_pageurl'] = $this_pageurl;
        $array['domain'] = $configcache['cfg_indexurl'];
        $array['agent_id'] = $configcache['cfg_uid'];
        $array['agent_md'] = $configcache['cfg_mid'];
        $array['Order_api'] = $GLOBALS['TmacConfig']['config']['Order_api'];
        $array['picurl'] = !empty($configcache['cfg_rewrite_pic']) ? 'http://' . $_SERVER['HTTP_HOST'] . '/pic.php?p=' : $GLOBALS['TmacConfig']['config']['picurl'];
        $array['ZNpicurl'] = !empty($configcache['cfg_rewrite_pic']) ? 'http://' . $_SERVER['HTTP_HOST'] . '/pic.php?m=zn&p=' : $GLOBALS['TmacConfig']['config']['ZNpicurl'];
        return $array;
    }

    /**
     * 取默认城市信息
     * @return <type>
     */
    public function getCfgcity()
    {
        $cityid = Tmac::config('configcache.config.cfg_indexCitylist', APP_NAME, '.inc.php');
        $cityid = explode(',', $cityid);
        $cityid = explode('|', $cityid[0]);
        $cityname = $cityid[1];
        $cityinfo = array('cityid' => $cityid[0], 'cityname' => $cityid[1]);
        return $cityinfo;
    }

    /**
     * 从数据库中读取广告     
     * @param <type> $css
     */
    public function ShowAD($ad_name)
    {
        $sql = "SELECT * FROM $this->ad_table WHERE ad_name = '$ad_name' AND ad_state_radio<>3 ORDER BY ad_order DESC, ad_id DESC LIMIT 0,1";
        $rs = $this->db->cacheGetRow($sql, 3600);
        if ($rs) {
            //如果状态是正常 就开始判断开始和结束时间
            if ($rs['ad_state_radio'] == 1) {
                if ($rs['ad_starttime'] > time() || $rs['ad_endtime'] < time()) {
                    $ad_body = null;
                    return $ad_body;
                }
            }
            switch ($rs['ad_type_radio'])
            {
                case 1:
                    $ad_body = "<a href='{$rs['ad_link']}' target='_blank' title='{$rs['ad_title']}'>{$rs['ad_title']}</a>";
                    break;
                case 2:
                    $ad_body = "<a href='{$rs['ad_link']}' target='_blank' title='{$rs['ad_title']}'><img width='{$rs['ad_width']}' height='{$rs['ad_height']}' src='{$rs['ad_uploadfile']}' border='0' alt='{$rs['ad_title']}' /></a>";
                    break;
                case 3:
                    $ad_body = "{$rs['ad_externallinks']}";
                    break;
            }
        } else {
            $ad_body = null;
        }

        return $ad_body;
    }

    /**
     * 由pid 或 cityid得到city的info
     * @param <type> $id
     * @param <type> $type 默认p的时候就是pid $type = 'c' cityid得到
     * @return <type> 
     */
    public function getCityinfo($id, $type = 'p')
    {
        if ($type == 'p') {
            $sql = "SELECT * FROM $this->city_table WHERE province = $id LIMIT 0,1";
        } elseif ($type == 'pid') {
            $sql = "SELECT * FROM $this->city_table WHERE province = 1 AND pid = $id LIMIT 0,1";
        } else {
            $sql = "SELECT * FROM $this->city_table WHERE cityid = $id LIMIT 0,1";
        }
        $rs = $this->db->getRow($sql);
        return $rs;
    }

    /**
     * 取星级
     * @param <type> $rank 
     */
    public function hotelranknamenum($rank)
    {
        if ($rank > 6 and $rank < 17) {
            $hotelranknamenum = 2; //经济型
        } elseif ($rank > 4 and $rank < 7) {
            $hotelranknamenum = 3; //舒适型
        } elseif ($rank > 2 and $rank < 5) {
            $hotelranknamenum = 4; //高档型
        } elseif ($rank > 0 and $rank < 3) {
            $hotelranknamenum = 5; //豪华型
        }
        echo $hotelranknamenum;
    }

    public function hotelrankname($rank)
    {
        If ($rank > 6 and $rank < 17) {
            $hotelrankname = "二星级"; //经济型
        } ElseIf ($rank > 4 and $rank < 7) {
            $hotelrankname = "三星级"; //舒适型
        } ElseIf ($rank > 2 and $rank < 5) {
            $hotelrankname = "四星级"; //高档型
        } ElseIf ($rank > 0 and $rank < 3) {
            $hotelrankname = "五星级"; //豪华型
        }
        echo $hotelrankname;
    }

}