<?php

/**
 * 后台系统配置参数模块 Model
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: config.class.php 118 2012-01-11 08:46:16Z zhaojianjun $
 * http://un.zhuna.cn；
 */
class config extends Model
{

    private $db;
    private $config_table;

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

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addConfig($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->config_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个系统配置参数信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getConfigInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->config_table WHERE sysid = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getConfigList($url = null)
    {
        $rs = $this->db->getAll("SELECT * FROM $this->config_table ORDER BY `order` DESC, sysid ASC");

        foreach ($rs AS $k => $v) {
            if ($v['type'] == 'select') {
                $select_option = null;
                //如果item值不是从db中取或从config配置文件中取
                if ($v['item'] != 'db' && $v['item'] != 'config') {
                    $select_option_ary = explode('{|}', $v['item']);
                    //遍历item值
                    foreach ($select_option_ary AS $kk => $vv) {
                        $select_option_array = explode(':', $vv);
                        $selected = $select_option_array[1] == $v['value'] ? 'selected' : null;
                        $select_option .= "<option value='{$select_option_array[1]}' {$selected}>" . strip_tags($select_option_array[0]) . "</option>";
                    }
                    $rs[$k]['select'] = '<select name="' . $v['varname'] . '" id="' . $v['varname'] . '">' . $select_option . '</select>';
                } else {
                    //db 城市
                    if ($v['varname'] == 'cfg_indexCitylist') {
                        $tmp_model = Tmac::model('article');
                        //取省级名字
                        $province_ary = $tmp_model->getCity('p');
                        //把缩写和名字组合在一起
                        $province_array = array();
                        foreach ($province_ary AS $kkk => $vvv) {
                            $province_array[$vvv{'Pid'}] = $vvv['Pname'];
                        }
                        $select_option = Utility::Option($province_array);
                        $rs[$k]['select'] = '<select id="province" onchange="rplCity(this.value)"><option value="0">--请选择--</option>' . $select_option . '</select>';
                        $rs[$k]['select'] .= ' 省/直辖市&nbsp;&nbsp;&nbsp;&nbsp;';
                        $rs[$k]['select'] .= "<select id='city' onchange='return changtag(this.value);'>";
                        $rs[$k]['select'] .= "<option selected='selected' value='0'>--请先选择城市--</option>";
                        $rs[$k]['select'] .= "</select> 城市/区域";
                        $citylist = explode(',', $v['value']);
                        $citylist_count = count($citylist);
                        $citylist_rs = null;
                        if ($citylist_count > 0) {
                            for ($i = 0; $i < $citylist_count; $i++) {
                                $cityitem = explode("|", $citylist[$i]);
                                $citylist_rs .= "<span ondblclick=\"return updatename(this,'$cityitem[1]')\" style='border:#CCC solid 1px;padding:3px;cursor:move;' value='{$cityitem[1]}' id='{$cityitem[0]}'>$cityitem[1]<em style='cursor:pointer' onclick=\"return delCity($cityitem[0]);\">×</em></span>";
                            }
                        } else {
                            $cityitem = explode("|", $citylist[0]);
                            $citylist_rs = "<span ondblclick='return updatename(this,'{$cityitem[1]}')' style='border:#CCC solid 1px;padding:3px;cursor:move;' value='{$cityitem[1]}' id='{$cityitem[0]}'>{$cityitem[1]}<em style='cursor:pointer' onclick='return delCity({$cityitem[0]});'>×</em></span>";
                        }
                        $rs[$k]['select'] .= "<div id='cityList'>$citylist_rs</div>";
                        $rs[$k]['select'] .= "<input type='hidden' value='{$v['value']}' name='{$v['varname']}' id='{$v['varname']}' />";
                    }
                    if ($v['varname'] == 'cfg_apiurl') {
                        //接口地址
                        $cfg_apiurl_ary = UtilityConfig::cfg_apiurl_ary();
                        $select_option = Utility::Option($cfg_apiurl_ary, $v['value']);
                        $rs[$k]['select'] = '<select name="' . $v['varname'] . '" id="' . $v['varname'] . '">' . $select_option . '</select>';
                    }
                }
            } elseif ($v['type'] == 'radio') {
                $radio_option_ary = explode('{|}', $v['item']);
                $cbox = null;
                //遍历item值
                foreach ($radio_option_ary AS $kk => $vv) {
                    $radio_option_array = explode(':', $vv);
                    $checked = $radio_option_array[1] == $v['value'] ? 'checked' : null;
                    if($v['varname'] == 'cfg_servertype')
                    {
                    	$cbox .= "<input type='radio' name='{$v['varname']}' id='{$v['varname']}' onclick='getrewrite({$radio_option_array[1]})' value='{$radio_option_array[1]}' {$checked} {$v['nameaction']} />&nbsp;{$radio_option_array[0]}";
                    }else{
                    	$cbox .= "<input type='radio' name='{$v['varname']}' value='{$radio_option_array[1]}' {$checked} {$v['nameaction']} />&nbsp;{$radio_option_array[0]}";
                    }
                    
                }
                $rs[$k]['radio'] = $cbox;
            }
        }
        return $rs;
    }

    /**
     * 写静态配置文件config.cache.php
     */
    public function ReWriteConfig($configfile)
    {
        if (!is_writeable($configfile)) {
            echo "配置文件'{$configfile}'不支持写入，无法修改系统配置参数！";
            exit();
        }
        $fp = fopen($configfile, 'w');
        flock($fp, 3);
        fwrite($fp, "<" . "?php\r\n");
        $getconfig = $this->db->getAll("select * from $this->config_table");
        foreach ($getconfig as $k => $v) {
            fwrite($fp, "\$config['config']['{$v['varname']}'] = '" . $v['value'] . "';\r\n");
        }
        fwrite($fp, "?" . ">");
        fclose($fp);
    }

}