xref: /webtrees/app/Module/CompactTreeChartModule.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
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    private const ROUTE_NAME = 'compact-chart';
47    private 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(self::ROUTE_NAME, self::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(self::ROUTE_NAME, [
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 = Individual::getInstance($xref, $tree);
164        $individual = Auth::checkIndividualAccess($individual);
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            return redirect(route(self::ROUTE_NAME, [
172                'tree' => $tree->name(),
173                'xref' => $request->getParsedBody()['xref'],
174            ]));
175        }
176
177        Auth::checkComponentAccess($this, 'chart', $tree, $user);
178
179        if ($ajax === '1') {
180            $this->layout = 'layouts/ajax';
181
182            return $this->viewResponse('modules/compact-chart/chart', [
183                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
184                'module'    => $this,
185            ]);
186        }
187
188        $ajax_url = $this->chartUrl($individual, [
189            'ajax' => true,
190        ]);
191
192        return $this->viewResponse('modules/compact-chart/page', [
193            'ajax_url'   => $ajax_url,
194            'individual' => $individual,
195            'module'     => $this->name(),
196            'title'      => $this->chartTitle($individual),
197            'tree'       => $tree,
198        ]);
199    }
200}
201