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