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