]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Storage.php
Rename Storage Backend labels
[friendica.git] / src / Module / Admin / Storage.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Admin;
23
24 use Friendica\Core\Renderer;
25 use Friendica\DI;
26 use Friendica\Model\Storage\InvalidClassStorageException;
27 use Friendica\Model\Storage\IWritableStorage;
28 use Friendica\Module\BaseAdmin;
29 use Friendica\Util\Strings;
30
31 class Storage extends BaseAdmin
32 {
33         public static function post(array $parameters = [])
34         {
35                 self::checkAdminAccess();
36
37                 self::checkFormSecurityTokenRedirectOnError('/admin/storage', 'admin_storage');
38
39                 $storagebackend = Strings::escapeTags(trim($parameters['name'] ?? ''));
40
41                 try {
42                         /** @var IWritableStorage $newstorage */
43                         $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
44                 } catch (InvalidClassStorageException $storageException) {
45                         notice(DI::l10n()->t('Storage backend, %s is invalid.', $storagebackend));
46                         DI::baseUrl()->redirect('admin/storage');
47                 }
48
49                 // save storage backend form
50                 $storage_opts        = $newstorage->getOptions();
51                 $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $storagebackend);
52                 $storage_opts_data   = [];
53                 foreach ($storage_opts as $name => $info) {
54                         $fieldname = $storage_form_prefix . '_' . $name;
55                         switch ($info[0]) { // type
56                                 case 'checkbox':
57                                 case 'yesno':
58                                         $value = !empty($_POST[$fieldname]);
59                                         break;
60                                 default:
61                                         $value = $_POST[$fieldname] ?? '';
62                         }
63                         $storage_opts_data[$name] = $value;
64                 }
65                 unset($name);
66                 unset($info);
67
68                 $storage_form_errors = $newstorage->saveOptions($storage_opts_data);
69                 if (count($storage_form_errors)) {
70                         foreach ($storage_form_errors as $name => $err) {
71                                 notice(DI::l10n()->t('Storage backend %s error: %s', $storage_opts[$name][1], $err));
72                         }
73                         DI::baseUrl()->redirect('admin/storage');
74                 }
75
76                 if (!empty($_POST['submit_save_set'])) {
77                         try {
78                                 /** @var IWritableStorage $newstorage */
79                                 $newstorage = DI::storageManager()->getWritableStorageByName($storagebackend);
80
81                                 if (!DI::storageManager()->setBackend($newstorage)) {
82                                         notice(DI::l10n()->t('Invalid storage backend setting value.'));
83                                 }
84                         } catch (InvalidClassStorageException $storageException) {
85                                 notice(DI::l10n()->t('Invalid storage backend setting value.'));
86                         }
87                 }
88
89                 DI::baseUrl()->redirect('admin/storage');
90         }
91
92         public static function content(array $parameters = [])
93         {
94                 parent::content($parameters);
95
96                 $current_storage_backend = DI::storage();
97                 $available_storage_forms = [];
98
99                 foreach (DI::storageManager()->listBackends() as $name) {
100
101                         // build storage config form,
102                         $storage_form_prefix = preg_replace('|[^a-zA-Z0-9]|', '', $name);
103
104                         $storage_form = [];
105                         foreach (DI::storageManager()->getWritableStorageByName($name)->getOptions() as $option => $info) {
106                                 $type = $info[0];
107                                 // Backward compatibilty with yesno field description
108                                 if ($type == 'yesno') {
109                                         $type = 'checkbox';
110                                         // Remove translated labels Yes No from field info
111                                         unset($info[4]);
112                                 }
113
114                                 $info[0]               = $storage_form_prefix . '_' . $option;
115                                 $info['type']          = $type;
116                                 $info['field']         = 'field_' . $type . '.tpl';
117                                 $storage_form[$option] = $info;
118                         }
119
120                         $available_storage_forms[] = [
121                                 'name'   => $name,
122                                 'prefix' => $storage_form_prefix,
123                                 'form'   => $storage_form,
124                                 'active' => $current_storage_backend instanceof IWritableStorage && $name === $current_storage_backend::getName(),
125                         ];
126                 }
127
128                 $t = Renderer::getMarkupTemplate('admin/storage.tpl');
129
130                 return Renderer::replaceMacros($t, [
131                         '$title'                 => DI::l10n()->t('Administration'),
132                         '$label_current'         => DI::l10n()->t('Current Storage Backend'),
133                         '$label_config'          => DI::l10n()->t('Storage Configuration'),
134                         '$page'                  => DI::l10n()->t('Storage'),
135                         '$save'                  => DI::l10n()->t('Save'),
136                         '$save_use'              => DI::l10n()->t('Save & Use storage backend'),
137                         '$use'                   => DI::l10n()->t('Use storage backend'),
138                         '$save_reload'           => DI::l10n()->t('Save & Reload'),
139                         '$noconfig'              => DI::l10n()->t('This backend doesn\'t have custom settings'),
140                         '$baseurl'               => DI::baseUrl()->get(true),
141                         '$form_security_token'   => self::getFormSecurityToken("admin_storage"),
142                         '$storagebackend'        => $current_storage_backend instanceof IWritableStorage ? $current_storage_backend::getName() : DI::l10n()->t('Database (legacy)'),
143                         '$availablestorageforms' => $available_storage_forms,
144                 ]);
145         }
146 }