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