1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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 82168ff6f3Sric2016 /** 83168ff6f3Sric2016 * A sentence describing what this module does. 84168ff6f3Sric2016 * 85168ff6f3Sric2016 * @return string 86168ff6f3Sric2016 */ 8749a243cbSGreg Roach public function description(): string 88c1010edaSGreg Roach { 89bbb76c12SGreg Roach /* I18N: Description of the “HourglassChart” module */ 90bbb76c12SGreg Roach return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.'); 91168ff6f3Sric2016 } 92168ff6f3Sric2016 93168ff6f3Sric2016 /** 94377a2979SGreg Roach * CSS class for the URL. 95377a2979SGreg Roach * 96377a2979SGreg Roach * @return string 97377a2979SGreg Roach */ 98377a2979SGreg Roach public function chartMenuClass(): string 99377a2979SGreg Roach { 100377a2979SGreg Roach return 'menu-chart-hourglass'; 101377a2979SGreg Roach } 102377a2979SGreg Roach 103377a2979SGreg Roach /** 1044eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1054eb71cfaSGreg Roach * 10660bc3e3fSGreg Roach * @param Individual $individual 10760bc3e3fSGreg Roach * 1084eb71cfaSGreg Roach * @return Menu|null 1094eb71cfaSGreg Roach */ 110377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 111c1010edaSGreg Roach { 112e6562982SGreg Roach return $this->chartMenu($individual); 113e6562982SGreg Roach } 114e6562982SGreg Roach 115e6562982SGreg Roach /** 11671378461SGreg Roach * The title for a specific instance of this chart. 117e6562982SGreg Roach * 11871378461SGreg Roach * @param Individual $individual 11971378461SGreg Roach * 12071378461SGreg Roach * @return string 12171378461SGreg Roach */ 12271378461SGreg Roach public function chartTitle(Individual $individual): string 12371378461SGreg Roach { 12471378461SGreg Roach /* I18N: %s is an individual’s name */ 12571378461SGreg Roach return I18N::translate('Hourglass chart of %s', $individual->fullName()); 12671378461SGreg Roach } 12771378461SGreg Roach 12871378461SGreg Roach /** 12971378461SGreg Roach * The URL for a page showing chart options. 13071378461SGreg Roach * 13171378461SGreg Roach * @param Individual $individual 13276d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 13371378461SGreg Roach * 13471378461SGreg Roach * @return string 13571378461SGreg Roach */ 13671378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 13771378461SGreg Roach { 13872f04adfSGreg Roach return route(static::class, [ 13971378461SGreg Roach 'xref' => $individual->xref(), 14071378461SGreg Roach 'tree' => $individual->tree()->name(), 14171378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 14271378461SGreg Roach } 14371378461SGreg Roach 14471378461SGreg Roach /** 1456ccdf4f0SGreg Roach * @param ServerRequestInterface $request 146e6562982SGreg Roach * 1476ccdf4f0SGreg Roach * @return ResponseInterface 148e6562982SGreg Roach */ 14971378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 150e6562982SGreg Roach { 151b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 152b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 153b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 154b55cbc6bSGreg Roach $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 155*685d6819SJonathan Jaubart $spouses = Validator::attributes($request)->boolean('spouses', self::DEFAULT_SPOUSES); 156b55cbc6bSGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 15726684e68SGreg Roach 15871378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 15971378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 16072f04adfSGreg Roach return redirect(route(static::class, [ 1614ea62551SGreg Roach 'tree' => $tree->name(), 162158900c2SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 163158900c2SGreg Roach 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 164*685d6819SJonathan Jaubart 'spouses' => Validator::parsedBody($request)->boolean('spouses', self::DEFAULT_SPOUSES), 16571378461SGreg Roach ])); 16671378461SGreg Roach } 16771378461SGreg Roach 168ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 16926684e68SGreg Roach 170b55cbc6bSGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 171b55cbc6bSGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 17226684e68SGreg Roach 173b55cbc6bSGreg Roach if ($ajax) { 17471378461SGreg Roach $this->layout = 'layouts/ajax'; 17571378461SGreg Roach 17671378461SGreg Roach return $this->viewResponse('modules/hourglass-chart/chart', [ 17771378461SGreg Roach 'generations' => $generations, 17871378461SGreg Roach 'individual' => $individual, 17971378461SGreg Roach 'spouses' => $spouses, 18071378461SGreg Roach ]); 18126684e68SGreg Roach } 18226684e68SGreg Roach 18326684e68SGreg Roach $ajax_url = $this->chartUrl($individual, [ 1849b5537c3SGreg Roach 'ajax' => true, 1852b29a33eSmakitso 'generations' => $generations, 18671378461SGreg Roach 'spouses' => $spouses, 18726684e68SGreg Roach ]); 18826684e68SGreg Roach 1899b5537c3SGreg Roach return $this->viewResponse('modules/hourglass-chart/page', [ 19026684e68SGreg Roach 'ajax_url' => $ajax_url, 19126684e68SGreg Roach 'generations' => $generations, 19226684e68SGreg Roach 'individual' => $individual, 193e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 19426684e68SGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 19571378461SGreg Roach 'module' => $this->name(), 19671378461SGreg Roach 'spouses' => $spouses, 19726684e68SGreg Roach 'title' => $this->chartTitle($individual), 198ef5d23f1SGreg Roach 'tree' => $tree, 19926684e68SGreg Roach ]); 20026684e68SGreg Roach } 20126684e68SGreg Roach 20226684e68SGreg Roach /** 203cb0eb4a9SGreg Roach * Generate an extension to the chart 204cb0eb4a9SGreg Roach * 205cb0eb4a9SGreg Roach * @param ServerRequestInterface $request 206cb0eb4a9SGreg Roach * 207cb0eb4a9SGreg Roach * @return ResponseInterface 208cb0eb4a9SGreg Roach */ 209cb0eb4a9SGreg Roach public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface 210cb0eb4a9SGreg Roach { 211b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 212158900c2SGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref'); 2136b9cb339SGreg Roach $family = Registry::familyFactory()->make($xref, $tree); 214ddeb3354SGreg Roach $family = Auth::checkFamilyAccess($family); 215cb0eb4a9SGreg Roach 216cb0eb4a9SGreg Roach return response(view('modules/hourglass-chart/parents', [ 217cb0eb4a9SGreg Roach 'family' => $family, 218cb0eb4a9SGreg Roach 'generations' => 1, 21926684e68SGreg Roach ])); 22026684e68SGreg Roach } 22126684e68SGreg Roach 22226684e68SGreg Roach /** 223cb0eb4a9SGreg Roach * Generate an extension to the chart 224cb0eb4a9SGreg Roach * 2256ccdf4f0SGreg Roach * @param ServerRequestInterface $request 22626684e68SGreg Roach * 2276ccdf4f0SGreg Roach * @return ResponseInterface 22826684e68SGreg Roach */ 229cb0eb4a9SGreg Roach public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 23026684e68SGreg Roach { 231b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 232158900c2SGreg Roach $xref = Validator::queryParams($request)->isXref()->string('xref'); 233158900c2SGreg Roach $spouses = Validator::queryParams($request)->boolean('spouses', false); 2346b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 2353c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 236a21e5374SGreg Roach 237a21e5374SGreg Roach $children = $individual->spouseFamilies()->map(static function (Family $family): Collection { 238a21e5374SGreg Roach return $family->children(); 239a21e5374SGreg Roach })->flatten(); 24026684e68SGreg Roach 241cb0eb4a9SGreg Roach return response(view('modules/hourglass-chart/children', [ 242cb0eb4a9SGreg Roach 'children' => $children, 243cb0eb4a9SGreg Roach 'generations' => 1, 24471378461SGreg Roach 'spouses' => $spouses, 245cb0eb4a9SGreg Roach ])); 246e6562982SGreg Roach } 247168ff6f3Sric2016} 248