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\I18N; 22use Fisharebest\Webtrees\Individual; 23use Fisharebest\Webtrees\Menu; 24use Psr\Http\Message\ResponseInterface; 25use Psr\Http\Message\ServerRequestInterface; 26use function view; 27 28/** 29 * Class FamilyBookChartModule 30 */ 31class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface 32{ 33 use ModuleChartTrait; 34 35 // Defaults 36 private const DEFAULT_GENERATIONS = '2'; 37 private const DEFAULT_DESCENDANT_GENERATIONS = '5'; 38 private const DEFAULT_MAXIMUM_GENERATIONS = '9'; 39 40 // Limits 41 public const MINIMUM_GENERATIONS = 2; 42 public const MAXIMUM_GENERATIONS = 10; 43 44 /** 45 * How should this module be identified in the control panel, etc.? 46 * 47 * @return string 48 */ 49 public function title(): string 50 { 51 /* I18N: Name of a module/chart */ 52 return I18N::translate('Family book'); 53 } 54 55 /** 56 * A sentence describing what this module does. 57 * 58 * @return string 59 */ 60 public function description(): string 61 { 62 /* I18N: Description of the “FamilyBookChart” module */ 63 return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.'); 64 } 65 66 /** 67 * CSS class for the URL. 68 * 69 * @return string 70 */ 71 public function chartMenuClass(): string 72 { 73 return 'menu-chart-familybook'; 74 } 75 76 /** 77 * Return a menu item for this chart - for use in individual boxes. 78 * 79 * @param Individual $individual 80 * 81 * @return Menu|null 82 */ 83 public function chartBoxMenu(Individual $individual): ?Menu 84 { 85 return $this->chartMenu($individual); 86 } 87 88 /** 89 * The title for a specific instance of this chart. 90 * 91 * @param Individual $individual 92 * 93 * @return string 94 */ 95 public function chartTitle(Individual $individual): string 96 { 97 /* I18N: %s is an individual’s name */ 98 return I18N::translate('Family book of %s', $individual->fullName()); 99 } 100 101 /** 102 * A form to request the chart parameters. 103 * 104 * @param ServerRequestInterface $request 105 * 106 * @return ResponseInterface 107 */ 108 public function getChartAction(ServerRequestInterface $request): ResponseInterface 109 { 110 $tree = $request->getAttribute('tree'); 111 $user = $request->getAttribute('user'); 112 $ajax = $request->getQueryParams()['ajax'] ?? ''; 113 $xref = $request->getQueryParams()['xref'] ?? ''; 114 $individual = Individual::getInstance($xref, $tree); 115 116 Auth::checkIndividualAccess($individual); 117 Auth::checkComponentAccess($this, 'chart', $tree, $user); 118 119 $show_spouse = (bool) ($request->getQueryParams()['show_spouse'] ?? false); 120 $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS); 121 $generations = min($generations, self::MAXIMUM_GENERATIONS); 122 $generations = max($generations, self::MINIMUM_GENERATIONS); 123 124 // Generations of ancestors/descendants in each mini-tree. 125 $book_size = (int) ($request->getQueryParams()['book_size'] ?? 2); 126 $book_size = min($book_size, 5); 127 $book_size = max($book_size, 2); 128 129 if ($ajax === '1') { 130 return $this->chart($individual, $generations, $book_size, $show_spouse); 131 } 132 133 $ajax_url = $this->chartUrl($individual, [ 134 'ajax' => true, 135 'book_size' => $book_size, 136 'generations' => $generations, 137 'show_spouse' => $show_spouse, 138 ]); 139 140 return $this->viewResponse('modules/family-book-chart/page', [ 141 'ajax_url' => $ajax_url, 142 'book_size' => $book_size, 143 'generations' => $generations, 144 'individual' => $individual, 145 'maximum_generations' => self::MAXIMUM_GENERATIONS, 146 'minimum_generations' => self::MINIMUM_GENERATIONS, 147 'module_name' => $this->name(), 148 'show_spouse' => $show_spouse, 149 'title' => $this->chartTitle($individual), 150 ]); 151 } 152 153 /** 154 * @param Individual $individual 155 * @param int $generations 156 * @param int $book_size 157 * @param bool $show_spouse 158 * 159 * @return ResponseInterface 160 */ 161 public function chart(Individual $individual, int $generations, int $book_size, bool $show_spouse): ResponseInterface 162 { 163 $html = view('modules/family-book-chart/chart', ['individual' => $individual, 'generations' => $generations, 'book_size' => $book_size, 'show_spouse' => $show_spouse]); 164 165 return response($html); 166 } 167} 168