Issues (2564)

app/Module/DescendancyChartModule.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fig\Http\Message\RequestMethodInterface;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\Http\Middleware\AuthNotRobot;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Individual;
27
use Fisharebest\Webtrees\Menu;
28
use Fisharebest\Webtrees\Registry;
29
use Fisharebest\Webtrees\Services\ChartService;
30
use Fisharebest\Webtrees\Validator;
31
use Psr\Http\Message\ResponseInterface;
32
use Psr\Http\Message\ServerRequestInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
35
use function route;
36
37
class DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
38
{
39
    use ModuleChartTrait;
40
41
    protected const string ROUTE_URL = '/tree/{tree}/descendants-{style}-{generations}/{xref}';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 41 at column 27
Loading history...
42
43
    // Chart styles
44
    public const string CHART_STYLE_TREE        = 'tree';
45
    public const string CHART_STYLE_INDIVIDUALS = 'individuals';
46
    public const string CHART_STYLE_FAMILIES    = 'families';
47
48
    // Defaults
49
    public const    string DEFAULT_STYLE       = self::CHART_STYLE_TREE;
50
    public const    string DEFAULT_GENERATIONS = '3';
51
    protected const array DEFAULT_PARAMETERS  = [
52
        'generations' => self::DEFAULT_GENERATIONS,
53
        'style'       => self::DEFAULT_STYLE,
54
    ];
55
56
    // Limits
57
    protected const int MINIMUM_GENERATIONS = 2;
58
    protected const int MAXIMUM_GENERATIONS = 10;
59
60
    private ChartService $chart_service;
61
62
    /**
63
     * @param ChartService $chart_service
64
     */
65
    public function __construct(ChartService $chart_service)
66
    {
67
        $this->chart_service = $chart_service;
68
    }
69
70
    /**
71
     * Initialization.
72
     *
73
     * @return void
74
     */
75
    public function boot(): void
76
    {
77
        Registry::routeFactory()->routeMap()
78
            ->get(static::class, static::ROUTE_URL, $this)
79
            ->allows(RequestMethodInterface::METHOD_POST)
80
            ->extras(['middleware' => [AuthNotRobot::class]]);
81
    }
82
83
    public function title(): string
84
    {
85
        /* I18N: Name of a module/chart */
86
        return I18N::translate('Descendants');
87
    }
88
89
    public function description(): string
90
    {
91
        /* I18N: Description of the “DescendancyChart” module */
92
        return I18N::translate('A chart of an individual’s descendants.');
93
    }
94
95
    /**
96
     * CSS class for the URL.
97
     *
98
     * @return string
99
     */
100
    public function chartMenuClass(): string
101
    {
102
        return 'menu-chart-descendants';
103
    }
104
105
    /**
106
     * Return a menu item for this chart - for use in individual boxes.
107
     */
108
    public function chartBoxMenu(Individual $individual): Menu|null
109
    {
110
        return $this->chartMenu($individual);
111
    }
112
113
    /**
114
     * The title for a specific instance of this chart.
115
     *
116
     * @param Individual $individual
117
     *
118
     * @return string
119
     */
120
    public function chartTitle(Individual $individual): string
121
    {
122
        /* I18N: %s is an individual’s name */
123
        return I18N::translate('Descendants of %s', $individual->fullName());
124
    }
125
126
    /**
127
     * The URL for a page showing chart options.
128
     *
129
     * @param Individual                                $individual
130
     * @param array<bool|int|string|array<string>|null> $parameters
131
     *
132
     * @return string
133
     */
134
    public function chartUrl(Individual $individual, array $parameters = []): string
135
    {
136
        return route(static::class, [
137
                'tree' => $individual->tree()->name(),
138
                'xref' => $individual->xref(),
139
            ] + $parameters + self::DEFAULT_PARAMETERS);
140
    }
141
142
    /**
143
     * @param ServerRequestInterface $request
144
     *
145
     * @return ResponseInterface
146
     */
147
    public function handle(ServerRequestInterface $request): ResponseInterface
148
    {
149
        $tree        = Validator::attributes($request)->tree();
150
        $user        = Validator::attributes($request)->user();
151
        $xref        = Validator::attributes($request)->isXref()->string('xref');
152
        $style       = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style');
153
        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
154
        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
155
156
        // Convert POST requests into GET requests for pretty URLs.
157
        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
158
            return redirect(route(static::class, [
159
                'tree'        => $tree->name(),
160
                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
161
                'style'       => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'),
162
                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
163
            ]));
164
        }
165
166
        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
167
168
        $individual  = Registry::individualFactory()->make($xref, $tree);
169
        $individual  = Auth::checkIndividualAccess($individual, false, true);
170
171
        if ($ajax) {
172
            $this->layout = 'layouts/ajax';
173
174
            switch ($style) {
175
                case self::CHART_STYLE_TREE:
176
                    return $this->viewResponse('modules/descendancy_chart/tree', [
177
                        'individual'  => $individual,
178
                        'generations' => $generations,
179
                        'daboville'   => '1',
180
                    ]);
181
182
                case self::CHART_STYLE_INDIVIDUALS:
183
                    $individuals = $this->chart_service->descendants($individual, $generations - 1);
184
185
                    return $this->viewResponse('lists/individuals-table', [
186
                        'individuals' => $individuals,
187
                        'sosa'        => false,
188
                        'tree'        => $tree,
189
                    ]);
190
191
                case self::CHART_STYLE_FAMILIES:
192
                    $families = $this->chart_service->descendantFamilies($individual, $generations - 1);
193
194
                    return $this->viewResponse('lists/families-table', [
195
                        'families' => $families,
196
                        'tree'     => $tree,
197
                    ]);
198
            }
199
        }
200
201
        $ajax_url = $this->chartUrl($individual, [
202
            'generations' => $generations,
203
            'style'       => $style,
204
            'ajax'        => true,
205
        ]);
206
207
        return $this->viewResponse('modules/descendancy_chart/page', [
208
            'ajax_url'            => $ajax_url,
209
            'style'               => $style,
210
            'styles'              => $this->styles(),
211
            'default_generations' => self::DEFAULT_GENERATIONS,
212
            'generations'         => $generations,
213
            'individual'          => $individual,
214
            'maximum_generations' => self::MAXIMUM_GENERATIONS,
215
            'minimum_generations' => self::MINIMUM_GENERATIONS,
216
            'module'              => $this->name(),
217
            'title'               => $this->chartTitle($individual),
218
            'tree'                => $tree,
219
        ]);
220
    }
221
222
    /**
223
     * This chart can display its output in a number of styles
224
     *
225
     * @return array<string>
226
     */
227
    protected function styles(): array
228
    {
229
        return [
230
            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
231
            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
232
            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
233
        ];
234
    }
235
}
236