<?php
/**
 * 房源特色
 *
 * @author user
 */
class house_feature extends MY_Model {
    
    private $_tbl = 'house_feature';
    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct(0,1,3);
    }

    public function find_feature($tbl, $infotypes)
    {
        //修改数据来源。从接口取，接口取自后台。实现后台维护标签的功能； 2018-8-28 by lk
        $key = 'zsb-publish'.$infotypes;
        if($_GET['update']){
            $this->mc->delete($key);
        }

        $cache = $this->mc->get($key);

        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $features = $cache['data'];
        }else{
            $scene_tag = 'zsb-publish';
            $url = 'http://newrent.house365.com/api/common/get-scene-tag?city='.GB_CITY.'&is_cache=1&scene_tag='.$scene_tag."&infotype=".$infotypes;
            $res = file_get_contents($url);
            $data = json_decode($res);
            $data = $this->objtoarr($data);
            $res = $data['data']['list'];
            $features = array();
            foreach($res as $each){
                $features[$each['id']] = iconv('UTF8','GBK',$each['name']);
            }
            $this->mc->add($key , array('is_ok' => 1 , 'data' => $features) , 1800);
        }
        return $features;
    }

    protected function objtoarr($obj){
        $ret = array();
        foreach($obj as $key =>$value){
            if(gettype($value) == 'array' || gettype($value) == 'object'){
                $ret[$key] = $this->objtoarr($value);
            }else{
                $ret[$key] = $value;
            }
        }
        return $ret;
    }
    public function find_house_feature($feature, $tbl = 'sell')
    {
        $housefeatur = array();

        if($feature == '')
        {
            return $housefeatur;
        }
        $farr = explode("$", $feature);
        if(empty($farr) || !is_array($farr))
        {
            return $housefeatur;
        }
        else
        {
            $feature_arr = $this->find_feature($tbl, '');

            foreach($farr as $val)
            {
                $val = intval($val);
                if($val > 0 && $feature_arr[$val] != '')
                {
                    $housefeatur[$val] = $feature_arr[$val];
                }
            }
        }
        return $housefeatur;
    }
}
