| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace Entity;
- class Config extends ModelBase
- {
- private static $config = array();
- protected function install()
- {
- $dbPrefix = $this->getDbPrefix();
- $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}config` (
- `lang` VARCHAR(8) NULL,
- `key` VARCHAR(64) NOT NULL,
- `value` TEXT NULL,
- UNIQUE(`lang`, `key`)
- )");
- if ($result === false)
- throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
- self::setConfig(null, "theme", "default");
- return true;
- }
- public static function setConfig($lang, $key, $value)
- {
- $fetcher = new self();
- $data = $fetcher->selects(array("lang" => $lang, "key" => $key));
- if (empty($data))
- {
- $data = new self();
- $data->lang = $lang;
- $data->key = $key;
- $data->value = $value;
- $data->save();
- }
- else
- {
- $data = $data[0];
- $data->value = $value;
- $data->save();
- }
- }
- public static function getConfig($lang =null, $key =null, $defaultValue =null)
- {
- $fetcher = new self();
- $_lang = $lang;
- if ($lang === null)
- $_lang = "nolang";
- if (isset(self::$config[$_lang]))
- return isset(self::$config[$_lang][$key]) ? self::$config[$_lang][$key] : $defaultValue;
- $values = $fetcher->selects(array("lang" => $lang));
- foreach ($values as $i)
- self::$config[$_lang][$i->key] = $i->value;
- if ($key)
- return (isset(self::$config[$_lang][$key]) ? self::$config[$_lang][$key] : $defaultValue);
- return $defaultValue;
- }
- }
|