From 97bafb3a591342cf34edb5c14aae8cb574cb78e0 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 18 Jul 2021 22:09:11 +0200 Subject: [PATCH] Extract Storage admin section to own page --- src/Module/Admin/Site.php | 69 ------------- src/Module/Admin/Storage.php | 125 +++++++++++++++++++++++ src/Module/BaseAdmin.php | 1 + static/routes.config.php | 2 + view/templates/admin/site.tpl | 5 - view/templates/admin/storage.tpl | 15 +++ view/theme/frio/templates/admin/site.tpl | 5 - 7 files changed, 143 insertions(+), 79 deletions(-) create mode 100644 src/Module/Admin/Storage.php create mode 100644 view/templates/admin/storage.tpl diff --git a/src/Module/Admin/Site.php b/src/Module/Admin/Site.php index 068b013269..9d0e9c9dad 100644 --- a/src/Module/Admin/Site.php +++ b/src/Module/Admin/Site.php @@ -212,39 +212,6 @@ class Site extends BaseAdmin $relay_user_tags = !empty($_POST['relay_user_tags']); $active_panel = (!empty($_POST['active_panel']) ? "#" . Strings::escapeTags(trim($_POST['active_panel'])) : ''); - $storagebackend = Strings::escapeTags(trim($_POST['storagebackend'] ?? '')); - - // save storage backend form - if (DI::storageManager()->setBackend($storagebackend)) { - $storage_opts = DI::storage()->getOptions(); - $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend); - $storage_opts_data = []; - foreach ($storage_opts as $name => $info) { - $fieldname = $storage_form_prefix . '_' . $name; - switch ($info[0]) { // type - case 'checkbox': - case 'yesno': - $value = !empty($_POST[$fieldname]); - break; - default: - $value = $_POST[$fieldname] ?? ''; - } - $storage_opts_data[$name] = $value; - } - unset($name); - unset($info); - - $storage_form_errors = DI::storage()->saveOptions($storage_opts_data); - if (count($storage_form_errors)) { - foreach ($storage_form_errors as $name => $err) { - notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err); - } - DI::baseUrl()->redirect('admin/site' . $active_panel); - } - } elseif (!empty($storagebackend)) { - notice(DI::l10n()->t('Invalid storage backend setting value.')); - } - // Has the directory url changed? If yes, then resubmit the existing profiles there if ($global_directory != DI::config()->get('system', 'directory') && ($global_directory != '')) { DI::config()->set('system', 'directory', $global_directory); @@ -526,40 +493,6 @@ class Site extends BaseAdmin $diaspora_able = (DI::baseUrl()->getUrlPath() == ''); - $current_storage_backend = DI::storage(); - $available_storage_backends = []; - - // show legacy option only if it is the current backend: - // once changed can't be selected anymore - if ($current_storage_backend == null) { - $available_storage_backends[''] = DI::l10n()->t('Database (legacy)'); - } - - foreach (DI::storageManager()->listBackends() as $name => $class) { - $available_storage_backends[$name] = $name; - } - - // build storage config form, - $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|' ,'', $current_storage_backend); - - $storage_form = []; - if (!is_null($current_storage_backend) && $current_storage_backend != '') { - foreach ($current_storage_backend->getOptions() as $name => $info) { - $type = $info[0]; - // Backward compatibilty with yesno field description - if ($type == 'yesno') { - $type = 'checkbox'; - // Remove translated labels Yes No from field info - unset($info[4]); - } - - $info[0] = $storage_form_prefix . '_' . $name; - $info['type'] = $type; - $info['field'] = 'field_' . $type . '.tpl'; - $storage_form[$name] = $info; - } - } - $t = Renderer::getMarkupTemplate('admin/site.tpl'); return Renderer::replaceMacros($t, [ '$title' => DI::l10n()->t('Administration'), @@ -600,8 +533,6 @@ class Site extends BaseAdmin '$hide_help' => ['hide_help', DI::l10n()->t('Hide help entry from navigation menu'), DI::config()->get('system', 'hide_help'), DI::l10n()->t('Hides the menu entry for the Help pages from the navigation menu. You can still access it calling /help directly.')], '$singleuser' => ['singleuser', DI::l10n()->t('Single user instance'), DI::config()->get('system', 'singleuser', '---'), DI::l10n()->t('Make this instance multi-user or single-user for the named user'), $user_names], - '$storagebackend' => ['storagebackend', DI::l10n()->t('File storage backend'), $current_storage_backend, DI::l10n()->t('The backend used to store uploaded data. If you change the storage backend, you can manually move the existing files. If you do not do so, the files uploaded before the change will still be available at the old backend. Please see the settings documentation for more information about the choices and the moving procedure.'), $available_storage_backends], - '$storageform' => $storage_form, '$maximagesize' => ['maximagesize', DI::l10n()->t('Maximum image size'), DI::config()->get('system', 'maximagesize'), DI::l10n()->t('Maximum size in bytes of uploaded images. Default is 0, which means no limits.')], '$maximagelength' => ['maximagelength', DI::l10n()->t('Maximum image length'), DI::config()->get('system', 'max_image_length'), DI::l10n()->t('Maximum length in pixels of the longest side of uploaded images. Default is -1, which means no limits.')], '$jpegimagequality' => ['jpegimagequality', DI::l10n()->t('JPEG image quality'), DI::config()->get('system', 'jpeg_quality'), DI::l10n()->t('Uploaded JPEGS will be saved at this quality setting [0-100]. Default is 100, which is full quality.')], diff --git a/src/Module/Admin/Storage.php b/src/Module/Admin/Storage.php new file mode 100644 index 0000000000..1e1678b138 --- /dev/null +++ b/src/Module/Admin/Storage.php @@ -0,0 +1,125 @@ +. + * + */ + +namespace Friendica\Module\Admin; + +use Friendica\Core\Renderer; +use Friendica\DI; +use Friendica\Module\BaseAdmin; +use Friendica\Util\Strings; +use Psr\Log\LogLevel; + +class Storage extends BaseAdmin +{ + public static function post(array $parameters = []) + { + self::checkAdminAccess(); + + self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage'); + + $storagebackend = Strings::escapeTags(trim($_POST['storagebackend'] ?? '')); + + // save storage backend form + if (DI::storageManager()->setBackend($storagebackend)) { + $storage_opts = DI::storage()->getOptions(); + $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend); + $storage_opts_data = []; + foreach ($storage_opts as $name => $info) { + $fieldname = $storage_form_prefix . '_' . $name; + switch ($info[0]) { // type + case 'checkbox': + case 'yesno': + $value = !empty($_POST[$fieldname]); + break; + default: + $value = $_POST[$fieldname] ?? ''; + } + $storage_opts_data[$name] = $value; + } + unset($name); + unset($info); + + $storage_form_errors = DI::storage()->saveOptions($storage_opts_data); + if (count($storage_form_errors)) { + foreach ($storage_form_errors as $name => $err) { + notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err); + } + DI::baseUrl()->redirect('admin/storage'); + } + } elseif (!empty($storagebackend)) { + notice(DI::l10n()->t('Invalid storage backend setting value.')); + } + + DI::baseUrl()->redirect('admin/storage'); + } + + public static function content(array $parameters = []) + { + parent::content($parameters); + + $current_storage_backend = DI::storage(); + $available_storage_backends = []; + + // show legacy option only if it is the current backend: + // once changed can't be selected anymore + if ($current_storage_backend == null) { + $available_storage_backends[''] = DI::l10n()->t('Database (legacy)'); + } + + foreach (DI::storageManager()->listBackends() as $name => $class) { + $available_storage_backends[$name] = $name; + } + + // build storage config form, + $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|' ,'', $current_storage_backend); + + $storage_form = []; + if (!is_null($current_storage_backend) && $current_storage_backend != '') { + foreach ($current_storage_backend->getOptions() as $name => $info) { + $type = $info[0]; + // Backward compatibilty with yesno field description + if ($type == 'yesno') { + $type = 'checkbox'; + // Remove translated labels Yes No from field info + unset($info[4]); + } + + $info[0] = $storage_form_prefix . '_' . $name; + $info['type'] = $type; + $info['field'] = 'field_' . $type . '.tpl'; + $storage_form[$name] = $info; + } + } + + $t = Renderer::getMarkupTemplate('admin/storage.tpl'); + + return Renderer::replaceMacros($t, [ + '$title' => DI::l10n()->t('Administration'), + '$page' => DI::l10n()->t('Storage'), + '$submit' => DI::l10n()->t('Save Settings'), + '$clear' => DI::l10n()->t('Clear'), + '$baseurl' => DI::baseUrl()->get(true), + '$form_security_token' => self::getFormSecurityToken("admin_storage"), + '$storagebackend' => ['storagebackend', DI::l10n()->t('File storage backend'), $current_storage_backend, DI::l10n()->t('The backend used to store uploaded data. If you change the storage backend, you can manually move the existing files. If you do not do so, the files uploaded before the change will still be available at the old backend. Please see the settings documentation for more information about the choices and the moving procedure.'), $available_storage_backends], + '$storageform' => $storage_form, + ]); + } +} diff --git a/src/Module/BaseAdmin.php b/src/Module/BaseAdmin.php index 90b246e432..5d5841f994 100644 --- a/src/Module/BaseAdmin.php +++ b/src/Module/BaseAdmin.php @@ -88,6 +88,7 @@ abstract class BaseAdmin extends BaseModule ]], 'configuration' => [DI::l10n()->t('Configuration'), [ 'site' => ['admin/site' , DI::l10n()->t('Site') , 'site'], + 'storage' => ['admin/storage' , DI::l10n()->t('Storage') , 'storage'], 'users' => ['admin/users' , DI::l10n()->t('Users') , 'users'], 'addons' => ['admin/addons' , DI::l10n()->t('Addons') , 'addons'], 'themes' => ['admin/themes' , DI::l10n()->t('Themes') , 'themes'], diff --git a/static/routes.config.php b/static/routes.config.php index 9780bf3cbe..05e266ff10 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -198,6 +198,8 @@ return [ '/site' => [Module\Admin\Site::class, [R::GET, R::POST]], + '/storage' => [Module\Admin\Storage::class, [R::GET, R::POST]], + '/themes' => [Module\Admin\Themes\Index::class, [R::GET, R::POST]], '/themes/{theme}' => [Module\Admin\Themes\Details::class, [R::GET, R::POST]], '/themes/{theme}/embed' => [Module\Admin\Themes\Embed::class, [R::GET, R::POST]], diff --git a/view/templates/admin/site.tpl b/view/templates/admin/site.tpl index 64d49d3a31..493abef61e 100644 --- a/view/templates/admin/site.tpl +++ b/view/templates/admin/site.tpl @@ -40,11 +40,6 @@

