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