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