19f667ff2SGreg Roach<?php 29f667ff2SGreg Roach 39f667ff2SGreg Roach/** 49f667ff2SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 239f667ff2SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 249f667ff2SGreg Roachuse Fisharebest\Webtrees\I18N; 2547166c20SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface; 269f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 279f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 2846b31fc1SGreg Roachuse Fisharebest\Webtrees\Validator; 299f667ff2SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 309f667ff2SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 319f667ff2SGreg Roach 329f667ff2SGreg Roachuse function array_flip; 339f667ff2SGreg Roach 349f667ff2SGreg Roach/** 359f667ff2SGreg Roach * Update a list of modules. 369f667ff2SGreg Roach */ 379f667ff2SGreg Roachabstract class AbstractModuleComponentAction implements RequestHandlerInterface 389f667ff2SGreg Roach{ 39ac732901SGreg Roach protected ModuleService $module_service; 409f667ff2SGreg Roach 41ac732901SGreg Roach protected TreeService $tree_service; 429f667ff2SGreg Roach 439f667ff2SGreg Roach /** 449f667ff2SGreg Roach * @param ModuleService $module_service 459f667ff2SGreg Roach * @param TreeService $tree_service 469f667ff2SGreg Roach */ 479f667ff2SGreg Roach public function __construct(ModuleService $module_service, TreeService $tree_service) 489f667ff2SGreg Roach { 499f667ff2SGreg Roach $this->module_service = $module_service; 509f667ff2SGreg Roach $this->tree_service = $tree_service; 519f667ff2SGreg Roach } 529f667ff2SGreg Roach 539f667ff2SGreg Roach /** 549f667ff2SGreg Roach * Update the access levels of the modules. 559f667ff2SGreg Roach * 5647166c20SGreg Roach * @template T of ModuleInterface 5747166c20SGreg Roach * 5847166c20SGreg Roach * @param class-string<T> $interface 599f667ff2SGreg Roach * @param ServerRequestInterface $request 609f667ff2SGreg Roach * 619f667ff2SGreg Roach * @return void 629f667ff2SGreg Roach */ 639f667ff2SGreg Roach protected function updateStatus(string $interface, ServerRequestInterface $request): void 649f667ff2SGreg Roach { 659f667ff2SGreg Roach $modules = $this->module_service->findByInterface($interface, true); 669f667ff2SGreg Roach 679f667ff2SGreg Roach foreach ($modules as $module) { 6846b31fc1SGreg Roach $enabled = Validator::parsedBody($request)->boolean('status-' . $module->name(), false); 699f667ff2SGreg Roach 709f667ff2SGreg Roach if ($enabled !== $module->isEnabled()) { 719f667ff2SGreg Roach DB::table('module') 729f667ff2SGreg Roach ->where('module_name', '=', $module->name()) 739f667ff2SGreg Roach ->update(['status' => $enabled ? 'enabled' : 'disabled']); 749f667ff2SGreg Roach 759f667ff2SGreg Roach if ($enabled) { 769f667ff2SGreg Roach $message = I18N::translate('The module “%s” has been enabled.', $module->title()); 779f667ff2SGreg Roach } else { 789f667ff2SGreg Roach $message = I18N::translate('The module “%s” has been disabled.', $module->title()); 799f667ff2SGreg Roach } 809f667ff2SGreg Roach 819f667ff2SGreg Roach FlashMessages::addMessage($message, 'success'); 829f667ff2SGreg Roach } 839f667ff2SGreg Roach } 849f667ff2SGreg Roach } 859f667ff2SGreg Roach 869f667ff2SGreg Roach /** 879f667ff2SGreg Roach * Update the access levels of the modules. 889f667ff2SGreg Roach * 8947166c20SGreg Roach * @template T of ModuleInterface 9047166c20SGreg Roach * 9147166c20SGreg Roach * @param class-string<T> $interface 929f667ff2SGreg Roach * @param ServerRequestInterface $request 939f667ff2SGreg Roach * 949f667ff2SGreg Roach * @return void 959f667ff2SGreg Roach */ 969f667ff2SGreg Roach protected function updateAccessLevel(string $interface, ServerRequestInterface $request): void 979f667ff2SGreg Roach { 989f667ff2SGreg Roach $modules = $this->module_service->findByInterface($interface, true); 999f667ff2SGreg Roach $trees = $this->tree_service->all(); 1009f667ff2SGreg Roach 1019f667ff2SGreg Roach foreach ($modules as $module) { 1029f667ff2SGreg Roach foreach ($trees as $tree) { 1039f667ff2SGreg Roach $key = 'access-' . $module->name() . '-' . $tree->id(); 104748dbe15SGreg Roach $access_level = Validator::parsedBody($request)->integer($key); 1059f667ff2SGreg Roach 1069f667ff2SGreg Roach if ($access_level !== $module->accessLevel($tree, $interface)) { 1079f667ff2SGreg Roach DB::table('module_privacy')->updateOrInsert([ 1089f667ff2SGreg Roach 'module_name' => $module->name(), 1099f667ff2SGreg Roach 'gedcom_id' => $tree->id(), 1109f667ff2SGreg Roach 'interface' => $interface, 1119f667ff2SGreg Roach ], [ 1129f667ff2SGreg Roach 'access_level' => $access_level, 1139f667ff2SGreg Roach ]); 1149f667ff2SGreg Roach } 1159f667ff2SGreg Roach } 1169f667ff2SGreg Roach } 1179f667ff2SGreg Roach } 1189f667ff2SGreg Roach 1199f667ff2SGreg Roach /** 1209f667ff2SGreg Roach * Update the access levels of the modules. 1219f667ff2SGreg Roach * 12247166c20SGreg Roach * @template T of ModuleInterface 12347166c20SGreg Roach * 12447166c20SGreg Roach * @param class-string<T> $interface 1259f667ff2SGreg Roach * @param string $column 1269f667ff2SGreg Roach * @param ServerRequestInterface $request 1279f667ff2SGreg Roach * 1289f667ff2SGreg Roach * @return void 1299f667ff2SGreg Roach */ 1309f667ff2SGreg Roach protected function updateOrder(string $interface, string $column, ServerRequestInterface $request): void 1319f667ff2SGreg Roach { 1329f667ff2SGreg Roach $modules = $this->module_service->findByInterface($interface, true); 133748dbe15SGreg Roach $order = Validator::parsedBody($request)->array('order'); 1349f667ff2SGreg Roach $order = array_flip($order); 1359f667ff2SGreg Roach 1369f667ff2SGreg Roach foreach ($modules as $module) { 1379f667ff2SGreg Roach DB::table('module') 1389f667ff2SGreg Roach ->where('module_name', '=', $module->name()) 1399f667ff2SGreg Roach ->update([ 1409f667ff2SGreg Roach $column => $order[$module->name()] ?? 0, 1419f667ff2SGreg Roach ]); 1429f667ff2SGreg Roach } 1439f667ff2SGreg Roach } 1449f667ff2SGreg Roach} 145