+ add the feature of db config extension.

This commit is contained in:
wangchunsheng
2012-06-23 11:11:04 +00:00
parent 2f48429e9b
commit 30992c04fa
4 changed files with 78 additions and 6 deletions
+14 -3
View File
@@ -1366,6 +1366,9 @@ class router
*/
public function loadConfig($moduleName, $exitIfNone = true)
{
global $config;
if(!is_object($config)) $config = new config();
$extConfigFiles = array();
/* Set the main config file and extension config file. */
@@ -1384,7 +1387,7 @@ class router
if(!is_file($mainConfigFile))
{
if($exitIfNone) self::error("config file $mainConfigFile not found", __FILE__, __LINE__, true);
if(empty($extConfigFiles)) return false; // and no extension file, exit.
if(empty($extConfigFiles) and !isset($config->system->$moduleName)) return false; // and no extension file or extension in db, exit.
$configFiles = $extConfigFiles;
}
else
@@ -1392,8 +1395,6 @@ class router
$configFiles = array_merge(array($mainConfigFile), $extConfigFiles);
}
global $config;
if(!is_object($config)) $config = new config();
static $loadedConfigs = array();
foreach($configFiles as $configFile)
{
@@ -1402,6 +1403,16 @@ class router
$loadedConfigs[] = $configFile;
}
/* Merge from the db configs. */
if(isset($config->system->$moduleName))
{
foreach($config->system->$moduleName as $item)
{
if($item->section) $config->{$moduleName}->{$item->section}->{$item->key} = $item->value;
if(!$item->section) $config->{$moduleName}->{$item->key} = $item->value;
}
}
$this->config = $config;
return $config;
+1
View File
@@ -24,6 +24,7 @@ class common extends control
$this->common->sendHeader();
$this->common->setCompany();
$this->common->setUser();
$this->common->loadConfigFromDB();
$this->app->loadLang('company');
}
+25
View File
@@ -92,6 +92,31 @@ class commonModel extends model
}
}
/**
* Load configs from database and save it to config->system and config->personal.
*
* @access public
* @return void
*/
public function loadConfigFromDB()
{
$account = $this->app->user->account;
$config = $this->loadModel('setting')->getSysAndPersonalConfig($account);
$this->config->system = isset($config['system']) ? $config['system'] : array();
$this->config->personal = isset($config[$account]) ? $config[$account] : array();
/* Overide the items defined in config/config.php and config/my.php. */
if(isset($this->config->system->common))
{
foreach($this->config->system->common as $record)
{
if($record->section) $this->config->{$record->section}->{$record->key} = $record->value;
if(!$record->section) $this->config->{$record->key} = $record->value;
}
}
}
/**
* Juage a method of one module is open or not?
*
+38 -3
View File
@@ -22,26 +22,54 @@ class settingModel extends model
*/
public function getVersion()
{
$version = $this->getItem('system', 'global', 'version');
$version = $this->getItem('system', 'common', 'global', 'version');
if($version == '3.0.stable') $version = '3.0'; // convert 3.0.stable to 3.0.
if($version) return $version;
return '0.3 beta';
}
/**
* Get config of system and one user.
*
* @param string $account
* @access public
* @return array
*/
public function getSysAndPersonalConfig($account = '')
{
$owner = 'system,' . ($account ? $account : $this->app->user->account);
$records = $this->dao->select('owner, module, section, `key`, value')
->from(TABLE_CONFIG)
->where('owner')->in($owner)
->fetchAll();
if(!$records) return array();
/* Group records by owner and module. */
$config = array();
foreach($records as $record)
{
if($record->section) $config[$record->owner]->{$record->module}[] = $record;
if(!$record->section) $config[$record->owner]->{$record->module}[] = $record;
}
return $config;
}
/**
* Get value of an item.
*
* @param string $owner
* @param string $module
* @param string $section
* @param string $key
* @access public
* @return misc
*/
public function getItem($owner, $section, $key)
public function getItem($owner, $module, $section, $key)
{
return $this->dao->select('`value`')->from(TABLE_CONFIG)
->where('company')->eq(0)
->andWhere('owner')->eq($owner)
->andWhere('module')->eq($module)
->andWhere('section')->eq($section)
->andWhere('`key`')->eq($key)
->fetch('value', $autoCompany = false);
@@ -51,16 +79,18 @@ class settingModel extends model
* Set value of an item.
*
* @param string $owner
* @param string $module
* @param string $section
* @param string $key
* @param string $value
* @access public
* @return void
*/
public function setItem($owner, $section, $key, $value = '')
public function setItem($owner, $module, $section, $key, $value = '')
{
$item->company = 0;
$item->owner = $owner;
$item->module = $module;
$item->section = $section;
$item->key = $key;
$item->value = $value;
@@ -68,6 +98,7 @@ class settingModel extends model
$config = $this->dao->select('`value`')->from(TABLE_CONFIG)
->where('company')->eq(0)
->andWhere('owner')->eq($owner)
->andWhere('module')->eq($module)
->andWhere('section')->eq($section)
->andWhere('`key`')->eq($key)
->fetch('', $autoComapny = false);
@@ -106,6 +137,7 @@ class settingModel extends model
{
$item->company = 0;
$item->owner = 'system';
$item->module = 'common';
$item->section = 'global';
$item->key = 'sn';
$item->value = $this->computeSN();
@@ -113,6 +145,7 @@ class settingModel extends model
$config = $this->dao->select('id, value')->from(TABLE_CONFIG)
->where('company')->eq(0)
->andWhere('owner')->eq('system')
->andWhere('module')->eq('common')
->andWhere('section')->eq('global')
->andWhere('`key`')->eq('sn')
->fetch('', $autoComapny = false);
@@ -140,6 +173,7 @@ class settingModel extends model
{
$item->company = 0;
$item->owner = 'system';
$item->module = 'common';
$item->section = 'global';
$item->key = 'version';
$item->value = $version;
@@ -147,6 +181,7 @@ class settingModel extends model
$configID = $this->dao->select('id')->from(TABLE_CONFIG)
->where('company')->eq(0)
->andWhere('owner')->eq('system')
->andWhere('module')->eq('common')
->andWhere('section')->eq('global')
->andWhere('`key`')->eq('version')
->fetch('id', $autoComapny = false);