Issues (2559)

app/Module/AncestorsChartModule.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 AncestorsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
38
{
39
    use ModuleChartTrait;
40
41
    protected const string ROUTE_URL = '/tree/{tree}/ancestors-{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_GENERATIONS = '4';
50
    public const string DEFAULT_STYLE       = self::CHART_STYLE_TREE;
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
            ->tokens([
82
                'generations' => '\d+',
83
                'style'       => implode('|', array_keys($this->styles())),
84
            ]);
85
    }
86
87
    public function title(): string
88
    {
89
        /* I18N: Name of a module/chart */
90
        return I18N::translate('Ancestors');
91
    }
92
93
    public function description(): string
94
    {
95
        /* I18N: Description of the “AncestorsChart” module */
96
        return I18N::translate('A chart of an individual’s ancestors.');
97
    }
98
99
    /**
100
     * CSS class for the URL.
101
     *
102
     * @return string
103
     */
104
    public function chartMenuClass(): string
105
    {
106
        return 'menu-chart-ancestry';
107
    }
108
109
    /**
110
     * Return a menu item for this chart - for use in individual boxes.
111
     */
112
    public function chartBoxMenu(Individual $individual): Menu|null
113
    {
114
        return $this->chartMenu($individual);
115
    }
116
117
    /**
118
     * The title for a specific instance of this chart.
119
     *
120
     * @param Individual $individual
121
     *
122
     * @return string
123
     */
124
    public function chartTitle(Individual $individual): string
125
    {
126
        /* I18N: %s is an individual’s name */
127
        return I18N::translate('Ancestors of %s', $individual->fullName());
128
    }
129
130
    /**
131
     * The URL for a page showing chart options.
132
     *
133
     * @param Individual                                $individual
134
     * @param array<bool|int|string|array<string>|null> $parameters
135
     *
136
     * @return string
137
     */
138
    public function chartUrl(Individual $individual, array $parameters = []): string
139
    {
140
        return route(static::class, [
141
                'xref' => $individual->xref(),
142
                'tree' => $individual->tree()->name(),
143
            ] + $parameters + self::DEFAULT_PARAMETERS);
144
    }
145
146
    /**
147
     * @param ServerRequestInterface $request
148
     *
149
     * @return ResponseInterface
150
     */
151
    public function handle(ServerRequestInterface $request): ResponseInterface
152
    {
153
        $tree        = Validator::attributes($request)->tree();
154
        $user        = Validator::attributes($request)->user();
155
        $style       = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style');
156
        $xref        = Validator::attributes($request)->isXref()->string('xref');
157
        $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations');
158
        $ajax        = Validator::queryParams($request)->boolean('ajax', false);
159
160
        // Convert POST requests into GET requests for pretty URLs.
161
        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
162
            return redirect(route(static::class, [
163
                'tree'        => $tree->name(),
164
                'xref'        => Validator::parsedBody($request)->isXref()->string('xref'),
165
                'style'       => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'),
166
                'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'),
167
            ]));
168
        }
169
170
        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
171
172
        $individual  = Registry::individualFactory()->make($xref, $tree);
173
        $individual  = Auth::checkIndividualAccess($individual, false, true);
174
175
        if ($ajax) {
176
            $this->layout = 'layouts/ajax';
177
178
            $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations);
179
180
            switch ($style) {
181
                default:
182
                case self::CHART_STYLE_TREE:
183
                    return $this->viewResponse('modules/ancestors-chart/tree', [
184
                        'individual'  => $individual,
185
                        'parents'     => $individual->childFamilies()->first(),
186
                        'generations' => $generations,
187
                        'sosa'        => 1,
188
                    ]);
189
190
                case self::CHART_STYLE_INDIVIDUALS:
191
                    return $this->viewResponse('lists/individuals-table', [
192
                        'individuals' => $ancestors,
193
                        'sosa'        => true,
194
                        'tree'        => $tree,
195
                    ]);
196
197
                case self::CHART_STYLE_FAMILIES:
198
                    $families = [];
199
200
                    foreach ($ancestors as $ancestor) {
201
                        foreach ($ancestor->childFamilies() as $family) {
202
                            $families[$family->xref()] = $family;
203
                        }
204
                    }
205
206
                    return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]);
207
            }
208
        }
209
210
        $ajax_url = $this->chartUrl($individual, [
211
            'ajax'        => true,
212
            'generations' => $generations,
213
            'style'       => $style,
214
            'xref'        => $xref,
215
        ]);
216
217
        return $this->viewResponse('modules/ancestors-chart/page', [
218
            'ajax_url'            => $ajax_url,
219
            'generations'         => $generations,
220
            'individual'          => $individual,
221
            'maximum_generations' => self::MAXIMUM_GENERATIONS,
222
            'minimum_generations' => self::MINIMUM_GENERATIONS,
223
            'module'              => $this->name(),
224
            'style'               => $style,
225
            'styles'              => $this->styles(),
226
            'title'               => $this->chartTitle($individual),
227
            'tree'                => $tree,
228
        ]);
229
    }
230
231
    /**
232
     * This chart can display its output in a number of styles
233
     *
234
     * @return array<string>
235
     */
236
    protected function styles(): array
237
    {
238
        return [
239
            self::CHART_STYLE_TREE        => I18N::translate('Tree'),
240
            self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'),
241
            self::CHART_STYLE_FAMILIES    => I18N::translate('Families'),
242
        ];
243
    }
244
}
245