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