xref: /webtrees/app/Module/CompactTreeChartModule.php (revision f735852025b7cf25907aa75da63403e46a63b49b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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\Services\ChartService;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function app;
34use function assert;
35use function route;
36
37/**
38 * Class CompactTreeChartModule
39 */
40class CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
41{
42    use ModuleChartTrait;
43
44    private const ROUTE_NAME = 'compact-chart';
45    private const ROUTE_URL  = '/tree/{tree}/compact/{xref}';
46
47    /** @var ChartService */
48    private $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(self::ROUTE_NAME, self::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 mixed[]    $parameters
137     *
138     * @return string
139     */
140    public function chartUrl(Individual $individual, array $parameters = []): string
141    {
142        return route(self::ROUTE_NAME, [
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       = $request->getAttribute('tree');
156        $user       = $request->getAttribute('user');
157        $xref       = $request->getAttribute('xref');
158        $ajax       = $request->getQueryParams()['ajax'] ?? '';
159        $individual = Individual::getInstance($xref, $tree);
160
161        // Convert POST requests into GET requests for pretty URLs.
162        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
163            return redirect(route(self::ROUTE_NAME, [
164                'tree' => $request->getAttribute('tree')->name(),
165                'xref' => $request->getParsedBody()['xref'],
166            ]));
167        }
168
169        Auth::checkIndividualAccess($individual);
170        Auth::checkComponentAccess($this, 'chart', $tree, $user);
171
172        if ($ajax === '1') {
173            $this->layout = 'layouts/ajax';
174
175            return $this->viewResponse('modules/compact-chart/chart', [
176                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
177                'module'    => $this,
178            ]);
179        }
180
181        $ajax_url = $this->chartUrl($individual, [
182            'ajax' => true,
183        ]);
184
185        return $this->viewResponse('modules/compact-chart/page', [
186            'ajax_url'   => $ajax_url,
187            'individual' => $individual,
188            'module'     => $this->name(),
189            'title'      => $this->chartTitle($individual),
190            'tree'       => $tree,
191        ]);
192    }
193}
194