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