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