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; 2326684e68SGreg Roachuse Fisharebest\Webtrees\Auth; 24cb0eb4a9SGreg Roachuse Fisharebest\Webtrees\Family; 25168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 26168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Registry; 29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 30cb0eb4a9SGreg Roachuse Illuminate\Support\Collection; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3371378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 343976b470SGreg Roach 35cb0eb4a9SGreg Roachuse function response; 36f4ba05e3SGreg Roachuse function view; 37168ff6f3Sric2016 38168ff6f3Sric2016/** 39168ff6f3Sric2016 * Class HourglassChartModule 40168ff6f3Sric2016 */ 4171378461SGreg Roachclass HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 42c1010edaSGreg Roach{ 4349a243cbSGreg Roach use ModuleChartTrait; 4449a243cbSGreg Roach 4572f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}'; 4671378461SGreg Roach 4726684e68SGreg Roach // Defaults 48266e9c61SGreg Roach public const DEFAULT_GENERATIONS = '3'; 49266e9c61SGreg Roach public const DEFAULT_SPOUSES = false; 5071378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 5171378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 5271378461SGreg Roach 'spouses' => self::DEFAULT_SPOUSES, 5371378461SGreg Roach ]; 5426684e68SGreg Roach 5526684e68SGreg Roach // Limits 5671378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 5771378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 5871378461SGreg Roach 5971378461SGreg Roach /** 6071378461SGreg Roach * Initialization. 6171378461SGreg Roach * 629e18e23bSGreg Roach * @return void 6371378461SGreg Roach */ 649e18e23bSGreg Roach public function boot(): void 6571378461SGreg Roach { 66158900c2SGreg Roach Registry::routeFactory()->routeMap() 6772f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 68158900c2SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 6971378461SGreg Roach } 7026684e68SGreg Roach 71168ff6f3Sric2016 /** 720cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 73168ff6f3Sric2016 * 74168ff6f3Sric2016 * @return string 75168ff6f3Sric2016 */ 7649a243cbSGreg Roach public function title(): string 77c1010edaSGreg Roach { 78bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 79bbb76c12SGreg Roach return I18N::translate('Hourglass chart'); 80168ff6f3Sric2016 } 81168ff6f3Sric2016 8249a243cbSGreg Roach public function description(): string 83c1010edaSGreg Roach { 84bbb76c12SGreg Roach /* I18N: Description of the “HourglassChart” module */ 85bbb76c12SGreg Roach return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.'); 86168ff6f3Sric2016 } 87168ff6f3Sric2016 88168ff6f3Sric2016 /** 89377a2979SGreg Roach * CSS class for the URL. 90377a2979SGreg Roach * 91377a2979SGreg Roach * @return string 92377a2979SGreg Roach */ 93377a2979SGreg Roach public function chartMenuClass(): string 94377a2979SGreg Roach { 95377a2979SGreg Roach return 'menu-chart-hourglass'; 96377a2979SGreg Roach } 97377a2979SGreg Roach 98377a2979SGreg Roach /** 994eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1004eb71cfaSGreg Roach * 10160bc3e3fSGreg Roach * @param Individual $individual 10260bc3e3fSGreg Roach * 1034eb71cfaSGreg Roach * @return Menu|null 1044eb71cfaSGreg Roach */ 105*1ff45046SGreg Roach public function chartBoxMenu(Individual $individual): Menu|null 106c1010edaSGreg Roach { 107e6562982SGreg Roach return $this->chartMenu($individual); 108e6562982SGreg Roach } 109e6562982SGreg Roach 110e6562982SGreg Roach /** 11171378461SGreg Roach * The title for a specific instance of this chart. 112e6562982SGreg Roach * 11371378461SGreg Roach * @param Individual $individual 11471378461SGreg Roach * 11571378461SGreg Roach * @return string 11671378461SGreg Roach */ 11771378461SGreg Roach public function chartTitle(Individual $individual): string 11871378461SGreg Roach { 11971378461SGreg Roach /* I18N: %s is an individual’s name */ 12071378461SGreg Roach return I18N::translate('Hourglass chart of %s', $individual->fullName()); 12171378461SGreg Roach } 12271378461SGreg Roach 12371378461SGreg Roach /** 12471378461SGreg Roach * The URL for a page showing chart options. 12571378461SGreg Roach * 12671378461SGreg Roach * @param Individual $individual 12776d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 12871378461SGreg Roach * 12971378461SGreg Roach * @return string 13071378461SGreg Roach */ 13171378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 13271378461SGreg Roach { 13372f04adfSGreg Roach return route(static::class, [ 13471378461SGreg Roach 'xref' => $individual->xref(), 13571378461SGreg Roach 'tree' => $individual->tree()->name(), 13671378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 13771378461SGreg Roach } 13871378461SGreg Roach 13971378461SGreg Roach /** 1406ccdf4f0SGreg Roach * @param ServerRequestInterface $request 141e6562982SGreg Roach * 1426ccdf4f0SGreg Roach * @return ResponseInterface 143e6562982SGreg Roach */ 14471378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 145e6562982SGreg Roach { 146b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 147b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 148b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 149b55cbc6bSGreg Roach $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 150685d6819SJonathan Jaubart $spouses = Validator::attributes($request)->boolean('spouses', self::DEFAULT_SPOUSES); 151b55cbc6bSGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 15226684e68SGreg Roach 15371378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 15471378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 15572f04adfSGreg Roach return redirect(route(static::class, [ 1564ea62551SGreg Roach 'tree' => $tree->name(), 157158900c2SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 158158900c2SGreg Roach 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 159685d6819SJonathan Jaubart 'spouses' => Validator::parsedBody($request)->boolean('spouses', self::DEFAULT_SPOUSES), 16071378461SGreg Roach ])); 16171378461SGreg Roach } 16271378461SGreg Roach 163ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 16426684e68SGreg Roach 165b55cbc6bSGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 166b55cbc6bSGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 16726684e68SGreg Roach 168b55cbc6bSGreg Roach if ($ajax) { 16971378461SGreg Roach $this->layout = 'layouts/ajax'; 17071378461SGreg Roach 17171378461SGreg Roach return $this->viewResponse('modules/hourglass-chart/chart', [ 17271378461SGreg Roach 'generations' => $generations, 17371378461SGreg Roach 'individual' => $individual, 17471378461SGreg Roach 'spouses' => $spouses, 17571378461SGreg Roach ]); 17626684e68SGreg Roach } 17726684e68SGreg Roach 17826684e68SGreg Roach $ajax_url = $this->chartUrl($individual, [ 1799b5537c3SGreg Roach 'ajax' => true, 1802b29a33eSmakitso 'generations' => $generations, 18171378461SGreg Roach 'spouses' => $spouses, 18226684e68SGreg Roach ]); 18326684e68SGreg Roach 1849b5537c3SGreg Roach return $this->viewResponse('modules/hourglass-chart/page', [ 18526684e68SGreg Roach 'ajax_url' => $ajax_url, 18626684e68SGreg Roach 'generations' => $generations, 18726684e68SGreg Roach 'individual' => $individual, 188e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 18926684e68SGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 19071378461SGreg Roach 'module' => $this->name(), 19171378461SGreg Roach 'spouses' => $spouses, 19226684e68SGreg Roach 'title' => $this->chartTitle($individual), 193ef5d23f1SGreg Roach 'tree' => $tree, 19426684e68SGreg Roach ]); 19526684e68SGreg Roach } 19626684e68SGreg Roach 19726684e68SGreg Roach /** 198cb0eb4a9SGreg Roach * Generate an extension to the chart 199cb0eb4a9SGreg Roach * 200cb0eb4a9SGreg Roach * @param ServerRequestInterface $request 201cb0eb4a9SGreg Roach * 202cb0eb4a9SGreg Roach * @return ResponseInterface 203cb0eb4a9SGreg Roach */ 204cb0eb4a9SGreg Roach public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface 205cb0eb4a9SGreg Roach { 206b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 207158900c2SGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref'); 2086b9cb339SGreg Roach $family = Registry::familyFactory()->make($xref, $tree); 209ddeb3354SGreg Roach $family = Auth::checkFamilyAccess($family); 210cb0eb4a9SGreg Roach 211cb0eb4a9SGreg Roach return response(view('modules/hourglass-chart/parents', [ 212cb0eb4a9SGreg Roach 'family' => $family, 213cb0eb4a9SGreg Roach 'generations' => 1, 21426684e68SGreg Roach ])); 21526684e68SGreg Roach } 21626684e68SGreg Roach 21726684e68SGreg Roach /** 218cb0eb4a9SGreg Roach * Generate an extension to the chart 219cb0eb4a9SGreg Roach * 2206ccdf4f0SGreg Roach * @param ServerRequestInterface $request 22126684e68SGreg Roach * 2226ccdf4f0SGreg Roach * @return ResponseInterface 22326684e68SGreg Roach */ 224cb0eb4a9SGreg Roach public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 22526684e68SGreg Roach { 226b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 227158900c2SGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref'); 228158900c2SGreg Roach $spouses = Validator::queryParams($request)->boolean('spouses', false); 2296b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 2303c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 231a21e5374SGreg Roach 232f25fc0f9SGreg Roach $children = $individual->spouseFamilies()->map(static fn (Family $family): Collection => $family->children())->flatten(); 23326684e68SGreg Roach 234cb0eb4a9SGreg Roach return response(view('modules/hourglass-chart/children', [ 235cb0eb4a9SGreg Roach 'children' => $children, 236cb0eb4a9SGreg Roach 'generations' => 1, 23771378461SGreg Roach 'spouses' => $spouses, 238cb0eb4a9SGreg Roach ])); 239e6562982SGreg Roach } 240168ff6f3Sric2016} 241