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