<?php
class setting
{
	var $channelid = 0;
	var $channelselect = 0;
	var $tables = array(
		1 => array('subject_pre' => '标题提示'),
		2 => array('subject_pre' => '标题提示', 'softtype' => '软件类型', 'use_type' => '授权方式', 'language' => '软件语言', 'plugin' =>'插件方式', 'systems'=>'运行环境'),
		3 => array('subject_pre' => '标题提示'),
		4 => array('subject_pre'=>'标题提示', 'play_area'=>'地区设置'),
	);
	function setting($channelid, $channelselect){
		$this->channelid = $channelid;
		$this->channelselect = $channelselect;
	}

	function op_get(){
		return $this->tables[$this->channelid];
	}
	
	function op_manage(){
		$temp = qdb::fetchAll( "select * from ".TABLEPRE."setting where channelid = '{$this->channelid}' " );
		$data = array();
		foreach($temp as $val){
			$data[$val['type']] = $val['val'];
		}
		return $data;
	}

	function op_get_cache(){
		if(false === ($data = cache::get('sys_setting'))){
			$data = $this->op_cache();
		}
		return $data;
	}

	function op_cache(){
		$r = qdb::fetchAll("select * from ".TABLEPRE."setting order by channelid asc");
		$data = array();
		$data_op = array();
		foreach($r as $ls) {
			$temp = array();
			$tt = explode("\n", $ls['val']);
			foreach($tt as $v) {
				$v = trim($v);
				if (!$v) continue;
				$temp[] = trim($v);
			} 
			$data[$ls['channelid']][$ls['type']] = $temp;
			$arr = array();
			foreach($temp as $k=>$v){
				$tp = explode('|', $v);
				$arr[$tp[0]] = trim($tp[1]);
			}
			$data_op[$ls['channelid']]['op_' . $ls['type']] = $arr;
		} 
		cache::set('sys_setting', $data, 86400 * 30);
		foreach($data_op as $cid => $val){
			cache::set('sys_setting_' . $cid, $val, 86400 * 30);
		}
		return $data;
	}

	function op_set(){
		foreach($this->tables[$this->channelselect] as $type=>$title){
			if(!isset($_POST[$type])) continue;
			$val = trim($_POST[$type]);
			qdb::Query("replace into ".TABLEPRE."setting (channelid, type, val) values ($this->channelid, '{$type}', '{$val}' )" );
		}
		$this->op_cache();
	}
}

?>
