1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 15168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16168ff6f3Sric2016 */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 21168ff6f3Sric2016 2271378461SGreg Roachuse Aura\Router\RouterContainer; 2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 24d84d3988SGreg Roachuse Fisharebest\Webtrees\Auth; 25168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 26168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 28d84d3988SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3171378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3271378461SGreg Roach 339e18e23bSGreg Roachuse function app; 349e18e23bSGreg Roachuse function assert; 3571378461SGreg Roachuse function route; 36168ff6f3Sric2016 37168ff6f3Sric2016/** 38168ff6f3Sric2016 * Class CompactTreeChartModule 39168ff6f3Sric2016 */ 4071378461SGreg Roachclass CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 41c1010edaSGreg Roach{ 4249a243cbSGreg Roach use ModuleChartTrait; 4349a243cbSGreg Roach 4471378461SGreg Roach private const ROUTE_NAME = 'compact-chart'; 4571378461SGreg Roach private const ROUTE_URL = '/tree/{tree}/compact/{xref}'; 4671378461SGreg Roach 4757ab2231SGreg Roach /** @var ChartService */ 4857ab2231SGreg Roach private $chart_service; 4957ab2231SGreg Roach 5057ab2231SGreg Roach /** 5157ab2231SGreg Roach * CompactTreeChartModule constructor. 5257ab2231SGreg Roach * 5357ab2231SGreg Roach * @param ChartService $chart_service 5457ab2231SGreg Roach */ 553976b470SGreg Roach public function __construct(ChartService $chart_service) 563976b470SGreg Roach { 5757ab2231SGreg Roach $this->chart_service = $chart_service; 5857ab2231SGreg Roach } 5957ab2231SGreg Roach 60168ff6f3Sric2016 /** 6171378461SGreg Roach * Initialization. 6271378461SGreg Roach * 639e18e23bSGreg Roach * @return void 6471378461SGreg Roach */ 659e18e23bSGreg Roach public function boot(): void 6671378461SGreg Roach { 679e18e23bSGreg Roach $router_container = app(RouterContainer::class); 689e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 699e18e23bSGreg Roach 7071378461SGreg Roach $router_container->getMap() 71*f7358520SGreg Roach ->get(self::ROUTE_NAME, self::ROUTE_URL, $this) 7271378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 7371378461SGreg Roach } 7471378461SGreg Roach 7571378461SGreg Roach /** 760cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 77168ff6f3Sric2016 * 78168ff6f3Sric2016 * @return string 79168ff6f3Sric2016 */ 8049a243cbSGreg Roach public function title(): string 81c1010edaSGreg Roach { 82bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 83bbb76c12SGreg Roach return I18N::translate('Compact tree'); 84168ff6f3Sric2016 } 85168ff6f3Sric2016 86168ff6f3Sric2016 /** 87168ff6f3Sric2016 * A sentence describing what this module does. 88168ff6f3Sric2016 * 89168ff6f3Sric2016 * @return string 90168ff6f3Sric2016 */ 9149a243cbSGreg Roach public function description(): string 92c1010edaSGreg Roach { 93bbb76c12SGreg Roach /* I18N: Description of the “CompactTreeChart” module */ 94bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors, as a compact tree.'); 95168ff6f3Sric2016 } 96168ff6f3Sric2016 97168ff6f3Sric2016 /** 98377a2979SGreg Roach * CSS class for the URL. 99377a2979SGreg Roach * 100377a2979SGreg Roach * @return string 101377a2979SGreg Roach */ 102377a2979SGreg Roach public function chartMenuClass(): string 103377a2979SGreg Roach { 104377a2979SGreg Roach return 'menu-chart-compact'; 105377a2979SGreg Roach } 106377a2979SGreg Roach 107377a2979SGreg Roach /** 1084eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1094eb71cfaSGreg Roach * 11060bc3e3fSGreg Roach * @param Individual $individual 11160bc3e3fSGreg Roach * 1124eb71cfaSGreg Roach * @return Menu|null 1134eb71cfaSGreg Roach */ 114377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 115c1010edaSGreg Roach { 116e6562982SGreg Roach return $this->chartMenu($individual); 117e6562982SGreg Roach } 118e6562982SGreg Roach 119e6562982SGreg Roach /** 120e6562982SGreg Roach * The title for a specific instance of this chart. 121e6562982SGreg Roach * 122e6562982SGreg Roach * @param Individual $individual 123e6562982SGreg Roach * 124e6562982SGreg Roach * @return string 125e6562982SGreg Roach */ 126e6562982SGreg Roach public function chartTitle(Individual $individual): string 127e6562982SGreg Roach { 128e6562982SGreg Roach /* I18N: %s is an individual’s name */ 12939ca88baSGreg Roach return I18N::translate('Compact tree of %s', $individual->fullName()); 130e6562982SGreg Roach } 131d84d3988SGreg Roach 132d84d3988SGreg Roach /** 13371378461SGreg Roach * The URL for a page showing chart options. 134d84d3988SGreg Roach * 13571378461SGreg Roach * @param Individual $individual 13659597b37SGreg Roach * @param mixed[] $parameters 13771378461SGreg Roach * 13871378461SGreg Roach * @return string 13971378461SGreg Roach */ 14071378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 14171378461SGreg Roach { 14271378461SGreg Roach return route(self::ROUTE_NAME, [ 14371378461SGreg Roach 'xref' => $individual->xref(), 14471378461SGreg Roach 'tree' => $individual->tree()->name(), 14571378461SGreg Roach ] + $parameters); 14671378461SGreg Roach } 14771378461SGreg Roach 14871378461SGreg Roach /** 1496ccdf4f0SGreg Roach * @param ServerRequestInterface $request 150d84d3988SGreg Roach * 1516ccdf4f0SGreg Roach * @return ResponseInterface 152d84d3988SGreg Roach */ 15371378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 154d84d3988SGreg Roach { 15557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 15657ab2231SGreg Roach $user = $request->getAttribute('user'); 15771378461SGreg Roach $xref = $request->getAttribute('xref'); 1580b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 159d84d3988SGreg Roach $individual = Individual::getInstance($xref, $tree); 160d84d3988SGreg Roach 16171378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 16271378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 16371378461SGreg Roach return redirect(route(self::ROUTE_NAME, [ 16471378461SGreg Roach 'tree' => $request->getAttribute('tree')->name(), 16571378461SGreg Roach 'xref' => $request->getParsedBody()['xref'], 16671378461SGreg Roach ])); 16771378461SGreg Roach } 16871378461SGreg Roach 169d84d3988SGreg Roach Auth::checkIndividualAccess($individual); 1709867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 171d84d3988SGreg Roach 1720b93976aSGreg Roach if ($ajax === '1') { 17371378461SGreg Roach $this->layout = 'layouts/ajax'; 17471378461SGreg Roach 17571378461SGreg Roach return $this->viewResponse('modules/compact-chart/chart', [ 17671378461SGreg Roach 'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5), 17771378461SGreg Roach 'module' => $this, 17871378461SGreg Roach ]); 179d84d3988SGreg Roach } 180d84d3988SGreg Roach 181389266c0SGreg Roach $ajax_url = $this->chartUrl($individual, [ 1829b5537c3SGreg Roach 'ajax' => true, 183389266c0SGreg Roach ]); 184389266c0SGreg Roach 1859b5537c3SGreg Roach return $this->viewResponse('modules/compact-chart/page', [ 186389266c0SGreg Roach 'ajax_url' => $ajax_url, 187d84d3988SGreg Roach 'individual' => $individual, 18871378461SGreg Roach 'module' => $this->name(), 189389266c0SGreg Roach 'title' => $this->chartTitle($individual), 190ef5d23f1SGreg Roach 'tree' => $tree, 191d84d3988SGreg Roach ]); 192d84d3988SGreg Roach } 193168ff6f3Sric2016} 194