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