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