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