1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2021 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 Fisharebest\Webtrees\Census\CensusInterface; |
23
|
|
|
use Fisharebest\Webtrees\Functions\Functions; |
24
|
|
|
use Fisharebest\Webtrees\Registry; |
25
|
|
|
use Fisharebest\Webtrees\I18N; |
26
|
|
|
use Fisharebest\Webtrees\Individual; |
27
|
|
|
use Fisharebest\Webtrees\Services\TreeService; |
28
|
|
|
use Fisharebest\Webtrees\Tree; |
29
|
|
|
use Illuminate\Support\Collection; |
30
|
|
|
use Psr\Http\Message\ResponseInterface; |
31
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
32
|
|
|
|
33
|
|
|
use function array_keys; |
34
|
|
|
use function assert; |
35
|
|
|
use function count; |
36
|
|
|
use function e; |
37
|
|
|
use function response; |
38
|
|
|
use function str_repeat; |
39
|
|
|
use function str_replace; |
40
|
|
|
use function strip_tags; |
41
|
|
|
use function view; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class CensusAssistantModule |
45
|
|
|
*/ |
46
|
|
|
class CensusAssistantModule extends AbstractModule |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* How should this module be identified in the control panel, etc.? |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function title(): string |
54
|
|
|
{ |
55
|
|
|
/* I18N: Name of a module */ |
56
|
|
|
return I18N::translate('Census assistant'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* A sentence describing what this module does. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function description(): string |
65
|
|
|
{ |
66
|
|
|
/* I18N: Description of the “Census assistant” module */ |
67
|
|
|
return I18N::translate('An alternative way to enter census transcripts and link them to individuals.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ServerRequestInterface $request |
72
|
|
|
* |
73
|
|
|
* @return ResponseInterface |
74
|
|
|
*/ |
75
|
|
|
public function postCensusInitializeAction(ServerRequestInterface $request): ResponseInterface |
76
|
|
|
{ |
77
|
|
|
$treeService = new TreeService(); |
78
|
|
|
$params = (array) $request->getParsedBody(); |
79
|
|
|
|
80
|
|
|
$censusClass = $params['census']; |
81
|
|
|
$xref = $params['xref']; |
82
|
|
|
$tree_id = (int) $params['tree_id']; |
83
|
|
|
$census = new $censusClass(); |
84
|
|
|
$individual = Registry::individualFactory()->make($xref, $treeService->find($tree_id)); |
85
|
|
|
|
86
|
|
|
assert($individual instanceof Individual); |
87
|
|
|
|
88
|
|
|
$data = json_encode([ |
89
|
|
|
'header' => $this->censusTableHeader($census), |
90
|
|
|
'family' => $this->familyMembers($individual, $census), |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
return response($data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ServerRequestInterface $request |
98
|
|
|
* |
99
|
|
|
* @return ResponseInterface |
100
|
|
|
*/ |
101
|
|
|
public function postCensusIndividualAction(ServerRequestInterface $request): ResponseInterface |
102
|
|
|
{ |
103
|
|
|
$tree = $request->getAttribute('tree'); |
104
|
|
|
assert($tree instanceof Tree); |
105
|
|
|
|
106
|
|
|
$params = (array) $request->getParsedBody(); |
107
|
|
|
$individual = Registry::individualFactory()->make($params['xref'], $tree); |
108
|
|
|
$head = Registry::individualFactory()->make($params['head'], $tree); |
109
|
|
|
$census_class = $params['census']; |
110
|
|
|
$census = new $census_class(); |
111
|
|
|
|
112
|
|
|
// No head of household? Create a fake one. |
113
|
|
|
$head = $head ?? Registry::individualFactory()->new('X', '0 @X@ INDI', null, $tree); |
114
|
|
|
|
115
|
|
|
// Generate columns (e.g. relationship name) using the correct language. |
116
|
|
|
I18N::init($census->censusLanguage()); |
117
|
|
|
|
118
|
|
|
if ($individual instanceof Individual && $head instanceof Individual) { |
119
|
|
|
$html = $this->censusTableRow($census, $individual, $head); |
120
|
|
|
} else { |
121
|
|
|
$html = $this->censusTableEmptyRow($census); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return response($html); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Individual $individual |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function createCensusAssistant(Individual $individual): string |
133
|
|
|
{ |
134
|
|
|
return view('modules/census-assistant', [ |
135
|
|
|
'individual' => $individual, |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param ServerRequestInterface $request |
141
|
|
|
* @param Individual $individual |
142
|
|
|
* @param string $fact_id |
143
|
|
|
* @param string $newged |
144
|
|
|
* @param bool $keep_chan |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function updateCensusAssistant(ServerRequestInterface $request, Individual $individual, string $fact_id, string $newged, bool $keep_chan): string |
149
|
|
|
{ |
150
|
|
|
$params = (array) $request->getParsedBody(); |
151
|
|
|
|
152
|
|
|
$ca_title = $params['ca_title'] ?? ''; |
153
|
|
|
$ca_place = $params['ca_place'] ?? ''; |
154
|
|
|
$ca_citation = $params['ca_citation'] ?? ''; |
155
|
|
|
$ca_individuals = $params['ca_individuals'] ?? []; |
156
|
|
|
$ca_notes = $params['ca_notes'] ?? ''; |
157
|
|
|
$ca_census = $params['ca_census'] ?? ''; |
158
|
|
|
|
159
|
|
|
if ($ca_census !== '' && $ca_individuals !== []) { |
160
|
|
|
$census = new $ca_census(); |
161
|
|
|
|
162
|
|
|
$note_text = $this->createNoteText($census, $ca_title, $ca_place, $ca_citation, $ca_individuals, $ca_notes); |
163
|
|
|
$note_gedcom = '0 @@ NOTE ' . str_replace("\n", "\n1 CONT ", $note_text); |
164
|
|
|
$note = $individual->tree()->createRecord($note_gedcom); |
165
|
|
|
|
166
|
|
|
$newged .= "\n2 NOTE @" . $note->xref() . '@'; |
167
|
|
|
|
168
|
|
|
// Add the census fact to the rest of the household |
169
|
|
|
foreach ($ca_individuals['xref'] ?? [] as $xref) { |
170
|
|
|
if ($xref !== '' && $xref !== $individual->xref()) { |
171
|
|
|
Registry::individualFactory()->make($xref, $individual->tree()) |
172
|
|
|
->updateFact($fact_id, $newged, !$keep_chan); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $newged; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param CensusInterface $census |
182
|
|
|
* @param string $ca_title |
183
|
|
|
* @param string $ca_place |
184
|
|
|
* @param string $ca_citation |
185
|
|
|
* @param string[][] $ca_individuals |
186
|
|
|
* @param string $ca_notes |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
private function createNoteText(CensusInterface $census, string $ca_title, string $ca_place, string $ca_citation, array $ca_individuals, string $ca_notes): string |
191
|
|
|
{ |
192
|
|
|
$text = $ca_title . "\n" . $ca_citation . "\n" . $ca_place . "\n\n|"; |
193
|
|
|
|
194
|
|
|
foreach ($census->columns() as $column) { |
195
|
|
|
$text .= ' ' . $column->abbreviation() . ' |'; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$text .= "\n|" . str_repeat(' ----- |', count($census->columns())); |
199
|
|
|
|
200
|
|
|
foreach (array_keys($ca_individuals['xref'] ?? []) as $key) { |
201
|
|
|
$text .= "\n|"; |
202
|
|
|
|
203
|
|
|
foreach ($census->columns() as $n => $column) { |
204
|
|
|
$text .= ' ' . $ca_individuals[$n][$key] . ' |'; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $text . "\n\n" . strtr($ca_notes, ["\r" => '']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Generate an HTML row of data for the census header |
213
|
|
|
* Add prefix cell (store XREF and drag/drop) |
214
|
|
|
* Add suffix cell (delete button) |
215
|
|
|
* |
216
|
|
|
* @param CensusInterface $census |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
protected function censusTableHeader(CensusInterface $census): string |
221
|
|
|
{ |
222
|
|
|
$html = ''; |
223
|
|
|
foreach ($census->columns() as $column) { |
224
|
|
|
$html .= '<th class="wt-census-assistant-field" title="' . $column->title() . '">' . $column->abbreviation() . '</th>'; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return '<tr class="wt-census-assistant-row"><th hidden></th>' . $html . '<th></th></tr>'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Generate an HTML row of data for the census |
232
|
|
|
* Add prefix cell (store XREF and drag/drop) |
233
|
|
|
* Add suffix cell (delete button) |
234
|
|
|
* |
235
|
|
|
* @param CensusInterface $census |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function censusTableEmptyRow(CensusInterface $census): string |
240
|
|
|
{ |
241
|
|
|
$html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]"></td>'; |
242
|
|
|
|
243
|
|
|
foreach ($census->columns() as $n => $column) { |
244
|
|
|
$html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" name="ca_individuals[' . $n . '][]"></td>'; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; |
248
|
|
|
|
249
|
|
|
return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Generate an HTML row of data for the census |
254
|
|
|
* Add prefix cell (store XREF and drag/drop) |
255
|
|
|
* Add suffix cell (delete button) |
256
|
|
|
* |
257
|
|
|
* @param CensusInterface $census |
258
|
|
|
* @param Individual $individual |
259
|
|
|
* @param Individual $head |
260
|
|
|
* |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
public function censusTableRow(CensusInterface $census, Individual $individual, Individual $head): string |
264
|
|
|
{ |
265
|
|
|
$html = '<td class="wt-census-assistant-field" hidden><input type="hidden" name="ca_individuals[xref][]" value="' . e($individual->xref()) . '"></td>'; |
266
|
|
|
|
267
|
|
|
foreach ($census->columns() as $n => $column) { |
268
|
|
|
$html .= '<td class="wt-census-assistant-field p-0"><input class="form-control wt-census-assistant-form-control p-0" type="text" value="' . $column->generate($individual, $head) . '" name="ca_individuals[' . $n . '][]"></td>'; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$html .= '<td class="wt-census-assistant-field"><a href="#" title="' . I18N::translate('Remove') . '">' . view('icons/delete') . '</a></td>'; |
272
|
|
|
|
273
|
|
|
return '<tr class="wt-census-assistant-row">' . $html . '</tr>'; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Produce a list of close family members |
278
|
|
|
* for quick selection |
279
|
|
|
* |
280
|
|
|
* @param Individual $individual |
281
|
|
|
* @param CensusInterface $census |
282
|
|
|
* |
283
|
|
|
* @return array<int, array<string, mixed>> |
284
|
|
|
*/ |
285
|
|
|
private function familyMembers(Individual $individual, CensusInterface $census): array |
286
|
|
|
{ |
287
|
|
|
$max_age = (int) $individual->tree()->getPreference('MAX_ALIVE_AGE'); |
288
|
|
|
$censusYear = (int) substr($census->censusDate(), -4); |
289
|
|
|
$options = []; |
290
|
|
|
$individuals = new Collection(); |
291
|
|
|
$families = $individual->childFamilies() |
292
|
|
|
->merge($individual->childStepFamilies()) |
293
|
|
|
->merge($individual->spouseFamilies()) |
294
|
|
|
->merge($individual->spouseStepFamilies()); |
295
|
|
|
|
296
|
|
|
$families->each(function ($family) use (&$individuals) { |
297
|
|
|
$individuals = $individuals |
298
|
|
|
->merge($family->spouses()) |
299
|
|
|
->merge($family->children()); |
300
|
|
|
}); |
301
|
|
|
|
302
|
|
|
$individuals->unique()->each(function ($indi) use (&$options, $individual, $censusYear, $max_age) { |
303
|
|
|
$birth_year = (int) $indi->getBirthDate()->minimumDate()->format('%Y') ?: 0; |
304
|
|
|
$death_year = (int) $indi->getDeathDate()->maximumDate()->format('%Y') ?: ($birth_year > 0 ? $birth_year + $max_age : PHP_INT_MAX); |
305
|
|
|
$options[] = [ |
306
|
|
|
'xref' => $indi->xref(), |
307
|
|
|
'text' => sprintf("%s (%s)", strip_tags($indi->fullName()), strip_tags($indi->lifespan())), |
308
|
|
|
'relationship' => Functions::getCloseRelationshipName($individual, $indi), |
309
|
|
|
'disabled' => ($censusYear < $birth_year) || ($censusYear > $death_year), |
310
|
|
|
]; |
311
|
|
|
}); |
312
|
|
|
|
313
|
|
|
return $options; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|