xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentAction.php (revision 1ff45046fabc22237b5d0d8e489c96f031fc598d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\DB;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Module\ModuleInterface;
26use Fisharebest\Webtrees\Services\ModuleService;
27use Fisharebest\Webtrees\Services\TreeService;
28use Fisharebest\Webtrees\Validator;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function array_flip;
33
34/**
35 * Update a list of modules.
36 */
37abstract class AbstractModuleComponentAction implements RequestHandlerInterface
38{
39    protected ModuleService $module_service;
40
41    protected TreeService $tree_service;
42
43    /**
44     * @param ModuleService $module_service
45     * @param TreeService   $tree_service
46     */
47    public function __construct(ModuleService $module_service, TreeService $tree_service)
48    {
49        $this->module_service = $module_service;
50        $this->tree_service   = $tree_service;
51    }
52
53    /**
54     * Update the access levels of the modules.
55     *
56     * @template T of ModuleInterface
57     *
58     * @param class-string<T>        $interface
59     * @param ServerRequestInterface $request
60     *
61     * @return void
62     */
63    protected function updateStatus(string $interface, ServerRequestInterface $request): void
64    {
65        $modules = $this->module_service->findByInterface($interface, true);
66
67        foreach ($modules as $module) {
68            $enabled = Validator::parsedBody($request)->boolean('status-' . $module->name(), false);
69
70            if ($enabled !== $module->isEnabled()) {
71                DB::table('module')
72                    ->where('module_name', '=', $module->name())
73                    ->update(['status' => $enabled ? 'enabled' : 'disabled']);
74
75                if ($enabled) {
76                    $message = I18N::translate('The module “%s” has been enabled.', $module->title());
77                } else {
78                    $message = I18N::translate('The module “%s” has been disabled.', $module->title());
79                }
80
81                FlashMessages::addMessage($message, 'success');
82            }
83        }
84    }
85
86    /**
87     * Update the access levels of the modules.
88     *
89     * @template T of ModuleInterface
90     *
91     * @param class-string<T>        $interface
92     * @param ServerRequestInterface $request
93     *
94     * @return void
95     */
96    protected function updateAccessLevel(string $interface, ServerRequestInterface $request): void
97    {
98        $modules = $this->module_service->findByInterface($interface, true);
99        $trees   = $this->tree_service->all();
100
101        foreach ($modules as $module) {
102            foreach ($trees as $tree) {
103                $key          = 'access-' . $module->name() . '-' . $tree->id();
104                $access_level = Validator::parsedBody($request)->integer($key);
105
106                if ($access_level !== $module->accessLevel($tree, $interface)) {
107                    DB::table('module_privacy')->updateOrInsert([
108                        'module_name' => $module->name(),
109                        'gedcom_id'   => $tree->id(),
110                        'interface'   => $interface,
111                    ], [
112                        'access_level' => $access_level,
113                    ]);
114                }
115            }
116        }
117    }
118
119    /**
120     * Update the access levels of the modules.
121     *
122     * @template T of ModuleInterface
123     *
124     * @param class-string<T>        $interface
125     * @param string                 $column
126     * @param ServerRequestInterface $request
127     *
128     * @return void
129     */
130    protected function updateOrder(string $interface, string $column, ServerRequestInterface $request): void
131    {
132        $modules = $this->module_service->findByInterface($interface, true);
133        $order   = Validator::parsedBody($request)->array('order');
134        $order   = array_flip($order);
135
136        foreach ($modules as $module) {
137            DB::table('module')
138                ->where('module_name', '=', $module->name())
139                ->update([
140                    $column => $order[$module->name()] ?? 0,
141                ]);
142        }
143    }
144}
145