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