Settings.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App;
  3. class Settings
  4. {
  5. private $grupa;
  6. private $data = [];
  7. public function __construct($grupa)
  8. {
  9. $this->grupa = $grupa;
  10. $this->load();
  11. }
  12. private function load()
  13. {
  14. $sql = "SELECT name, value FROM settings WHERE grupa = ?";
  15. $results = db()->fetchAll($sql, [$this->grupa]);
  16. $this->data = [];
  17. foreach ($results as $row) {
  18. $this->data[$row['name']] = $row['value'];
  19. }
  20. }
  21. public function get($name, $default = null)
  22. {
  23. return isset($this->data[$name]) ? $this->data[$name] : $default;
  24. }
  25. public function getAll()
  26. {
  27. return $this->data;
  28. }
  29. public function set($name, $value)
  30. {
  31. $exists = db()->fetchOne("SELECT id FROM settings WHERE grupa = ? AND name = ?", [$this->grupa, $name]);
  32. if ($exists) {
  33. db()->execute("UPDATE settings SET value = ? WHERE grupa = ? AND name = ?", [$value, $this->grupa, $name]);
  34. } else {
  35. db()->execute("INSERT INTO settings (grupa, name, value) VALUES (?, ?, ?)", [$this->grupa, $name, $value]);
  36. }
  37. $this->data[$name] = $value;
  38. }
  39. public function delete($name)
  40. {
  41. db()->execute("DELETE FROM settings WHERE grupa = ? AND name = ?", [$this->grupa, $name]);
  42. unset($this->data[$name]);
  43. }
  44. }