X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FAdmin%2FStorage.php;h=434b43b32ed6e7095ed75b5b1f2fdfbc3e65fedd;hb=c81a9d1ddd7e5b7fff6a173a159ae78ee7fc7698;hp=374d692bf7330273b7c680494990d7e1feda6748;hpb=29c7552df57c31d81e8ce2458e2873f4d2ece35e;p=friendica.git diff --git a/src/Module/Admin/Storage.php b/src/Module/Admin/Storage.php index 374d692bf7..434b43b32e 100644 --- a/src/Module/Admin/Storage.php +++ b/src/Module/Admin/Storage.php @@ -1,6 +1,6 @@ getSelectableStorageByName($storagebackend); - - // save storage backend form - $storage_opts = $newstorage->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; + $storagebackend = trim($this->parameters['name'] ?? ''); + + try { + /** @var ICanConfigureStorage|false $newStorageConfig */ + $newStorageConfig = DI::storageManager()->getConfigurationByName($storagebackend); + } catch (InvalidClassStorageException $storageException) { + DI::sysmsg()->addNotice(DI::l10n()->t('Storage backend, %s is invalid.', $storagebackend)); + DI::baseUrl()->redirect('admin/storage'); } - unset($name); - unset($info); - $storage_form_errors = $newstorage->saveOptions($storage_opts_data); - if (count($storage_form_errors)) { - foreach ($storage_form_errors as $name => $err) { - notice('Storage backend, ' . $storage_opts[$name][1] . ': ' . $err); + if ($newStorageConfig !== false) { + // save storage backend form + $storage_opts = $newStorageConfig->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 = $newStorageConfig->saveOptions($storage_opts_data); + if (count($storage_form_errors)) { + foreach ($storage_form_errors as $name => $err) { + DI::sysmsg()->addNotice(DI::l10n()->t('Storage backend %s error: %s', $storage_opts[$name][1], $err)); + } + DI::baseUrl()->redirect('admin/storage'); } - DI::baseUrl()->redirect('admin/storage'); } - if (!empty($_POST['submit_save_set'])) { - /** @var ISelectableStorage $newstorage */ - $newstorage = DI::storageManager()->getSelectableStorageByName($storagebackend); + if (!empty($_POST['submit_save_set']) && DI::config()->isWritable('storage', 'name') ) { + try { + $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend); - if (!DI::storageManager()->setBackend($newstorage)) { - notice(DI::l10n()->t('Invalid storage backend setting value.')); + if (!DI::storageManager()->setBackend($newstorage)) { + DI::sysmsg()->addNotice(DI::l10n()->t('Invalid storage backend setting value.')); + } + } catch (InvalidClassStorageException $storageException) { + DI::sysmsg()->addNotice(DI::l10n()->t('Invalid storage backend setting value.')); } } DI::baseUrl()->redirect('admin/storage'); } - public static function content(array $parameters = []) + protected function content(array $request = []): string { - parent::content($parameters); + parent::content(); $current_storage_backend = DI::storage(); $available_storage_forms = []; - foreach (DI::storageManager()->listBackends() as $name => $class) { + foreach (DI::storageManager()->listBackends() as $name) { // build storage config form, $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name); - $storage_form = []; - foreach (DI::storageManager()->getSelectableStorageByName($name)->getOptions() as $option => $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]); - } + $storage_form = []; + $storageConfig = DI::storageManager()->getConfigurationByName($name); - $info[0] = $storage_form_prefix . '_' . $option; - $info['type'] = $type; - $info['field'] = 'field_' . $type . '.tpl'; - $storage_form[$option] = $info; + if ($storageConfig !== false) { + foreach ($storageConfig->getOptions() as $option => $info) { + + $type = $info[0]; + // Backward compatibility 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 . '_' . $option; + $info['type'] = $type; + $info['field'] = 'field_' . $type . '.tpl'; + $storage_form[$option] = $info; + } } $available_storage_forms[] = [ 'name' => $name, 'prefix' => $storage_form_prefix, 'form' => $storage_form, - 'active' => $current_storage_backend instanceof ISelectableStorage && $name === $current_storage_backend::getName(), + 'active' => $current_storage_backend instanceof ICanWriteToStorage && $name === $current_storage_backend::getName(), ]; } @@ -119,15 +136,18 @@ class Storage extends BaseAdmin return Renderer::replaceMacros($t, [ '$title' => DI::l10n()->t('Administration'), + '$label_current' => DI::l10n()->t('Current Storage Backend'), + '$label_config' => DI::l10n()->t('Storage Configuration'), '$page' => DI::l10n()->t('Storage'), '$save' => DI::l10n()->t('Save'), - '$save_activate' => DI::l10n()->t('Save & Activate'), - '$activate' => DI::l10n()->t('Activate'), + '$save_use' => DI::l10n()->t('Save & Use storage backend'), + '$use' => DI::l10n()->t('Use storage backend'), '$save_reload' => DI::l10n()->t('Save & Reload'), '$noconfig' => DI::l10n()->t('This backend doesn\'t have custom settings'), - '$baseurl' => DI::baseUrl()->get(true), '$form_security_token' => self::getFormSecurityToken("admin_storage"), - '$storagebackend' => $current_storage_backend instanceof ISelectableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'), + '$storagebackend_ro_txt' => !DI::config()->isWritable('storage', 'name') ? DI::l10n()->t('Changing the current backend is prohibited because it is set by an environment variable') : '', + '$is_writable' => DI::config()->isWritable('storage', 'name'), + '$storagebackend' => $current_storage_backend instanceof ICanWriteToStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'), '$availablestorageforms' => $available_storage_forms, ]); }