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