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