xref: /webtrees/app/Module/AncestorsChartModule.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;
24e539f5c6SGreg Roachuse Fisharebest\Webtrees\Auth;
25168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
26168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
28e539f5c6SGreg Roachuse Fisharebest\Webtrees\Services\ChartService;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3171378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
323976b470SGreg Roach
3371378461SGreg Roachuse function max;
3471378461SGreg Roachuse function min;
3571378461SGreg Roachuse function route;
36168ff6f3Sric2016
37168ff6f3Sric2016/**
38168ff6f3Sric2016 * Class AncestorsChartModule
39168ff6f3Sric2016 */
4071378461SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
41c1010edaSGreg Roach{
4249a243cbSGreg Roach    use ModuleChartTrait;
4349a243cbSGreg Roach
4471378461SGreg Roach    private const ROUTE_NAME = 'ancestors-chart';
4571378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/ancestors-{style}-{generations}/{xref}';
4671378461SGreg Roach
47e539f5c6SGreg Roach    // Chart styles
4871378461SGreg Roach    public const CHART_STYLE_TREE        = 'tree';
4971378461SGreg Roach    public const CHART_STYLE_INDIVIDUALS = 'individuals';
5071378461SGreg Roach    public const CHART_STYLE_FAMILIES    = 'families';
51e539f5c6SGreg Roach
52e539f5c6SGreg Roach    // Defaults
530cf94a7cSGreg Roach    protected const DEFAULT_GENERATIONS = '4';
5471378461SGreg Roach    protected const DEFAULT_STYLE       = self::CHART_STYLE_TREE;
5571378461SGreg Roach    protected const DEFAULT_PARAMETERS  = [
5671378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
5771378461SGreg Roach        'style'       => self::DEFAULT_STYLE,
5871378461SGreg Roach    ];
59e759aebbSGreg Roach
60e759aebbSGreg Roach    // Limits
61e759aebbSGreg Roach    protected const MINIMUM_GENERATIONS = 2;
62e759aebbSGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
63e539f5c6SGreg Roach
6457ab2231SGreg Roach    /** @var ChartService */
6557ab2231SGreg Roach    private $chart_service;
6657ab2231SGreg Roach
6757ab2231SGreg Roach    /**
6857ab2231SGreg Roach     * CompactTreeChartModule constructor.
6957ab2231SGreg Roach     *
7057ab2231SGreg Roach     * @param ChartService $chart_service
7157ab2231SGreg Roach     */
723976b470SGreg Roach    public function __construct(ChartService $chart_service)
733976b470SGreg Roach    {
7457ab2231SGreg Roach        $this->chart_service = $chart_service;
7557ab2231SGreg Roach    }
7657ab2231SGreg Roach
77168ff6f3Sric2016    /**
7871378461SGreg Roach     * Initialization.
7971378461SGreg Roach     *
8071378461SGreg Roach     * @param RouterContainer $router_container
8171378461SGreg Roach     */
8271378461SGreg Roach    public function boot(RouterContainer $router_container)
8371378461SGreg Roach    {
8471378461SGreg Roach        $router_container->getMap()
8571378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
8671378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
8771378461SGreg Roach            ->tokens([
8871378461SGreg Roach                'generations' => '\d+',
8971378461SGreg Roach                'style'       => implode('|', array_keys($this->styles())),
9071378461SGreg Roach            ]);
9171378461SGreg Roach    }
9271378461SGreg Roach
9371378461SGreg Roach    /**
940cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
95168ff6f3Sric2016     *
96168ff6f3Sric2016     * @return string
97168ff6f3Sric2016     */
9849a243cbSGreg Roach    public function title(): string
99c1010edaSGreg Roach    {
100bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
101bbb76c12SGreg Roach        return I18N::translate('Ancestors');
102168ff6f3Sric2016    }
103168ff6f3Sric2016
104168ff6f3Sric2016    /**
105168ff6f3Sric2016     * A sentence describing what this module does.
106168ff6f3Sric2016     *
107168ff6f3Sric2016     * @return string
108168ff6f3Sric2016     */
10949a243cbSGreg Roach    public function description(): string
110c1010edaSGreg Roach    {
111bbb76c12SGreg Roach        /* I18N: Description of the “AncestorsChart” module */
112bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors.');
113168ff6f3Sric2016    }
114168ff6f3Sric2016
115168ff6f3Sric2016    /**
116377a2979SGreg Roach     * CSS class for the URL.
117377a2979SGreg Roach     *
118377a2979SGreg Roach     * @return string
119377a2979SGreg Roach     */
120377a2979SGreg Roach    public function chartMenuClass(): string
121377a2979SGreg Roach    {
122377a2979SGreg Roach        return 'menu-chart-ancestry';
123377a2979SGreg Roach    }
124377a2979SGreg Roach
125377a2979SGreg Roach    /**
1264eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1274eb71cfaSGreg Roach     *
12860bc3e3fSGreg Roach     * @param Individual $individual
12960bc3e3fSGreg Roach     *
1304eb71cfaSGreg Roach     * @return Menu|null
1314eb71cfaSGreg Roach     */
132377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
133c1010edaSGreg Roach    {
134e6562982SGreg Roach        return $this->chartMenu($individual);
135e6562982SGreg Roach    }
136e6562982SGreg Roach
137e6562982SGreg Roach    /**
138e6562982SGreg Roach     * The title for a specific instance of this chart.
139e6562982SGreg Roach     *
140e6562982SGreg Roach     * @param Individual $individual
141e6562982SGreg Roach     *
142e6562982SGreg Roach     * @return string
143e6562982SGreg Roach     */
144e6562982SGreg Roach    public function chartTitle(Individual $individual): string
145e6562982SGreg Roach    {
146e6562982SGreg Roach        /* I18N: %s is an individual’s name */
14739ca88baSGreg Roach        return I18N::translate('Ancestors of %s', $individual->fullName());
148e6562982SGreg Roach    }
149e6562982SGreg Roach
150e6562982SGreg Roach    /**
15171378461SGreg Roach     * The URL for a page showing chart options.
152e539f5c6SGreg Roach     *
15371378461SGreg Roach     * @param Individual $individual
154*59597b37SGreg Roach     * @param mixed[]    $parameters
15571378461SGreg Roach     *
15671378461SGreg Roach     * @return string
15771378461SGreg Roach     */
15871378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
15971378461SGreg Roach    {
16071378461SGreg Roach        return route(self::ROUTE_NAME, [
16171378461SGreg Roach                'xref' => $individual->xref(),
16271378461SGreg Roach                'tree' => $individual->tree()->name(),
16371378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
16471378461SGreg Roach    }
16571378461SGreg Roach
16671378461SGreg Roach    /**
1676ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
168e539f5c6SGreg Roach     *
1696ccdf4f0SGreg Roach     * @return ResponseInterface
170e539f5c6SGreg Roach     */
17171378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
172e539f5c6SGreg Roach    {
17371378461SGreg Roach        $ajax        = $request->getQueryParams()['ajax'] ?? '';
17471378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
17571378461SGreg Roach        $style       = $request->getAttribute('style');
17657ab2231SGreg Roach        $tree        = $request->getAttribute('tree');
17757ab2231SGreg Roach        $user        = $request->getAttribute('user');
17871378461SGreg Roach        $xref        = $request->getAttribute('xref');
179e539f5c6SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
180e539f5c6SGreg Roach
18171378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
18271378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
18371378461SGreg Roach            return redirect(route(self::ROUTE_NAME, [
18471378461SGreg Roach                'tree'        => $request->getAttribute('tree')->name(),
18571378461SGreg Roach                'xref'        => $request->getParsedBody()['xref'],
18671378461SGreg Roach                'style'       => $request->getParsedBody()['style'],
18771378461SGreg Roach                'generations' => $request->getParsedBody()['generations'],
18871378461SGreg Roach            ]));
18971378461SGreg Roach        }
19071378461SGreg Roach
191e539f5c6SGreg Roach        Auth::checkIndividualAccess($individual);
1929867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
193e539f5c6SGreg Roach
1940cf94a7cSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
1950cf94a7cSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
196e539f5c6SGreg Roach
1970b93976aSGreg Roach        if ($ajax === '1') {
198e539f5c6SGreg Roach            $this->layout = 'layouts/ajax';
199e539f5c6SGreg Roach
20071378461SGreg Roach            $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations);
20171378461SGreg Roach
20271378461SGreg Roach            switch ($style) {
20371378461SGreg Roach                default:
20471378461SGreg Roach                case self::CHART_STYLE_TREE:
20571378461SGreg Roach                    return $this->viewResponse('modules/ancestors-chart/tree', [
20671378461SGreg Roach                        'individual'  => $individual,
20771378461SGreg Roach                        'parents'     => $individual->primaryChildFamily(),
20871378461SGreg Roach                        'generations' => $generations,
20971378461SGreg Roach                        'sosa'        => 1,
21071378461SGreg Roach                    ]);
21171378461SGreg Roach
21271378461SGreg Roach                case self::CHART_STYLE_INDIVIDUALS:
213e539f5c6SGreg Roach                    return $this->viewResponse('lists/individuals-table', [
214e539f5c6SGreg Roach                        'individuals' => $ancestors,
215e539f5c6SGreg Roach                        'sosa'        => true,
216e539f5c6SGreg Roach                        'tree'        => $tree,
217e539f5c6SGreg Roach                    ]);
218e539f5c6SGreg Roach
21971378461SGreg Roach                case self::CHART_STYLE_FAMILIES:
220e539f5c6SGreg Roach                    $families = [];
22171378461SGreg Roach
222e539f5c6SGreg Roach                    foreach ($ancestors as $individual) {
22339ca88baSGreg Roach                        foreach ($individual->childFamilies() as $family) {
224e539f5c6SGreg Roach                            $families[$family->xref()] = $family;
225e539f5c6SGreg Roach                        }
226e539f5c6SGreg Roach                    }
227e539f5c6SGreg Roach
228e539f5c6SGreg Roach                    return $this->viewResponse('lists/families-table', [
229e539f5c6SGreg Roach                        'families' => $families,
230e539f5c6SGreg Roach                        'tree'     => $tree,
231e539f5c6SGreg Roach                    ]);
232e539f5c6SGreg Roach            }
23371378461SGreg Roach        }
23471378461SGreg Roach
23571378461SGreg Roach        $ajax_url = $this->chartUrl($individual, [
23671378461SGreg Roach            'ajax'        => true,
23771378461SGreg Roach            'generations' => $generations,
23871378461SGreg Roach            'style'       => $style,
23971378461SGreg Roach            'xref'        => $xref,
24071378461SGreg Roach        ]);
24171378461SGreg Roach
24271378461SGreg Roach        return $this->viewResponse('modules/ancestors-chart/page', [
24371378461SGreg Roach            'ajax_url'            => $ajax_url,
24471378461SGreg Roach            'generations'         => $generations,
24571378461SGreg Roach            'individual'          => $individual,
24671378461SGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
24771378461SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
24871378461SGreg Roach            'module'              => $this->name(),
24971378461SGreg Roach            'style'               => $style,
25071378461SGreg Roach            'styles'              => $this->styles(),
25171378461SGreg Roach            'title'               => $this->chartTitle($individual),
25271378461SGreg Roach        ]);
25371378461SGreg Roach    }
254e539f5c6SGreg Roach
255e539f5c6SGreg Roach    /**
256e539f5c6SGreg Roach     * This chart can display its output in a number of styles
257e539f5c6SGreg Roach     *
25871378461SGreg Roach     * @return string[]
259e539f5c6SGreg Roach     */
26071378461SGreg Roach    protected function styles(): array
261e539f5c6SGreg Roach    {
262e539f5c6SGreg Roach        return [
2630f1e0f10SGreg Roach            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
264e539f5c6SGreg Roach            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
265e539f5c6SGreg Roach            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
266e539f5c6SGreg Roach        ];
267e539f5c6SGreg Roach    }
268168ff6f3Sric2016}
269