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