xref: /webtrees/app/Module/HourglassChartModule.php (revision 9f37cd254b1a3e6bd44a0e3b12637fa6f632784c)
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\Family;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Menu;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Validator;
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 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        Registry::routeFactory()->routeMap()
70            ->get(static::class, static::ROUTE_URL, $this)
71            ->allows(RequestMethodInterface::METHOD_POST);
72    }
73
74    /**
75     * How should this module be identified in the control panel, etc.?
76     *
77     * @return string
78     */
79    public function title(): string
80    {
81        /* I18N: Name of a module/chart */
82        return I18N::translate('Hourglass chart');
83    }
84
85    /**
86     * A sentence describing what this module does.
87     *
88     * @return string
89     */
90    public function description(): string
91    {
92        /* I18N: Description of the “HourglassChart” module */
93        return I18N::translate('An hourglass chart of an individual’s ancestors and descendants.');
94    }
95
96    /**
97     * CSS class for the URL.
98     *
99     * @return string
100     */
101    public function chartMenuClass(): string
102    {
103        return 'menu-chart-hourglass';
104    }
105
106    /**
107     * Return a menu item for this chart - for use in individual boxes.
108     *
109     * @param Individual $individual
110     *
111     * @return Menu|null
112     */
113    public function chartBoxMenu(Individual $individual): ?Menu
114    {
115        return $this->chartMenu($individual);
116    }
117
118    /**
119     * The title for a specific instance of this chart.
120     *
121     * @param Individual $individual
122     *
123     * @return string
124     */
125    public function chartTitle(Individual $individual): string
126    {
127        /* I18N: %s is an individual’s name */
128        return I18N::translate('Hourglass chart of %s', $individual->fullName());
129    }
130
131    /**
132     * The URL for a page showing chart options.
133     *
134     * @param Individual                                $individual
135     * @param array<bool|int|string|array<string>|null> $parameters
136     *
137     * @return string
138     */
139    public function chartUrl(Individual $individual, array $parameters = []): string
140    {
141        return route(static::class, [
142                'xref' => $individual->xref(),
143                'tree' => $individual->tree()->name(),
144            ] + $parameters + self::DEFAULT_PARAMETERS);
145    }
146
147    /**
148     * @param ServerRequestInterface $request
149     *
150     * @return ResponseInterface
151     */
152    public function handle(ServerRequestInterface $request): ResponseInterface
153    {
154        $tree        = Validator::attributes($request)->tree();
155        $xref        = Validator::attributes($request)->isXref()->string('xref');
156        $user        = Validator::attributes($request)->user();
157        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
158        $spouses     = Validator::attributes($request)->boolean('spouses');
159        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
160
161        // Convert POST requests into GET requests for pretty URLs.
162        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
163            return redirect(route(static::class, [
164                'tree'        => $tree->name(),
165                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
166                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
167                'spouses'     => Validator::parsedBody($request)->boolean('spouses'),
168            ]));
169        }
170
171        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
172
173        $individual  = Registry::individualFactory()->make($xref, $tree);
174        $individual  = Auth::checkIndividualAccess($individual, false, true);
175
176        if ($ajax) {
177            $this->layout = 'layouts/ajax';
178
179            return $this->viewResponse('modules/hourglass-chart/chart', [
180                'generations' => $generations,
181                'individual'  => $individual,
182                'spouses'     => $spouses,
183            ]);
184        }
185
186        $ajax_url = $this->chartUrl($individual, [
187            'ajax'        => true,
188            'generations' => $generations,
189            'spouses'     => $spouses,
190        ]);
191
192        return $this->viewResponse('modules/hourglass-chart/page', [
193            'ajax_url'            => $ajax_url,
194            'generations'         => $generations,
195            'individual'          => $individual,
196            'maximum_generations' => self::MAXIMUM_GENERATIONS,
197            'minimum_generations' => self::MINIMUM_GENERATIONS,
198            'module'              => $this->name(),
199            'spouses'             => $spouses,
200            'title'               => $this->chartTitle($individual),
201            'tree'                => $tree,
202        ]);
203    }
204
205    /**
206     * Generate an extension to the chart
207     *
208     * @param ServerRequestInterface $request
209     *
210     * @return ResponseInterface
211     */
212    public function getAncestorsAction(ServerRequestInterface $request): ResponseInterface
213    {
214        $tree   = Validator::attributes($request)->tree();
215        $xref   = Validator::queryParams($request)->isXref()->string('xref');
216        $family = Registry::familyFactory()->make($xref, $tree);
217        $family = Auth::checkFamilyAccess($family);
218
219        return response(view('modules/hourglass-chart/parents', [
220            'family'      => $family,
221            'generations' => 1,
222        ]));
223    }
224
225    /**
226     * Generate an extension to the chart
227     *
228     * @param ServerRequestInterface $request
229     *
230     * @return ResponseInterface
231     */
232    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
233    {
234        $tree       = Validator::attributes($request)->tree();
235        $xref       = Validator::queryParams($request)->isXref()->string('xref');
236        $spouses    = Validator::queryParams($request)->boolean('spouses', false);
237        $individual = Registry::individualFactory()->make($xref, $tree);
238        $individual = Auth::checkIndividualAccess($individual, false, true);
239
240        $children = $individual->spouseFamilies()->map(static function (Family $family): Collection {
241            return $family->children();
242        })->flatten();
243
244        return response(view('modules/hourglass-chart/children', [
245            'children'    => $children,
246            'generations' => 1,
247            'spouses'     => $spouses,
248        ]));
249    }
250}
251