xref: /webtrees/app/Module/HourglassChartModule.php (revision 1baf69dec287f195fbbefc3bae3c0b33957a89c3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Aura\Router\RouterContainer;
23use Fig\Http\Message\RequestMethodInterface;
24use Fisharebest\Webtrees\Auth;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Family;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Menu;
30use Fisharebest\Webtrees\Tree;
31use Illuminate\Support\Collection;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\RequestHandlerInterface;
35
36use function app;
37use function assert;
38use function is_string;
39use function response;
40use function view;
41
42/**
43 * Class HourglassChartModule
44 */
45class HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
46{
47    use ModuleChartTrait;
48
49    protected const ROUTE_URL = '/tree/{tree}/hourglass-{generations}-{spouses}/{xref}';
50
51    // Defaults
52    private const   DEFAULT_GENERATIONS = '3';
53    private const   DEFAULT_SPOUSES     = false;
54    protected const DEFAULT_PARAMETERS  = [
55        'generations' => self::DEFAULT_GENERATIONS,
56        'spouses'     => self::DEFAULT_SPOUSES,
57    ];
58
59    // Limits
60    protected const MINIMUM_GENERATIONS = 2;
61    protected const MAXIMUM_GENERATIONS = 10;
62
63    /**
64     * Initialization.
65     *
66     * @return void
67     */
68    public function boot(): void
69    {
70        $router_container = app(RouterContainer::class);
71        assert($router_container instanceof RouterContainer);
72
73        $router_container->getMap()
74            ->get(static::class, static::ROUTE_URL, $this)
75            ->allows(RequestMethodInterface::METHOD_POST)
76            ->tokens([
77                'generations' => '\d+',
78                'spouses'     => '1?',
79            ]);
80    }
81
82    /**
83     * How should this module be identified in the control panel, etc.?
84     *
85     * @return string
86     */
87    public function title(): string
88    {
89        /* I18N: Name of a module/chart */
90        return I18N::translate('Hourglass chart');
91    }
92
93    /**
94     * A sentence describing what this module does.
95     *
96     * @return string
97     */
98    public function description(): string
99    {
100        /* I18N: Description of the “HourglassChart” module */
101        return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.');
102    }
103
104    /**
105     * CSS class for the URL.
106     *
107     * @return string
108     */
109    public function chartMenuClass(): string
110    {
111        return 'menu-chart-hourglass';
112    }
113
114    /**
115     * Return a menu item for this chart - for use in individual boxes.
116     *
117     * @param Individual $individual
118     *
119     * @return Menu|null
120     */
121    public function chartBoxMenu(Individual $individual): ?Menu
122    {
123        return $this->chartMenu($individual);
124    }
125
126    /**
127     * The title for a specific instance of this chart.
128     *
129     * @param Individual $individual
130     *
131     * @return string
132     */
133    public function chartTitle(Individual $individual): string
134    {
135        /* I18N: %s is an individual’s name */
136        return I18N::translate('Hourglass chart of %s', $individual->fullName());
137    }
138
139    /**
140     * The URL for a page showing chart options.
141     *
142     * @param Individual                        $individual
143     * @param array<bool|int|string|array|null> $parameters
144     *
145     * @return string
146     */
147    public function chartUrl(Individual $individual, array $parameters = []): string
148    {
149        return route(static::class, [
150                'xref' => $individual->xref(),
151                'tree' => $individual->tree()->name(),
152            ] + $parameters + self::DEFAULT_PARAMETERS);
153    }
154
155    /**
156     * @param ServerRequestInterface $request
157     *
158     * @return ResponseInterface
159     */
160    public function handle(ServerRequestInterface $request): ResponseInterface
161    {
162        $tree = $request->getAttribute('tree');
163        assert($tree instanceof Tree);
164
165        $xref = $request->getAttribute('xref');
166        assert(is_string($xref));
167
168        $individual = Registry::individualFactory()->make($xref, $tree);
169        $individual = Auth::checkIndividualAccess($individual, false, true);
170
171        $user        = $request->getAttribute('user');
172        $generations = (int) $request->getAttribute('generations');
173        $spouses     = (bool) $request->getAttribute('spouses');
174        $ajax        = $request->getQueryParams()['ajax'] ?? '';
175
176        // Convert POST requests into GET requests for pretty URLs.
177        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
178            $params = (array) $request->getParsedBody();
179
180            return redirect(route(static::class, [
181                'tree'        => $tree->name(),
182                'xref'        => $params['xref'],
183                'generations' => $params['generations'],
184                'spouses'     => $params['spouses'] ?? false,
185            ]));
186        }
187
188        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
189
190        $generations = min($generations, self::MAXIMUM_GENERATIONS);
191        $generations = max($generations, self::MINIMUM_GENERATIONS);
192
193        if ($ajax === '1') {
194            $this->layout = 'layouts/ajax';
195
196            return $this->viewResponse('modules/hourglass-chart/chart', [
197                'generations' => $generations,
198                'individual'  => $individual,
199                'spouses'     => $spouses,
200            ]);
201        }
202
203        $ajax_url = $this->chartUrl($individual, [
204            'ajax'        => true,
205            'generations' => $generations,
206            'spouses'     => $spouses,
207        ]);
208
209        return $this->viewResponse('modules/hourglass-chart/page', [
210            'ajax_url'            => $ajax_url,
211            'generations'         => $generations,
212            'individual'          => $individual,
213            'maximum_generations' => self::MAXIMUM_GENERATIONS,
214            'minimum_generations' => self::MINIMUM_GENERATIONS,
215            'module'              => $this->name(),
216            'spouses'             => $spouses,
217            'title'               => $this->chartTitle($individual),
218            'tree'                => $tree,
219        ]);
220    }
221
222    /**
223     * Generate an extension to the chart
224     *
225     * @param ServerRequestInterface $request
226     *
227     * @return ResponseInterface
228     */
229    public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface
230    {
231        $tree = $request->getAttribute('tree');
232        assert($tree instanceof Tree);
233
234        $xref = $request->getQueryParams()['xref'] ?? '';
235
236        $family = Registry::familyFactory()->make($xref, $tree);
237        $family = Auth::checkFamilyAccess($family);
238
239        return response(view('modules/hourglass-chart/parents', [
240            'family'      => $family,
241            'generations' => 1,
242        ]));
243    }
244
245    /**
246     * Generate an extension to the chart
247     *
248     * @param ServerRequestInterface $request
249     *
250     * @return ResponseInterface
251     */
252    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
253    {
254        $tree = $request->getAttribute('tree');
255        assert($tree instanceof Tree);
256
257        $xref = $request->getQueryParams()['xref'] ?? '';
258
259        $spouses    = (bool) ($request->getQueryParams()['spouses'] ?? false);
260        $individual = Registry::individualFactory()->make($xref, $tree);
261        $individual = Auth::checkIndividualAccess($individual, false, true);
262
263        $children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
264            return $family->children();
265        })->flatten();
266
267        return response(view('modules/hourglass-chart/children', [
268            'children'    => $children,
269            'generations' => 1,
270            'spouses'     => $spouses,
271        ]));
272    }
273}
274