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