Config.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. return true;
  18. }
  19. public static function setConfig($lang, $key, $value)
  20. {
  21. $fetcher = new self();
  22. $data = $fetcher->selects(array("lang" => $lang, "key" => $key));
  23. if (empty($data))
  24. {
  25. $data = new self();
  26. $data->lang = $lang;
  27. $data->key = $key;
  28. $data->value = $value;
  29. $data->save();
  30. }
  31. else
  32. {
  33. $data = $data[0];
  34. $data->value = $value;
  35. $data->save();
  36. }
  37. }
  38. public static function getConfig($lang =null, $key =null, $defaultValue =null)
  39. {
  40. $fetcher = new self();
  41. if (isset(self::$config[$lang]))
  42. return;
  43. $values = $fetcher->selects(array("lang" => $lang));
  44. foreach ($values as $i)
  45. self::$config[$lang][$i->key] = $i->value;
  46. if ($key)
  47. return (isset(self::$config[$lang][$key]) ? self::$config[$lang][$key] : $defaultValue);
  48. return $defaultValue;
  49. }
  50. }