1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify 7168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by 8168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or 9168ff6f3Sric2016 * (at your option) any later version. 10168ff6f3Sric2016 * This program is distributed in the hope that it will be useful, 11168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13168ff6f3Sric2016 * GNU General Public License for more details. 14168ff6f3Sric2016 * 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/>. 16168ff6f3Sric2016 */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 21168ff6f3Sric2016 2271378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 23d84d3988SGreg Roachuse Fisharebest\Webtrees\Auth; 24168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 25168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Registry; 28d84d3988SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3271378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3371378461SGreg Roach 3471378461SGreg Roachuse function route; 35168ff6f3Sric2016 36168ff6f3Sric2016/** 37168ff6f3Sric2016 * Class CompactTreeChartModule 38168ff6f3Sric2016 */ 3971378461SGreg Roachclass CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleChartTrait; 4249a243cbSGreg Roach 4372f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/compact/{xref}'; 4471378461SGreg Roach 4543f2f523SGreg Roach private ChartService $chart_service; 4657ab2231SGreg Roach 4757ab2231SGreg Roach /** 4857ab2231SGreg Roach * @param ChartService $chart_service 4957ab2231SGreg Roach */ 503976b470SGreg Roach public function __construct(ChartService $chart_service) 513976b470SGreg Roach { 5257ab2231SGreg Roach $this->chart_service = $chart_service; 5357ab2231SGreg Roach } 5457ab2231SGreg Roach 55168ff6f3Sric2016 /** 5671378461SGreg Roach * Initialization. 5771378461SGreg Roach * 589e18e23bSGreg Roach * @return void 5971378461SGreg Roach */ 609e18e23bSGreg Roach public function boot(): void 6171378461SGreg Roach { 62158900c2SGreg Roach Registry::routeFactory()->routeMap() 6372f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 6471378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 6571378461SGreg Roach } 6671378461SGreg Roach 6771378461SGreg Roach /** 680cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 69168ff6f3Sric2016 * 70168ff6f3Sric2016 * @return string 71168ff6f3Sric2016 */ 7249a243cbSGreg Roach public function title(): string 73c1010edaSGreg Roach { 74bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 75bbb76c12SGreg Roach return I18N::translate('Compact tree'); 76168ff6f3Sric2016 } 77168ff6f3Sric2016 7849a243cbSGreg Roach public function description(): string 79c1010edaSGreg Roach { 80bbb76c12SGreg Roach /* I18N: Description of the “CompactTreeChart” module */ 81bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors, as a compact tree.'); 82168ff6f3Sric2016 } 83168ff6f3Sric2016 84168ff6f3Sric2016 /** 85377a2979SGreg Roach * CSS class for the URL. 86377a2979SGreg Roach * 87377a2979SGreg Roach * @return string 88377a2979SGreg Roach */ 89377a2979SGreg Roach public function chartMenuClass(): string 90377a2979SGreg Roach { 91377a2979SGreg Roach return 'menu-chart-compact'; 92377a2979SGreg Roach } 93377a2979SGreg Roach 94377a2979SGreg Roach /** 954eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 964eb71cfaSGreg Roach */ 97*1ff45046SGreg Roach public function chartBoxMenu(Individual $individual): Menu|null 98c1010edaSGreg Roach { 99e6562982SGreg Roach return $this->chartMenu($individual); 100e6562982SGreg Roach } 101e6562982SGreg Roach 102e6562982SGreg Roach /** 103e6562982SGreg Roach * The title for a specific instance of this chart. 104e6562982SGreg Roach * 105e6562982SGreg Roach * @param Individual $individual 106e6562982SGreg Roach * 107e6562982SGreg Roach * @return string 108e6562982SGreg Roach */ 109e6562982SGreg Roach public function chartTitle(Individual $individual): string 110e6562982SGreg Roach { 111e6562982SGreg Roach /* I18N: %s is an individual’s name */ 11239ca88baSGreg Roach return I18N::translate('Compact tree of %s', $individual->fullName()); 113e6562982SGreg Roach } 114d84d3988SGreg Roach 115d84d3988SGreg Roach /** 11671378461SGreg Roach * The URL for a page showing chart options. 117d84d3988SGreg Roach * 11871378461SGreg Roach * @param Individual $individual 11976d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 12071378461SGreg Roach * 12171378461SGreg Roach * @return string 12271378461SGreg Roach */ 12371378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 12471378461SGreg Roach { 12572f04adfSGreg Roach return route(static::class, [ 12671378461SGreg Roach 'xref' => $individual->xref(), 12771378461SGreg Roach 'tree' => $individual->tree()->name(), 12871378461SGreg Roach ] + $parameters); 12971378461SGreg Roach } 13071378461SGreg Roach 13171378461SGreg Roach /** 1326ccdf4f0SGreg Roach * @param ServerRequestInterface $request 133d84d3988SGreg Roach * 1346ccdf4f0SGreg Roach * @return ResponseInterface 135d84d3988SGreg Roach */ 13671378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 137d84d3988SGreg Roach { 138b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 139b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 140b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 141b55cbc6bSGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 142d84d3988SGreg Roach 14371378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 14471378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 14572f04adfSGreg Roach return redirect(route(static::class, [ 1464ea62551SGreg Roach 'tree' => $tree->name(), 147b55cbc6bSGreg Roach 'xref' => Validator::parsedBody($request)->string('xref', ''), 14871378461SGreg Roach ])); 14971378461SGreg Roach } 15071378461SGreg Roach 151ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 152d84d3988SGreg Roach 153b55cbc6bSGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 154b55cbc6bSGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 155b55cbc6bSGreg Roach 156b55cbc6bSGreg Roach if ($ajax) { 15771378461SGreg Roach $this->layout = 'layouts/ajax'; 15871378461SGreg Roach 15971378461SGreg Roach return $this->viewResponse('modules/compact-chart/chart', [ 16071378461SGreg Roach 'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5), 16171378461SGreg Roach 'module' => $this, 16271378461SGreg Roach ]); 163d84d3988SGreg Roach } 164d84d3988SGreg Roach 165389266c0SGreg Roach $ajax_url = $this->chartUrl($individual, [ 1669b5537c3SGreg Roach 'ajax' => true, 167389266c0SGreg Roach ]); 168389266c0SGreg Roach 1699b5537c3SGreg Roach return $this->viewResponse('modules/compact-chart/page', [ 170389266c0SGreg Roach 'ajax_url' => $ajax_url, 171d84d3988SGreg Roach 'individual' => $individual, 17271378461SGreg Roach 'module' => $this->name(), 173389266c0SGreg Roach 'title' => $this->chartTitle($individual), 174ef5d23f1SGreg Roach 'tree' => $tree, 175d84d3988SGreg Roach ]); 176d84d3988SGreg Roach } 177168ff6f3Sric2016} 178