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\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Menu; 28use Fisharebest\Webtrees\Registry; 29use Fisharebest\Webtrees\Services\ChartService; 30use Fisharebest\Webtrees\Validator; 31use Psr\Http\Message\ResponseInterface; 32use Psr\Http\Message\ServerRequestInterface; 33use Psr\Http\Server\RequestHandlerInterface; 34 35use function app; 36use function assert; 37use function route; 38 39/** 40 * Class AncestorsChartModule 41 */ 42class AncestorsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 43{ 44 use ModuleChartTrait; 45 46 protected const ROUTE_URL = '/tree/{tree}/ancestors-{style}-{generations}/{xref}'; 47 48 // Chart styles 49 public const CHART_STYLE_TREE = 'tree'; 50 public const CHART_STYLE_INDIVIDUALS = 'individuals'; 51 public const CHART_STYLE_FAMILIES = 'families'; 52 53 // Defaults 54 protected const DEFAULT_GENERATIONS = '4'; 55 protected const DEFAULT_STYLE = self::CHART_STYLE_TREE; 56 protected const DEFAULT_PARAMETERS = [ 57 'generations' => self::DEFAULT_GENERATIONS, 58 'style' => self::DEFAULT_STYLE, 59 ]; 60 61 // Limits 62 protected const MINIMUM_GENERATIONS = 2; 63 protected const MAXIMUM_GENERATIONS = 10; 64 65 private ChartService $chart_service; 66 67 /** 68 * CompactTreeChartModule constructor. 69 * 70 * @param ChartService $chart_service 71 */ 72 public function __construct(ChartService $chart_service) 73 { 74 $this->chart_service = $chart_service; 75 } 76 77 /** 78 * Initialization. 79 * 80 * @return void 81 */ 82 public function boot(): void 83 { 84 Registry::routeFactory()->routeMap() 85 ->get(static::class, static::ROUTE_URL, $this) 86 ->allows(RequestMethodInterface::METHOD_POST) 87 ->tokens([ 88 'generations' => '\d+', 89 'style' => implode('|', array_keys($this->styles())), 90 ]); 91 } 92 93 /** 94 * How should this module be identified in the control panel, etc.? 95 * 96 * @return string 97 */ 98 public function title(): string 99 { 100 /* I18N: Name of a module/chart */ 101 return I18N::translate('Ancestors'); 102 } 103 104 /** 105 * A sentence describing what this module does. 106 * 107 * @return string 108 */ 109 public function description(): string 110 { 111 /* I18N: Description of the “AncestorsChart” module */ 112 return I18N::translate('A chart of an individual’s ancestors.'); 113 } 114 115 /** 116 * CSS class for the URL. 117 * 118 * @return string 119 */ 120 public function chartMenuClass(): string 121 { 122 return 'menu-chart-ancestry'; 123 } 124 125 /** 126 * Return a menu item for this chart - for use in individual boxes. 127 * 128 * @param Individual $individual 129 * 130 * @return Menu|null 131 */ 132 public function chartBoxMenu(Individual $individual): ?Menu 133 { 134 return $this->chartMenu($individual); 135 } 136 137 /** 138 * The title for a specific instance of this chart. 139 * 140 * @param Individual $individual 141 * 142 * @return string 143 */ 144 public function chartTitle(Individual $individual): string 145 { 146 /* I18N: %s is an individual’s name */ 147 return I18N::translate('Ancestors of %s', $individual->fullName()); 148 } 149 150 /** 151 * The URL for a page showing chart options. 152 * 153 * @param Individual $individual 154 * @param array<bool|int|string|array<string>|null> $parameters 155 * 156 * @return string 157 */ 158 public function chartUrl(Individual $individual, array $parameters = []): string 159 { 160 return route(static::class, [ 161 'xref' => $individual->xref(), 162 'tree' => $individual->tree()->name(), 163 ] + $parameters + self::DEFAULT_PARAMETERS); 164 } 165 166 /** 167 * @param ServerRequestInterface $request 168 * 169 * @return ResponseInterface 170 */ 171 public function handle(ServerRequestInterface $request): ResponseInterface 172 { 173 $tree = Validator::attributes($request)->tree(); 174 $user = Validator::attributes($request)->user(); 175 $style = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style'); 176 $xref = Validator::attributes($request)->isXref()->string('xref'); 177 $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 178 $ajax = Validator::queryParams($request)->boolean('ajax', false); 179 180 // Convert POST requests into GET requests for pretty URLs. 181 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 182 return redirect(route(static::class, [ 183 'tree' => $tree->name(), 184 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 185 'style' => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'), 186 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 187 ])); 188 } 189 190 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 191 192 $individual = Registry::individualFactory()->make($xref, $tree); 193 $individual = Auth::checkIndividualAccess($individual, false, true); 194 195 if ($ajax) { 196 $this->layout = 'layouts/ajax'; 197 198 $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations); 199 200 switch ($style) { 201 default: 202 case self::CHART_STYLE_TREE: 203 return $this->viewResponse('modules/ancestors-chart/tree', [ 204 'individual' => $individual, 205 'parents' => $individual->childFamilies()->first(), 206 'generations' => $generations, 207 'sosa' => 1, 208 ]); 209 210 case self::CHART_STYLE_INDIVIDUALS: 211 return $this->viewResponse('lists/individuals-table', [ 212 'individuals' => $ancestors, 213 'sosa' => true, 214 'tree' => $tree, 215 ]); 216 217 case self::CHART_STYLE_FAMILIES: 218 $families = []; 219 220 foreach ($ancestors as $individual) { 221 foreach ($individual->childFamilies() as $family) { 222 $families[$family->xref()] = $family; 223 } 224 } 225 226 return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]); 227 } 228 } 229 230 $ajax_url = $this->chartUrl($individual, [ 231 'ajax' => true, 232 'generations' => $generations, 233 'style' => $style, 234 'xref' => $xref, 235 ]); 236 237 return $this->viewResponse('modules/ancestors-chart/page', [ 238 'ajax_url' => $ajax_url, 239 'generations' => $generations, 240 'individual' => $individual, 241 'maximum_generations' => self::MAXIMUM_GENERATIONS, 242 'minimum_generations' => self::MINIMUM_GENERATIONS, 243 'module' => $this->name(), 244 'style' => $style, 245 'styles' => $this->styles(), 246 'title' => $this->chartTitle($individual), 247 'tree' => $tree, 248 ]); 249 } 250 251 /** 252 * This chart can display its output in a number of styles 253 * 254 * @return array<string> 255 */ 256 protected function styles(): array 257 { 258 return [ 259 self::CHART_STYLE_TREE => I18N::translate('Tree'), 260 self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 261 self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 262 ]; 263 } 264} 265