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