xref: /webtrees/app/Module/AncestorsChartModule.php (revision ef483801e2cad5494dfc3dbf1a76bb1354331524)
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;
24e539f5c6SGreg Roachuse Fisharebest\Webtrees\Auth;
25a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
26168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
27168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
29e539f5c6SGreg 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;
3412662bfbSGreg Roach
359e18e23bSGreg Roachuse function app;
369e18e23bSGreg Roachuse function assert;
37ddeb3354SGreg Roachuse function is_string;
3871378461SGreg Roachuse function max;
3971378461SGreg Roachuse function min;
4071378461SGreg Roachuse function route;
41168ff6f3Sric2016
42168ff6f3Sric2016/**
43168ff6f3Sric2016 * Class AncestorsChartModule
44168ff6f3Sric2016 */
4571378461SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
46c1010edaSGreg Roach{
4749a243cbSGreg Roach    use ModuleChartTrait;
4849a243cbSGreg Roach
4972f04adfSGreg Roach    protected const ROUTE_URL  = '/tree/{tree}/ancestors-{style}-{generations}/{xref}';
5071378461SGreg Roach
51e539f5c6SGreg Roach    // Chart styles
5271378461SGreg Roach    public const CHART_STYLE_TREE        = 'tree';
5371378461SGreg Roach    public const CHART_STYLE_INDIVIDUALS = 'individuals';
5471378461SGreg Roach    public const CHART_STYLE_FAMILIES    = 'families';
55e539f5c6SGreg Roach
56e539f5c6SGreg Roach    // Defaults
570cf94a7cSGreg Roach    protected const DEFAULT_GENERATIONS = '4';
5871378461SGreg Roach    protected const DEFAULT_STYLE       = self::CHART_STYLE_TREE;
5971378461SGreg Roach    protected const DEFAULT_PARAMETERS  = [
6071378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
6171378461SGreg Roach        'style'       => self::DEFAULT_STYLE,
6271378461SGreg Roach    ];
63e759aebbSGreg Roach
64e759aebbSGreg Roach    // Limits
65e759aebbSGreg Roach    protected const MINIMUM_GENERATIONS = 2;
66e759aebbSGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
67e539f5c6SGreg Roach
6857ab2231SGreg Roach    /** @var ChartService */
6957ab2231SGreg Roach    private $chart_service;
7057ab2231SGreg Roach
7157ab2231SGreg Roach    /**
7257ab2231SGreg Roach     * CompactTreeChartModule constructor.
7357ab2231SGreg Roach     *
7457ab2231SGreg Roach     * @param ChartService $chart_service
7557ab2231SGreg Roach     */
763976b470SGreg Roach    public function __construct(ChartService $chart_service)
773976b470SGreg Roach    {
7857ab2231SGreg Roach        $this->chart_service = $chart_service;
7957ab2231SGreg Roach    }
8057ab2231SGreg Roach
81168ff6f3Sric2016    /**
8271378461SGreg Roach     * Initialization.
8371378461SGreg Roach     *
849e18e23bSGreg Roach     * @return void
8571378461SGreg Roach     */
869e18e23bSGreg Roach    public function boot(): void
8771378461SGreg Roach    {
889e18e23bSGreg Roach        $router_container = app(RouterContainer::class);
899e18e23bSGreg Roach        assert($router_container instanceof RouterContainer);
909e18e23bSGreg Roach
9171378461SGreg Roach        $router_container->getMap()
9272f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
9371378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
9471378461SGreg Roach            ->tokens([
9571378461SGreg Roach                'generations' => '\d+',
9671378461SGreg Roach                'style'       => implode('|', array_keys($this->styles())),
9771378461SGreg Roach            ]);
9871378461SGreg Roach    }
9971378461SGreg Roach
10071378461SGreg Roach    /**
1010cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
102168ff6f3Sric2016     *
103168ff6f3Sric2016     * @return string
104168ff6f3Sric2016     */
10549a243cbSGreg Roach    public function title(): string
106c1010edaSGreg Roach    {
107bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
108bbb76c12SGreg Roach        return I18N::translate('Ancestors');
109168ff6f3Sric2016    }
110168ff6f3Sric2016
111168ff6f3Sric2016    /**
112168ff6f3Sric2016     * A sentence describing what this module does.
113168ff6f3Sric2016     *
114168ff6f3Sric2016     * @return string
115168ff6f3Sric2016     */
11649a243cbSGreg Roach    public function description(): string
117c1010edaSGreg Roach    {
118bbb76c12SGreg Roach        /* I18N: Description of the “AncestorsChart” module */
119bbb76c12SGreg Roach        return I18N::translate('A chart of an individual’s ancestors.');
120168ff6f3Sric2016    }
121168ff6f3Sric2016
122168ff6f3Sric2016    /**
123377a2979SGreg Roach     * CSS class for the URL.
124377a2979SGreg Roach     *
125377a2979SGreg Roach     * @return string
126377a2979SGreg Roach     */
127377a2979SGreg Roach    public function chartMenuClass(): string
128377a2979SGreg Roach    {
129377a2979SGreg Roach        return 'menu-chart-ancestry';
130377a2979SGreg Roach    }
131377a2979SGreg Roach
132377a2979SGreg Roach    /**
1334eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1344eb71cfaSGreg Roach     *
13560bc3e3fSGreg Roach     * @param Individual $individual
13660bc3e3fSGreg Roach     *
1374eb71cfaSGreg Roach     * @return Menu|null
1384eb71cfaSGreg Roach     */
139377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
140c1010edaSGreg Roach    {
141e6562982SGreg Roach        return $this->chartMenu($individual);
142e6562982SGreg Roach    }
143e6562982SGreg Roach
144e6562982SGreg Roach    /**
145e6562982SGreg Roach     * The title for a specific instance of this chart.
146e6562982SGreg Roach     *
147e6562982SGreg Roach     * @param Individual $individual
148e6562982SGreg Roach     *
149e6562982SGreg Roach     * @return string
150e6562982SGreg Roach     */
151e6562982SGreg Roach    public function chartTitle(Individual $individual): string
152e6562982SGreg Roach    {
153e6562982SGreg Roach        /* I18N: %s is an individual’s name */
15439ca88baSGreg Roach        return I18N::translate('Ancestors of %s', $individual->fullName());
155e6562982SGreg Roach    }
156e6562982SGreg Roach
157e6562982SGreg Roach    /**
15871378461SGreg Roach     * The URL for a page showing chart options.
159e539f5c6SGreg Roach     *
16071378461SGreg Roach     * @param Individual $individual
16159597b37SGreg Roach     * @param mixed[]    $parameters
16271378461SGreg Roach     *
16371378461SGreg Roach     * @return string
16471378461SGreg Roach     */
16571378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
16671378461SGreg Roach    {
16772f04adfSGreg Roach        return route(static::class, [
16871378461SGreg Roach                'xref' => $individual->xref(),
16971378461SGreg Roach                'tree' => $individual->tree()->name(),
17071378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
17171378461SGreg Roach    }
17271378461SGreg Roach
17371378461SGreg Roach    /**
1746ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
175e539f5c6SGreg Roach     *
1766ccdf4f0SGreg Roach     * @return ResponseInterface
177e539f5c6SGreg Roach     */
17871378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
179e539f5c6SGreg Roach    {
1804ea62551SGreg Roach        $tree = $request->getAttribute('tree');
1814ea62551SGreg Roach        assert($tree instanceof Tree);
1824ea62551SGreg Roach
183ddeb3354SGreg Roach        $xref = $request->getAttribute('xref');
184ddeb3354SGreg Roach        assert(is_string($xref));
185ddeb3354SGreg Roach
186a091ac74SGreg Roach        $individual  = Factory::individual()->make($xref, $tree);
1873c3fd0a5SGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
188ddeb3354SGreg Roach
18971378461SGreg Roach        $ajax        = $request->getQueryParams()['ajax'] ?? '';
19071378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
19171378461SGreg Roach        $style       = $request->getAttribute('style');
19257ab2231SGreg Roach        $user        = $request->getAttribute('user');
193ddeb3354SGreg Roach
194*ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
195ddeb3354SGreg Roach
196e539f5c6SGreg Roach
19771378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
19871378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
199b46c87bdSGreg Roach            $params = (array) $request->getParsedBody();
200b46c87bdSGreg Roach
20172f04adfSGreg Roach            return redirect(route(static::class, [
2024ea62551SGreg Roach                'tree'        => $tree->name(),
203b46c87bdSGreg Roach                'xref'        => $params['xref'],
204b46c87bdSGreg Roach                'style'       => $params['style'],
205b46c87bdSGreg Roach                'generations' => $params['generations'],
20671378461SGreg Roach            ]));
20771378461SGreg Roach        }
20871378461SGreg Roach
2090cf94a7cSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
2100cf94a7cSGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
211e539f5c6SGreg Roach
2120b93976aSGreg Roach        if ($ajax === '1') {
213e539f5c6SGreg Roach            $this->layout = 'layouts/ajax';
214e539f5c6SGreg Roach
21571378461SGreg Roach            $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations);
21671378461SGreg Roach
21771378461SGreg Roach            switch ($style) {
21871378461SGreg Roach                default:
21971378461SGreg Roach                case self::CHART_STYLE_TREE:
22071378461SGreg Roach                    return $this->viewResponse('modules/ancestors-chart/tree', [
22171378461SGreg Roach                        'individual'  => $individual,
2221afbbc50SGreg Roach                        'parents'     => $individual->childFamilies()->first(),
22371378461SGreg Roach                        'generations' => $generations,
22471378461SGreg Roach                        'sosa'        => 1,
22571378461SGreg Roach                    ]);
22671378461SGreg Roach
22771378461SGreg Roach                case self::CHART_STYLE_INDIVIDUALS:
228e539f5c6SGreg Roach                    return $this->viewResponse('lists/individuals-table', [
229e539f5c6SGreg Roach                        'individuals' => $ancestors,
230e539f5c6SGreg Roach                        'sosa'        => true,
231e539f5c6SGreg Roach                        'tree'        => $tree,
232e539f5c6SGreg Roach                    ]);
233e539f5c6SGreg Roach
23471378461SGreg Roach                case self::CHART_STYLE_FAMILIES:
235e539f5c6SGreg Roach                    $families = [];
23671378461SGreg Roach
237e539f5c6SGreg Roach                    foreach ($ancestors as $individual) {
23839ca88baSGreg Roach                        foreach ($individual->childFamilies() as $family) {
239e539f5c6SGreg Roach                            $families[$family->xref()] = $family;
240e539f5c6SGreg Roach                        }
241e539f5c6SGreg Roach                    }
242e539f5c6SGreg Roach
243ef5d23f1SGreg Roach                    return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]);
244e539f5c6SGreg Roach            }
24571378461SGreg Roach        }
24671378461SGreg Roach
24771378461SGreg Roach        $ajax_url = $this->chartUrl($individual, [
24871378461SGreg Roach            'ajax'        => true,
24971378461SGreg Roach            'generations' => $generations,
25071378461SGreg Roach            'style'       => $style,
25171378461SGreg Roach            'xref'        => $xref,
25271378461SGreg Roach        ]);
25371378461SGreg Roach
25471378461SGreg Roach        return $this->viewResponse('modules/ancestors-chart/page', [
25571378461SGreg Roach            'ajax_url'            => $ajax_url,
25671378461SGreg Roach            'generations'         => $generations,
25771378461SGreg Roach            'individual'          => $individual,
25871378461SGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
25971378461SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
26071378461SGreg Roach            'module'              => $this->name(),
26171378461SGreg Roach            'style'               => $style,
26271378461SGreg Roach            'styles'              => $this->styles(),
26371378461SGreg Roach            'title'               => $this->chartTitle($individual),
264ef5d23f1SGreg Roach            'tree'                => $tree,
26571378461SGreg Roach        ]);
26671378461SGreg Roach    }
267e539f5c6SGreg Roach
268e539f5c6SGreg Roach    /**
269e539f5c6SGreg Roach     * This chart can display its output in a number of styles
270e539f5c6SGreg Roach     *
27171378461SGreg Roach     * @return string[]
272e539f5c6SGreg Roach     */
27371378461SGreg Roach    protected function styles(): array
274e539f5c6SGreg Roach    {
275e539f5c6SGreg Roach        return [
2760f1e0f10SGreg Roach            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
277e539f5c6SGreg Roach            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
278e539f5c6SGreg Roach            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
279e539f5c6SGreg Roach        ];
280e539f5c6SGreg Roach    }
281168ff6f3Sric2016}
282