1168ff6f3Sric2016<?php 2168ff6f3Sric2016/** 3168ff6f3Sric2016 * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify 6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by 7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or 8168ff6f3Sric2016 * (at your option) any later version. 9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful, 10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12168ff6f3Sric2016 * GNU General Public License for more details. 13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License 14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15168ff6f3Sric2016 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 19168ff6f3Sric2016 20d84d3988SGreg Roachuse Fisharebest\Webtrees\Auth; 21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 22168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 23168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 24e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 25d84d3988SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 26d84d3988SGreg Roachuse Fisharebest\Webtrees\Tree; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29168ff6f3Sric2016 30168ff6f3Sric2016/** 31168ff6f3Sric2016 * Class CompactTreeChartModule 32168ff6f3Sric2016 */ 3337eb8894SGreg Roachclass CompactTreeChartModule extends AbstractModule implements ModuleChartInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleChartTrait; 3649a243cbSGreg Roach 37168ff6f3Sric2016 /** 380cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 39168ff6f3Sric2016 * 40168ff6f3Sric2016 * @return string 41168ff6f3Sric2016 */ 4249a243cbSGreg Roach public function title(): string 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 45bbb76c12SGreg Roach return I18N::translate('Compact tree'); 46168ff6f3Sric2016 } 47168ff6f3Sric2016 48168ff6f3Sric2016 /** 49168ff6f3Sric2016 * A sentence describing what this module does. 50168ff6f3Sric2016 * 51168ff6f3Sric2016 * @return string 52168ff6f3Sric2016 */ 5349a243cbSGreg Roach public function description(): string 54c1010edaSGreg Roach { 55bbb76c12SGreg Roach /* I18N: Description of the “CompactTreeChart” module */ 56bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors, as a compact tree.'); 57168ff6f3Sric2016 } 58168ff6f3Sric2016 59168ff6f3Sric2016 /** 60377a2979SGreg Roach * CSS class for the URL. 61377a2979SGreg Roach * 62377a2979SGreg Roach * @return string 63377a2979SGreg Roach */ 64377a2979SGreg Roach public function chartMenuClass(): string 65377a2979SGreg Roach { 66377a2979SGreg Roach return 'menu-chart-compact'; 67377a2979SGreg Roach } 68377a2979SGreg Roach 69377a2979SGreg Roach /** 704eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 714eb71cfaSGreg Roach * 7260bc3e3fSGreg Roach * @param Individual $individual 7360bc3e3fSGreg Roach * 744eb71cfaSGreg Roach * @return Menu|null 754eb71cfaSGreg Roach */ 76377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 77c1010edaSGreg Roach { 78e6562982SGreg Roach return $this->chartMenu($individual); 79e6562982SGreg Roach } 80e6562982SGreg Roach 81e6562982SGreg Roach /** 82e6562982SGreg Roach * The title for a specific instance of this chart. 83e6562982SGreg Roach * 84e6562982SGreg Roach * @param Individual $individual 85e6562982SGreg Roach * 86e6562982SGreg Roach * @return string 87e6562982SGreg Roach */ 88e6562982SGreg Roach public function chartTitle(Individual $individual): string 89e6562982SGreg Roach { 90e6562982SGreg Roach /* I18N: %s is an individual’s name */ 9139ca88baSGreg Roach return I18N::translate('Compact tree of %s', $individual->fullName()); 92e6562982SGreg Roach } 93d84d3988SGreg Roach 94d84d3988SGreg Roach /** 95d84d3988SGreg Roach * A form to request the chart parameters. 96d84d3988SGreg Roach * 976ccdf4f0SGreg Roach * @param ServerRequestInterface $request 98d84d3988SGreg Roach * @param Tree $tree 99e5a6b4d4SGreg Roach * @param UserInterface $user 100d84d3988SGreg Roach * @param ChartService $chart_service 101d84d3988SGreg Roach * 1026ccdf4f0SGreg Roach * @return ResponseInterface 103d84d3988SGreg Roach */ 1046ccdf4f0SGreg Roach public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user, ChartService $chart_service): ResponseInterface 105d84d3988SGreg Roach { 106*0b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 107*0b93976aSGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 108d84d3988SGreg Roach $individual = Individual::getInstance($xref, $tree); 109d84d3988SGreg Roach 110d84d3988SGreg Roach Auth::checkIndividualAccess($individual); 1119867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 112d84d3988SGreg Roach 113*0b93976aSGreg Roach if ($ajax === '1') { 114d84d3988SGreg Roach return $this->chartCompact($individual, $chart_service); 115d84d3988SGreg Roach } 116d84d3988SGreg Roach 117389266c0SGreg Roach $ajax_url = $this->chartUrl($individual, [ 1189b5537c3SGreg Roach 'ajax' => true, 119389266c0SGreg Roach ]); 120389266c0SGreg Roach 1219b5537c3SGreg Roach return $this->viewResponse('modules/compact-chart/page', [ 122389266c0SGreg Roach 'ajax_url' => $ajax_url, 123d84d3988SGreg Roach 'individual' => $individual, 12426684e68SGreg Roach 'module_name' => $this->name(), 125389266c0SGreg Roach 'title' => $this->chartTitle($individual), 126d84d3988SGreg Roach ]); 127d84d3988SGreg Roach } 128d84d3988SGreg Roach 129d84d3988SGreg Roach /** 130d84d3988SGreg Roach * @param Individual $individual 131d84d3988SGreg Roach * @param ChartService $chart_service 132d84d3988SGreg Roach * 1336ccdf4f0SGreg Roach * @return ResponseInterface 134d84d3988SGreg Roach */ 1356ccdf4f0SGreg Roach protected function chartCompact(Individual $individual, ChartService $chart_service): ResponseInterface 136d84d3988SGreg Roach { 137d84d3988SGreg Roach $ancestors = $chart_service->sosaStradonitzAncestors($individual, 5); 138d84d3988SGreg Roach 139d84d3988SGreg Roach $html = view('modules/compact-chart/chart', [ 140d84d3988SGreg Roach 'ancestors' => $ancestors, 141242a7862SGreg Roach 'module' => $this, 142d84d3988SGreg Roach ]); 143d84d3988SGreg Roach 1446ccdf4f0SGreg Roach return response($html); 145d84d3988SGreg Roach } 146168ff6f3Sric2016} 147