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