@@ -25,277 +25,277 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class Settings { |
| 27 | 27 | |
| 28 | - //in-memory cache of settings |
|
| 29 | - protected static $settings = array(); |
|
| 30 | - |
|
| 31 | - //flag, if global settings was initialized |
|
| 32 | - protected static $initialized = false; |
|
| 33 | - |
|
| 34 | - protected static $async_save_list = array(); |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * get value of setting |
|
| 38 | - * |
|
| 39 | - * @package com.jukusoft.cms.settings |
|
| 40 | - * |
|
| 41 | - * @param key setting |
|
| 42 | - * |
|
| 43 | - * @throws IllegalStateException of key doesnt exists and no default value is set |
|
| 44 | - * |
|
| 45 | - * @return mixed or null, if key doesnt exists in database / default value, if set |
|
| 46 | - */ |
|
| 47 | - public static function get (string $key, $default_value = null) { |
|
| 48 | - //load settings if neccessary |
|
| 49 | - self::loadSettingsIfAbsent(); |
|
| 50 | - |
|
| 51 | - if (!isset(self::$settings[$key])) { |
|
| 52 | - if ($default_value !== null) { |
|
| 53 | - return $default_value; |
|
| 54 | - } else { |
|
| 55 | - throw new IllegalStateException("Settings key '" . $key . "' doesnt exists."); |
|
| 56 | - } |
|
| 57 | - } else { |
|
| 58 | - return unserialize(self::$settings[$key]); |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * set setting |
|
| 64 | - * |
|
| 65 | - * @param $key setting |
|
| 66 | - * @param $value mixed value to set |
|
| 67 | - */ |
|
| 68 | - public static function set (string $key, $value) { |
|
| 69 | - //serialize data |
|
| 70 | - $value = serialize($value); |
|
| 71 | - |
|
| 72 | - //update database |
|
| 73 | - Database::getInstance()->execute("UPDATE `{praefix}global_settings` SET `value` = :value WHERE `key` = :key;", array( |
|
| 74 | - 'value' => $value, |
|
| 75 | - 'key' => $key |
|
| 76 | - )); |
|
| 77 | - |
|
| 78 | - //update local in-memory cache |
|
| 79 | - self::$settings[$key] = $value; |
|
| 80 | - |
|
| 81 | - //clear cache (area "global_settings") |
|
| 82 | - Cache::clear("global_settings"); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public static function setAsync (string $key, $value) { |
|
| 86 | - self::$async_save_list[$key] = serialize($value); |
|
| 87 | - |
|
| 88 | - //update local in-memory cache |
|
| 89 | - self::$settings[$key] = $value; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public static function saveAsync () { |
|
| 93 | - if (!empty(self::$async_save_list)) { |
|
| 94 | - foreach (self::$async_save_list as $key=>$value) { |
|
| 95 | - Settings::set($key, $value); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * check if settings key exists |
|
| 102 | - * |
|
| 103 | - * @param $key string settings key |
|
| 104 | - * |
|
| 105 | - * @return true if setting key exists |
|
| 106 | - */ |
|
| 107 | - public static function contains (string $key) : bool { |
|
| 108 | - //load settings if neccessary |
|
| 109 | - self::loadSettingsIfAbsent(); |
|
| 110 | - |
|
| 111 | - //escape key |
|
| 112 | - //$key = Database::getInstance()->escape($key); |
|
| 113 | - |
|
| 114 | - return isset(self::$settings[$key]); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * set setting if key is absent |
|
| 119 | - * |
|
| 120 | - * @param $key setting |
|
| 121 | - * @param $value mixed value to set |
|
| 122 | - */ |
|
| 123 | - public static function setIfAbsent (string $key, $value) { |
|
| 124 | - self::loadSettingsIfAbsent(); |
|
| 125 | - |
|
| 126 | - if (!isset(self::$settings[$key])) { |
|
| 127 | - self::set($key, $value); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * create setting (so it can be shown on settings page) |
|
| 133 | - */ |
|
| 134 | - public static function create (string $key, $value, string $title, string $description, string $owner, string $category = "general", string $datatype = "DataType_String", $datatype_params = "", bool $editable = true, $visible_permissions = "can_see_global_settings", $change_permissions = "can_change_global_settings", int $order = 10, string $icon_path = "none", string $last_update = "0000-00-00 00:00:00") { |
|
| 135 | - self::loadSettingsIfAbsent(); |
|
| 136 | - |
|
| 137 | - if (strlen($key) > 255) { |
|
| 138 | - throw new IllegalArgumentException("max key length is 255, your key: " . $key); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - //check, if setting already exists |
|
| 142 | - /*if (isset(self::$settings[$key])) { |
|
| 28 | + //in-memory cache of settings |
|
| 29 | + protected static $settings = array(); |
|
| 30 | + |
|
| 31 | + //flag, if global settings was initialized |
|
| 32 | + protected static $initialized = false; |
|
| 33 | + |
|
| 34 | + protected static $async_save_list = array(); |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * get value of setting |
|
| 38 | + * |
|
| 39 | + * @package com.jukusoft.cms.settings |
|
| 40 | + * |
|
| 41 | + * @param key setting |
|
| 42 | + * |
|
| 43 | + * @throws IllegalStateException of key doesnt exists and no default value is set |
|
| 44 | + * |
|
| 45 | + * @return mixed or null, if key doesnt exists in database / default value, if set |
|
| 46 | + */ |
|
| 47 | + public static function get (string $key, $default_value = null) { |
|
| 48 | + //load settings if neccessary |
|
| 49 | + self::loadSettingsIfAbsent(); |
|
| 50 | + |
|
| 51 | + if (!isset(self::$settings[$key])) { |
|
| 52 | + if ($default_value !== null) { |
|
| 53 | + return $default_value; |
|
| 54 | + } else { |
|
| 55 | + throw new IllegalStateException("Settings key '" . $key . "' doesnt exists."); |
|
| 56 | + } |
|
| 57 | + } else { |
|
| 58 | + return unserialize(self::$settings[$key]); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * set setting |
|
| 64 | + * |
|
| 65 | + * @param $key setting |
|
| 66 | + * @param $value mixed value to set |
|
| 67 | + */ |
|
| 68 | + public static function set (string $key, $value) { |
|
| 69 | + //serialize data |
|
| 70 | + $value = serialize($value); |
|
| 71 | + |
|
| 72 | + //update database |
|
| 73 | + Database::getInstance()->execute("UPDATE `{praefix}global_settings` SET `value` = :value WHERE `key` = :key;", array( |
|
| 74 | + 'value' => $value, |
|
| 75 | + 'key' => $key |
|
| 76 | + )); |
|
| 77 | + |
|
| 78 | + //update local in-memory cache |
|
| 79 | + self::$settings[$key] = $value; |
|
| 80 | + |
|
| 81 | + //clear cache (area "global_settings") |
|
| 82 | + Cache::clear("global_settings"); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public static function setAsync (string $key, $value) { |
|
| 86 | + self::$async_save_list[$key] = serialize($value); |
|
| 87 | + |
|
| 88 | + //update local in-memory cache |
|
| 89 | + self::$settings[$key] = $value; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public static function saveAsync () { |
|
| 93 | + if (!empty(self::$async_save_list)) { |
|
| 94 | + foreach (self::$async_save_list as $key=>$value) { |
|
| 95 | + Settings::set($key, $value); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * check if settings key exists |
|
| 102 | + * |
|
| 103 | + * @param $key string settings key |
|
| 104 | + * |
|
| 105 | + * @return true if setting key exists |
|
| 106 | + */ |
|
| 107 | + public static function contains (string $key) : bool { |
|
| 108 | + //load settings if neccessary |
|
| 109 | + self::loadSettingsIfAbsent(); |
|
| 110 | + |
|
| 111 | + //escape key |
|
| 112 | + //$key = Database::getInstance()->escape($key); |
|
| 113 | + |
|
| 114 | + return isset(self::$settings[$key]); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * set setting if key is absent |
|
| 119 | + * |
|
| 120 | + * @param $key setting |
|
| 121 | + * @param $value mixed value to set |
|
| 122 | + */ |
|
| 123 | + public static function setIfAbsent (string $key, $value) { |
|
| 124 | + self::loadSettingsIfAbsent(); |
|
| 125 | + |
|
| 126 | + if (!isset(self::$settings[$key])) { |
|
| 127 | + self::set($key, $value); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * create setting (so it can be shown on settings page) |
|
| 133 | + */ |
|
| 134 | + public static function create (string $key, $value, string $title, string $description, string $owner, string $category = "general", string $datatype = "DataType_String", $datatype_params = "", bool $editable = true, $visible_permissions = "can_see_global_settings", $change_permissions = "can_change_global_settings", int $order = 10, string $icon_path = "none", string $last_update = "0000-00-00 00:00:00") { |
|
| 135 | + self::loadSettingsIfAbsent(); |
|
| 136 | + |
|
| 137 | + if (strlen($key) > 255) { |
|
| 138 | + throw new IllegalArgumentException("max key length is 255, your key: " . $key); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + //check, if setting already exists |
|
| 142 | + /*if (isset(self::$settings[$key])) { |
|
| 143 | 143 | throw new IllegalArgumentException("global setting key '" . $key . "' already exists in database."); |
| 144 | 144 | }*/ |
| 145 | 145 | |
| 146 | - //allow more than one possible permission as array |
|
| 147 | - if (is_array($visible_permissions)) { |
|
| 148 | - $visible_permissions = implode("|", $visible_permissions); |
|
| 149 | - } |
|
| 146 | + //allow more than one possible permission as array |
|
| 147 | + if (is_array($visible_permissions)) { |
|
| 148 | + $visible_permissions = implode("|", $visible_permissions); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - //allow more than one possible permission as array |
|
| 152 | - if (is_array($change_permissions)) { |
|
| 153 | - $change_permissions = implode("|", $change_permissions); |
|
| 154 | - } |
|
| 151 | + //allow more than one possible permission as array |
|
| 152 | + if (is_array($change_permissions)) { |
|
| 153 | + $change_permissions = implode("|", $change_permissions); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - //serialize value |
|
| 157 | - $value = serialize($value); |
|
| 158 | - $datatype_params = serialize($datatype_params); |
|
| 156 | + //serialize value |
|
| 157 | + $value = serialize($value); |
|
| 158 | + $datatype_params = serialize($datatype_params); |
|
| 159 | 159 | |
| 160 | - //insert setting into database |
|
| 161 | - Database::getInstance()->execute("INSERT INTO `{praefix}global_settings` ( |
|
| 160 | + //insert setting into database |
|
| 161 | + Database::getInstance()->execute("INSERT INTO `{praefix}global_settings` ( |
|
| 162 | 162 | `key`, `value`, `title`, `description`, `visible_permission`, `change_permission`, `owner`, `order`, `icon_path`, `last_update`, `datatype`, `datatype_params`, `editable`, `category`, `activated` |
| 163 | 163 | ) VALUES ( |
| 164 | 164 | :key, :value, :title, :description, :visible_permissions, :change_permissions, :owner, :order, :icon_path, '0000-00-00 00:00:00', :datatype, :datatype_params, :editable, :category, :activated |
| 165 | 165 | ) ON DUPLICATE KEY UPDATE `title` = :title, `description` = :description, `visible_permission` = :visible_permissions, `change_permission` = :change_permissions, `owner` = :owner, `order` = :order, `icon_path` = :icon_path, `last_update` = CURRENT_TIMESTAMP, `datatype` = :datatype, `datatype_params` = :datatype_params, `editable` = :editable, `category` = :category; ", array( |
| 166 | - 'key' => $key, |
|
| 167 | - 'value' => $value, |
|
| 168 | - 'title' => $title, |
|
| 169 | - 'description' => $description, |
|
| 170 | - 'visible_permissions' => $visible_permissions, |
|
| 171 | - 'change_permissions' => $change_permissions, |
|
| 172 | - 'owner' => $owner, |
|
| 173 | - 'order' => (int) $order, |
|
| 174 | - 'icon_path' => $icon_path, |
|
| 175 | - 'datatype' => $datatype, |
|
| 176 | - 'datatype_params' => $datatype_params, |
|
| 177 | - 'editable' => ($editable ? 1 : 0), |
|
| 178 | - 'category' => $category, |
|
| 179 | - 'activated' => 1 |
|
| 180 | - )); |
|
| 181 | - |
|
| 182 | - //update value in local in-memory cache |
|
| 183 | - self::$settings[$key] = $value; |
|
| 184 | - |
|
| 185 | - //clear cache (area "global_settings") |
|
| 186 | - Cache::clear("global_settings"); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * delete setting |
|
| 191 | - */ |
|
| 192 | - public static function delete (string $key) { |
|
| 193 | - //remove from in-memory cache |
|
| 194 | - unset(self::$settings[$key]); |
|
| 195 | - |
|
| 196 | - //remove key in database |
|
| 197 | - Database::getInstance()->execute("DELETE FROM `{praefix}global_settings` WHERE `key` = :key; ", array('key' => $key)); |
|
| 198 | - |
|
| 199 | - //clear cache (area "global_settings") |
|
| 200 | - Cache::clear("global_settings"); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * initialize settings and get global settings |
|
| 205 | - */ |
|
| 206 | - protected static function loadCategorySettings (string $category) { |
|
| 207 | - //$category = Database::getInstance()->escape($category); |
|
| 208 | - |
|
| 209 | - $category_settings = array(); |
|
| 210 | - |
|
| 211 | - if (Cache::contains("global_settings", "settings-category-" + $category)) { |
|
| 212 | - $category_settings = Cache::get("global_settings", "settings-category-" + $category); |
|
| 213 | - } else { |
|
| 214 | - //load settings from database |
|
| 215 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `category` = :category AND `activated` = '1' ORDER BY `order`; ", array( |
|
| 216 | - 'category' => array( |
|
| 217 | - 'type' => PDO::PARAM_STR, |
|
| 218 | - 'value' => $category |
|
| 219 | - ) |
|
| 220 | - )); |
|
| 221 | - |
|
| 222 | - foreach ($rows as $row) { |
|
| 223 | - $category_settings[$row['key']] = $row['value']; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - //cache rows |
|
| 227 | - Cache::put("global_settings", "settings-category-" + $category, $category_settings); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - //merge arrays |
|
| 231 | - self::$settings = array_merge(self::$settings, $category_settings); |
|
| 232 | - |
|
| 233 | - self::$initialized = true; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * initialize settings and get global settings |
|
| 238 | - */ |
|
| 239 | - protected static function loadAllSettings () { |
|
| 240 | - if (Cache::contains("global_settings", "all-settings")) { |
|
| 241 | - self::$settings = Cache::get("global_settings", "all-settings"); |
|
| 242 | - } else { |
|
| 243 | - //load settings from database |
|
| 244 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `activated` = '1' ORDER BY `order`; "); |
|
| 166 | + 'key' => $key, |
|
| 167 | + 'value' => $value, |
|
| 168 | + 'title' => $title, |
|
| 169 | + 'description' => $description, |
|
| 170 | + 'visible_permissions' => $visible_permissions, |
|
| 171 | + 'change_permissions' => $change_permissions, |
|
| 172 | + 'owner' => $owner, |
|
| 173 | + 'order' => (int) $order, |
|
| 174 | + 'icon_path' => $icon_path, |
|
| 175 | + 'datatype' => $datatype, |
|
| 176 | + 'datatype_params' => $datatype_params, |
|
| 177 | + 'editable' => ($editable ? 1 : 0), |
|
| 178 | + 'category' => $category, |
|
| 179 | + 'activated' => 1 |
|
| 180 | + )); |
|
| 181 | + |
|
| 182 | + //update value in local in-memory cache |
|
| 183 | + self::$settings[$key] = $value; |
|
| 184 | + |
|
| 185 | + //clear cache (area "global_settings") |
|
| 186 | + Cache::clear("global_settings"); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * delete setting |
|
| 191 | + */ |
|
| 192 | + public static function delete (string $key) { |
|
| 193 | + //remove from in-memory cache |
|
| 194 | + unset(self::$settings[$key]); |
|
| 195 | + |
|
| 196 | + //remove key in database |
|
| 197 | + Database::getInstance()->execute("DELETE FROM `{praefix}global_settings` WHERE `key` = :key; ", array('key' => $key)); |
|
| 198 | + |
|
| 199 | + //clear cache (area "global_settings") |
|
| 200 | + Cache::clear("global_settings"); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * initialize settings and get global settings |
|
| 205 | + */ |
|
| 206 | + protected static function loadCategorySettings (string $category) { |
|
| 207 | + //$category = Database::getInstance()->escape($category); |
|
| 208 | + |
|
| 209 | + $category_settings = array(); |
|
| 210 | + |
|
| 211 | + if (Cache::contains("global_settings", "settings-category-" + $category)) { |
|
| 212 | + $category_settings = Cache::get("global_settings", "settings-category-" + $category); |
|
| 213 | + } else { |
|
| 214 | + //load settings from database |
|
| 215 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `category` = :category AND `activated` = '1' ORDER BY `order`; ", array( |
|
| 216 | + 'category' => array( |
|
| 217 | + 'type' => PDO::PARAM_STR, |
|
| 218 | + 'value' => $category |
|
| 219 | + ) |
|
| 220 | + )); |
|
| 221 | + |
|
| 222 | + foreach ($rows as $row) { |
|
| 223 | + $category_settings[$row['key']] = $row['value']; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + //cache rows |
|
| 227 | + Cache::put("global_settings", "settings-category-" + $category, $category_settings); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + //merge arrays |
|
| 231 | + self::$settings = array_merge(self::$settings, $category_settings); |
|
| 232 | + |
|
| 233 | + self::$initialized = true; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * initialize settings and get global settings |
|
| 238 | + */ |
|
| 239 | + protected static function loadAllSettings () { |
|
| 240 | + if (Cache::contains("global_settings", "all-settings")) { |
|
| 241 | + self::$settings = Cache::get("global_settings", "all-settings"); |
|
| 242 | + } else { |
|
| 243 | + //load settings from database |
|
| 244 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `activated` = '1' ORDER BY `order`; "); |
|
| 245 | 245 | |
| 246 | - self::$settings = array(); |
|
| 246 | + self::$settings = array(); |
|
| 247 | 247 | |
| 248 | - foreach ($rows as $row) { |
|
| 249 | - self::$settings[$row['key']] = $row['value']; |
|
| 250 | - } |
|
| 248 | + foreach ($rows as $row) { |
|
| 249 | + self::$settings[$row['key']] = $row['value']; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - //cache rows |
|
| 253 | - Cache::put("global_settings", "all-settings", self::$settings); |
|
| 254 | - } |
|
| 252 | + //cache rows |
|
| 253 | + Cache::put("global_settings", "all-settings", self::$settings); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - self::$initialized = true; |
|
| 257 | - } |
|
| 256 | + self::$initialized = true; |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - public static function listAllSettingsByCategory () { |
|
| 260 | - //load settings if neccessary |
|
| 261 | - self::loadSettingsIfAbsent(); |
|
| 259 | + public static function listAllSettingsByCategory () { |
|
| 260 | + //load settings if neccessary |
|
| 261 | + self::loadSettingsIfAbsent(); |
|
| 262 | 262 | |
| 263 | - $rows = array(); |
|
| 263 | + $rows = array(); |
|
| 264 | 264 | |
| 265 | - if (Cache::contains("global_settings", "all_rows")) { |
|
| 266 | - $rows = Cache::get("global_settings", "all_rows"); |
|
| 267 | - } else { |
|
| 268 | - //load settings from database |
|
| 269 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `activated` = '1' ORDER BY `order`; "); |
|
| 265 | + if (Cache::contains("global_settings", "all_rows")) { |
|
| 266 | + $rows = Cache::get("global_settings", "all_rows"); |
|
| 267 | + } else { |
|
| 268 | + //load settings from database |
|
| 269 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `activated` = '1' ORDER BY `order`; "); |
|
| 270 | 270 | |
| 271 | - Cache::put("global_settings", "all_rows", $rows); |
|
| 272 | - } |
|
| 271 | + Cache::put("global_settings", "all_rows", $rows); |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - $list = array(); |
|
| 274 | + $list = array(); |
|
| 275 | 275 | |
| 276 | - foreach ($rows as $row) { |
|
| 277 | - $category = $row['category']; |
|
| 278 | - $key = $row['key']; |
|
| 276 | + foreach ($rows as $row) { |
|
| 277 | + $category = $row['category']; |
|
| 278 | + $key = $row['key']; |
|
| 279 | 279 | |
| 280 | - //add array, if key doesnt exists in array |
|
| 281 | - if (!isset($list[$category])) { |
|
| 282 | - $list[$category] = array(); |
|
| 283 | - } |
|
| 280 | + //add array, if key doesnt exists in array |
|
| 281 | + if (!isset($list[$category])) { |
|
| 282 | + $list[$category] = array(); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - $list[$category][$key] = $row; |
|
| 286 | - } |
|
| 285 | + $list[$category][$key] = $row; |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - return $list; |
|
| 289 | - } |
|
| 288 | + return $list; |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * load settings, if class was not initialized yet |
|
| 293 | - */ |
|
| 294 | - protected static function loadSettingsIfAbsent () { |
|
| 295 | - if (!self::$initialized) { |
|
| 296 | - self::loadAllSettings(); |
|
| 297 | - } |
|
| 298 | - } |
|
| 291 | + /** |
|
| 292 | + * load settings, if class was not initialized yet |
|
| 293 | + */ |
|
| 294 | + protected static function loadSettingsIfAbsent () { |
|
| 295 | + if (!self::$initialized) { |
|
| 296 | + self::loadAllSettings(); |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | 300 | } |
| 301 | 301 | |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @return mixed or null, if key doesnt exists in database / default value, if set |
| 46 | 46 | */ |
| 47 | - public static function get (string $key, $default_value = null) { |
|
| 47 | + public static function get(string $key, $default_value = null) { |
|
| 48 | 48 | //load settings if neccessary |
| 49 | 49 | self::loadSettingsIfAbsent(); |
| 50 | 50 | |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * @param $key setting |
| 66 | 66 | * @param $value mixed value to set |
| 67 | 67 | */ |
| 68 | - public static function set (string $key, $value) { |
|
| 68 | + public static function set(string $key, $value) { |
|
| 69 | 69 | //serialize data |
| 70 | 70 | $value = serialize($value); |
| 71 | 71 | |
@@ -82,14 +82,14 @@ discard block |
||
| 82 | 82 | Cache::clear("global_settings"); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public static function setAsync (string $key, $value) { |
|
| 85 | + public static function setAsync(string $key, $value) { |
|
| 86 | 86 | self::$async_save_list[$key] = serialize($value); |
| 87 | 87 | |
| 88 | 88 | //update local in-memory cache |
| 89 | 89 | self::$settings[$key] = $value; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - public static function saveAsync () { |
|
| 92 | + public static function saveAsync() { |
|
| 93 | 93 | if (!empty(self::$async_save_list)) { |
| 94 | 94 | foreach (self::$async_save_list as $key=>$value) { |
| 95 | 95 | Settings::set($key, $value); |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | * |
| 105 | 105 | * @return true if setting key exists |
| 106 | 106 | */ |
| 107 | - public static function contains (string $key) : bool { |
|
| 107 | + public static function contains(string $key) : bool { |
|
| 108 | 108 | //load settings if neccessary |
| 109 | 109 | self::loadSettingsIfAbsent(); |
| 110 | 110 | |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | * @param $key setting |
| 121 | 121 | * @param $value mixed value to set |
| 122 | 122 | */ |
| 123 | - public static function setIfAbsent (string $key, $value) { |
|
| 123 | + public static function setIfAbsent(string $key, $value) { |
|
| 124 | 124 | self::loadSettingsIfAbsent(); |
| 125 | 125 | |
| 126 | 126 | if (!isset(self::$settings[$key])) { |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | /** |
| 132 | 132 | * create setting (so it can be shown on settings page) |
| 133 | 133 | */ |
| 134 | - public static function create (string $key, $value, string $title, string $description, string $owner, string $category = "general", string $datatype = "DataType_String", $datatype_params = "", bool $editable = true, $visible_permissions = "can_see_global_settings", $change_permissions = "can_change_global_settings", int $order = 10, string $icon_path = "none", string $last_update = "0000-00-00 00:00:00") { |
|
| 134 | + public static function create(string $key, $value, string $title, string $description, string $owner, string $category = "general", string $datatype = "DataType_String", $datatype_params = "", bool $editable = true, $visible_permissions = "can_see_global_settings", $change_permissions = "can_change_global_settings", int $order = 10, string $icon_path = "none", string $last_update = "0000-00-00 00:00:00") { |
|
| 135 | 135 | self::loadSettingsIfAbsent(); |
| 136 | 136 | |
| 137 | 137 | if (strlen($key) > 255) { |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | /** |
| 190 | 190 | * delete setting |
| 191 | 191 | */ |
| 192 | - public static function delete (string $key) { |
|
| 192 | + public static function delete(string $key) { |
|
| 193 | 193 | //remove from in-memory cache |
| 194 | 194 | unset(self::$settings[$key]); |
| 195 | 195 | |
@@ -203,13 +203,13 @@ discard block |
||
| 203 | 203 | /** |
| 204 | 204 | * initialize settings and get global settings |
| 205 | 205 | */ |
| 206 | - protected static function loadCategorySettings (string $category) { |
|
| 206 | + protected static function loadCategorySettings(string $category) { |
|
| 207 | 207 | //$category = Database::getInstance()->escape($category); |
| 208 | 208 | |
| 209 | 209 | $category_settings = array(); |
| 210 | 210 | |
| 211 | - if (Cache::contains("global_settings", "settings-category-" + $category)) { |
|
| 212 | - $category_settings = Cache::get("global_settings", "settings-category-" + $category); |
|
| 211 | + if (Cache::contains("global_settings", "settings-category-" +$category)) { |
|
| 212 | + $category_settings = Cache::get("global_settings", "settings-category-" +$category); |
|
| 213 | 213 | } else { |
| 214 | 214 | //load settings from database |
| 215 | 215 | $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}global_settings` WHERE `category` = :category AND `activated` = '1' ORDER BY `order`; ", array( |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | //cache rows |
| 227 | - Cache::put("global_settings", "settings-category-" + $category, $category_settings); |
|
| 227 | + Cache::put("global_settings", "settings-category-" +$category, $category_settings); |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | //merge arrays |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | /** |
| 237 | 237 | * initialize settings and get global settings |
| 238 | 238 | */ |
| 239 | - protected static function loadAllSettings () { |
|
| 239 | + protected static function loadAllSettings() { |
|
| 240 | 240 | if (Cache::contains("global_settings", "all-settings")) { |
| 241 | 241 | self::$settings = Cache::get("global_settings", "all-settings"); |
| 242 | 242 | } else { |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | self::$initialized = true; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | - public static function listAllSettingsByCategory () { |
|
| 259 | + public static function listAllSettingsByCategory() { |
|
| 260 | 260 | //load settings if neccessary |
| 261 | 261 | self::loadSettingsIfAbsent(); |
| 262 | 262 | |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | /** |
| 292 | 292 | * load settings, if class was not initialized yet |
| 293 | 293 | */ |
| 294 | - protected static function loadSettingsIfAbsent () { |
|
| 294 | + protected static function loadSettingsIfAbsent() { |
|
| 295 | 295 | if (!self::$initialized) { |
| 296 | 296 | self::loadAllSettings(); |
| 297 | 297 | } |