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