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; 2426684e68SGreg Roachuse Fisharebest\Webtrees\Auth; 25cb0eb4a9SGreg Roachuse Fisharebest\Webtrees\Family; 26168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 27168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 295229eadeSGreg Roachuse Fisharebest\Webtrees\Tree; 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 359e18e23bSGreg Roachuse function app; 365229eadeSGreg Roachuse function assert; 37ddeb3354SGreg Roachuse function is_string; 38cb0eb4a9SGreg Roachuse function response; 39f4ba05e3SGreg Roachuse function view; 40168ff6f3Sric2016 41168ff6f3Sric2016/** 42168ff6f3Sric2016 * Class HourglassChartModule 43168ff6f3Sric2016 */ 4471378461SGreg Roachclass HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 45c1010edaSGreg Roach{ 4649a243cbSGreg Roach use ModuleChartTrait; 4749a243cbSGreg Roach 4871378461SGreg Roach private const ROUTE_NAME = 'hourglass-chart'; 4971378461SGreg Roach private const ROUTE_URL = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}'; 5071378461SGreg Roach 5126684e68SGreg Roach // Defaults 5226684e68SGreg Roach private const DEFAULT_GENERATIONS = '3'; 5371378461SGreg Roach private const DEFAULT_SPOUSES = false; 5471378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 5571378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 5671378461SGreg Roach 'spouses' => self::DEFAULT_SPOUSES, 5771378461SGreg Roach ]; 5826684e68SGreg Roach 5926684e68SGreg Roach // Limits 6071378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 6171378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 6271378461SGreg Roach 6371378461SGreg Roach /** 6471378461SGreg Roach * Initialization. 6571378461SGreg Roach * 669e18e23bSGreg Roach * @return void 6771378461SGreg Roach */ 689e18e23bSGreg Roach public function boot(): void 6971378461SGreg Roach { 709e18e23bSGreg Roach $router_container = app(RouterContainer::class); 719e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 729e18e23bSGreg Roach 7371378461SGreg Roach $router_container->getMap() 74f7358520SGreg Roach ->get(self::ROUTE_NAME, self::ROUTE_URL, $this) 7571378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 7671378461SGreg Roach ->tokens([ 7771378461SGreg Roach 'generations' => '\d+', 7871378461SGreg Roach 'spouses' => '1?', 7971378461SGreg Roach ]); 8071378461SGreg Roach } 8126684e68SGreg Roach 82168ff6f3Sric2016 /** 830cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 84168ff6f3Sric2016 * 85168ff6f3Sric2016 * @return string 86168ff6f3Sric2016 */ 8749a243cbSGreg Roach public function title(): string 88c1010edaSGreg Roach { 89bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 90bbb76c12SGreg Roach return I18N::translate('Hourglass chart'); 91168ff6f3Sric2016 } 92168ff6f3Sric2016 93168ff6f3Sric2016 /** 94168ff6f3Sric2016 * A sentence describing what this module does. 95168ff6f3Sric2016 * 96168ff6f3Sric2016 * @return string 97168ff6f3Sric2016 */ 9849a243cbSGreg Roach public function description(): string 99c1010edaSGreg Roach { 100bbb76c12SGreg Roach /* I18N: Description of the “HourglassChart” module */ 101bbb76c12SGreg Roach return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.'); 102168ff6f3Sric2016 } 103168ff6f3Sric2016 104168ff6f3Sric2016 /** 105377a2979SGreg Roach * CSS class for the URL. 106377a2979SGreg Roach * 107377a2979SGreg Roach * @return string 108377a2979SGreg Roach */ 109377a2979SGreg Roach public function chartMenuClass(): string 110377a2979SGreg Roach { 111377a2979SGreg Roach return 'menu-chart-hourglass'; 112377a2979SGreg Roach } 113377a2979SGreg Roach 114377a2979SGreg Roach /** 1154eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1164eb71cfaSGreg Roach * 11760bc3e3fSGreg Roach * @param Individual $individual 11860bc3e3fSGreg Roach * 1194eb71cfaSGreg Roach * @return Menu|null 1204eb71cfaSGreg Roach */ 121377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 122c1010edaSGreg Roach { 123e6562982SGreg Roach return $this->chartMenu($individual); 124e6562982SGreg Roach } 125e6562982SGreg Roach 126e6562982SGreg Roach /** 12771378461SGreg Roach * The title for a specific instance of this chart. 128e6562982SGreg Roach * 12971378461SGreg Roach * @param Individual $individual 13071378461SGreg Roach * 13171378461SGreg Roach * @return string 13271378461SGreg Roach */ 13371378461SGreg Roach public function chartTitle(Individual $individual): string 13471378461SGreg Roach { 13571378461SGreg Roach /* I18N: %s is an individual’s name */ 13671378461SGreg Roach return I18N::translate('Hourglass chart of %s', $individual->fullName()); 13771378461SGreg Roach } 13871378461SGreg Roach 13971378461SGreg Roach /** 14071378461SGreg Roach * The URL for a page showing chart options. 14171378461SGreg Roach * 14271378461SGreg Roach * @param Individual $individual 14359597b37SGreg Roach * @param mixed[] $parameters 14471378461SGreg Roach * 14571378461SGreg Roach * @return string 14671378461SGreg Roach */ 14771378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 14871378461SGreg Roach { 14971378461SGreg Roach return route(self::ROUTE_NAME, [ 15071378461SGreg Roach 'xref' => $individual->xref(), 15171378461SGreg Roach 'tree' => $individual->tree()->name(), 15271378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 15371378461SGreg Roach } 15471378461SGreg Roach 15571378461SGreg Roach /** 1566ccdf4f0SGreg Roach * @param ServerRequestInterface $request 157e6562982SGreg Roach * 1586ccdf4f0SGreg Roach * @return ResponseInterface 159e6562982SGreg Roach */ 16071378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 161e6562982SGreg Roach { 16257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1634ea62551SGreg Roach assert($tree instanceof Tree); 1644ea62551SGreg Roach 16571378461SGreg Roach $xref = $request->getAttribute('xref'); 166ddeb3354SGreg Roach assert(is_string($xref)); 167ddeb3354SGreg Roach 168ddeb3354SGreg Roach $individual = Individual::getInstance($xref, $tree); 1693c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 170ddeb3354SGreg Roach 171ddeb3354SGreg Roach $user = $request->getAttribute('user'); 17271378461SGreg Roach $generations = (int) $request->getAttribute('generations'); 17371378461SGreg Roach $spouses = (bool) $request->getAttribute('spouses'); 1740b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 17526684e68SGreg Roach 17671378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 17771378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 178*b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 179*b46c87bdSGreg Roach 18071378461SGreg Roach return redirect(route(self::ROUTE_NAME, [ 1814ea62551SGreg Roach 'tree' => $tree->name(), 182*b46c87bdSGreg Roach 'xref' => $params['xref'], 183*b46c87bdSGreg Roach 'generations' => $params['generations'], 184*b46c87bdSGreg Roach 'spouses' => $params['spouses'] ?? false, 18571378461SGreg Roach ])); 18671378461SGreg Roach } 18771378461SGreg Roach 1889867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 18926684e68SGreg Roach 190e759aebbSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 19126684e68SGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 19226684e68SGreg Roach 1930b93976aSGreg Roach if ($ajax === '1') { 19471378461SGreg Roach $this->layout = 'layouts/ajax'; 19571378461SGreg Roach 19671378461SGreg Roach return $this->viewResponse('modules/hourglass-chart/chart', [ 19771378461SGreg Roach 'generations' => $generations, 19871378461SGreg Roach 'individual' => $individual, 19971378461SGreg Roach 'spouses' => $spouses, 20071378461SGreg Roach ]); 20126684e68SGreg Roach } 20226684e68SGreg Roach 20326684e68SGreg Roach $ajax_url = $this->chartUrl($individual, [ 2049b5537c3SGreg Roach 'ajax' => true, 2052b29a33eSmakitso 'generations' => $generations, 20671378461SGreg Roach 'spouses' => $spouses, 20726684e68SGreg Roach ]); 20826684e68SGreg Roach 2099b5537c3SGreg Roach return $this->viewResponse('modules/hourglass-chart/page', [ 21026684e68SGreg Roach 'ajax_url' => $ajax_url, 21126684e68SGreg Roach 'generations' => $generations, 21226684e68SGreg Roach 'individual' => $individual, 213e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 21426684e68SGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 21571378461SGreg Roach 'module' => $this->name(), 21671378461SGreg Roach 'spouses' => $spouses, 21726684e68SGreg Roach 'title' => $this->chartTitle($individual), 218ef5d23f1SGreg Roach 'tree' => $tree, 21926684e68SGreg Roach ]); 22026684e68SGreg Roach } 22126684e68SGreg Roach 22226684e68SGreg Roach /** 223cb0eb4a9SGreg Roach * Generate an extension to the chart 224cb0eb4a9SGreg Roach * 225cb0eb4a9SGreg Roach * @param ServerRequestInterface $request 226cb0eb4a9SGreg Roach * 227cb0eb4a9SGreg Roach * @return ResponseInterface 228cb0eb4a9SGreg Roach */ 229cb0eb4a9SGreg Roach public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface 230cb0eb4a9SGreg Roach { 231f1d4b4a2SGreg Roach $tree = $request->getAttribute('tree'); 23275964c75SGreg Roach assert($tree instanceof Tree); 2335229eadeSGreg Roach 234cb0eb4a9SGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 235cb0eb4a9SGreg Roach 236cb0eb4a9SGreg Roach $family = Family::getInstance($xref, $tree); 237ddeb3354SGreg Roach $family = Auth::checkFamilyAccess($family); 238cb0eb4a9SGreg Roach 239cb0eb4a9SGreg Roach return response(view('modules/hourglass-chart/parents', [ 240cb0eb4a9SGreg Roach 'family' => $family, 241cb0eb4a9SGreg Roach 'generations' => 1, 24226684e68SGreg Roach ])); 24326684e68SGreg Roach } 24426684e68SGreg Roach 24526684e68SGreg Roach /** 246cb0eb4a9SGreg Roach * Generate an extension to the chart 247cb0eb4a9SGreg Roach * 2486ccdf4f0SGreg Roach * @param ServerRequestInterface $request 24926684e68SGreg Roach * 2506ccdf4f0SGreg Roach * @return ResponseInterface 25126684e68SGreg Roach */ 252cb0eb4a9SGreg Roach public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 25326684e68SGreg Roach { 254f1d4b4a2SGreg Roach $tree = $request->getAttribute('tree'); 25575964c75SGreg Roach assert($tree instanceof Tree); 2565229eadeSGreg Roach 257cb0eb4a9SGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 25826684e68SGreg Roach 25971378461SGreg Roach $spouses = (bool) ($request->getQueryParams()['spouses'] ?? false); 260cb0eb4a9SGreg Roach $individual = Individual::getInstance($xref, $tree); 2613c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 262a21e5374SGreg Roach 263a21e5374SGreg Roach $children = $individual->spouseFamilies()->map(static function (Family $family): Collection { 264a21e5374SGreg Roach return $family->children(); 265a21e5374SGreg Roach })->flatten(); 26626684e68SGreg Roach 267cb0eb4a9SGreg Roach return response(view('modules/hourglass-chart/children', [ 268cb0eb4a9SGreg Roach 'children' => $children, 269cb0eb4a9SGreg Roach 'generations' => 1, 27071378461SGreg Roach 'spouses' => $spouses, 271cb0eb4a9SGreg Roach ])); 272e6562982SGreg Roach } 273168ff6f3Sric2016} 274