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