xref: /webtrees/app/Module/CompactTreeChartModule.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
5a091ac74SGreg Roach * Copyright (C) 2020 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;
25*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
27168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
29d84d3988SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
304ea62551SGreg Roachuse Fisharebest\Webtrees\Tree;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3371378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3471378461SGreg Roach
359e18e23bSGreg Roachuse function app;
369e18e23bSGreg Roachuse function assert;
37ddeb3354SGreg Roachuse function is_string;
3871378461SGreg Roachuse function route;
39168ff6f3Sric2016
40168ff6f3Sric2016/**
41168ff6f3Sric2016 * Class CompactTreeChartModule
42168ff6f3Sric2016 */
4371378461SGreg Roachclass CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
44c1010edaSGreg Roach{
4549a243cbSGreg Roach    use ModuleChartTrait;
4649a243cbSGreg Roach
4772f04adfSGreg Roach    protected const ROUTE_URL  = '/tree/{tree}/compact/{xref}';
4871378461SGreg Roach
4957ab2231SGreg Roach    /** @var ChartService */
5057ab2231SGreg Roach    private $chart_service;
5157ab2231SGreg Roach
5257ab2231SGreg Roach    /**
5357ab2231SGreg Roach     * CompactTreeChartModule constructor.
5457ab2231SGreg Roach     *
5557ab2231SGreg Roach     * @param ChartService $chart_service
5657ab2231SGreg Roach     */
573976b470SGreg Roach    public function __construct(ChartService $chart_service)
583976b470SGreg Roach    {
5957ab2231SGreg Roach        $this->chart_service = $chart_service;
6057ab2231SGreg Roach    }
6157ab2231SGreg Roach
62168ff6f3Sric2016    /**
6371378461SGreg Roach     * Initialization.
6471378461SGreg Roach     *
659e18e23bSGreg Roach     * @return void
6671378461SGreg Roach     */
679e18e23bSGreg Roach    public function boot(): void
6871378461SGreg Roach    {
699e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
709e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
719e18e23bSGreg Roach
7271378461SGreg Roach        $router_container->getMap()
7372f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
7471378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
7571378461SGreg Roach    }
7671378461SGreg Roach
7771378461SGreg Roach    /**
780cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
79168ff6f3Sric2016     *
80168ff6f3Sric2016     * @return string
81168ff6f3Sric2016     */
8249a243cbSGreg Roach    public function title(): string
83c1010edaSGreg Roach    {
84bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
85bbb76c12SGreg Roach        return I18N::translate('Compact tree');
86168ff6f3Sric2016    }
87168ff6f3Sric2016
88168ff6f3Sric2016    /**
89168ff6f3Sric2016     * A sentence describing what this module does.
90168ff6f3Sric2016     *
91168ff6f3Sric2016     * @return string
92168ff6f3Sric2016     */
9349a243cbSGreg Roach    public function description(): string
94c1010edaSGreg Roach    {
95bbb76c12SGreg Roach        /* I18N: Description of the “CompactTreeChart” module */
96bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors, as a compact tree.');
97168ff6f3Sric2016    }
98168ff6f3Sric2016
99168ff6f3Sric2016    /**
100377a2979SGreg Roach     * CSS class for the URL.
101377a2979SGreg Roach     *
102377a2979SGreg Roach     * @return string
103377a2979SGreg Roach     */
104377a2979SGreg Roach    public function chartMenuClass(): string
105377a2979SGreg Roach    {
106377a2979SGreg Roach        return 'menu-chart-compact';
107377a2979SGreg Roach    }
108377a2979SGreg Roach
109377a2979SGreg Roach    /**
1104eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1114eb71cfaSGreg Roach     *
11260bc3e3fSGreg Roach     * @param Individual $individual
11360bc3e3fSGreg Roach     *
1144eb71cfaSGreg Roach     * @return Menu|null
1154eb71cfaSGreg Roach     */
116377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
117c1010edaSGreg Roach    {
118e6562982SGreg Roach        return $this->chartMenu($individual);
119e6562982SGreg Roach    }
120e6562982SGreg Roach
121e6562982SGreg Roach    /**
122e6562982SGreg Roach     * The title for a specific instance of this chart.
123e6562982SGreg Roach     *
124e6562982SGreg Roach     * @param Individual $individual
125e6562982SGreg Roach     *
126e6562982SGreg Roach     * @return string
127e6562982SGreg Roach     */
128e6562982SGreg Roach    public function chartTitle(Individual $individual): string
129e6562982SGreg Roach    {
130e6562982SGreg Roach        /* I18N: %s is an individual’s name */
13139ca88baSGreg Roach        return I18N::translate('Compact tree of %s', $individual->fullName());
132e6562982SGreg Roach    }
133d84d3988SGreg Roach
134d84d3988SGreg Roach    /**
13571378461SGreg Roach     * The URL for a page showing chart options.
136d84d3988SGreg Roach     *
13771378461SGreg Roach     * @param Individual $individual
13859597b37SGreg Roach     * @param mixed[]    $parameters
13971378461SGreg Roach     *
14071378461SGreg Roach     * @return string
14171378461SGreg Roach     */
14271378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
14371378461SGreg Roach    {
14472f04adfSGreg Roach        return route(static::class, [
14571378461SGreg Roach                'xref' => $individual->xref(),
14671378461SGreg Roach                'tree' => $individual->tree()->name(),
14771378461SGreg Roach            ] + $parameters);
14871378461SGreg Roach    }
14971378461SGreg Roach
15071378461SGreg Roach    /**
1516ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
152d84d3988SGreg Roach     *
1536ccdf4f0SGreg Roach     * @return ResponseInterface
154d84d3988SGreg Roach     */
15571378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
156d84d3988SGreg Roach    {
15757ab2231SGreg Roach        $tree = $request->getAttribute('tree');
1584ea62551SGreg Roach        assert($tree instanceof Tree);
1594ea62551SGreg Roach
16071378461SGreg Roach        $xref = $request->getAttribute('xref');
161ddeb3354SGreg Roach        assert(is_string($xref));
162ddeb3354SGreg Roach
163*6b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
1643c3fd0a5SGreg Roach        $individual = Auth::checkIndividualAccess($individual, false, true);
165ddeb3354SGreg Roach
166ddeb3354SGreg Roach        $user = $request->getAttribute('user');
167ddeb3354SGreg Roach        $ajax = $request->getQueryParams()['ajax'] ?? '';
168d84d3988SGreg Roach
16971378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
17071378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
171b46c87bdSGreg Roach            $params = (array) $request->getParsedBody();
172b46c87bdSGreg Roach
17372f04adfSGreg Roach            return redirect(route(static::class, [
1744ea62551SGreg Roach                'tree' => $tree->name(),
175b46c87bdSGreg Roach                'xref' => $params['xref'],
17671378461SGreg Roach            ]));
17771378461SGreg Roach        }
17871378461SGreg Roach
179ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
180d84d3988SGreg Roach
1810b93976aSGreg Roach        if ($ajax === '1') {
18271378461SGreg Roach            $this->layout = 'layouts/ajax';
18371378461SGreg Roach
18471378461SGreg Roach            return $this->viewResponse('modules/compact-chart/chart', [
18571378461SGreg Roach                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
18671378461SGreg Roach                'module'    => $this,
18771378461SGreg Roach            ]);
188d84d3988SGreg Roach        }
189d84d3988SGreg Roach
190389266c0SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1919b5537c3SGreg Roach            'ajax' => true,
192389266c0SGreg Roach        ]);
193389266c0SGreg Roach
1949b5537c3SGreg Roach        return $this->viewResponse('modules/compact-chart/page', [
195389266c0SGreg Roach            'ajax_url'   => $ajax_url,
196d84d3988SGreg Roach            'individual' => $individual,
19771378461SGreg Roach            'module'     => $this->name(),
198389266c0SGreg Roach            'title'      => $this->chartTitle($individual),
199ef5d23f1SGreg Roach            'tree'       => $tree,
200d84d3988SGreg Roach        ]);
201d84d3988SGreg Roach    }
202168ff6f3Sric2016}
203