gastrodata/Setting.class.php

46 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2020-08-02 12:07:03 +00:00
<?php
2020-08-09 12:51:57 +00:00
require_once("Database.class.php");
2020-08-02 12:07:03 +00:00
class Setting {
private $settingKey = "";
public function __construct($setting_key)
{
2020-08-09 12:51:57 +00:00
$this->settingKey = Database::escape_string($setting_key);
2020-08-02 12:07:03 +00:00
}
2020-08-20 18:37:49 +00:00
public function get($default="", $set=false)
2020-08-02 12:07:03 +00:00
{
$query = "SELECT `setting_value` FROM `settings` WHERE `setting_key` = '" . $this->settingKey . "';";
2020-08-09 12:51:57 +00:00
$result = Database::execute_query($query);
2020-08-02 12:07:03 +00:00
if ($result->num_rows == 1) {
2020-08-20 18:37:49 +00:00
return $result->fetch_assoc()["setting_value"];
2020-08-02 12:07:03 +00:00
} else {
if (!empty($default)) {
2020-08-20 18:37:49 +00:00
if ($set) {
$this->set($default);
}
2020-08-02 12:07:03 +00:00
return $default;
}
}
2020-08-20 18:37:49 +00:00
return false;
2020-08-02 12:07:03 +00:00
}
public function set($value)
{
2020-08-09 12:51:57 +00:00
$escaped_value = Database::escape_string($value);
2020-08-02 12:07:03 +00:00
if ($this->get()) {
$query = "UPDATE `settings` SET `setting_value` = '" . $escaped_value . "' WHERE `setting_key` = '" . $this->settingKey . "';";
} else {
$query = "INSERT INTO `settings` (`setting_key`, `setting_value`) VALUES ('". $this->settingKey . "', '" . $escaped_value . "');";
}
2020-08-09 12:51:57 +00:00
if (!Database::execute_query($query)) {
return false;
2020-08-02 12:07:03 +00:00
}
return true;
2020-08-02 12:07:03 +00:00
}
}