xref: /webtrees/app/Module/CompactTreeChartModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://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\Registry;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Menu;
29use Fisharebest\Webtrees\Services\ChartService;
30use Fisharebest\Webtrees\Tree;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function app;
36use function assert;
37use function is_string;
38use function route;
39
40/**
41 * Class CompactTreeChartModule
42 */
43class CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
44{
45    use ModuleChartTrait;
46
47    protected const ROUTE_URL = '/tree/{tree}/compact/{xref}';
48
49    private ChartService $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(static::class, static::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 array<bool|int|string|array<string>|null> $parameters
138     *
139     * @return string
140     */
141    public function chartUrl(Individual $individual, array $parameters = []): string
142    {
143        return route(static::class, [
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        $xref = $request->getAttribute('xref');
160        assert(is_string($xref));
161
162        $individual = Registry::individualFactory()->make($xref, $tree);
163        $individual = Auth::checkIndividualAccess($individual, false, true);
164
165        $user = $request->getAttribute('user');
166        $ajax = $request->getQueryParams()['ajax'] ?? '';
167
168        // Convert POST requests into GET requests for pretty URLs.
169        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
170            $params = (array) $request->getParsedBody();
171
172            return redirect(route(static::class, [
173                'tree' => $tree->name(),
174                'xref' => $params['xref'],
175            ]));
176        }
177
178        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
179
180        if ($ajax === '1') {
181            $this->layout = 'layouts/ajax';
182
183            return $this->viewResponse('modules/compact-chart/chart', [
184                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
185                'module'    => $this,
186            ]);
187        }
188
189        $ajax_url = $this->chartUrl($individual, [
190            'ajax' => true,
191        ]);
192
193        return $this->viewResponse('modules/compact-chart/page', [
194            'ajax_url'   => $ajax_url,
195            'individual' => $individual,
196            'module'     => $this->name(),
197            'title'      => $this->chartTitle($individual),
198            'tree'       => $tree,
199        ]);
200    }
201}
202