xref: /webtrees/app/Module/DescendancyChartModule.php (revision b8fc901f205cd6af65496b916bf63547a3065a2f)
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 DescendancyChartModule
43 */
44class DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
45{
46    use ModuleChartTrait;
47
48    private const ROUTE_NAME = 'descendancy-chart';
49    private const ROUTE_URL  = '/tree/{tree}/descendants-{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    public const    DEFAULT_STYLE       = self::CHART_STYLE_TREE;
58    public const    DEFAULT_GENERATIONS = '3';
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     * DescendancyChartModule 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('Descendants');
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 “DescendancyChart” module */
119        return I18N::translate('A chart of an individual’s descendants.');
120    }
121
122    /**
123     * CSS class for the URL.
124     *
125     * @return string
126     */
127    public function chartMenuClass(): string
128    {
129        return 'menu-chart-descendants';
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('Descendants 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                'tree' => $individual->tree()->name(),
169                'xref' => $individual->xref(),
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        $user        = $request->getAttribute('user');
190        $style       = $request->getAttribute('style');
191        $generations = (int) $request->getAttribute('generations');
192        $ajax        = $request->getQueryParams()['ajax'] ?? '';
193
194        // Convert POST requests into GET requests for pretty URLs.
195        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
196            return redirect(route(self::ROUTE_NAME, [
197                'tree'        => $tree->name(),
198                'xref'        => $request->getParsedBody()['xref'],
199                'style'       => $request->getParsedBody()['style'],
200                'generations' => $request->getParsedBody()['generations'],
201            ]));
202        }
203
204        Auth::checkComponentAccess($this, 'chart', $tree, $user);
205
206        $generations = min($generations, self::MAXIMUM_GENERATIONS);
207        $generations = max($generations, self::MINIMUM_GENERATIONS);
208
209        if ($ajax === '1') {
210            $this->layout = 'layouts/ajax';
211
212            switch ($style) {
213                case self::CHART_STYLE_TREE:
214                    return $this->viewResponse('modules/descendancy_chart/tree', [
215                        'individual'  => $individual,
216                        'generations' => $generations,
217                        'daboville'   => '1',
218                    ]);
219
220                case self::CHART_STYLE_INDIVIDUALS:
221                    return $this->viewResponse('lists/individuals-table', [
222                        'individuals' => $this->chart_service->descendants($individual, $generations - 1),
223                        'sosa'        => false,
224                        'tree'        => $tree,
225                    ]);
226
227                case self::CHART_STYLE_FAMILIES:
228                    $families = $this->chart_service->descendantFamilies($individual, $generations - 1);
229
230                    return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]);
231            }
232        }
233
234        $ajax_url = $this->chartUrl($individual, [
235            'generations' => $generations,
236            'style'       => $style,
237            'ajax'        => true,
238        ]);
239
240        return $this->viewResponse('modules/descendancy_chart/page', [
241            'ajax_url'            => $ajax_url,
242            'style'               => $style,
243            'styles'              => $this->styles(),
244            'default_generations' => self::DEFAULT_GENERATIONS,
245            'generations'         => $generations,
246            'individual'          => $individual,
247            'maximum_generations' => self::MAXIMUM_GENERATIONS,
248            'minimum_generations' => self::MINIMUM_GENERATIONS,
249            'module'              => $this->name(),
250            'title'               => $this->chartTitle($individual),
251            'tree'                => $tree,
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