config.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Model;
  3. /**
  4. * Class Config
  5. *
  6. * @property int $id
  7. * @property string $attribute
  8. * @property string $value
  9. */
  10. class Config extends \Model {
  11. protected $_table_name = "config";
  12. protected static $requiredFields = array('attribute', 'value');
  13. /**
  14. * Loads the configuration for the site
  15. */
  16. public static function loadAll() {
  17. $f3 = \Base::instance();
  18. $db = $f3->get("db.instance");
  19. $result = $db->exec("SELECT attribute,value FROM config");
  20. $foundAttributes = array();
  21. foreach($result as $item) {
  22. $foundAttributes[] = $item["attribute"];
  23. $f3->set($item["attribute"], $item["value"]);
  24. }
  25. if(!in_array("site.name", $foundAttributes)) {
  26. self::importAll();
  27. }
  28. }
  29. /**
  30. * Imports the settings from config.ini to the database
  31. *
  32. * This will overwrite config.ini with only database connection settings!
  33. */
  34. public static function importAll() {
  35. $f3 = \Base::instance();
  36. $root = $f3->get("ROOT") . $f3->get("BASE");
  37. // Import existing config
  38. $ini = parse_ini_file($root."/config.ini");
  39. $ini = $ini + parse_ini_file($root."/config-base.ini");
  40. foreach($ini as $key => $val) {
  41. if(substr($key, 0, 3) == "db.") {
  42. continue;
  43. }
  44. $conf = new Config;
  45. $conf->attribute = $key;
  46. $conf->value = $val;
  47. $conf->save();
  48. }
  49. // Write new config.ini
  50. $data = "[globals]\n";
  51. $data .= "db.host=\"{$ini['db.host']}\"\n";
  52. $data .= "db.user=\"{$ini['db.user']}\"\n";
  53. $data .= "db.pass=\"{$ini['db.pass']}\"\n";
  54. $data .= "db.name=\"{$ini['db.name']}\"\n";
  55. file_put_contents($root . "/config.ini", $data);
  56. }
  57. /**
  58. * Set a configuration value
  59. * @param string $key
  60. * @param mixed $value
  61. * @return Config
  62. */
  63. public static function setVal($key, $value) {
  64. $f3 = \Base::instance();
  65. $f3->set($key, $value);
  66. $item = new static();
  67. $item->load(array('attribute = ?', $key));
  68. $item->attribute = $key;
  69. $item->value = $value;
  70. $item->save();
  71. return $item;
  72. }
  73. }