xref: /webtrees/app/Module/HourglassChartModule.php (revision 66c7498f9d559689b159190509749c1997dc5403)
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 response;
38use function view;
39
40/**
41 * Class HourglassChartModule
42 */
43class HourglassChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
44{
45    use ModuleChartTrait;
46
47    private const ROUTE_NAME = 'hourglass-chart';
48    private 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(self::ROUTE_NAME, self::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(self::ROUTE_NAME, [
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        $user        = $request->getAttribute('user');
165        $xref        = $request->getAttribute('xref');
166        $generations = (int) $request->getAttribute('generations');
167        $spouses     = (bool) $request->getAttribute('spouses');
168        $ajax        = $request->getQueryParams()['ajax'] ?? '';
169        $individual  = Individual::getInstance($xref, $tree);
170
171        // Convert POST requests into GET requests for pretty URLs.
172        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
173            return redirect(route(self::ROUTE_NAME, [
174                'tree'        => $tree->name(),
175                'xref'        => $request->getParsedBody()['xref'],
176                'generations' => $request->getParsedBody()['generations'],
177                'spouses'     => $request->getParsedBody()['spouses'] ?? false,
178            ]));
179        }
180
181        Auth::checkIndividualAccess($individual);
182        Auth::checkComponentAccess($this, 'chart', $tree, $user);
183
184        $generations = min($generations, self::MAXIMUM_GENERATIONS);
185        $generations = max($generations, self::MINIMUM_GENERATIONS);
186
187        if ($ajax === '1') {
188            $this->layout = 'layouts/ajax';
189
190            return $this->viewResponse('modules/hourglass-chart/chart', [
191                'generations' => $generations,
192                'individual'  => $individual,
193                'spouses'     => $spouses,
194            ]);
195        }
196
197        $ajax_url = $this->chartUrl($individual, [
198            'ajax'        => true,
199            'generations' => $generations,
200            'spouses'     => $spouses,
201        ]);
202
203        return $this->viewResponse('modules/hourglass-chart/page', [
204            'ajax_url'            => $ajax_url,
205            'generations'         => $generations,
206            'individual'          => $individual,
207            'maximum_generations' => self::MAXIMUM_GENERATIONS,
208            'minimum_generations' => self::MINIMUM_GENERATIONS,
209            'module'              => $this->name(),
210            'spouses'             => $spouses,
211            'title'               => $this->chartTitle($individual),
212            'tree'                => $tree,
213        ]);
214    }
215
216    /**
217     * Generate an extension to the chart
218     *
219     * @param ServerRequestInterface $request
220     *
221     * @return ResponseInterface
222     */
223    public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface
224    {
225        $tree = $request->getAttribute('tree');
226        assert($tree instanceof Tree);
227
228        $xref = $request->getQueryParams()['xref'] ?? '';
229
230        $family = Family::getInstance($xref, $tree);
231        Auth::checkFamilyAccess($family);
232
233        return response(view('modules/hourglass-chart/parents', [
234            'family'      => $family,
235            'generations' => 1,
236        ]));
237    }
238
239    /**
240     * Generate an extension to the chart
241     *
242     * @param ServerRequestInterface $request
243     *
244     * @return ResponseInterface
245     */
246    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
247    {
248        $tree = $request->getAttribute('tree');
249        assert($tree instanceof Tree);
250
251        $xref = $request->getQueryParams()['xref'] ?? '';
252
253        $spouses    = (bool) ($request->getQueryParams()['spouses'] ?? false);
254        $individual = Individual::getInstance($xref, $tree);
255
256        Auth::checkIndividualAccess($individual);
257
258        $children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
259            return $family->children();
260        })->flatten();
261
262        return response(view('modules/hourglass-chart/children', [
263            'children'    => $children,
264            'generations' => 1,
265            'spouses'     => $spouses,
266        ]));
267    }
268}
269