{{$upload}}

- {{include file="field_select.tpl" field=$storagebackend}} - {{foreach from=$storageform item=$field}} - {{include file=$field.field field=$field}} - {{/foreach}} -
{{include file="field_input.tpl" field=$maximagesize}} {{include file="field_input.tpl" field=$maximagelength}} {{include file="field_input.tpl" field=$jpegimagequality}} diff --git a/view/templates/admin/storage.tpl b/view/templates/admin/storage.tpl new file mode 100644 index 0000000000..af4f1fbced --- /dev/null +++ b/view/templates/admin/storage.tpl @@ -0,0 +1,15 @@ +
+

{{$title}} - {{$page}}

+ +
+ + + {{include file="field_select.tpl" field=$storagebackend}} + {{foreach from=$storageform item=$field}} + {{include file=$field.field field=$field}} + {{/foreach}} + +
+ +
+
diff --git a/view/theme/frio/templates/admin/site.tpl b/view/theme/frio/templates/admin/site.tpl index ad87151777..ef8ef9786d 100644 --- a/view/theme/frio/templates/admin/site.tpl +++ b/view/theme/frio/templates/admin/site.tpl @@ -100,11 +100,6 @@
- {{include file="field_select.tpl" field=$storagebackend}} - {{foreach from=$storageform item=$field}} - {{include file=$field.field field=$field}} - {{/foreach}} -
{{include file="field_input.tpl" field=$maximagesize}} {{include file="field_input.tpl" field=$maximagelength}} {{include file="field_input.tpl" field=$jpegimagequality}}