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\Validator; |
||
30 | use Psr\Http\Message\ResponseInterface; |
||
31 | use Psr\Http\Message\ServerRequestInterface; |
||
32 | use Psr\Http\Server\RequestHandlerInterface; |
||
33 | |||
34 | use function route; |
||
35 | |||
36 | class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface |
||
37 | { |
||
38 | use ModuleChartTrait; |
||
39 | |||
40 | protected const string ROUTE_URL = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}'; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
41 | |||
42 | // Defaults |
||
43 | public const string DEFAULT_GENERATIONS = '2'; |
||
44 | public const string DEFAULT_DESCENDANT_GENERATIONS = '5'; |
||
45 | protected const array DEFAULT_PARAMETERS = [ |
||
46 | 'book_size' => self::DEFAULT_GENERATIONS, |
||
47 | 'generations' => self::DEFAULT_DESCENDANT_GENERATIONS, |
||
48 | 'spouses' => false, |
||
49 | ]; |
||
50 | |||
51 | // Limits |
||
52 | protected const int MINIMUM_BOOK_SIZE = 2; |
||
53 | protected const int MAXIMUM_BOOK_SIZE = 5; |
||
54 | |||
55 | protected const int MINIMUM_GENERATIONS = 2; |
||
56 | protected const int MAXIMUM_GENERATIONS = 10; |
||
57 | |||
58 | /** |
||
59 | * Initialization. |
||
60 | * |
||
61 | * @return void |
||
62 | */ |
||
63 | public function boot(): void |
||
64 | { |
||
65 | Registry::routeFactory()->routeMap() |
||
66 | ->get(static::class, static::ROUTE_URL, $this) |
||
67 | ->allows(RequestMethodInterface::METHOD_POST) |
||
68 | ->extras(['middleware' => [AuthNotRobot::class]]); |
||
69 | } |
||
70 | |||
71 | public function title(): string |
||
72 | { |
||
73 | /* I18N: Name of a module/chart */ |
||
74 | return I18N::translate('Family book'); |
||
75 | } |
||
76 | |||
77 | public function description(): string |
||
78 | { |
||
79 | /* I18N: Description of the “FamilyBookChart” module */ |
||
80 | return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.'); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * CSS class for the URL. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function chartMenuClass(): string |
||
89 | { |
||
90 | return 'menu-chart-familybook'; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Return a menu item for this chart - for use in individual boxes. |
||
95 | */ |
||
96 | public function chartBoxMenu(Individual $individual): Menu|null |
||
97 | { |
||
98 | return $this->chartMenu($individual); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * The title for a specific instance of this chart. |
||
103 | * |
||
104 | * @param Individual $individual |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function chartTitle(Individual $individual): string |
||
109 | { |
||
110 | /* I18N: %s is an individual’s name */ |
||
111 | return I18N::translate('Family book of %s', $individual->fullName()); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * The URL for a page showing chart options. |
||
116 | * |
||
117 | * @param Individual $individual |
||
118 | * @param array<bool|int|string|array<string>|null> $parameters |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function chartUrl(Individual $individual, array $parameters = []): string |
||
123 | { |
||
124 | return route(static::class, [ |
||
125 | 'xref' => $individual->xref(), |
||
126 | 'tree' => $individual->tree()->name(), |
||
127 | ] + $parameters + self::DEFAULT_PARAMETERS); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param ServerRequestInterface $request |
||
132 | * |
||
133 | * @return ResponseInterface |
||
134 | */ |
||
135 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
136 | { |
||
137 | $tree = Validator::attributes($request)->tree(); |
||
138 | $user = Validator::attributes($request)->user(); |
||
139 | $xref = Validator::attributes($request)->isXref()->string('xref'); |
||
140 | $book_size = Validator::attributes($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'); |
||
141 | $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); |
||
142 | $spouses = Validator::attributes($request)->boolean('spouses', false); |
||
143 | $ajax = Validator::queryParams($request)->boolean('ajax', false); |
||
144 | |||
145 | // Convert POST requests into GET requests for pretty URLs. |
||
146 | if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
||
147 | return redirect(route(static::class, [ |
||
148 | 'tree' => $tree->name(), |
||
149 | 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), |
||
150 | 'book_size' => Validator::parsedBody($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'), |
||
151 | 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), |
||
152 | 'spouses' => Validator::parsedBody($request)->boolean('spouses', false), |
||
153 | ])); |
||
154 | } |
||
155 | |||
156 | Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); |
||
157 | |||
158 | $individual = Registry::individualFactory()->make($xref, $tree); |
||
159 | $individual = Auth::checkIndividualAccess($individual, false, true); |
||
160 | |||
161 | if ($ajax) { |
||
162 | $this->layout = 'layouts/ajax'; |
||
163 | |||
164 | return $this->viewResponse('modules/family-book-chart/chart', [ |
||
165 | 'individual' => $individual, |
||
166 | 'generations' => $generations, |
||
167 | 'book_size' => $book_size, |
||
168 | 'spouses' => $spouses, |
||
169 | ]); |
||
170 | } |
||
171 | |||
172 | $ajax_url = $this->chartUrl($individual, [ |
||
173 | 'ajax' => true, |
||
174 | 'book_size' => $book_size, |
||
175 | 'generations' => $generations, |
||
176 | 'spouses' => $spouses, |
||
177 | ]); |
||
178 | |||
179 | return $this->viewResponse('modules/family-book-chart/page', [ |
||
180 | 'ajax_url' => $ajax_url, |
||
181 | 'book_size' => $book_size, |
||
182 | 'generations' => $generations, |
||
183 | 'individual' => $individual, |
||
184 | 'maximum_book_size' => self::MAXIMUM_BOOK_SIZE, |
||
185 | 'minimum_book_size' => self::MINIMUM_BOOK_SIZE, |
||
186 | 'maximum_generations' => self::MAXIMUM_GENERATIONS, |
||
187 | 'minimum_generations' => self::MINIMUM_GENERATIONS, |
||
188 | 'module' => $this->name(), |
||
189 | 'spouses' => $spouses, |
||
190 | 'title' => $this->chartTitle($individual), |
||
191 | 'tree' => $tree, |
||
192 | ]); |
||
193 | } |
||
194 | } |
||
195 |