Issues (2559)

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