xref: /webtrees/app/Module/AncestorsChartModule.php (revision 1d1f373cc23093682b1736881f66a527f62ccc46)
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 AncestorsChartModule
39 */
40class AncestorsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
41{
42    use ModuleChartTrait;
43
44    private const ROUTE_NAME = 'ancestors-chart';
45    private const ROUTE_URL  = '/tree/{tree}/ancestors-{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    protected const DEFAULT_GENERATIONS = '4';
54    protected const DEFAULT_STYLE       = self::CHART_STYLE_TREE;
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     * CompactTreeChartModule 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('Ancestors');
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 “AncestorsChart” module */
112        return I18N::translate('A chart of an individual’s ancestors.');
113    }
114
115    /**
116     * CSS class for the URL.
117     *
118     * @return string
119     */
120    public function chartMenuClass(): string
121    {
122        return 'menu-chart-ancestry';
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('Ancestors 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                'xref' => $individual->xref(),
162                'tree' => $individual->tree()->name(),
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        $ajax        = $request->getQueryParams()['ajax'] ?? '';
174        $generations = (int) $request->getAttribute('generations');
175        $style       = $request->getAttribute('style');
176        $tree        = $request->getAttribute('tree');
177        $user        = $request->getAttribute('user');
178        $xref        = $request->getAttribute('xref');
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            $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations);
201
202            switch ($style) {
203                default:
204                case self::CHART_STYLE_TREE:
205                    return $this->viewResponse('modules/ancestors-chart/tree', [
206                        'individual'  => $individual,
207                        'parents'     => $individual->primaryChildFamily(),
208                        'generations' => $generations,
209                        'sosa'        => 1,
210                    ]);
211
212                case self::CHART_STYLE_INDIVIDUALS:
213                    return $this->viewResponse('lists/individuals-table', [
214                        'individuals' => $ancestors,
215                        'sosa'        => true,
216                        'tree'        => $tree,
217                    ]);
218
219                case self::CHART_STYLE_FAMILIES:
220                    $families = [];
221
222                    foreach ($ancestors as $individual) {
223                        foreach ($individual->childFamilies() as $family) {
224                            $families[$family->xref()] = $family;
225                        }
226                    }
227
228                    return $this->viewResponse('lists/families-table', [
229                        'families' => $families,
230                        'tree'     => $tree,
231                    ]);
232            }
233        }
234
235        $ajax_url = $this->chartUrl($individual, [
236            'ajax'        => true,
237            'generations' => $generations,
238            'style'       => $style,
239            'xref'        => $xref,
240        ]);
241
242        return $this->viewResponse('modules/ancestors-chart/page', [
243            'ajax_url'            => $ajax_url,
244            'generations'         => $generations,
245            'individual'          => $individual,
246            'maximum_generations' => self::MAXIMUM_GENERATIONS,
247            'minimum_generations' => self::MINIMUM_GENERATIONS,
248            'module'              => $this->name(),
249            'style'               => $style,
250            'styles'              => $this->styles(),
251            'title'               => $this->chartTitle($individual),
252        ]);
253    }
254
255    /**
256     * This chart can display its output in a number of styles
257     *
258     * @return string[]
259     */
260    protected function styles(): array
261    {
262        return [
263            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
264            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
265            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
266        ];
267    }
268}
269