xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentAction.php (revision 46b31fc102c48c09a46fa5b1685ad83d1204ac6a)
19f667ff2SGreg Roach<?php
29f667ff2SGreg Roach
39f667ff2SGreg Roach/**
49f667ff2SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
69f667ff2SGreg Roach * This program is free software: you can redistribute it and/or modify
79f667ff2SGreg Roach * it under the terms of the GNU General Public License as published by
89f667ff2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
99f667ff2SGreg Roach * (at your option) any later version.
109f667ff2SGreg Roach * This program is distributed in the hope that it will be useful,
119f667ff2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
129f667ff2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139f667ff2SGreg Roach * GNU General Public License for more details.
149f667ff2SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
169f667ff2SGreg Roach */
179f667ff2SGreg Roach
189f667ff2SGreg Roachdeclare(strict_types=1);
199f667ff2SGreg Roach
209f667ff2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
219f667ff2SGreg Roach
229f667ff2SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
239f667ff2SGreg Roachuse Fisharebest\Webtrees\I18N;
249f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
259f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
26*46b31fc1SGreg Roachuse Fisharebest\Webtrees\Validator;
279f667ff2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
289f667ff2SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
299f667ff2SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
309f667ff2SGreg Roach
319f667ff2SGreg Roachuse function array_flip;
329f667ff2SGreg Roach
339f667ff2SGreg Roach/**
349f667ff2SGreg Roach * Update a list of modules.
359f667ff2SGreg Roach */
369f667ff2SGreg Roachabstract class AbstractModuleComponentAction implements RequestHandlerInterface
379f667ff2SGreg Roach{
38ac732901SGreg Roach    protected ModuleService $module_service;
399f667ff2SGreg Roach
40ac732901SGreg Roach    protected TreeService $tree_service;
419f667ff2SGreg Roach
429f667ff2SGreg Roach    /**
439f667ff2SGreg Roach     * @param ModuleService $module_service
449f667ff2SGreg Roach     * @param TreeService   $tree_service
459f667ff2SGreg Roach     */
469f667ff2SGreg Roach    public function __construct(ModuleService $module_service, TreeService $tree_service)
479f667ff2SGreg Roach    {
489f667ff2SGreg Roach        $this->module_service = $module_service;
499f667ff2SGreg Roach        $this->tree_service   = $tree_service;
509f667ff2SGreg Roach    }
519f667ff2SGreg Roach
529f667ff2SGreg Roach    /**
539f667ff2SGreg Roach     * Update the access levels of the modules.
549f667ff2SGreg Roach     *
559f667ff2SGreg Roach     * @param string                 $interface
569f667ff2SGreg Roach     * @param ServerRequestInterface $request
579f667ff2SGreg Roach     *
589f667ff2SGreg Roach     * @return void
599f667ff2SGreg Roach     */
609f667ff2SGreg Roach    protected function updateStatus(string $interface, ServerRequestInterface $request): void
619f667ff2SGreg Roach    {
629f667ff2SGreg Roach        $modules = $this->module_service->findByInterface($interface, true);
639f667ff2SGreg Roach
649f667ff2SGreg Roach        foreach ($modules as $module) {
65*46b31fc1SGreg Roach            $enabled = Validator::parsedBody($request)->boolean('status-' . $module->name(), false);
669f667ff2SGreg Roach
679f667ff2SGreg Roach            if ($enabled !== $module->isEnabled()) {
689f667ff2SGreg Roach                DB::table('module')
699f667ff2SGreg Roach                    ->where('module_name', '=', $module->name())
709f667ff2SGreg Roach                    ->update(['status' => $enabled ? 'enabled' : 'disabled']);
719f667ff2SGreg Roach
729f667ff2SGreg Roach                if ($enabled) {
739f667ff2SGreg Roach                    $message = I18N::translate('The module “%s” has been enabled.', $module->title());
749f667ff2SGreg Roach                } else {
759f667ff2SGreg Roach                    $message = I18N::translate('The module “%s” has been disabled.', $module->title());
769f667ff2SGreg Roach                }
779f667ff2SGreg Roach
789f667ff2SGreg Roach                FlashMessages::addMessage($message, 'success');
799f667ff2SGreg Roach            }
809f667ff2SGreg Roach        }
819f667ff2SGreg Roach    }
829f667ff2SGreg Roach
839f667ff2SGreg Roach    /**
849f667ff2SGreg Roach     * Update the access levels of the modules.
859f667ff2SGreg Roach     *
869f667ff2SGreg Roach     * @param string                 $interface
879f667ff2SGreg Roach     * @param ServerRequestInterface $request
889f667ff2SGreg Roach     *
899f667ff2SGreg Roach     * @return void
909f667ff2SGreg Roach     */
919f667ff2SGreg Roach    protected function updateAccessLevel(string $interface, ServerRequestInterface $request): void
929f667ff2SGreg Roach    {
939f667ff2SGreg Roach        $modules = $this->module_service->findByInterface($interface, true);
949f667ff2SGreg Roach
959f667ff2SGreg Roach        $params = (array) $request->getParsedBody();
969f667ff2SGreg Roach
979f667ff2SGreg Roach        $trees = $this->tree_service->all();
989f667ff2SGreg Roach
999f667ff2SGreg Roach        foreach ($modules as $module) {
1009f667ff2SGreg Roach            foreach ($trees as $tree) {
1019f667ff2SGreg Roach                $key          = 'access-' . $module->name() . '-' . $tree->id();
1029f667ff2SGreg Roach                $access_level = (int) ($params[$key] ?? 0);
1039f667ff2SGreg Roach
1049f667ff2SGreg Roach                if ($access_level !== $module->accessLevel($tree, $interface)) {
1059f667ff2SGreg Roach                    DB::table('module_privacy')->updateOrInsert([
1069f667ff2SGreg Roach                        'module_name' => $module->name(),
1079f667ff2SGreg Roach                        'gedcom_id'   => $tree->id(),
1089f667ff2SGreg Roach                        'interface'   => $interface,
1099f667ff2SGreg Roach                    ], [
1109f667ff2SGreg Roach                        'access_level' => $access_level,
1119f667ff2SGreg Roach                    ]);
1129f667ff2SGreg Roach                }
1139f667ff2SGreg Roach            }
1149f667ff2SGreg Roach        }
1159f667ff2SGreg Roach    }
1169f667ff2SGreg Roach
1179f667ff2SGreg Roach    /**
1189f667ff2SGreg Roach     * Update the access levels of the modules.
1199f667ff2SGreg Roach     *
1209f667ff2SGreg Roach     * @param string                 $interface
1219f667ff2SGreg Roach     * @param string                 $column
1229f667ff2SGreg Roach     * @param ServerRequestInterface $request
1239f667ff2SGreg Roach     *
1249f667ff2SGreg Roach     * @return void
1259f667ff2SGreg Roach     */
1269f667ff2SGreg Roach    protected function updateOrder(string $interface, string $column, ServerRequestInterface $request): void
1279f667ff2SGreg Roach    {
1289f667ff2SGreg Roach        $modules = $this->module_service->findByInterface($interface, true);
1299f667ff2SGreg Roach
1309f667ff2SGreg Roach        $params = (array) $request->getParsedBody();
1319f667ff2SGreg Roach
1329f667ff2SGreg Roach        $order = (array) ($params['order'] ?? []);
1339f667ff2SGreg Roach        $order = array_flip($order);
1349f667ff2SGreg Roach
1359f667ff2SGreg Roach        foreach ($modules as $module) {
1369f667ff2SGreg Roach            DB::table('module')
1379f667ff2SGreg Roach                ->where('module_name', '=', $module->name())
1389f667ff2SGreg Roach                ->update([
1399f667ff2SGreg Roach                    $column => $order[$module->name()] ?? 0,
1409f667ff2SGreg Roach                ]);
1419f667ff2SGreg Roach        }
1429f667ff2SGreg Roach    }
1439f667ff2SGreg Roach}
144