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\Module; 21 22use Fig\Http\Message\RequestMethodInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Individual; 26use Fisharebest\Webtrees\Menu; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\ChartService; 29use Fisharebest\Webtrees\Validator; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function route; 35 36/** 37 * Class CompactTreeChartModule 38 */ 39class CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 40{ 41 use ModuleChartTrait; 42 43 protected const ROUTE_URL = '/tree/{tree}/compact/{xref}'; 44 45 private ChartService $chart_service; 46 47 /** 48 * @param ChartService $chart_service 49 */ 50 public function __construct(ChartService $chart_service) 51 { 52 $this->chart_service = $chart_service; 53 } 54 55 /** 56 * Initialization. 57 * 58 * @return void 59 */ 60 public function boot(): void 61 { 62 Registry::routeFactory()->routeMap() 63 ->get(static::class, static::ROUTE_URL, $this) 64 ->allows(RequestMethodInterface::METHOD_POST); 65 } 66 67 /** 68 * How should this module be identified in the control panel, etc.? 69 * 70 * @return string 71 */ 72 public function title(): string 73 { 74 /* I18N: Name of a module/chart */ 75 return I18N::translate('Compact tree'); 76 } 77 78 /** 79 * A sentence describing what this module does. 80 * 81 * @return string 82 */ 83 public function description(): string 84 { 85 /* I18N: Description of the “CompactTreeChart” module */ 86 return I18N::translate('A chart of an individual’s ancestors, as a compact tree.'); 87 } 88 89 /** 90 * CSS class for the URL. 91 * 92 * @return string 93 */ 94 public function chartMenuClass(): string 95 { 96 return 'menu-chart-compact'; 97 } 98 99 /** 100 * Return a menu item for this chart - for use in individual boxes. 101 */ 102 public function chartBoxMenu(Individual $individual): Menu|null 103 { 104 return $this->chartMenu($individual); 105 } 106 107 /** 108 * The title for a specific instance of this chart. 109 * 110 * @param Individual $individual 111 * 112 * @return string 113 */ 114 public function chartTitle(Individual $individual): string 115 { 116 /* I18N: %s is an individual’s name */ 117 return I18N::translate('Compact tree of %s', $individual->fullName()); 118 } 119 120 /** 121 * The URL for a page showing chart options. 122 * 123 * @param Individual $individual 124 * @param array<bool|int|string|array<string>|null> $parameters 125 * 126 * @return string 127 */ 128 public function chartUrl(Individual $individual, array $parameters = []): string 129 { 130 return route(static::class, [ 131 'xref' => $individual->xref(), 132 'tree' => $individual->tree()->name(), 133 ] + $parameters); 134 } 135 136 /** 137 * @param ServerRequestInterface $request 138 * 139 * @return ResponseInterface 140 */ 141 public function handle(ServerRequestInterface $request): ResponseInterface 142 { 143 $tree = Validator::attributes($request)->tree(); 144 $user = Validator::attributes($request)->user(); 145 $xref = Validator::attributes($request)->isXref()->string('xref'); 146 $ajax = Validator::queryParams($request)->boolean('ajax', false); 147 148 // Convert POST requests into GET requests for pretty URLs. 149 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 150 return redirect(route(static::class, [ 151 'tree' => $tree->name(), 152 'xref' => Validator::parsedBody($request)->string('xref', ''), 153 ])); 154 } 155 156 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 157 158 $individual = Registry::individualFactory()->make($xref, $tree); 159 $individual = Auth::checkIndividualAccess($individual, false, true); 160 161 if ($ajax) { 162 $this->layout = 'layouts/ajax'; 163 164 return $this->viewResponse('modules/compact-chart/chart', [ 165 'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5), 166 'module' => $this, 167 ]); 168 } 169 170 $ajax_url = $this->chartUrl($individual, [ 171 'ajax' => true, 172 ]); 173 174 return $this->viewResponse('modules/compact-chart/page', [ 175 'ajax_url' => $ajax_url, 176 'individual' => $individual, 177 'module' => $this->name(), 178 'title' => $this->chartTitle($individual), 179 'tree' => $tree, 180 ]); 181 } 182} 183