1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\Controllers;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Soundex;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Find all branches of families with a given surname.
*/
class BranchesController extends AbstractBaseController
{
/**
* A form to request the page parameters.
*
* @param Request $request
*
* @return Response
*/
public function page(Request $request): Response
{
$surname = $request->get('surname', '');
$soundex_std = (bool) $request->get('soundex_std');
$soundex_dm = (bool) $request->get('soundex_dm');
if ($surname !== '') {
/* I18N: %s is a surname */
$title = I18N::translate('Branches of the %s family', e($surname));
} else {
/* I18N: Branches of a family tree */
$title = I18N::translate('Branches');
}
return $this->viewResponse('branches-page', [
'soundex_dm' => $soundex_dm,
'soundex_std' => $soundex_std,
'surname' => $surname,
'title' => $title,
]);
}
/**
* @param Request $request
* @param Tree $tree
* @param User $user
*
* @return Response
*/
public function list(Request $request, Tree $tree, User $user): Response
{
$soundex_dm = (bool) $request->get('soundex_dm');
$soundex_std = (bool) $request->get('soundex_std');
$surname = $request->get('surname', '');
// Highlight direct-line ancestors of this individual.
$self = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree);
if ($surname !== '') {
$individuals = $this->loadIndividuals($tree, $surname, $soundex_dm, $soundex_std);
} else {
$individuals = [];
}
if ($self !== null) {
$ancestors = $this->allAncestors($self);
} else {
$ancestors = [];
}
// @TODO - convert this to use views
$html = view('branches-list', [
'branches' => $this->getPatriarchsHtml($individuals, $ancestors, $surname, $soundex_dm, $soundex_std),
]);
return new Response($html);
}
/**
* Find all ancestors of an individual, indexed by the Sosa-Stradonitz number.
*
* @param Individual $individual
*
* @return Individual[]
*/
protected function allAncestors(Individual $individual): array
{
/** @var Individual[] $ancestors */
$ancestors = [
1 => $individual,
];
do {
$sosa = key($ancestors);
$family = $ancestors[$sosa]->getPrimaryChildFamily();
if ($family !== null) {
if ($family->getHusband() !== null) {
$ancestors[$sosa * 2] = $family->getHusband();
}
if ($family->getWife() !== null) {
$ancestors[$sosa * 2 + 1] = $family->getWife();
}
}
} while (next($ancestors));
return $ancestors;
}
/**
* Fetch all individuals with a matching surname
*
* @param Tree $tree
* @param string $surname
* @param bool $soundex_dm
* @param bool $soundex_std
*
* @return Individual[]
*/
private function loadIndividuals(Tree $tree, string $surname, bool $soundex_dm, bool $soundex_std): array
{
$sql =
"SELECT DISTINCT i_id AS xref, i_gedcom AS gedcom" .
" FROM `##individuals`" .
" JOIN `##name` ON (i_id=n_id AND i_file=n_file)" .
" WHERE n_file = ?" .
" AND n_type != ?" .
" AND (n_surn = ? OR n_surname = ?";
$args = [
$tree->id(),
'_MARNM',
$surname,
$surname,
];
if ($soundex_std) {
$sdx = Soundex::russell($surname);
if ($sdx !== '') {
foreach (explode(':', $sdx) as $value) {
$sql .= " OR n_soundex_surn_std LIKE CONCAT('%', ?, '%')";
$args[] = $value;
}
}
}
if ($soundex_dm) {
$sdx = Soundex::daitchMokotoff($surname);
if ($sdx !== '') {
foreach (explode(':', $sdx) as $value) {
$sql .= " OR n_soundex_surn_dm LIKE CONCAT('%', ?, '%')";
$args[] = $value;
}
}
}
$sql .= ')';
$rows = Database::prepare($sql)->execute($args)->fetchAll();
$individuals = [];
foreach ($rows as $row) {
$individuals[] = Individual::getInstance($row->xref, $tree, $row->gedcom);
}
usort($individuals, '\Fisharebest\Webtrees\Individual::compareBirthDate');
return $individuals;
}
/**
* For each individual with no ancestors, list their descendants.
*
* @param Individual[] $individuals
* @param Individual[] $ancestors
* @param string $surname
* @param bool $soundex_dm
* @param bool $soundex_std
*
* @return string
*/
public function getPatriarchsHtml(array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std): string
{
$html = '';
foreach ($individuals as $individual) {
foreach ($individual->getChildFamilies() as $family) {
foreach ($family->getSpouses() as $parent) {
if (in_array($parent, $individuals, true)) {
continue 3;
}
}
}
$html .= $this->getDescendantsHtml($individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $individual, null);
}
return $html;
}
/**
* Generate a recursive list of descendants of an individual.
* If parents are specified, we can also show the pedigree (adopted, etc.).
*
* @param Individual[] $individuals
* @param Individual[] $ancestors
* @param string $surname
* @param bool $soundex_dm
* @param bool $soundex_std
* @param Individual $individual
* @param Family|null $parents
*
* @return string
*/
private function getDescendantsHtml(array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std, Individual $individual, Family $parents = null)
{
// A person has many names. Select the one that matches the searched surname
$person_name = '';
foreach ($individual->getAllNames() as $name) {
list($surn1) = explode(',', $name['sort']);
if (// one name is a substring of the other
stripos($surn1, $surname) !== false ||
stripos($surname, $surn1) !== false ||
// one name sounds like the other
$soundex_std && Soundex::compare(Soundex::russell($surn1), Soundex::russell($surname)) ||
$soundex_dm && Soundex::compare(Soundex::daitchMokotoff($surn1), Soundex::daitchMokotoff($surname))
) {
$person_name = $name['full'];
break;
}
}
// No matching name? Typically children with a different surname. The branch stops here.
if (!$person_name) {
return '<li title="' . strip_tags($individual->getFullName()) . '">' . $individual->getSexImage() . '…</li>';
}
// Is this individual one of our ancestors?
$sosa = array_search($individual, $ancestors, true);
if (is_int($sosa)) {
$sosa_class = 'search_hit';
$sosa_html = ' <a class="details1 ' . $individual->getBoxStyle() . '" title="' . I18N::translate('Sosa') . '" href="' . e(route('relationships', [
'xref1' => $individual->xref(),
'xref2' => $ancestors[1]->xref(),
'ged' => $individual->tree()->name(),
])) . '" rel="nofollow">' . $sosa . '</a>' . self::sosaGeneration($sosa);
} else {
$sosa_class = '';
$sosa_html = '';
}
// Generate HTML for this individual, and all their descendants
$indi_html = $individual->getSexImage() . '<a class="' . $sosa_class . '" href="' . e($individual->url()) . '">' . $person_name . '</a> ' . $individual->getLifeSpan() . $sosa_html;
// If this is not a birth pedigree (e.g. an adoption), highlight it
if ($parents) {
$pedi = '';
foreach ($individual->facts('FAMC') as $fact) {
if ($fact->target() === $parents) {
$pedi = $fact->attribute('PEDI');
break;
}
}
if ($pedi !== '' && $pedi !== 'birth') {
$indi_html = '<span class="red">' . GedcomCodePedi::getValue($pedi, $individual) . '</span> ' . $indi_html;
}
}
// spouses and children
$spouse_families = $individual->getSpouseFamilies();
if ($spouse_families) {
usort($spouse_families, '\Fisharebest\Webtrees\Family::compareMarrDate');
$fam_html = '';
foreach ($spouse_families as $family) {
$fam_html .= $indi_html; // Repeat the individual details for each spouse.
$spouse = $family->getSpouse($individual);
if ($spouse instanceof Individual) {
$sosa = array_search($spouse, $ancestors, true);
if (is_int($sosa)) {
$sosa_class = 'search_hit';
$sosa_html = ' <a class="details1 ' . $spouse->getBoxStyle() . '" title="' . I18N::translate('Sosa') . '" href="' . e(route('relationships', [
'xref2' => $ancestors[1]->xref(),
'ged' => $individual->tree()->name(),
])) . '" rel="nofollow"> ' . $sosa . ' </a>' . self::sosaGeneration($sosa);
} else {
$sosa_class = '';
$sosa_html = '';
}
$marriage_year = $family->getMarriageYear();
if ($marriage_year) {
$fam_html .= ' <a href="' . e($family->url()) . '" title="' . strip_tags($family->getMarriageDate()->display()) . '"><i class="icon-rings"></i>' . $marriage_year . '</a>';
} elseif ($family->getFirstFact('MARR')) {
$fam_html .= ' <a href="' . e($family->url()) . '" title="' . I18N::translate('Marriage') . '"><i class="icon-rings"></i></a>';
} else {
$fam_html .= ' <a href="' . e($family->url()) . '" title="' . I18N::translate('Not married') . '"><i class="icon-rings"></i></a>';
}
$fam_html .= ' ' . $spouse->getSexImage() . '<a class="' . $sosa_class . '" href="' . e($spouse->url()) . '">' . $spouse->getFullName() . '</a> ' . $spouse->getLifeSpan() . ' ' . $sosa_html;
}
$fam_html .= '<ol>';
foreach ($family->getChildren() as $child) {
$fam_html .= $this->getDescendantsHtml($individuals, $ancestors, $surname, $soundex_dm, $soundex_std, $child, $family);
}
$fam_html .= '</ol>';
}
return '<li>' . $fam_html . '</li>';
}
// No spouses - just show the individual
return '<li>' . $indi_html . '</li>';
}
/**
* Convert a SOSA number into a generation number. e.g. 8 = great-grandfather = 3 generations
*
* @param int $sosa
*
* @return string
*/
private static function sosaGeneration($sosa): string
{
$generation = (int) log($sosa, 2) + 1;
return '<sup title="' . I18N::translate('Generation') . '">' . $generation . '</sup>';
}
}
|