xref: /webtrees/app/Module/DescendancyChartModule.php (revision 4ea6255165d5776629956d29cdf24b33b104cfa6)
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 DescendancyChartModule
42 */
43class DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
44{
45    use ModuleChartTrait;
46
47    private const ROUTE_NAME = 'descendancy-chart';
48    private const ROUTE_URL  = '/tree/{tree}/descendants-{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    public const    DEFAULT_STYLE       = self::CHART_STYLE_TREE;
57    public const    DEFAULT_GENERATIONS = '3';
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     * DescendancyChartModule 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('Descendants');
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 “DescendancyChart” module */
118        return I18N::translate('A chart of an individual’s descendants.');
119    }
120
121    /**
122     * CSS class for the URL.
123     *
124     * @return string
125     */
126    public function chartMenuClass(): string
127    {
128        return 'menu-chart-descendants';
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('Descendants 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                'tree' => $individual->tree()->name(),
168                'xref' => $individual->xref(),
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        $user        = $request->getAttribute('user');
183        $xref        = $request->getAttribute('xref');
184        $style       = $request->getAttribute('style');
185        $generations = (int) $request->getAttribute('generations');
186        $ajax        = $request->getQueryParams()['ajax'] ?? '';
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            switch ($style) {
209                case self::CHART_STYLE_TREE:
210                    return $this->viewResponse('modules/descendancy_chart/tree', [
211                        'individual'  => $individual,
212                        'generations' => $generations,
213                        'daboville'   => '1',
214                    ]);
215
216                case self::CHART_STYLE_INDIVIDUALS:
217                    return $this->viewResponse('lists/individuals-table', [
218                        'individuals' => $this->chart_service->descendants($individual, $generations - 1),
219                        'sosa'        => false,
220                        'tree'        => $tree,
221                    ]);
222
223                case self::CHART_STYLE_FAMILIES:
224                    $families = $this->chart_service->descendantFamilies($individual, $generations - 1);
225
226                    return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]);
227            }
228        }
229
230        $ajax_url = $this->chartUrl($individual, [
231            'generations' => $generations,
232            'style'       => $style,
233            'ajax'        => true,
234        ]);
235
236        return $this->viewResponse('modules/descendancy_chart/page', [
237            'ajax_url'            => $ajax_url,
238            'style'               => $style,
239            'styles'              => $this->styles(),
240            'default_generations' => self::DEFAULT_GENERATIONS,
241            'generations'         => $generations,
242            'individual'          => $individual,
243            'maximum_generations' => self::MAXIMUM_GENERATIONS,
244            'minimum_generations' => self::MINIMUM_GENERATIONS,
245            'module'              => $this->name(),
246            'title'               => $this->chartTitle($individual),
247            'tree'                => $tree,
248        ]);
249    }
250
251    /**
252     * This chart can display its output in a number of styles
253     *
254     * @return string[]
255     */
256    protected function styles(): array
257    {
258        return [
259            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
260            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
261            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
262        ];
263    }
264}
265