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 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\RouterContainer; 23use Fig\Http\Message\RequestMethodInterface; 24use Fisharebest\Webtrees\Auth; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Menu; 28use Fisharebest\Webtrees\Services\ChartService; 29use Fisharebest\Webtrees\Tree; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function app; 35use function assert; 36use function route; 37 38/** 39 * Class CompactTreeChartModule 40 */ 41class CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 42{ 43 use ModuleChartTrait; 44 45 private const ROUTE_NAME = 'compact-chart'; 46 private const ROUTE_URL = '/tree/{tree}/compact/{xref}'; 47 48 /** @var ChartService */ 49 private $chart_service; 50 51 /** 52 * CompactTreeChartModule constructor. 53 * 54 * @param ChartService $chart_service 55 */ 56 public function __construct(ChartService $chart_service) 57 { 58 $this->chart_service = $chart_service; 59 } 60 61 /** 62 * Initialization. 63 * 64 * @return void 65 */ 66 public function boot(): void 67 { 68 $router_container = app(RouterContainer::class); 69 assert($router_container instanceof RouterContainer); 70 71 $router_container->getMap() 72 ->get(self::ROUTE_NAME, self::ROUTE_URL, $this) 73 ->allows(RequestMethodInterface::METHOD_POST); 74 } 75 76 /** 77 * How should this module be identified in the control panel, etc.? 78 * 79 * @return string 80 */ 81 public function title(): string 82 { 83 /* I18N: Name of a module/chart */ 84 return I18N::translate('Compact tree'); 85 } 86 87 /** 88 * A sentence describing what this module does. 89 * 90 * @return string 91 */ 92 public function description(): string 93 { 94 /* I18N: Description of the “CompactTreeChart” module */ 95 return I18N::translate('A chart of an individual’s ancestors, as a compact tree.'); 96 } 97 98 /** 99 * CSS class for the URL. 100 * 101 * @return string 102 */ 103 public function chartMenuClass(): string 104 { 105 return 'menu-chart-compact'; 106 } 107 108 /** 109 * Return a menu item for this chart - for use in individual boxes. 110 * 111 * @param Individual $individual 112 * 113 * @return Menu|null 114 */ 115 public function chartBoxMenu(Individual $individual): ?Menu 116 { 117 return $this->chartMenu($individual); 118 } 119 120 /** 121 * The title for a specific instance of this chart. 122 * 123 * @param Individual $individual 124 * 125 * @return string 126 */ 127 public function chartTitle(Individual $individual): string 128 { 129 /* I18N: %s is an individual’s name */ 130 return I18N::translate('Compact tree of %s', $individual->fullName()); 131 } 132 133 /** 134 * The URL for a page showing chart options. 135 * 136 * @param Individual $individual 137 * @param mixed[] $parameters 138 * 139 * @return string 140 */ 141 public function chartUrl(Individual $individual, array $parameters = []): string 142 { 143 return route(self::ROUTE_NAME, [ 144 'xref' => $individual->xref(), 145 'tree' => $individual->tree()->name(), 146 ] + $parameters); 147 } 148 149 /** 150 * @param ServerRequestInterface $request 151 * 152 * @return ResponseInterface 153 */ 154 public function handle(ServerRequestInterface $request): ResponseInterface 155 { 156 $tree = $request->getAttribute('tree'); 157 assert($tree instanceof Tree); 158 159 $user = $request->getAttribute('user'); 160 $xref = $request->getAttribute('xref'); 161 $ajax = $request->getQueryParams()['ajax'] ?? ''; 162 $individual = Individual::getInstance($xref, $tree); 163 164 // Convert POST requests into GET requests for pretty URLs. 165 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 166 return redirect(route(self::ROUTE_NAME, [ 167 'tree' => $tree->name(), 168 'xref' => $request->getParsedBody()['xref'], 169 ])); 170 } 171 172 Auth::checkIndividualAccess($individual); 173 Auth::checkComponentAccess($this, 'chart', $tree, $user); 174 175 if ($ajax === '1') { 176 $this->layout = 'layouts/ajax'; 177 178 return $this->viewResponse('modules/compact-chart/chart', [ 179 'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5), 180 'module' => $this, 181 ]); 182 } 183 184 $ajax_url = $this->chartUrl($individual, [ 185 'ajax' => true, 186 ]); 187 188 return $this->viewResponse('modules/compact-chart/page', [ 189 'ajax_url' => $ajax_url, 190 'individual' => $individual, 191 'module' => $this->name(), 192 'title' => $this->chartTitle($individual), 193 'tree' => $tree, 194 ]); 195 } 196} 197