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