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