xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentAction.php (revision 842a873c0b92c28d4af3fa9db0e9156057005569)
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    /** @var ModuleService */
38    protected $module_service;
39
40    /** @var TreeService */
41    protected $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     * @param string                 $interface
57     * @param ServerRequestInterface $request
58     *
59     * @return void
60     */
61    protected function updateStatus(string $interface, ServerRequestInterface $request): void
62    {
63        $modules = $this->module_service->findByInterface($interface, true);
64
65        $params = (array) $request->getParsedBody();
66
67        foreach ($modules as $module) {
68            $enabled = (bool) ($params['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     * @param string                 $interface
90     * @param ServerRequestInterface $request
91     *
92     * @return void
93     */
94    protected function updateAccessLevel(string $interface, ServerRequestInterface $request): void
95    {
96        $modules = $this->module_service->findByInterface($interface, true);
97
98        $params = (array) $request->getParsedBody();
99
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 = (int) ($params[$key] ?? 0);
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     * @param string                 $interface
124     * @param string                 $column
125     * @param ServerRequestInterface $request
126     *
127     * @return void
128     */
129    protected function updateOrder(string $interface, string $column, ServerRequestInterface $request): void
130    {
131        $modules = $this->module_service->findByInterface($interface, true);
132
133        $params = (array) $request->getParsedBody();
134
135        $order = (array) ($params['order'] ?? []);
136        $order = array_flip($order);
137
138        foreach ($modules as $module) {
139            DB::table('module')
140                ->where('module_name', '=', $module->name())
141                ->update([
142                    $column => $order[$module->name()] ?? 0,
143                ]);
144        }
145    }
146}
147