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