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