xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentAction.php (revision b63ebfff10c4f748c10374a14a84550d3b653912)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\ModuleService;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\Validator;
27use Illuminate\Database\Capsule\Manager as DB;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31use function array_flip;
32
33/**
34 * Update a list of modules.
35 */
36abstract class AbstractModuleComponentAction implements RequestHandlerInterface
37{
38    protected ModuleService $module_service;
39
40    protected TreeService $tree_service;
41
42    /**
43     * @param ModuleService $module_service
44     * @param TreeService   $tree_service
45     */
46    public function __construct(ModuleService $module_service, TreeService $tree_service)
47    {
48        $this->module_service = $module_service;
49        $this->tree_service   = $tree_service;
50    }
51
52    /**
53     * Update the access levels of the modules.
54     *
55     * @param string                 $interface
56     * @param ServerRequestInterface $request
57     *
58     * @return void
59     */
60    protected function updateStatus(string $interface, ServerRequestInterface $request): void
61    {
62        $modules = $this->module_service->findByInterface($interface, true);
63
64        foreach ($modules as $module) {
65            $enabled = Validator::parsedBody($request)->boolean('status-' . $module->name(), false);
66
67            if ($enabled !== $module->isEnabled()) {
68                DB::table('module')
69                    ->where('module_name', '=', $module->name())
70                    ->update(['status' => $enabled ? 'enabled' : 'disabled']);
71
72                if ($enabled) {
73                    $message = I18N::translate('The module “%s” has been enabled.', $module->title());
74                } else {
75                    $message = I18N::translate('The module “%s” has been disabled.', $module->title());
76                }
77
78                FlashMessages::addMessage($message, 'success');
79            }
80        }
81    }
82
83    /**
84     * Update the access levels of the modules.
85     *
86     * @param string                 $interface
87     * @param ServerRequestInterface $request
88     *
89     * @return void
90     */
91    protected function updateAccessLevel(string $interface, ServerRequestInterface $request): void
92    {
93        $modules = $this->module_service->findByInterface($interface, true);
94
95        $params = (array) $request->getParsedBody();
96
97        $trees = $this->tree_service->all();
98
99        foreach ($modules as $module) {
100            foreach ($trees as $tree) {
101                $key          = 'access-' . $module->name() . '-' . $tree->id();
102                $access_level = (int) ($params[$key] ?? 0);
103
104                if ($access_level !== $module->accessLevel($tree, $interface)) {
105                    DB::table('module_privacy')->updateOrInsert([
106                        'module_name' => $module->name(),
107                        'gedcom_id'   => $tree->id(),
108                        'interface'   => $interface,
109                    ], [
110                        'access_level' => $access_level,
111                    ]);
112                }
113            }
114        }
115    }
116
117    /**
118     * Update the access levels of the modules.
119     *
120     * @param string                 $interface
121     * @param string                 $column
122     * @param ServerRequestInterface $request
123     *
124     * @return void
125     */
126    protected function updateOrder(string $interface, string $column, ServerRequestInterface $request): void
127    {
128        $modules = $this->module_service->findByInterface($interface, true);
129
130        $params = (array) $request->getParsedBody();
131
132        $order = (array) ($params['order'] ?? []);
133        $order = array_flip($order);
134
135        foreach ($modules as $module) {
136            DB::table('module')
137                ->where('module_name', '=', $module->name())
138                ->update([
139                    $column => $order[$module->name()] ?? 0,
140                ]);
141        }
142    }
143}
144