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