xref: /webtrees/app/Module/CompactTreeChartModule.php (revision 7afd76dffa1031f0b39c55661deb3bb5722f932e)
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    /**
79     * A sentence describing what this module does.
80     *
81     * @return string
82     */
83    public function description(): string
84    {
85        /* I18N: Description of the “CompactTreeChart” module */
86        return I18N::translate('A chart of an individual’s ancestors, as a compact tree.');
87    }
88
89    /**
90     * CSS class for the URL.
91     *
92     * @return string
93     */
94    public function chartMenuClass(): string
95    {
96        return 'menu-chart-compact';
97    }
98
99    /**
100     * Return a menu item for this chart - for use in individual boxes.
101     *
102     * @param Individual $individual
103     *
104     * @return Menu|null
105     */
106    public function chartBoxMenu(Individual $individual): ?Menu
107    {
108        return $this->chartMenu($individual);
109    }
110
111    /**
112     * The title for a specific instance of this chart.
113     *
114     * @param Individual $individual
115     *
116     * @return string
117     */
118    public function chartTitle(Individual $individual): string
119    {
120        /* I18N: %s is an individual’s name */
121        return I18N::translate('Compact tree of %s', $individual->fullName());
122    }
123
124    /**
125     * The URL for a page showing chart options.
126     *
127     * @param Individual                                $individual
128     * @param array<bool|int|string|array<string>|null> $parameters
129     *
130     * @return string
131     */
132    public function chartUrl(Individual $individual, array $parameters = []): string
133    {
134        return route(static::class, [
135                'xref' => $individual->xref(),
136                'tree' => $individual->tree()->name(),
137            ] + $parameters);
138    }
139
140    /**
141     * @param ServerRequestInterface $request
142     *
143     * @return ResponseInterface
144     */
145    public function handle(ServerRequestInterface $request): ResponseInterface
146    {
147        $tree = Validator::attributes($request)->tree();
148        $user = Validator::attributes($request)->user();
149        $xref = Validator::attributes($request)->isXref()->string('xref');
150        $ajax = Validator::queryParams($request)->boolean('ajax', false);
151
152        // Convert POST requests into GET requests for pretty URLs.
153        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
154            return redirect(route(static::class, [
155                'tree' => $tree->name(),
156                'xref' => Validator::parsedBody($request)->string('xref', ''),
157            ]));
158        }
159
160        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
161
162        $individual = Registry::individualFactory()->make($xref, $tree);
163        $individual = Auth::checkIndividualAccess($individual, false, true);
164
165        if ($ajax) {
166            $this->layout = 'layouts/ajax';
167
168            return $this->viewResponse('modules/compact-chart/chart', [
169                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
170                'module'    => $this,
171            ]);
172        }
173
174        $ajax_url = $this->chartUrl($individual, [
175            'ajax' => true,
176        ]);
177
178        return $this->viewResponse('modules/compact-chart/page', [
179            'ajax_url'   => $ajax_url,
180            'individual' => $individual,
181            'module'     => $this->name(),
182            'title'      => $this->chartTitle($individual),
183            'tree'       => $tree,
184        ]);
185    }
186}
187