Issues (2503)

app/Module/CompactTreeChartModule.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 CompactTreeChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface
38
{
39
    use ModuleChartTrait;
40
41
    protected const string ROUTE_URL = '/tree/{tree}/compact/{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
    private ChartService $chart_service;
44
45
    /**
46
     * @param ChartService $chart_service
47
     */
48
    public function __construct(ChartService $chart_service)
49
    {
50
        $this->chart_service = $chart_service;
51
    }
52
53
    /**
54
     * Initialization.
55
     *
56
     * @return void
57
     */
58
    public function boot(): void
59
    {
60
        Registry::routeFactory()->routeMap()
61
            ->get(static::class, static::ROUTE_URL, $this)
62
            ->allows(RequestMethodInterface::METHOD_POST)
63
            ->extras(['middleware' => [AuthNotRobot::class]]);
64
    }
65
66
    public function title(): string
67
    {
68
        /* I18N: Name of a module/chart */
69
        return I18N::translate('Compact tree');
70
    }
71
72
    public function description(): string
73
    {
74
        /* I18N: Description of the “CompactTreeChart” module */
75
        return I18N::translate('A chart of an individual’s ancestors, as a compact tree.');
76
    }
77
78
    /**
79
     * CSS class for the URL.
80
     *
81
     * @return string
82
     */
83
    public function chartMenuClass(): string
84
    {
85
        return 'menu-chart-compact';
86
    }
87
88
    /**
89
     * Return a menu item for this chart - for use in individual boxes.
90
     */
91
    public function chartBoxMenu(Individual $individual): Menu|null
92
    {
93
        return $this->chartMenu($individual);
94
    }
95
96
    /**
97
     * The title for a specific instance of this chart.
98
     *
99
     * @param Individual $individual
100
     *
101
     * @return string
102
     */
103
    public function chartTitle(Individual $individual): string
104
    {
105
        /* I18N: %s is an individual’s name */
106
        return I18N::translate('Compact tree of %s', $individual->fullName());
107
    }
108
109
    /**
110
     * The URL for a page showing chart options.
111
     *
112
     * @param Individual                                $individual
113
     * @param array<bool|int|string|array<string>|null> $parameters
114
     *
115
     * @return string
116
     */
117
    public function chartUrl(Individual $individual, array $parameters = []): string
118
    {
119
        return route(static::class, [
120
                'xref' => $individual->xref(),
121
                'tree' => $individual->tree()->name(),
122
            ] + $parameters);
123
    }
124
125
    /**
126
     * @param ServerRequestInterface $request
127
     *
128
     * @return ResponseInterface
129
     */
130
    public function handle(ServerRequestInterface $request): ResponseInterface
131
    {
132
        $tree = Validator::attributes($request)->tree();
133
        $user = Validator::attributes($request)->user();
134
        $xref = Validator::attributes($request)->isXref()->string('xref');
135
        $ajax = Validator::queryParams($request)->boolean('ajax', false);
136
137
        // Convert POST requests into GET requests for pretty URLs.
138
        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
139
            return redirect(route(static::class, [
140
                'tree' => $tree->name(),
141
                'xref' => Validator::parsedBody($request)->string('xref', ''),
142
            ]));
143
        }
144
145
        Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
146
147
        $individual = Registry::individualFactory()->make($xref, $tree);
148
        $individual = Auth::checkIndividualAccess($individual, false, true);
149
150
        if ($ajax) {
151
            $this->layout = 'layouts/ajax';
152
153
            return $this->viewResponse('modules/compact-chart/chart', [
154
                'ancestors' => $this->chart_service->sosaStradonitzAncestors($individual, 5),
155
                'module'    => $this,
156
            ]);
157
        }
158
159
        $ajax_url = $this->chartUrl($individual, [
160
            'ajax' => true,
161
        ]);
162
163
        return $this->viewResponse('modules/compact-chart/page', [
164
            'ajax_url'   => $ajax_url,
165
            'individual' => $individual,
166
            'module'     => $this->name(),
167
            'title'      => $this->chartTitle($individual),
168
            'tree'       => $tree,
169
        ]);
170
    }
171
}
172