1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\RouterContainer; 23use Fig\Http\Message\RequestMethodInterface; 24use Fisharebest\Webtrees\Auth; 25use Fisharebest\Webtrees\Family; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Menu; 29use Fisharebest\Webtrees\Registry; 30use Fisharebest\Webtrees\Validator; 31use Illuminate\Support\Collection; 32use Psr\Http\Message\ResponseInterface; 33use Psr\Http\Message\ServerRequestInterface; 34use Psr\Http\Server\RequestHandlerInterface; 35 36use function app; 37use function assert; 38use function response; 39use function view; 40 41/** 42 * Class HourglassChartModule 43 */ 44class HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 45{ 46 use ModuleChartTrait; 47 48 protected const ROUTE_URL = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}'; 49 50 // Defaults 51 private const DEFAULT_GENERATIONS = '3'; 52 private const DEFAULT_SPOUSES = false; 53 protected const DEFAULT_PARAMETERS = [ 54 'generations' => self::DEFAULT_GENERATIONS, 55 'spouses' => self::DEFAULT_SPOUSES, 56 ]; 57 58 // Limits 59 protected const MINIMUM_GENERATIONS = 2; 60 protected const MAXIMUM_GENERATIONS = 10; 61 62 /** 63 * Initialization. 64 * 65 * @return void 66 */ 67 public function boot(): void 68 { 69 $router_container = app(RouterContainer::class); 70 assert($router_container instanceof RouterContainer); 71 72 $router_container->getMap() 73 ->get(static::class, static::ROUTE_URL, $this) 74 ->allows(RequestMethodInterface::METHOD_POST) 75 ->tokens([ 76 'generations' => '\d+', 77 'spouses' => '1?', 78 ]); 79 } 80 81 /** 82 * How should this module be identified in the control panel, etc.? 83 * 84 * @return string 85 */ 86 public function title(): string 87 { 88 /* I18N: Name of a module/chart */ 89 return I18N::translate('Hourglass chart'); 90 } 91 92 /** 93 * A sentence describing what this module does. 94 * 95 * @return string 96 */ 97 public function description(): string 98 { 99 /* I18N: Description of the “HourglassChart” module */ 100 return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.'); 101 } 102 103 /** 104 * CSS class for the URL. 105 * 106 * @return string 107 */ 108 public function chartMenuClass(): string 109 { 110 return 'menu-chart-hourglass'; 111 } 112 113 /** 114 * Return a menu item for this chart - for use in individual boxes. 115 * 116 * @param Individual $individual 117 * 118 * @return Menu|null 119 */ 120 public function chartBoxMenu(Individual $individual): ?Menu 121 { 122 return $this->chartMenu($individual); 123 } 124 125 /** 126 * The title for a specific instance of this chart. 127 * 128 * @param Individual $individual 129 * 130 * @return string 131 */ 132 public function chartTitle(Individual $individual): string 133 { 134 /* I18N: %s is an individual’s name */ 135 return I18N::translate('Hourglass chart of %s', $individual->fullName()); 136 } 137 138 /** 139 * The URL for a page showing chart options. 140 * 141 * @param Individual $individual 142 * @param array<bool|int|string|array<string>|null> $parameters 143 * 144 * @return string 145 */ 146 public function chartUrl(Individual $individual, array $parameters = []): string 147 { 148 return route(static::class, [ 149 'xref' => $individual->xref(), 150 'tree' => $individual->tree()->name(), 151 ] + $parameters + self::DEFAULT_PARAMETERS); 152 } 153 154 /** 155 * @param ServerRequestInterface $request 156 * 157 * @return ResponseInterface 158 */ 159 public function handle(ServerRequestInterface $request): ResponseInterface 160 { 161 $tree = Validator::attributes($request)->tree(); 162 $xref = Validator::attributes($request)->isXref()->string('xref'); 163 $user = Validator::attributes($request)->user(); 164 $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 165 $spouses = Validator::attributes($request)->boolean('spouses'); 166 $ajax = Validator::queryParams($request)->boolean('ajax', false); 167 168 // Convert POST requests into GET requests for pretty URLs. 169 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 170 return redirect(route(static::class, [ 171 'tree' => $tree->name(), 172 'xref' => Validator::parsedBody($request)->string('xref', ''), 173 'generations' => Validator::parsedBody($request)->string('generations', ''), 174 'spouses' => Validator::parsedBody($request)->string('spouses', ''), 175 ])); 176 } 177 178 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 179 180 $individual = Registry::individualFactory()->make($xref, $tree); 181 $individual = Auth::checkIndividualAccess($individual, false, true); 182 183 if ($ajax) { 184 $this->layout = 'layouts/ajax'; 185 186 return $this->viewResponse('modules/hourglass-chart/chart', [ 187 'generations' => $generations, 188 'individual' => $individual, 189 'spouses' => $spouses, 190 ]); 191 } 192 193 $ajax_url = $this->chartUrl($individual, [ 194 'ajax' => true, 195 'generations' => $generations, 196 'spouses' => $spouses, 197 ]); 198 199 return $this->viewResponse('modules/hourglass-chart/page', [ 200 'ajax_url' => $ajax_url, 201 'generations' => $generations, 202 'individual' => $individual, 203 'maximum_generations' => self::MAXIMUM_GENERATIONS, 204 'minimum_generations' => self::MINIMUM_GENERATIONS, 205 'module' => $this->name(), 206 'spouses' => $spouses, 207 'title' => $this->chartTitle($individual), 208 'tree' => $tree, 209 ]); 210 } 211 212 /** 213 * Generate an extension to the chart 214 * 215 * @param ServerRequestInterface $request 216 * 217 * @return ResponseInterface 218 */ 219 public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface 220 { 221 $tree = Validator::attributes($request)->tree(); 222 223 $xref = $request->getQueryParams()['xref'] ?? ''; 224 225 $family = Registry::familyFactory()->make($xref, $tree); 226 $family = Auth::checkFamilyAccess($family); 227 228 return response(view('modules/hourglass-chart/parents', [ 229 'family' => $family, 230 'generations' => 1, 231 ])); 232 } 233 234 /** 235 * Generate an extension to the chart 236 * 237 * @param ServerRequestInterface $request 238 * 239 * @return ResponseInterface 240 */ 241 public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 242 { 243 $tree = Validator::attributes($request)->tree(); 244 245 $xref = $request->getQueryParams()['xref'] ?? ''; 246 247 $spouses = (bool) ($request->getQueryParams()['spouses'] ?? false); 248 $individual = Registry::individualFactory()->make($xref, $tree); 249 $individual = Auth::checkIndividualAccess($individual, false, true); 250 251 $children = $individual->spouseFamilies()->map(static function (Family $family): Collection { 252 return $family->children(); 253 })->flatten(); 254 255 return response(view('modules/hourglass-chart/children', [ 256 'children' => $children, 257 'generations' => 1, 258 'spouses' => $spouses, 259 ])); 260 } 261} 262