xref: /webtrees/app/Module/HourglassChartModule.php (revision 5229eade2a8cdd1381e19f96f67fc1c8b92ca95d)
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 */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
19168ff6f3Sric2016namespace Fisharebest\Webtrees\Module;
20168ff6f3Sric2016
2171378461SGreg Roachuse Aura\Router\RouterContainer;
2271378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
2326684e68SGreg Roachuse Fisharebest\Webtrees\Auth;
24cb0eb4a9SGreg Roachuse Fisharebest\Webtrees\Family;
25168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
26168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
28*5229eadeSGreg Roachuse Fisharebest\Webtrees\Tree;
29cb0eb4a9SGreg Roachuse Illuminate\Support\Collection;
30*5229eadeSGreg Roachuse InvalidArgumentException;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3371378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
343976b470SGreg Roach
35*5229eadeSGreg Roachuse function assert;
36cb0eb4a9SGreg Roachuse function response;
37f4ba05e3SGreg Roachuse function view;
38168ff6f3Sric2016
39168ff6f3Sric2016/**
40168ff6f3Sric2016 * Class HourglassChartModule
41168ff6f3Sric2016 */
4271378461SGreg Roachclass HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
43c1010edaSGreg Roach{
4449a243cbSGreg Roach    use ModuleChartTrait;
4549a243cbSGreg Roach
4671378461SGreg Roach    private const ROUTE_NAME = 'hourglass-chart';
4771378461SGreg Roach    private const ROUTE_URL  = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}';
4871378461SGreg Roach
4926684e68SGreg Roach    // Defaults
5026684e68SGreg Roach    private const   DEFAULT_GENERATIONS = '3';
5171378461SGreg Roach    private const   DEFAULT_SPOUSES     = false;
5271378461SGreg Roach    protected const DEFAULT_PARAMETERS  = [
5371378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
5471378461SGreg Roach        'spouses'     => self::DEFAULT_SPOUSES,
5571378461SGreg Roach    ];
5626684e68SGreg Roach
5726684e68SGreg Roach    // Limits
5871378461SGreg Roach    protected const MINIMUM_GENERATIONS = 2;
5971378461SGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
6071378461SGreg Roach
6171378461SGreg Roach    /**
6271378461SGreg Roach     * Initialization.
6371378461SGreg Roach     *
6471378461SGreg Roach     * @param RouterContainer $router_container
6571378461SGreg Roach     */
6671378461SGreg Roach    public function boot(RouterContainer $router_container)
6771378461SGreg Roach    {
6871378461SGreg Roach        $router_container->getMap()
6971378461SGreg Roach            ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class)
7071378461SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST)
7171378461SGreg Roach            ->tokens([
7271378461SGreg Roach                'generations' => '\d+',
7371378461SGreg Roach                'spouses'     => '1?',
7471378461SGreg Roach            ]);
7571378461SGreg Roach    }
7626684e68SGreg Roach
77168ff6f3Sric2016    /**
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('Hourglass chart');
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 “HourglassChart” module */
96bbb76c12SGreg Roach        return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.');
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-hourglass';
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    /**
12271378461SGreg Roach     * The title for a specific instance of this chart.
123e6562982SGreg Roach     *
12471378461SGreg Roach     * @param Individual $individual
12571378461SGreg Roach     *
12671378461SGreg Roach     * @return string
12771378461SGreg Roach     */
12871378461SGreg Roach    public function chartTitle(Individual $individual): string
12971378461SGreg Roach    {
13071378461SGreg Roach        /* I18N: %s is an individual’s name */
13171378461SGreg Roach        return I18N::translate('Hourglass chart of %s', $individual->fullName());
13271378461SGreg Roach    }
13371378461SGreg Roach
13471378461SGreg Roach    /**
13571378461SGreg Roach     * The URL for a page showing chart options.
13671378461SGreg Roach     *
13771378461SGreg Roach     * @param Individual $individual
13871378461SGreg Roach     * @param string[]   $parameters
13971378461SGreg Roach     *
14071378461SGreg Roach     * @return string
14171378461SGreg Roach     */
14271378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
14371378461SGreg Roach    {
14471378461SGreg Roach        return route(self::ROUTE_NAME, [
14571378461SGreg Roach                'xref' => $individual->xref(),
14671378461SGreg Roach                'tree' => $individual->tree()->name(),
14771378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
14871378461SGreg Roach    }
14971378461SGreg Roach
15071378461SGreg Roach    /**
1516ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
152e6562982SGreg Roach     *
1536ccdf4f0SGreg Roach     * @return ResponseInterface
154e6562982SGreg Roach     */
15571378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
156e6562982SGreg Roach    {
15757ab2231SGreg Roach        $tree        = $request->getAttribute('tree');
15857ab2231SGreg Roach        $user        = $request->getAttribute('user');
15971378461SGreg Roach        $xref        = $request->getAttribute('xref');
16071378461SGreg Roach        $generations = (int) $request->getAttribute('generations');
16171378461SGreg Roach        $spouses     = (bool) $request->getAttribute('spouses');
1620b93976aSGreg Roach        $ajax        = $request->getQueryParams()['ajax'] ?? '';
16326684e68SGreg Roach        $individual  = Individual::getInstance($xref, $tree);
16426684e68SGreg Roach
16571378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
16671378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
16771378461SGreg Roach            return redirect(route(self::ROUTE_NAME, [
16871378461SGreg Roach                'tree'        => $request->getAttribute('tree')->name(),
16971378461SGreg Roach                'xref'        => $request->getParsedBody()['xref'],
17071378461SGreg Roach                'generations' => $request->getParsedBody()['generations'],
17171378461SGreg Roach                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
17271378461SGreg Roach            ]));
17371378461SGreg Roach        }
17471378461SGreg Roach
17526684e68SGreg Roach        Auth::checkIndividualAccess($individual);
1769867b2f0SGreg Roach        Auth::checkComponentAccess($this, 'chart', $tree, $user);
17726684e68SGreg Roach
178e759aebbSGreg Roach        $generations = min($generations, self::MAXIMUM_GENERATIONS);
17926684e68SGreg Roach        $generations = max($generations, self::MINIMUM_GENERATIONS);
18026684e68SGreg Roach
1810b93976aSGreg Roach        if ($ajax === '1') {
18271378461SGreg Roach            $this->layout = 'layouts/ajax';
18371378461SGreg Roach
18471378461SGreg Roach            return $this->viewResponse('modules/hourglass-chart/chart', [
18571378461SGreg Roach                'generations' => $generations,
18671378461SGreg Roach                'individual'  => $individual,
18771378461SGreg Roach                'spouses'     => $spouses,
18871378461SGreg Roach            ]);
18926684e68SGreg Roach        }
19026684e68SGreg Roach
19126684e68SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1929b5537c3SGreg Roach            'ajax'        => true,
1932b29a33eSmakitso            'generations' => $generations,
19471378461SGreg Roach            'spouses'     => $spouses,
19526684e68SGreg Roach        ]);
19626684e68SGreg Roach
1979b5537c3SGreg Roach        return $this->viewResponse('modules/hourglass-chart/page', [
19826684e68SGreg Roach            'ajax_url'            => $ajax_url,
19926684e68SGreg Roach            'generations'         => $generations,
20026684e68SGreg Roach            'individual'          => $individual,
201e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
20226684e68SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
20371378461SGreg Roach            'module'              => $this->name(),
20471378461SGreg Roach            'spouses'             => $spouses,
20526684e68SGreg Roach            'title'               => $this->chartTitle($individual),
20626684e68SGreg Roach        ]);
20726684e68SGreg Roach    }
20826684e68SGreg Roach
20926684e68SGreg Roach    /**
210cb0eb4a9SGreg Roach     * Generate an extension to the chart
211cb0eb4a9SGreg Roach     *
212cb0eb4a9SGreg Roach     * @param ServerRequestInterface $request
213cb0eb4a9SGreg Roach     *
214cb0eb4a9SGreg Roach     * @return ResponseInterface
215cb0eb4a9SGreg Roach     */
216cb0eb4a9SGreg Roach    public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface
217cb0eb4a9SGreg Roach    {
218f1d4b4a2SGreg Roach        $tree = $request->getAttribute('tree');
219*5229eadeSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
220*5229eadeSGreg Roach
221cb0eb4a9SGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
222cb0eb4a9SGreg Roach
223cb0eb4a9SGreg Roach        $family = Family::getInstance($xref, $tree);
224cb0eb4a9SGreg Roach        Auth::checkFamilyAccess($family);
225cb0eb4a9SGreg Roach
226cb0eb4a9SGreg Roach        return response(view('modules/hourglass-chart/parents', [
227cb0eb4a9SGreg Roach            'family'      => $family,
228cb0eb4a9SGreg Roach            'generations' => 1,
22926684e68SGreg Roach        ]));
23026684e68SGreg Roach    }
23126684e68SGreg Roach
23226684e68SGreg Roach    /**
233cb0eb4a9SGreg Roach     * Generate an extension to the chart
234cb0eb4a9SGreg Roach     *
2356ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
23626684e68SGreg Roach     *
2376ccdf4f0SGreg Roach     * @return ResponseInterface
23826684e68SGreg Roach     */
239cb0eb4a9SGreg Roach    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
24026684e68SGreg Roach    {
241f1d4b4a2SGreg Roach        $tree = $request->getAttribute('tree');
242*5229eadeSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
243*5229eadeSGreg Roach
244cb0eb4a9SGreg Roach        $xref = $request->getQueryParams()['xref'] ?? '';
24526684e68SGreg Roach
24671378461SGreg Roach        $spouses    = (bool) ($request->getQueryParams()['spouses'] ?? false);
247cb0eb4a9SGreg Roach        $individual = Individual::getInstance($xref, $tree);
248a21e5374SGreg Roach
24926684e68SGreg Roach        Auth::checkIndividualAccess($individual);
250a21e5374SGreg Roach
251a21e5374SGreg Roach        $children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
252a21e5374SGreg Roach            return $family->children();
253a21e5374SGreg Roach        })->flatten();
25426684e68SGreg Roach
255cb0eb4a9SGreg Roach        return response(view('modules/hourglass-chart/children', [
256cb0eb4a9SGreg Roach            'children'    => $children,
257cb0eb4a9SGreg Roach            'generations' => 1,
25871378461SGreg Roach            'spouses'     => $spouses,
259cb0eb4a9SGreg Roach        ]));
260e6562982SGreg Roach    }
261168ff6f3Sric2016}
262