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 DescendancyChartModule 44 */ 45class DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 46{ 47 use ModuleChartTrait; 48 49 protected const ROUTE_URL = '/tree/{tree}/descendants-{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 public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 58 public const DEFAULT_GENERATIONS = '3'; 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 * DescendancyChartModule 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('Descendants'); 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 “DescendancyChart” module */ 119 return I18N::translate('A chart of an individual’s descendants.'); 120 } 121 122 /** 123 * CSS class for the URL. 124 * 125 * @return string 126 */ 127 public function chartMenuClass(): string 128 { 129 return 'menu-chart-descendants'; 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('Descendants 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 'tree' => $individual->tree()->name(), 169 'xref' => $individual->xref(), 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 $user = $request->getAttribute('user'); 190 $style = $request->getAttribute('style'); 191 $generations = (int) $request->getAttribute('generations'); 192 $ajax = $request->getQueryParams()['ajax'] ?? ''; 193 194 // Convert POST requests into GET requests for pretty URLs. 195 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 196 $params = (array) $request->getParsedBody(); 197 198 return redirect(route(static::class, [ 199 'tree' => $tree->name(), 200 'xref' => $params['xref'], 201 'style' => $params['style'], 202 'generations' => $params['generations'], 203 ])); 204 } 205 206 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 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 switch ($style) { 215 case self::CHART_STYLE_TREE: 216 return $this->viewResponse('modules/descendancy_chart/tree', [ 217 'individual' => $individual, 218 'generations' => $generations, 219 'daboville' => '1', 220 ]); 221 222 case self::CHART_STYLE_INDIVIDUALS: 223 return $this->viewResponse('lists/individuals-table', [ 224 'individuals' => $this->chart_service->descendants($individual, $generations - 1), 225 'sosa' => false, 226 'tree' => $tree, 227 ]); 228 229 case self::CHART_STYLE_FAMILIES: 230 $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 231 232 return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]); 233 } 234 } 235 236 $ajax_url = $this->chartUrl($individual, [ 237 'generations' => $generations, 238 'style' => $style, 239 'ajax' => true, 240 ]); 241 242 return $this->viewResponse('modules/descendancy_chart/page', [ 243 'ajax_url' => $ajax_url, 244 'style' => $style, 245 'styles' => $this->styles(), 246 'default_generations' => self::DEFAULT_GENERATIONS, 247 'generations' => $generations, 248 'individual' => $individual, 249 'maximum_generations' => self::MAXIMUM_GENERATIONS, 250 'minimum_generations' => self::MINIMUM_GENERATIONS, 251 'module' => $this->name(), 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