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