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