<?php

/**
 * 前台 资讯信息 模块 Model
 * ============================================================================
 * ZhunaCMS 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: newsinfo.class.php 53 2011-12-05 11:10:53Z zhangwentao $
 * http://un.zhuna.cn；
 */
class newsinfo extends Model
{

    private $db;
    private $article_table;
    private $class_table;
    private $hotel_table;
    private $hotel_left_model;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->article_table = DB_PREFIX . 'article';
        $this->class_table = DB_PREFIX . 'article_class';
        $this->hotel_table = DB_PREFIX . 'hotel';
        $this->hotel_left_model = Tmac::model('hotelLeft');
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getArticleInfo($aid)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->article_table WHERE aid = $aid LIMIT 0,1");
        return $rs;
    }

    /**
     * 更新文章浏览量
     */
    public function updateArticle($aid)
    {
        $sql = "UPDATE $this->article_table SET view_num = view_num+1 WHERE aid = $aid";
        $rs = $this->db->execute($sql);
        return $rs;
    }

    /**
     * 最新资讯信息
     * @param <type> $limit
     */
    public function articleList($limit)
    {
        $rs = $this->db->getAll("SELECT aid, title FROM $this->article_table ORDER BY time DESC LIMIT 0, $limit");
        return $rs;
    }

    /**
     * 取替换的词
     * @param <type> $cityname
     * @param <type> $key
     * @param <type> $num
     * @return <type>
     */
    public function getHotel($cityname, $key, $num)
    {
        $picurl = $GLOBALS['TmacConfig']['config']['picurl'];
        $cityname = strip_tags($cityname); //去掉城市名字的html标签
        $key = strip_tags($key);

        $cityid = $this->hotel_left_model->getCityid($cityname);
        if (!empty($key)) {
            $html_key = str_replace('%', '', urlencode($key));
        } else {
            $html_key = '';
        }
        $keyName = 'hotel_id, hotelname, star, address, content, cbd_id, cbd_name, chain_name, chain_id, picture, haoping, cityname, cityid, min_price, nearbypoints';
        //缓存链接
        $thisfile_name = "hotel_{$cityid}_{$html_key}_{$num}.shtml";
        $thisfile_url = VAR_ROOT . "xml/html/";
        $thisurl = $thisfile_url . $thisfile_name;
        if ($this->hotel_left_model->checkXML($thisurl, 86400)) {   //判断缓存的文件是不是否存
            $hotel_juli_ary = array();
            $hotel_xml = array();
            if (!empty($key)) {
                $key_urlencode = urlencode($key);
                $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');
                $api_url = $cfg_apiurl . "getkeyxy_json.php?cityid={$cityid}";
                $keyword = urlencode($key);
                $api_url .= "&key={$keyword}";
//            echo $api_url.'<br>';
                $getkeyxy_result = json_decode(Functions::GetHttps($api_url), true);
                if ($getkeyxy_result['retmsg'] != 'ok') {
                    $getHotel = '<div class="no_find"><p>系统没有查询到"<font color="#207DD4">' . $searchArray['cityname'] . '  ' . $searchArray['key'] . '</font>"附近相关酒店,请更换条件或选择以下酒店。</p></div>';
                    return $getHotel;
                } else {
                    $key_x = $getkeyxy_result['reqdata']['x'];
                    $key_y = $getkeyxy_result['reqdata']['y'];
                    $key = $getkeyxy_result['reqdata']['result_q'];
                }

                $keyName .= ",juli({$key_x},{$key_y},x,y) AS juli ";
                $order = ' ORDER BY juli ASC';
            } else {
                $order = '';
            }

            $sql = "SELECT $keyName FROM $this->hotel_table ";
            $sql .= "WHERE cityid = {$cityid}";
            $sql = $sql . $order . " LIMIT $num";
            $rs = $this->db->getAll($sql);
            if ($rs) {
                $getHotel = "<div class=\"Article_hotel\">";
                foreach ($rs AS $kk => $vv) {
                    $juli = round($vv['juli'] / 1000, 2);
                    $address = Functions::cut_str($vv['address'], 15);
                    $getHotel .= "<dl><dt><a href='index.php?m=hotelpicture&hotel_id={$vv['hotel_id']}'><img width='100' height='75' alt='{$vv['hotelname']}' src='{$picurl}{$vv['picture']}'></a></dt><dd><a href='index.php?m=hotelinfo&hotel_id={$vv['hotel_id']}'><strong>{$vv['hotelname']}</strong></a><span title='地址:{$vv['address']}'>{$address}</span><br>最低{$vv['min_price']}元 <br>";
                    if (!empty($key)) {
                        $getHotel .= "距<b>{$key}</b>约<b style='color: red;'>{$juli}</b>公里";
                    } else {
                        $getHotel .= "<a title='{$vv['cbd_name']}酒店预订'  href='index.php?m=hotellist&cityid={$cityid}&cbd_id={$vv['cbd_id']}'>{$vv['cbd_name']}</a>";
                    }
                    $getHotel .= "</dd></dl>";
                }
                if (!empty($key)) {
                    $getHotel .= "<dl><a href='index.php?m=hotellist&cityid={$cityid}&key={$key_urlencode}'><b>查看更多{$cityname}{$key}附近酒店&gt;&gt;</b></a></dl>";
                } else {
                    $getHotel .= "<dl><a href='index.php?m=hotellist&cityid={$cityid}'><b>查看更多{$cityname}附近酒店&gt;&gt;</b></a></dl>";
                }
                $getHotel .= '</div>';
                if (!empty($getHotel) && $getHotel <> "#标签有误！请修改！#") {
                    $this->createHtml($thisfile_url, $thisfile_name, $getHotel);
                }
            } else {
                $getHotel = "#标签有误！请修改！#";
            }
        } else {
            $getHotel = file_get_contents($thisurl);
        }
        return $getHotel;
    }

    /**
     * 创建html
     * @param <type> $html_root  //创建的html本地路径
     * @param <type> $content   //content
     */
    public function createHtml($html_root, $html_name, $content)
    {
        $html_root_all = $html_root . $html_name;  //xml接口的本地路径+文件全名
        $image_model = $this->P('Image');
        $image_model->CheckFolder($html_root);
        if (file_put_contents($html_root_all, $content)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 替换content
     * @param <type> $content
     */
    public function replaceTag($content)
    {
        @preg_match_all("/{([^#]*)\#([^#]*)\#([0-9]*)}/", $content, $out);

        $search_str = empty($out[0][0]) ? '' : $out[0][0];
        $cityname = empty($out[1][0]) ? '' : $out[1][0];
        $key = empty($out[2][0]) ? '' : $out[2][0];
        $num = empty($out[3][0]) ? '10' : $out[3][0];

        $str = $this->getHotel($cityname, $key, $num);
        $content = str_replace($search_str, $str, $content);
        return $content;
    }

}