]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Server/Index.php
Add user settings page to manage remote server settings
[friendica.git] / src / Module / Settings / Server / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Settings\Server;
23
24 use Friendica\App;
25 use Friendica\Content\Pager;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Module\Response;
31 use Friendica\Navigation\SystemMessages;
32 use Friendica\Network\HTTPException\NotFoundException;
33 use Friendica\User\Settings\Entity\UserGServer;
34 use Friendica\User\Settings\Repository;
35 use Friendica\Util\Profiler;
36 use Psr\Log\LoggerInterface;
37
38 class Index extends BaseSettings
39 {
40         /** @var Repository\UserGServer */
41         private $repository;
42         /** @var SystemMessages */
43         private $systemMessages;
44
45         public function __construct(SystemMessages $systemMessages, Repository\UserGServer $repository, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
46         {
47                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48
49                 $this->repository     = $repository;
50                 $this->systemMessages = $systemMessages;
51         }
52
53         protected function post(array $request = [])
54         {
55                 self::checkFormSecurityTokenRedirectOnError($this->args->getQueryString(), 'settings-server');
56
57                 foreach ($request['delete'] ?? [] as $gsid => $delete) {
58                         if ($delete) {
59                                 unset($request['ignored'][$gsid]);
60
61                                 try {
62                                         $userGServer = $this->repository->selectOneByUserAndServer($this->session->getLocalUserId(), $gsid, false);
63                                         $this->repository->delete($userGServer);
64                                 } catch (NotFoundException $e) {
65                                         // Nothing to delete
66                                 }
67                         }
68                 }
69
70                 foreach ($request['ignored'] ?? [] as $gsid => $ignored) {
71                         $userGServer = $this->repository->getOneByUserAndServer($this->session->getLocalUserId(), $gsid, false);
72                         if ($userGServer->ignored != $ignored) {
73                                 $userGServer->toggleIgnored();
74                                 $this->repository->save($userGServer);
75                         }
76                 }
77
78                 $this->systemMessages->addInfo($this->t('Settings saved'));
79
80                 $this->baseUrl->redirect($this->args->getQueryString());
81         }
82
83         protected function content(array $request = []): string
84         {
85                 parent::content();
86
87                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
88
89                 $total = $this->repository->countByUser($this->session->getLocalUserId());
90
91                 $servers = $this->repository->selectByUserWithPagination($this->session->getLocalUserId(), $pager);
92
93                 $ignoredCheckboxes = array_map(function (UserGServer $server) {
94                         return ['ignored[' . $server->gsid . ']', '', $server->ignored];
95                 }, $servers->getArrayCopy());
96
97                 $deleteCheckboxes = array_map(function (UserGServer $server) {
98                         return ['delete[' . $server->gsid . ']'];
99                 }, $servers->getArrayCopy());
100
101                 $tpl = Renderer::getMarkupTemplate('settings/server/index.tpl');
102                 return Renderer::replaceMacros($tpl, [
103                         '$l10n' => [
104                                 'title'         => $this->t('Remote server settings'),
105                                 'desc'          => $this->t('Here you can find all the remote servers you have taken individual moderation actions against. For a list of servers your node has blocked, please check out the <a href="friendica">Information</a> page.'),
106                                 'siteName'      => $this->t('Server Name'),
107                                 'ignored'       => $this->t('Ignored'),
108                                 'ignored_title' => $this->t("You won't see any content from this server including reshares in your Network page, the community pages and individual conversations."),
109                                 'delete'        => $this->t('Delete'),
110                                 'delete_title'  => $this->t('Delete all your settings for the remote server'),
111                                 'submit'        => $this->t('Save changes'),
112                         ],
113
114                         '$count' => $total,
115
116                         '$servers' => $servers,
117
118                         '$form_security_token' => self::getFormSecurityToken('settings-server'),
119
120                         '$ignoredCheckboxes' => $ignoredCheckboxes,
121                         '$deleteCheckboxes'  => $deleteCheckboxes,
122
123                         '$paginate' => $pager->renderFull($total),
124                 ]);
125         }
126 }