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 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Module; 20 21use Fisharebest\Webtrees\Auth; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Individual; 24use Fisharebest\Webtrees\Menu; 25use Fisharebest\Webtrees\Services\ChartService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Support\Collection; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30 31/** 32 * Class DescendancyChartModule 33 */ 34class DescendancyChartModule extends AbstractModule implements ModuleChartInterface 35{ 36 use ModuleChartTrait; 37 38 // Chart styles 39 public const CHART_STYLE_TREE = 'tree'; 40 public const CHART_STYLE_INDIVIDUALS = 'individuals'; 41 public const CHART_STYLE_FAMILIES = 'families'; 42 43 // Defaults 44 public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 45 public const DEFAULT_GENERATIONS = '3'; 46 47 // Limits 48 public const MINIMUM_GENERATIONS = 2; 49 public const MAXIMUM_GENERATIONS = 10; 50 51 /** @var int[] */ 52 protected $dabo_num = []; 53 54 /** @var string[] */ 55 protected $dabo_sex = []; 56 57 /** @var ChartService */ 58 private $chart_service; 59 60 /** 61 * DescendancyChartModule constructor. 62 * 63 * @param ChartService $chart_service 64 */ 65 public function __construct(ChartService $chart_service) 66 { 67 $this->chart_service = $chart_service; 68 } 69 70 /** 71 * How should this module be identified in the control panel, etc.? 72 * 73 * @return string 74 */ 75 public function title(): string 76 { 77 /* I18N: Name of a module/chart */ 78 return I18N::translate('Descendants'); 79 } 80 81 /** 82 * A sentence describing what this module does. 83 * 84 * @return string 85 */ 86 public function description(): string 87 { 88 /* I18N: Description of the “DescendancyChart” module */ 89 return I18N::translate('A chart of an individual’s descendants.'); 90 } 91 92 /** 93 * CSS class for the URL. 94 * 95 * @return string 96 */ 97 public function chartMenuClass(): string 98 { 99 return 'menu-chart-descendants'; 100 } 101 102 /** 103 * Return a menu item for this chart - for use in individual boxes. 104 * 105 * @param Individual $individual 106 * 107 * @return Menu|null 108 */ 109 public function chartBoxMenu(Individual $individual): ?Menu 110 { 111 return $this->chartMenu($individual); 112 } 113 114 /** 115 * The title for a specific instance of this chart. 116 * 117 * @param Individual $individual 118 * 119 * @return string 120 */ 121 public function chartTitle(Individual $individual): string 122 { 123 /* I18N: %s is an individual’s name */ 124 return I18N::translate('Descendants of %s', $individual->fullName()); 125 } 126 127 /** 128 * A form to request the chart parameters. 129 * 130 * @param ServerRequestInterface $request 131 * 132 * @return ResponseInterface 133 */ 134 public function getChartAction(ServerRequestInterface $request): ResponseInterface 135 { 136 $tree = $request->getAttribute('tree'); 137 $user = $request->getAttribute('user'); 138 $ajax = $request->getQueryParams()['ajax'] ?? ''; 139 $xref = $request->getQueryParams()['xref'] ?? ''; 140 $individual = Individual::getInstance($xref, $tree); 141 142 Auth::checkIndividualAccess($individual); 143 Auth::checkComponentAccess($this, 'chart', $tree, $user); 144 145 $chart_style = $request->getQueryParams()['chart_style'] ?? self::DEFAULT_STYLE; 146 $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS); 147 148 $generations = min($generations, self::MAXIMUM_GENERATIONS); 149 $generations = max($generations, self::MINIMUM_GENERATIONS); 150 151 if ($ajax === '1') { 152 return $this->chart($request); 153 } 154 155 $ajax_url = $this->chartUrl($individual, [ 156 'chart_style' => $chart_style, 157 'generations' => $generations, 158 'ajax' => true, 159 ]); 160 161 return $this->viewResponse('modules/descendancy_chart/page', [ 162 'ajax_url' => $ajax_url, 163 'chart_style' => $chart_style, 164 'chart_styles' => $this->chartStyles(), 165 'default_generations' => self::DEFAULT_GENERATIONS, 166 'generations' => $generations, 167 'individual' => $individual, 168 'maximum_generations' => self::MAXIMUM_GENERATIONS, 169 'minimum_generations' => self::MINIMUM_GENERATIONS, 170 'module_name' => $this->name(), 171 'title' => $this->chartTitle($individual), 172 ]); 173 } 174 175 /** 176 * @param ServerRequestInterface $request 177 * 178 * @return ResponseInterface 179 */ 180 public function chart(ServerRequestInterface $request): ResponseInterface 181 { 182 $this->layout = 'layouts/ajax'; 183 184 $tree = $request->getAttribute('tree'); 185 $xref = $request->getQueryParams()['xref']; 186 $individual = Individual::getInstance($xref, $tree); 187 188 Auth::checkIndividualAccess($individual); 189 190 $chart_style = $request->getQueryParams()['chart_style']; 191 $generations = (int) $request->getQueryParams()['generations']; 192 193 $generations = min($generations, self::MAXIMUM_GENERATIONS); 194 $generations = max($generations, self::MINIMUM_GENERATIONS); 195 196 switch ($chart_style) { 197 case self::CHART_STYLE_TREE: 198 default: 199 return response(view('modules/descendancy_chart/tree', ['individual' => $individual, 'generations' => $generations, 'daboville' => '1'])); 200 201 case self::CHART_STYLE_INDIVIDUALS: 202 $individuals = $this->chart_service->descendants($individual, $generations - 1); 203 204 return $this->descendantsIndividuals($tree, $individuals); 205 206 case self::CHART_STYLE_FAMILIES: 207 $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 208 209 return $this->descendantsFamilies($tree, $families); 210 } 211 } 212 213 /** 214 * Show a tabular list of individual descendants. 215 * 216 * @param Tree $tree 217 * @param Collection $individuals 218 * 219 * @return ResponseInterface 220 */ 221 private function descendantsIndividuals(Tree $tree, Collection $individuals): ResponseInterface 222 { 223 $this->layout = 'layouts/ajax'; 224 225 return $this->viewResponse('lists/individuals-table', [ 226 'individuals' => $individuals, 227 'sosa' => false, 228 'tree' => $tree, 229 ]); 230 } 231 232 /** 233 * Show a tabular list of individual descendants. 234 * 235 * @param Tree $tree 236 * @param Collection $families 237 * 238 * @return ResponseInterface 239 */ 240 private function descendantsFamilies(Tree $tree, Collection $families): ResponseInterface 241 { 242 $this->layout = 'layouts/ajax'; 243 244 return $this->viewResponse('lists/families-table', [ 245 'families' => $families, 246 'tree' => $tree, 247 ]); 248 } 249 250 /** 251 * This chart can display its output in a number of styles 252 * 253 * @return string[] 254 */ 255 private function chartStyles(): 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