1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Family; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Individual; 24use Fisharebest\Webtrees\Menu; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Support\Collection; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29use function app; 30use function response; 31use function view; 32 33/** 34 * Class HourglassChartModule 35 */ 36class HourglassChartModule extends AbstractModule implements ModuleChartInterface 37{ 38 use ModuleChartTrait; 39 40 // Defaults 41 private const DEFAULT_GENERATIONS = '3'; 42 private const DEFAULT_MAXIMUM_GENERATIONS = '9'; 43 44 // Limits 45 private const MAXIMUM_GENERATIONS = 10; 46 private const MINIMUM_GENERATIONS = 2; 47 48 /** 49 * How should this module be identified in the control panel, etc.? 50 * 51 * @return string 52 */ 53 public function title(): string 54 { 55 /* I18N: Name of a module/chart */ 56 return I18N::translate('Hourglass chart'); 57 } 58 59 /** 60 * A sentence describing what this module does. 61 * 62 * @return string 63 */ 64 public function description(): string 65 { 66 /* I18N: Description of the “HourglassChart” module */ 67 return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.'); 68 } 69 70 /** 71 * CSS class for the URL. 72 * 73 * @return string 74 */ 75 public function chartMenuClass(): string 76 { 77 return 'menu-chart-hourglass'; 78 } 79 80 /** 81 * Return a menu item for this chart - for use in individual boxes. 82 * 83 * @param Individual $individual 84 * 85 * @return Menu|null 86 */ 87 public function chartBoxMenu(Individual $individual): ?Menu 88 { 89 return $this->chartMenu($individual); 90 } 91 92 /** 93 * A form to request the chart parameters. 94 * 95 * @param ServerRequestInterface $request 96 * 97 * @return ResponseInterface 98 */ 99 public function getChartAction(ServerRequestInterface $request): ResponseInterface 100 { 101 $tree = $request->getAttribute('tree'); 102 $user = $request->getAttribute('user'); 103 $ajax = $request->getQueryParams()['ajax'] ?? ''; 104 $xref = $request->getQueryParams()['xref'] ?? ''; 105 $individual = Individual::getInstance($xref, $tree); 106 107 Auth::checkIndividualAccess($individual); 108 Auth::checkComponentAccess($this, 'chart', $tree, $user); 109 110 $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS); 111 112 $generations = min($generations, self::MAXIMUM_GENERATIONS); 113 $generations = max($generations, self::MINIMUM_GENERATIONS); 114 115 $show_spouse = (bool) ($request->getQueryParams()['show_spouse'] ?? false); 116 117 if ($ajax === '1') { 118 return $this->chart($individual, $generations, $show_spouse); 119 } 120 121 $ajax_url = $this->chartUrl($individual, [ 122 'ajax' => true, 123 'generations' => $generations, 124 'show_spouse' => $show_spouse, 125 ]); 126 127 return $this->viewResponse('modules/hourglass-chart/page', [ 128 'ajax_url' => $ajax_url, 129 'generations' => $generations, 130 'individual' => $individual, 131 'maximum_generations' => self::MAXIMUM_GENERATIONS, 132 'minimum_generations' => self::MINIMUM_GENERATIONS, 133 'module_name' => $this->name(), 134 'show_spouse' => $show_spouse, 135 'title' => $this->chartTitle($individual), 136 ]); 137 } 138 139 /** 140 * Generate the initial generations of the chart 141 * 142 * @param Individual $individual 143 * @param int $generations 144 * @param bool $show_spouse 145 * 146 * @return ResponseInterface 147 */ 148 protected function chart(Individual $individual, int $generations, bool $show_spouse): ResponseInterface 149 { 150 $this->layout = 'layouts/ajax'; 151 152 return $this->viewResponse('modules/hourglass-chart/chart', [ 153 'generations' => $generations, 154 'individual' => $individual, 155 'show_spouse' => $show_spouse, 156 ]); 157 } 158 159 /** 160 * Generate an extension to the chart 161 * 162 * @param ServerRequestInterface $request 163 * 164 * @return ResponseInterface 165 */ 166 public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface 167 { 168 $tree = app(Tree::class); 169 $xref = $request->getQueryParams()['xref'] ?? ''; 170 171 $family = Family::getInstance($xref, $tree); 172 Auth::checkFamilyAccess($family); 173 174 return response(view('modules/hourglass-chart/parents', [ 175 'family' => $family, 176 'generations' => 1, 177 ])); 178 } 179 180 /** 181 * Generate an extension to the chart 182 * 183 * @param ServerRequestInterface $request 184 * 185 * @return ResponseInterface 186 */ 187 public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface 188 { 189 $tree = app(Tree::class); 190 $xref = $request->getQueryParams()['xref'] ?? ''; 191 192 $show_spouse = (bool) ($request->getQueryParams()['show_spouse'] ?? false); 193 $individual = Individual::getInstance($xref, $tree); 194 195 Auth::checkIndividualAccess($individual); 196 197 $children = $individual->spouseFamilies()->map(static function (Family $family): Collection { 198 return $family->children(); 199 })->flatten(); 200 201 return response(view('modules/hourglass-chart/children', [ 202 'children' => $children, 203 'generations' => 1, 204 'show_spouse' => $show_spouse, 205 ])); 206 } 207} 208