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