From e83880653a539573469a7a1510c671cc8248ac35 Mon Sep 17 00:00:00 2001 From: slawkens Date: Thu, 13 Jul 2023 15:29:19 +0200 Subject: [PATCH] Extract Settings:save function --- admin/pages/settings.php | 14 ++------------ system/libs/Settings.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/admin/pages/settings.php b/admin/pages/settings.php index 00f3e003..e73fdf46 100644 --- a/admin/pages/settings.php +++ b/admin/pages/settings.php @@ -45,19 +45,9 @@ if (!is_array($settingsFile)) { $settingsKeyName = ($plugin == 'core' ? $plugin : $settingsFile['key']); if (isset($_POST['save'])) { - $db->query('DELETE FROM `' . TABLE_PREFIX . 'settings` WHERE `name` = ' . $db->quote($settingsKeyName) . ';'); - foreach ($_POST['settings'] as $key => $value) { - try { - $db->insert(TABLE_PREFIX . 'settings', ['name' => $settingsKeyName, 'key' => $key, 'value' => $value]); - } catch (PDOException $error) { - warning('Error while saving setting (' . $plugin . ' - ' . $key . '): ' . $error->getMessage()); - } - } + $settings = Settings::getInstance(); - $cache = Cache::getInstance(); - if ($cache->enabled()) { - $cache->delete('settings'); - } + $settings->save($settingsKeyName, $_POST['settings']); success('Saved at ' . date('H:i')); } diff --git a/system/libs/Settings.php b/system/libs/Settings.php index 0a3489a5..6a4ff933 100644 --- a/system/libs/Settings.php +++ b/system/libs/Settings.php @@ -53,6 +53,24 @@ class Settings implements ArrayAccess } } + public function save($pluginName, $settings) { + global $db; + + $db->query('DELETE FROM `' . TABLE_PREFIX . 'settings` WHERE `name` = ' . $db->quote($pluginName) . ';'); + foreach ($settings as $key => $value) { + try { + $db->insert(TABLE_PREFIX . 'settings', ['name' => $pluginName, 'key' => $key, 'value' => $value]); + } catch (PDOException $error) { + warning('Error while saving setting (' . $pluginName . ' - ' . $key . '): ' . $error->getMessage()); + } + } + + $cache = Cache::getInstance(); + if ($cache->enabled()) { + $cache->delete('settings'); + } + } + public function updateInDatabase($pluginName, $key, $value) { global $db;