xref: /webtrees/app/Module/HourglassChartModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1168ff6f3Sric2016<?php
23976b470SGreg Roach
3168ff6f3Sric2016/**
4168ff6f3Sric2016 * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
2426684e68SGreg Roachuse Fisharebest\Webtrees\Auth;
25cb0eb4a9SGreg Roachuse Fisharebest\Webtrees\Family;
26168ff6f3Sric2016use Fisharebest\Webtrees\I18N;
27168ff6f3Sric2016use Fisharebest\Webtrees\Individual;
28e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Registry;
30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
31cb0eb4a9SGreg Roachuse Illuminate\Support\Collection;
326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3471378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
353976b470SGreg Roach
369e18e23bSGreg Roachuse function app;
375229eadeSGreg Roachuse function assert;
38cb0eb4a9SGreg Roachuse function response;
39f4ba05e3SGreg Roachuse function view;
40168ff6f3Sric2016
41168ff6f3Sric2016/**
42168ff6f3Sric2016 * Class HourglassChartModule
43168ff6f3Sric2016 */
4471378461SGreg Roachclass HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
45c1010edaSGreg Roach{
4649a243cbSGreg Roach    use ModuleChartTrait;
4749a243cbSGreg Roach
4872f04adfSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}';
4971378461SGreg Roach
5026684e68SGreg Roach    // Defaults
5126684e68SGreg Roach    private const   DEFAULT_GENERATIONS = '3';
5271378461SGreg Roach    private const   DEFAULT_SPOUSES     = false;
5371378461SGreg Roach    protected const DEFAULT_PARAMETERS  = [
5471378461SGreg Roach        'generations' => self::DEFAULT_GENERATIONS,
5571378461SGreg Roach        'spouses'     => self::DEFAULT_SPOUSES,
5671378461SGreg Roach    ];
5726684e68SGreg Roach
5826684e68SGreg Roach    // Limits
5971378461SGreg Roach    protected const MINIMUM_GENERATIONS = 2;
6071378461SGreg Roach    protected const MAXIMUM_GENERATIONS = 10;
6171378461SGreg Roach
6271378461SGreg Roach    /**
6371378461SGreg Roach     * Initialization.
6471378461SGreg Roach     *
659e18e23bSGreg Roach     * @return void
6671378461SGreg Roach     */
679e18e23bSGreg Roach    public function boot(): void
6871378461SGreg Roach    {
69158900c2SGreg Roach        Registry::routeFactory()->routeMap()
7072f04adfSGreg Roach            ->get(static::class, static::ROUTE_URL, $this)
71158900c2SGreg Roach            ->allows(RequestMethodInterface::METHOD_POST);
7271378461SGreg Roach    }
7326684e68SGreg Roach
74168ff6f3Sric2016    /**
750cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
76168ff6f3Sric2016     *
77168ff6f3Sric2016     * @return string
78168ff6f3Sric2016     */
7949a243cbSGreg Roach    public function title(): string
80c1010edaSGreg Roach    {
81bbb76c12SGreg Roach        /* I18N: Name of a module/chart */
82bbb76c12SGreg Roach        return I18N::translate('Hourglass chart');
83168ff6f3Sric2016    }
84168ff6f3Sric2016
85168ff6f3Sric2016    /**
86168ff6f3Sric2016     * A sentence describing what this module does.
87168ff6f3Sric2016     *
88168ff6f3Sric2016     * @return string
89168ff6f3Sric2016     */
9049a243cbSGreg Roach    public function description(): string
91c1010edaSGreg Roach    {
92bbb76c12SGreg Roach        /* I18N: Description of the “HourglassChart” module */
93bbb76c12SGreg Roach        return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.');
94168ff6f3Sric2016    }
95168ff6f3Sric2016
96168ff6f3Sric2016    /**
97377a2979SGreg Roach     * CSS class for the URL.
98377a2979SGreg Roach     *
99377a2979SGreg Roach     * @return string
100377a2979SGreg Roach     */
101377a2979SGreg Roach    public function chartMenuClass(): string
102377a2979SGreg Roach    {
103377a2979SGreg Roach        return 'menu-chart-hourglass';
104377a2979SGreg Roach    }
105377a2979SGreg Roach
106377a2979SGreg Roach    /**
1074eb71cfaSGreg Roach     * Return a menu item for this chart - for use in individual boxes.
1084eb71cfaSGreg Roach     *
10960bc3e3fSGreg Roach     * @param Individual $individual
11060bc3e3fSGreg Roach     *
1114eb71cfaSGreg Roach     * @return Menu|null
1124eb71cfaSGreg Roach     */
113377a2979SGreg Roach    public function chartBoxMenu(Individual $individual): ?Menu
114c1010edaSGreg Roach    {
115e6562982SGreg Roach        return $this->chartMenu($individual);
116e6562982SGreg Roach    }
117e6562982SGreg Roach
118e6562982SGreg Roach    /**
11971378461SGreg Roach     * The title for a specific instance of this chart.
120e6562982SGreg Roach     *
12171378461SGreg Roach     * @param Individual $individual
12271378461SGreg Roach     *
12371378461SGreg Roach     * @return string
12471378461SGreg Roach     */
12571378461SGreg Roach    public function chartTitle(Individual $individual): string
12671378461SGreg Roach    {
12771378461SGreg Roach        /* I18N: %s is an individual’s name */
12871378461SGreg Roach        return I18N::translate('Hourglass chart of %s', $individual->fullName());
12971378461SGreg Roach    }
13071378461SGreg Roach
13171378461SGreg Roach    /**
13271378461SGreg Roach     * The URL for a page showing chart options.
13371378461SGreg Roach     *
13471378461SGreg Roach     * @param Individual                                $individual
13576d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
13671378461SGreg Roach     *
13771378461SGreg Roach     * @return string
13871378461SGreg Roach     */
13971378461SGreg Roach    public function chartUrl(Individual $individual, array $parameters = []): string
14071378461SGreg Roach    {
14172f04adfSGreg Roach        return route(static::class, [
14271378461SGreg Roach                'xref' => $individual->xref(),
14371378461SGreg Roach                'tree' => $individual->tree()->name(),
14471378461SGreg Roach            ] + $parameters + self::DEFAULT_PARAMETERS);
14571378461SGreg Roach    }
14671378461SGreg Roach
14771378461SGreg Roach    /**
1486ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
149e6562982SGreg Roach     *
1506ccdf4f0SGreg Roach     * @return ResponseInterface
151e6562982SGreg Roach     */
15271378461SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
153e6562982SGreg Roach    {
154b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
155b55cbc6bSGreg Roach        $xref        = Validator::attributes($request)->isXref()->string('xref');
156b55cbc6bSGreg Roach        $user        = Validator::attributes($request)->user();
157b55cbc6bSGreg Roach        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
158b55cbc6bSGreg Roach        $spouses     = Validator::attributes($request)->boolean('spouses');
159b55cbc6bSGreg Roach        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
16026684e68SGreg Roach
16171378461SGreg Roach        // Convert POST requests into GET requests for pretty URLs.
16271378461SGreg Roach        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
16372f04adfSGreg Roach            return redirect(route(static::class, [
1644ea62551SGreg Roach                'tree'        => $tree->name(),
165158900c2SGreg Roach                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
166158900c2SGreg Roach                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
167158900c2SGreg Roach                'spouses'     => Validator::parsedBody($request)->boolean('spouses'),
16871378461SGreg Roach            ]));
16971378461SGreg Roach        }
17071378461SGreg Roach
171ef483801SGreg Roach        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
17226684e68SGreg Roach
173b55cbc6bSGreg Roach        $individual  = Registry::individualFactory()->make($xref, $tree);
174b55cbc6bSGreg Roach        $individual  = Auth::checkIndividualAccess($individual, false, true);
17526684e68SGreg Roach
176b55cbc6bSGreg Roach        if ($ajax) {
17771378461SGreg Roach            $this->layout = 'layouts/ajax';
17871378461SGreg Roach
17971378461SGreg Roach            return $this->viewResponse('modules/hourglass-chart/chart', [
18071378461SGreg Roach                'generations' => $generations,
18171378461SGreg Roach                'individual'  => $individual,
18271378461SGreg Roach                'spouses'     => $spouses,
18371378461SGreg Roach            ]);
18426684e68SGreg Roach        }
18526684e68SGreg Roach
18626684e68SGreg Roach        $ajax_url = $this->chartUrl($individual, [
1879b5537c3SGreg Roach            'ajax'        => true,
1882b29a33eSmakitso            'generations' => $generations,
18971378461SGreg Roach            'spouses'     => $spouses,
19026684e68SGreg Roach        ]);
19126684e68SGreg Roach
1929b5537c3SGreg Roach        return $this->viewResponse('modules/hourglass-chart/page', [
19326684e68SGreg Roach            'ajax_url'            => $ajax_url,
19426684e68SGreg Roach            'generations'         => $generations,
19526684e68SGreg Roach            'individual'          => $individual,
196e759aebbSGreg Roach            'maximum_generations' => self::MAXIMUM_GENERATIONS,
19726684e68SGreg Roach            'minimum_generations' => self::MINIMUM_GENERATIONS,
19871378461SGreg Roach            'module'              => $this->name(),
19971378461SGreg Roach            'spouses'             => $spouses,
20026684e68SGreg Roach            'title'               => $this->chartTitle($individual),
201ef5d23f1SGreg Roach            'tree'                => $tree,
20226684e68SGreg Roach        ]);
20326684e68SGreg Roach    }
20426684e68SGreg Roach
20526684e68SGreg Roach    /**
206cb0eb4a9SGreg Roach     * Generate an extension to the chart
207cb0eb4a9SGreg Roach     *
208cb0eb4a9SGreg Roach     * @param ServerRequestInterface $request
209cb0eb4a9SGreg Roach     *
210cb0eb4a9SGreg Roach     * @return ResponseInterface
211cb0eb4a9SGreg Roach     */
212cb0eb4a9SGreg Roach    public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface
213cb0eb4a9SGreg Roach    {
214b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
215158900c2SGreg Roach        $xref   = Validator::queryParams($request)->isXref()->string('xref');
2166b9cb339SGreg Roach        $family = Registry::familyFactory()->make($xref, $tree);
217ddeb3354SGreg Roach        $family = Auth::checkFamilyAccess($family);
218cb0eb4a9SGreg Roach
219cb0eb4a9SGreg Roach        return response(view('modules/hourglass-chart/parents', [
220cb0eb4a9SGreg Roach            'family'      => $family,
221cb0eb4a9SGreg Roach            'generations' => 1,
22226684e68SGreg Roach        ]));
22326684e68SGreg Roach    }
22426684e68SGreg Roach
22526684e68SGreg Roach    /**
226cb0eb4a9SGreg Roach     * Generate an extension to the chart
227cb0eb4a9SGreg Roach     *
2286ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
22926684e68SGreg Roach     *
2306ccdf4f0SGreg Roach     * @return ResponseInterface
23126684e68SGreg Roach     */
232cb0eb4a9SGreg Roach    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
23326684e68SGreg Roach    {
234b55cbc6bSGreg Roach        $tree       = Validator::attributes($request)->tree();
235158900c2SGreg Roach        $xref       = Validator::queryParams($request)->isXref()->string('xref');
236158900c2SGreg Roach        $spouses    = Validator::queryParams($request)->boolean('spouses', false);
2376b9cb339SGreg Roach        $individual = Registry::individualFactory()->make($xref, $tree);
2383c3fd0a5SGreg Roach        $individual = Auth::checkIndividualAccess($individual, false, true);
239a21e5374SGreg Roach
240a21e5374SGreg Roach        $children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
241a21e5374SGreg Roach            return $family->children();
242a21e5374SGreg Roach        })->flatten();
24326684e68SGreg Roach
244cb0eb4a9SGreg Roach        return response(view('modules/hourglass-chart/children', [
245cb0eb4a9SGreg Roach            'children'    => $children,
246cb0eb4a9SGreg Roach            'generations' => 1,
24771378461SGreg Roach            'spouses'     => $spouses,
248cb0eb4a9SGreg Roach        ]));
249e6562982SGreg Roach    }
250168ff6f3Sric2016}
251