<?php
// 配置模型
class ConfigModel extends Model {
	/**
	 * 保存配置信息
	 *
	 * @access public
	 * @param $config 配置数组
	 * @param $moduleName 模块名称
	 * @return array
	 */
	public function save($config, $moduleName = '') {
		if (is_array($config)) {
			if ($moduleName) {
				$this -> tableName = $moduleName . '_config';
			}
			foreach($config as $k => $v) {
				$this -> create(array('variable' => $k, 'value' => trim($v))) -> add(true);
			}
			$this -> cache($moduleName);
			return true;
		}
		return false;
	}
	/**
	 * 返回配置信息
	 *
	 * @access public
	 * @param $moduleName 模块名称
	 * @return array
	 */
	public function get($moduleName = '') {
		if ($moduleName) $this -> tableName = $moduleName . '_config';
		$r = $this -> select();
		$array = array();
		if (is_array($r)) {
			foreach($r as $t) $array[$t['variable']] = $t['value'];
		}
		return $array;
	}
	public function cache($moduleName = '') {
		if ($moduleName) $this -> tableName = $moduleName . '_config';
		$r = $this -> field(array('variable', 'value')) -> select();
		if (is_array($r)) {
			$array = array();
			foreach($r as $t) $array[$t['variable']] = $t['value'];
			Cache :: config($array, $moduleName);
		}
	}
}
