xref: /webtrees/app/Module/DescendancyChartModule.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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    /**
96     * A sentence describing what this module does.
97     *
98     * @return string
99     */
100    public function description(): string
101    {
102        /* I18N: Description of the “DescendancyChart” module */
103        return I18N::translate('A chart of an individual’s descendants.');
104    }
105
106    /**
107     * CSS class for the URL.
108     *
109     * @return string
110     */
111    public function chartMenuClass(): string
112    {
113        return 'menu-chart-descendants';
114    }
115
116    /**
117     * Return a menu item for this chart - for use in individual boxes.
118     */
119    public function chartBoxMenu(Individual $individual): Menu|null
120    {
121        return $this->chartMenu($individual);
122    }
123
124    /**
125     * The title for a specific instance of this chart.
126     *
127     * @param Individual $individual
128     *
129     * @return string
130     */
131    public function chartTitle(Individual $individual): string
132    {
133        /* I18N: %s is an individual’s name */
134        return I18N::translate('Descendants of %s', $individual->fullName());
135    }
136
137    /**
138     * The URL for a page showing chart options.
139     *
140     * @param Individual                                $individual
141     * @param array<bool|int|string|array<string>|null> $parameters
142     *
143     * @return string
144     */
145    public function chartUrl(Individual $individual, array $parameters = []): string
146    {
147        return route(static::class, [
148                'tree' => $individual->tree()->name(),
149                'xref' => $individual->xref(),
150            ] + $parameters + self::DEFAULT_PARAMETERS);
151    }
152
153    /**
154     * @param ServerRequestInterface $request
155     *
156     * @return ResponseInterface
157     */
158    public function handle(ServerRequestInterface $request): ResponseInterface
159    {
160        $tree        = Validator::attributes($request)->tree();
161        $user        = Validator::attributes($request)->user();
162        $xref        = Validator::attributes($request)->isXref()->string('xref');
163        $style       = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style');
164        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
165        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
166
167        // Convert POST requests into GET requests for pretty URLs.
168        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
169            return redirect(route(static::class, [
170                'tree'        => $tree->name(),
171                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
172                'style'       => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'),
173                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
174            ]));
175        }
176
177        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
178
179        $individual  = Registry::individualFactory()->make($xref, $tree);
180        $individual  = Auth::checkIndividualAccess($individual, false, true);
181
182        if ($ajax) {
183            $this->layout = 'layouts/ajax';
184
185            switch ($style) {
186                case self::CHART_STYLE_TREE:
187                    return $this->viewResponse('modules/descendancy_chart/tree', [
188                        'individual'  => $individual,
189                        'generations' => $generations,
190                        'daboville'   => '1',
191                    ]);
192
193                case self::CHART_STYLE_INDIVIDUALS:
194                    $individuals = $this->chart_service->descendants($individual, $generations - 1);
195
196                    return $this->viewResponse('lists/individuals-table', [
197                        'individuals' => $individuals,
198                        'sosa'        => false,
199                        'tree'        => $tree,
200                    ]);
201
202                case self::CHART_STYLE_FAMILIES:
203                    $families = $this->chart_service->descendantFamilies($individual, $generations - 1);
204
205                    return $this->viewResponse('lists/families-table', [
206                        'families' => $families,
207                        'tree'     => $tree,
208                    ]);
209            }
210        }
211
212        $ajax_url = $this->chartUrl($individual, [
213            'generations' => $generations,
214            'style'       => $style,
215            'ajax'        => true,
216        ]);
217
218        return $this->viewResponse('modules/descendancy_chart/page', [
219            'ajax_url'            => $ajax_url,
220            'style'               => $style,
221            'styles'              => $this->styles(),
222            'default_generations' => self::DEFAULT_GENERATIONS,
223            'generations'         => $generations,
224            'individual'          => $individual,
225            'maximum_generations' => self::MAXIMUM_GENERATIONS,
226            'minimum_generations' => self::MINIMUM_GENERATIONS,
227            'module'              => $this->name(),
228            'title'               => $this->chartTitle($individual),
229            'tree'                => $tree,
230        ]);
231    }
232
233    /**
234     * This chart can display its output in a number of styles
235     *
236     * @return array<string>
237     */
238    protected function styles(): array
239    {
240        return [
241            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
242            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
243            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
244        ];
245    }
246}
247