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