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 DescendancyChartModule 41 */ 42class DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 43{ 44 use ModuleChartTrait; 45 46 private const ROUTE_NAME = 'descendancy-chart'; 47 private const ROUTE_URL = '/tree/{tree}/descendants-{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 public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 56 public const DEFAULT_GENERATIONS = '3'; 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 * DescendancyChartModule 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, self::class) 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('Descendants'); 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 “DescendancyChart” module */ 117 return I18N::translate('A chart of an individual’s descendants.'); 118 } 119 120 /** 121 * CSS class for the URL. 122 * 123 * @return string 124 */ 125 public function chartMenuClass(): string 126 { 127 return 'menu-chart-descendants'; 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('Descendants 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 'tree' => $individual->tree()->name(), 167 'xref' => $individual->xref(), 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 $tree = $request->getAttribute('tree'); 179 $user = $request->getAttribute('user'); 180 $xref = $request->getAttribute('xref'); 181 $style = $request->getAttribute('style'); 182 $generations = (int) $request->getAttribute('generations'); 183 $ajax = $request->getQueryParams()['ajax'] ?? ''; 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 switch ($style) { 206 case self::CHART_STYLE_TREE: 207 return $this->viewResponse('modules/descendancy_chart/tree', [ 208 'individual' => $individual, 209 'generations' => $generations, 210 'daboville' => '1', 211 ]); 212 213 case self::CHART_STYLE_INDIVIDUALS: 214 return $this->viewResponse('lists/individuals-table', [ 215 'individuals' => $this->chart_service->descendants($individual, $generations - 1), 216 'sosa' => false, 217 'tree' => $tree, 218 ]); 219 220 case self::CHART_STYLE_FAMILIES: 221 $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 222 223 return $this->viewResponse('lists/families-table', [ 224 'families' => $families, 225 'tree' => $tree, 226 ]); 227 } 228 } 229 230 $ajax_url = $this->chartUrl($individual, [ 231 'generations' => $generations, 232 'style' => $style, 233 'ajax' => true, 234 ]); 235 236 return $this->viewResponse('modules/descendancy_chart/page', [ 237 'ajax_url' => $ajax_url, 238 'style' => $style, 239 'styles' => $this->styles(), 240 'default_generations' => self::DEFAULT_GENERATIONS, 241 'generations' => $generations, 242 'individual' => $individual, 243 'maximum_generations' => self::MAXIMUM_GENERATIONS, 244 'minimum_generations' => self::MINIMUM_GENERATIONS, 245 'module' => $this->name(), 246 'title' => $this->chartTitle($individual), 247 ]); 248 } 249 250 /** 251 * This chart can display its output in a number of styles 252 * 253 * @return string[] 254 */ 255 protected function styles(): array 256 { 257 return [ 258 self::CHART_STYLE_TREE => I18N::translate('Tree'), 259 self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 260 self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 261 ]; 262 } 263} 264