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