diff --git a/appinfo/info.xml b/appinfo/info.xml index eb0502f..c806986 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -31,6 +31,8 @@ more flags to follow soon! + OCA\PrideFlags\Settings\AdminSection + OCA\PrideFlags\Settings\AdminSettings OCA\PrideFlags\Settings\PersonalSection OCA\PrideFlags\Settings\PersonalSettings diff --git a/appinfo/routes.php b/appinfo/routes.php index 84c8f2e..fffb917 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -4,5 +4,7 @@ return [ 'routes' => [ ['name' => 'settings#get', 'url' => '/settings', 'verb' => 'GET'], ['name' => 'settings#set', 'url' => '/settings', 'verb' => 'POST'], + ['name' => 'settings#getGlobal', 'url' => '/settings/global', 'verb' => 'GET'], + ['name' => 'settings#setGlobal', 'url' => '/settings/global', 'verb' => 'POST'], ] ]; diff --git a/js/pride-settings-global.js b/js/pride-settings-global.js new file mode 100644 index 0000000..0237cd3 --- /dev/null +++ b/js/pride-settings-global.js @@ -0,0 +1,38 @@ +(function(window) { + const folder_selector = document.querySelectorAll('.pride_flags_server_settings .server-settings.folder-flavour select')[0]; + const button_selector = document.querySelectorAll('.pride_flags_server_settings .server-settings.button-flavour select')[0]; + const submit_button = document.querySelectorAll('.pride_flags_user_settings .settings-pride-submit'); + const container = document.querySelector('.pride_flags_server_settings .settings-section'); + + function load() { + fetch(OC.generateUrl('/apps/pride_flags/settings/global')) + .then(res => res.json()) + .then(({folderVariant, buttonVariant}) => { + folder_selector.value = folderVariant; + button_selector.value = buttonVariant; + container.classList.remove('hidden'); + }); + } + + function save() { + const payload = { + folderVariant: folder_selector.value, + buttonVariant: button_selector.value, + } + fetch(OC.generateUrl('/apps/pride_flags/settings/global'), { + method: 'POST', + body: JSON.stringify(payload), + headers: { + "Content-Type": "application/json", + "requesttoken": OC.requestToken, + } + }).then(response => { + if (response.ok) { + window.location.reload(); + } + }) + } + submit_button.forEach(node => node.addEventListener('click', e => save())); + + load(); +})(window); diff --git a/js/pride-settings.js b/js/pride-settings.js index 7a269a6..adcba88 100644 --- a/js/pride-settings.js +++ b/js/pride-settings.js @@ -1,36 +1,38 @@ +(function(window) { + const folder_selector = document.querySelectorAll('.pride_flags_user_settings .user-settings.folder-flavour select')[0]; + const button_selector = document.querySelectorAll('.pride_flags_user_settings .user-settings.button-flavour select')[0]; + const submit_button = document.querySelectorAll('.pride_flags_user_settings .settings-pride-submit'); + const container = document.querySelector('.pride_flags_user_settings .settings-section'); - -const folder_selector = document.querySelectorAll('.pride_flags_user_settings .user-settings.folder-flavour select')[0]; -const button_selector = document.querySelectorAll('.pride_flags_user_settings .user-settings.button-flavour select')[0]; -const submit_button = document.querySelectorAll('.pride_flags_user_settings .settings-pride-submit'); - -function load() { - fetch(OC.generateUrl('/apps/pride_flags/settings')) - .then(res => res.json()) - .then(({folderVariant, buttonVariant}) => { - folder_selector.value = folderVariant; - button_selector.value = buttonVariant; - }); -} - -function save() { - const payload = { - folderVariant: folder_selector.value, - buttonVariant: button_selector.value, + function load() { + fetch(OC.generateUrl('/apps/pride_flags/settings')) + .then(res => res.json()) + .then(({folderVariant, buttonVariant}) => { + folder_selector.value = folderVariant; + button_selector.value = buttonVariant; + container.classList.remove('hidden'); + }); } - fetch(OC.generateUrl('/apps/pride_flags/settings'), { - method: 'POST', - body: JSON.stringify(payload), - headers: { - "Content-Type": "application/json", - "requesttoken": OC.requestToken, - } - }).then(response => { - if (response.ok) { - window.location.reload(); - } - }) -} -submit_button.forEach(node => node.addEventListener('click', e => save())); -load(); + function save() { + const payload = { + folderVariant: folder_selector.value, + buttonVariant: button_selector.value, + } + fetch(OC.generateUrl('/apps/pride_flags/settings'), { + method: 'POST', + body: JSON.stringify(payload), + headers: { + "Content-Type": "application/json", + "requesttoken": OC.requestToken, + } + }).then(response => { + if (response.ok) { + window.location.reload(); + } + }) + } + submit_button.forEach(node => node.addEventListener('click', e => save())); + + load(); +})(window); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 421ed89..a635d9f 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -14,6 +14,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\Attribute\NoCSRFRequired; +use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting; class SettingsController extends Controller { @@ -33,6 +34,12 @@ class SettingsController extends Controller { return $this->makeJSONResponse(fn () => $this->appSettings->getAll($this->userId)); } + #[AuthorizedAdminSetting] + public function setGlobal(string $folderVariant, string $buttonVariant): JSONResponse { + $this->appSettings->setGlobal($folderVariant, $buttonVariant); + return $this->makeJSONResponse(fn () => $this->appSettings->getGlobal()); + } + protected function makeJSONResponse(Closure $closure): JSONResponse { try { return new JSONResponse($closure(), HTTP::STATUS_OK); diff --git a/lib/Settings/AdminSection.php b/lib/Settings/AdminSection.php new file mode 100644 index 0000000..e02ebdb --- /dev/null +++ b/lib/Settings/AdminSection.php @@ -0,0 +1,30 @@ +urlGenerator->imagePath('core', 'actions/settings-dark.svg'); + } +} diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php new file mode 100644 index 0000000..ddfcf93 --- /dev/null +++ b/lib/Settings/AdminSettings.php @@ -0,0 +1,25 @@ +config->getUserValue(AppConstants::APP_ID, $userId, $key) ?: $default; + return $this->config->getUserValue(AppConstants::APP_ID, $userId, $key) + ?: ($this->config->getAppValue(AppConstants::APP_ID, $userId, $key) ?: $default); } public function setStringSetting($userId, string $key, string $value): void { $this->config->setUserValue(AppConstants::APP_ID, $userId, $key, $value); } + public function setAppStringSetting(string $key, string $value): void { + $this->config->setAppValue(AppConstants::APP_ID, $key, $value); + } + public function getAll($userId): array { return [ AppSettings::FOLDER_VARIANT => $this->getStringSetting($userId, AppSettings::FOLDER_VARIANT, 'pride'), @@ -27,8 +32,20 @@ class AppSettings { ]; } + public function getGlobal(): array { + return [ + AppSettings::FOLDER_VARIANT => $this->config->getAppValue(AppSettings::APP_ID, AppSettings::FOLDER_VARIANT, 'pride'), + AppSettings::BUTTON_VARIANT => $this->config->getAppValue(AppSettings::APP_ID, AppSettings::BUTTON_VARIANT, 'trans'), + ]; + } + public function set($userId, $folder, $button): void { $this->setStringSetting($userId, AppSettings::FOLDER_VARIANT, $folder); $this->setStringSetting($userId, AppSettings::BUTTON_VARIANT, $button); } + + public function setGlobal($folder, $button): void { + $this->setAppStringSetting(AppSettings::FOLDER_VARIANT, $folder); + $this->setAppStringSetting(AppSettings::BUTTON_VARIANT, $button); + } } diff --git a/templates/server-settings.php b/templates/server-settings.php new file mode 100644 index 0000000..66b1c54 --- /dev/null +++ b/templates/server-settings.php @@ -0,0 +1,30 @@ +
+ +
diff --git a/templates/settings.php b/templates/settings.php index 83ff783..91529d7 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -2,7 +2,7 @@ ?>
-
+