Config.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Entity;
  3. class Config extends ModelBase
  4. {
  5. private static $config = array();
  6. protected function install()
  7. {
  8. $dbPrefix = $this->getDbPrefix();
  9. $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}config` (
  10. `lang` VARCHAR(8) NULL,
  11. `key` VARCHAR(64) NOT NULL,
  12. `value` TEXT NULL,
  13. UNIQUE(`lang`, `key`)
  14. )");
  15. if ($result === false)
  16. throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
  17. self::setConfig(null, "theme", "default");
  18. return true;
  19. }
  20. public static function setConfig($lang, $key, $value)
  21. {
  22. $fetcher = new self();
  23. $data = $fetcher->selects(array("lang" => $lang, "key" => $key));
  24. if (empty($data))
  25. {
  26. $data = new self();
  27. $data->lang = $lang;
  28. $data->key = $key;
  29. $data->value = $value;
  30. $data->save();
  31. }
  32. else
  33. {
  34. $data = $data[0];
  35. $data->value = $value;
  36. $data->save();
  37. }
  38. }
  39. public static function getConfig($lang =null, $key =null, $defaultValue =null)
  40. {
  41. $fetcher = new self();
  42. $_lang = $lang;
  43. if ($lang === null)
  44. $_lang = "nolang";
  45. if (isset(self::$config[$_lang]))
  46. return isset(self::$config[$_lang][$key]) ? self::$config[$_lang][$key] : $defaultValue;
  47. $values = $fetcher->selects(array("lang" => $lang));
  48. foreach ($values as $i)
  49. self::$config[$_lang][$i->key] = $i->value;
  50. if ($key)
  51. return (isset(self::$config[$_lang][$key]) ? self::$config[$_lang][$key] : $defaultValue);
  52. return $defaultValue;
  53. }
  54. }