xref: /webtrees/app/Module/CompactTreeChartModule.php (revision 59597b37d69e8147c3f4a27643e9c8edaa2a0592)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify
7168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by
8168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or
9168ff6f3Sric2016 * (at your option) any later version.
10168ff6f3Sric2016 * This program is distributed in the hope that it will be useful,
11168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13168ff6f3Sric2016 * GNU General Public License for more details.
14168ff6f3Sric2016 * You should have received a copy of the GNU General Public License
15168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16168ff6f3Sric2016 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
21168ff6f3Sric2016
2271378461SGreg Roachuse Aura\Router\RouterContainer;
2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
24d84d3988SGreg Roachuse Fisharebest\Webtrees\Auth;
25168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
26168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
28d84d3988SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3171378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3271378461SGreg Roach
3371378461SGreg Roachuse function route;
34168ff6f3Sric2016
35168ff6f3Sric2016/**
36168ff6f3Sric2016 * Class CompactTreeChartModule
37168ff6f3Sric2016 */
3871378461SGreg Roachclass CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
39c1010edaSGreg Roach{
4049a243cbSGreg Roach    use ModuleChartTrait;
4149a243cbSGreg Roach
4271378461SGreg Roach    private const ROUTE_NAME = 'compact-chart';
4371378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/compact/{xref}';
4471378461SGreg Roach
4557ab2231SGreg Roach    /** @var ChartService */
4657ab2231SGreg Roach    private $chart_service;
4757ab2231SGreg Roach
4857ab2231SGreg Roach    /**
4957ab2231SGreg Roach     * CompactTreeChartModule constructor.
5057ab2231SGreg Roach     *
5157ab2231SGreg Roach     * @param ChartService $chart_service
5257ab2231SGreg Roach     */
533976b470SGreg Roach    public function __construct(ChartService $chart_service)
543976b470SGreg Roach    {
5557ab2231SGreg Roach        $this->chart_service = $chart_service;
5657ab2231SGreg Roach    }
5757ab2231SGreg Roach
58168ff6f3Sric2016    /**
5971378461SGreg Roach     * Initialization.
6071378461SGreg Roach     *
6171378461SGreg Roach     * @param RouterContainer $router_container
6271378461SGreg Roach     */
6371378461SGreg Roach    public function boot(RouterContainer $router_container)
6471378461SGreg Roach    {
6571378461SGreg Roach        $router_container->getMap()
6671378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
6771378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
6871378461SGreg Roach    }
6971378461SGreg Roach
7071378461SGreg Roach    /**
710cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
72168ff6f3Sric2016     *
73168ff6f3Sric2016     * @return string
74168ff6f3Sric2016     */
7549a243cbSGreg Roach    public function title(): string
76c1010edaSGreg Roach    {
77bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
78bbb76c12SGreg Roach        return I18N::translate('Compact tree');
79168ff6f3Sric2016    }
80168ff6f3Sric2016
81168ff6f3Sric2016    /**
82168ff6f3Sric2016     * A sentence describing what this module does.
83168ff6f3Sric2016     *
84168ff6f3Sric2016     * @return string
85168ff6f3Sric2016     */
8649a243cbSGreg Roach    public function description(): string
87c1010edaSGreg Roach    {
88bbb76c12SGreg Roach        /* I18N: Description of the “CompactTreeChart” module */
89bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors, as a compact tree.');
90168ff6f3Sric2016    }
91168ff6f3Sric2016
92168ff6f3Sric2016    /**
93377a2979SGreg Roach     * CSS class for the URL.
94377a2979SGreg Roach     *
95377a2979SGreg Roach     * @return string
96377a2979SGreg Roach     */
97377a2979SGreg Roach    public function chartMenuClass(): string
98377a2979SGreg Roach    {
99377a2979SGreg Roach        return 'menu-chart-compact';
100377a2979SGreg Roach    }
101377a2979SGreg Roach
102377a2979SGreg Roach    /**
1034eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1044eb71cfaSGreg Roach     *
10560bc3e3fSGreg Roach     * @param Individual $individual
10660bc3e3fSGreg Roach     *
1074eb71cfaSGreg Roach     * @return Menu|null
1084eb71cfaSGreg Roach     */
109377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
110c1010edaSGreg Roach    {
111e6562982SGreg Roach        return $this->chartMenu($individual);
112e6562982SGreg Roach    }
113e6562982SGreg Roach
114e6562982SGreg Roach    /**
115e6562982SGreg Roach     * The title for a specific instance of this chart.
116e6562982SGreg Roach     *
117e6562982SGreg Roach     * @param Individual $individual
118e6562982SGreg Roach     *
119e6562982SGreg Roach     * @return string
120e6562982SGreg Roach     */
121e6562982SGreg Roach    public function chartTitle(Individual $individual): string
122e6562982SGreg Roach    {
123e6562982SGreg Roach        /* I18N: %s is an individual’s name */
12439ca88baSGreg Roach        return I18N::translate('Compact tree of %s', $individual->fullName());
125e6562982SGreg Roach    }
126d84d3988SGreg Roach
127d84d3988SGreg Roach    /**
12871378461SGreg Roach     * The URL for a page showing chart options.
129d84d3988SGreg Roach     *
13071378461SGreg Roach     * @param Individual $individual
131*59597b37SGreg Roach     * @param mixed[]    $parameters
13271378461SGreg Roach     *
13371378461SGreg Roach     * @return string
13471378461SGreg Roach     */
13571378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
13671378461SGreg Roach    {
13771378461SGreg Roach        return route(self::ROUTE_NAME, [
13871378461SGreg Roach                'xref' => $individual->xref(),
13971378461SGreg Roach                'tree' => $individual->tree()->name(),
14071378461SGreg Roach            ] + $parameters);
14171378461SGreg Roach    }
14271378461SGreg Roach
14371378461SGreg Roach    /**
1446ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
145d84d3988SGreg Roach     *
1466ccdf4f0SGreg Roach     * @return ResponseInterface
147d84d3988SGreg Roach     */
14871378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
149d84d3988SGreg Roach    {
15057ab2231SGreg Roach        $tree       = $request->getAttribute('tree');
15157ab2231SGreg Roach        $user       = $request->getAttribute('user');
15271378461SGreg Roach        $xref       = $request->getAttribute('xref');
1530b93976aSGreg Roach        $ajax       = $request->getQueryParams()['ajax'] ?? '';
154d84d3988SGreg Roach        $individual = Individual::getInstance($xref, $tree);
155d84d3988SGreg Roach
15671378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
15771378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
15871378461SGreg Roach            return redirect(route(self::ROUTE_NAME, [
15971378461SGreg Roach                'tree' => $request->getAttribute('tree')->name(),
16071378461SGreg Roach                'xref' => $request->getParsedBody()['xref'],
16171378461SGreg Roach            ]));
16271378461SGreg Roach        }
16371378461SGreg Roach
164d84d3988SGreg Roach        Auth::checkIndividualAccess($individual);
1659867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
166d84d3988SGreg Roach
1670b93976aSGreg Roach        if ($ajax === '1') {
16871378461SGreg Roach            $this->layout = 'layouts/ajax';
16971378461SGreg Roach
17071378461SGreg Roach            return $this->viewResponse('modules/compact-chart/chart', [
17171378461SGreg Roach                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
17271378461SGreg Roach                'module'    => $this,
17371378461SGreg Roach            ]);
174d84d3988SGreg Roach        }
175d84d3988SGreg Roach
176389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1779b5537c3SGreg Roach            'ajax' => true,
178389266c0SGreg Roach        ]);
179389266c0SGreg Roach
1809b5537c3SGreg Roach        return $this->viewResponse('modules/compact-chart/page', [
181389266c0SGreg Roach            'ajax_url'   => $ajax_url,
182d84d3988SGreg Roach            'individual' => $individual,
18371378461SGreg Roach            'module'     => $this->name(),
184389266c0SGreg Roach            'title'      => $this->chartTitle($individual),
185d84d3988SGreg Roach        ]);
186d84d3988SGreg Roach    }
187168ff6f3Sric2016}
188