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