1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\DB; 25use Fisharebest\Webtrees\Family; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Registry; 29use Fisharebest\Webtrees\Session; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\Validator; 32use Illuminate\Database\Query\Builder; 33use Illuminate\Database\Query\Expression; 34use Illuminate\Database\Query\JoinClause; 35use Illuminate\Support\Collection; 36use Psr\Http\Message\ResponseInterface; 37use Psr\Http\Message\ServerRequestInterface; 38use Psr\Http\Server\RequestHandlerInterface; 39 40use function array_filter; 41use function array_keys; 42use function array_map; 43use function array_merge; 44use function array_sum; 45use function array_values; 46use function assert; 47use function e; 48use function implode; 49use function ob_get_clean; 50use function ob_start; 51use function route; 52use function uksort; 53use function usort; 54use function view; 55 56use const ARRAY_FILTER_USE_KEY; 57 58/** 59 * Class IndividualListModule 60 */ 61class IndividualListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface 62{ 63 use ModuleListTrait; 64 65 protected const ROUTE_URL = '/tree/{tree}/individual-list'; 66 67 // The individual list and family list use the same code/logic. 68 // They just display different lists. 69 protected bool $families = false; 70 71 /** 72 * Initialization. 73 * 74 * @return void 75 */ 76 public function boot(): void 77 { 78 Registry::routeFactory()->routeMap() 79 ->get(static::class, static::ROUTE_URL, $this); 80 } 81 82 /** 83 * How should this module be identified in the control panel, etc.? 84 * 85 * @return string 86 */ 87 public function title(): string 88 { 89 /* I18N: Name of a module/list */ 90 return I18N::translate('Individuals'); 91 } 92 93 /** 94 * A sentence describing what this module does. 95 * 96 * @return string 97 */ 98 public function description(): string 99 { 100 /* I18N: Description of the “Individuals” module */ 101 return I18N::translate('A list of individuals.'); 102 } 103 104 /** 105 * CSS class for the URL. 106 * 107 * @return string 108 */ 109 public function listMenuClass(): string 110 { 111 return 'menu-list-indi'; 112 } 113 114 /** 115 * @param Tree $tree 116 * @param array<bool|int|string|array<string>|null> $parameters 117 * 118 * @return string 119 */ 120 public function listUrl(Tree $tree, array $parameters = []): string 121 { 122 $request = Registry::container()->get(ServerRequestInterface::class); 123 $xref = Validator::attributes($request)->isXref()->string('xref', ''); 124 125 if ($xref !== '') { 126 $individual = Registry::individualFactory()->make($xref, $tree); 127 128 if ($individual instanceof Individual && $individual->canShow()) { 129 $primary_name = $individual->getPrimaryName(); 130 131 $parameters['surname'] ??= $individual->getAllNames()[$primary_name]['surn'] ?? null; 132 } 133 } 134 135 $parameters['tree'] = $tree->name(); 136 137 return route(static::class, $parameters); 138 } 139 140 /** 141 * @return array<string> 142 */ 143 public function listUrlAttributes(): array 144 { 145 return []; 146 } 147 148 /** 149 * @param ServerRequestInterface $request 150 * 151 * @return ResponseInterface 152 */ 153 public function handle(ServerRequestInterface $request): ResponseInterface 154 { 155 $tree = Validator::attributes($request)->tree(); 156 $user = Validator::attributes($request)->user(); 157 158 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 159 160 // All individuals with this surname 161 $surname_param = Validator::queryParams($request)->string('surname', ''); 162 $surname = I18N::strtoupper(I18N::language()->normalize($surname_param)); 163 164 // All surnames beginning with this letter, where "@" is unknown and "," is none 165 $alpha = Validator::queryParams($request)->string('alpha', ''); 166 167 // All first names beginning with this letter where "@" is unknown 168 $falpha = Validator::queryParams($request)->string('falpha', ''); 169 170 // What type of list to display, if any 171 $show = Validator::queryParams($request)->string('show', 'surn'); 172 173 // All individuals 174 $show_all = Validator::queryParams($request)->string('show_all', ''); 175 176 // Include/exclude married names 177 $show_marnm = Validator::queryParams($request)->string('show_marnm', ''); 178 179 // Break long lists down by given name 180 $show_all_firstnames = Validator::queryParams($request)->string('show_all_firstnames', ''); 181 182 $params = [ 183 'alpha' => $alpha, 184 'falpha' => $falpha, 185 'show' => $show, 186 'show_all' => $show_all, 187 'show_all_firstnames' => $show_all_firstnames, 188 'show_marnm' => $show_marnm, 189 'surname' => $surname, 190 ]; 191 192 if ($surname_param !== $surname) { 193 return Registry::responseFactory() 194 ->redirectUrl($this->listUrl($tree, $params), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); 195 } 196 197 198 // Make sure parameters are consistent with each other. 199 if ($show_all_firstnames === 'yes') { 200 $falpha = ''; 201 } 202 203 if ($show_all === 'yes') { 204 $alpha = ''; 205 $surname = ''; 206 } 207 208 if ($surname !== '') { 209 $alpha = I18N::language()->initialLetter($surname); 210 } 211 212 $all_surnames = $this->allSurnames($tree, $show_marnm === 'yes', $this->families); 213 $surname_initials = $this->surnameInitials($all_surnames); 214 215 // Make sure selections are consistent. 216 // i.e. can’t specify show_all and surname at the same time. 217 if ($show_all === 'yes') { 218 if ($show_all_firstnames === 'yes') { 219 $legend = I18N::translate('All'); 220 $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm]; 221 $show = 'indi'; 222 } elseif ($falpha !== '') { 223 $legend = I18N::translate('All') . ', ' . e($falpha) . '…'; 224 $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm]; 225 $show = 'indi'; 226 } else { 227 $legend = I18N::translate('All'); 228 $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm]; 229 } 230 } elseif ($surname !== '') { 231 $show_all = 'no'; 232 if ($surname === Individual::NOMEN_NESCIO) { 233 $legend = I18N::translateContext('Unknown surname', '…'); 234 $show = 'indi'; // The surname list makes no sense with only one surname. 235 } else { 236 // The surname parameter is a root/canonical form. Display the actual surnames found. 237 $variants = array_keys($all_surnames[$surname] ?? [$surname => $surname]); 238 usort($variants, I18N::comparator()); 239 $variants = array_map(static fn (string $x): string => $x === '' ? I18N::translate('No surname') : $x, $variants); 240 $legend = implode('/', $variants); 241 $show = 'indi'; // The surname list makes no sense with only one surname. 242 } 243 $params = ['tree' => $tree->name(), 'surname' => $surname, 'falpha' => $falpha, 'show_marnm' => $show_marnm]; 244 switch ($falpha) { 245 case '': 246 break; 247 case '@': 248 $legend .= ', ' . I18N::translateContext('Unknown given name', '…'); 249 break; 250 default: 251 $legend .= ', ' . e($falpha) . '…'; 252 break; 253 } 254 } elseif ($alpha === '@') { 255 $show_all = 'no'; 256 $legend = I18N::translateContext('Unknown surname', '…'); 257 $params = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm]; 258 $surname = Individual::NOMEN_NESCIO; 259 $show = 'indi'; // SURN list makes no sense here 260 } elseif ($alpha === ',') { 261 $show_all = 'no'; 262 $legend = I18N::translate('No surname'); 263 $params = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm]; 264 $show = 'indi'; // SURN list makes no sense here 265 } elseif ($alpha !== '') { 266 $show_all = 'no'; 267 $legend = e($alpha) . '…'; 268 $params = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm]; 269 } else { 270 $show_all = 'no'; 271 $legend = '…'; 272 $params = ['tree' => $tree->name(), 'show_marnm' => $show_marnm]; 273 $show = 'none'; // Don't show lists until something is chosen 274 } 275 $legend = '<bdi>' . $legend . '</bdi>'; 276 277 if ($this->families) { 278 $title = I18N::translate('Families') . ' — ' . $legend; 279 } else { 280 $title = I18N::translate('Individuals') . ' — ' . $legend; 281 } 282 283 ob_start(); ?> 284 <div class="d-flex flex-column wt-page-options wt-page-options-individual-list d-print-none"> 285 <ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-surname"> 286 287 <?php foreach ($surname_initials as $letter => $count) : ?> 288 <li class="wt-initials-list-item d-flex"> 289 <?php if ($count > 0) : ?> 290 <a href="<?= e($this->listUrl($tree, ['alpha' => $letter, 'show_marnm' => $show_marnm, 'tree' => $tree->name()])) ?>" class="wt-initial px-1<?= $letter === $alpha ? ' active' : '' ?> '" title="<?= I18N::number($count) ?>"><?= $this->displaySurnameInitial((string) $letter) ?></a> 291 <?php else : ?> 292 <span class="wt-initial px-1 text-muted"><?= $this->displaySurnameInitial((string) $letter) ?></span> 293 294 <?php endif ?> 295 </li> 296 <?php endforeach ?> 297 298 <?php if (Session::has('initiated')) : ?> 299 <!-- Search spiders don't get the "show all" option as the other links give them everything. --> 300 <li class="wt-initials-list-item d-flex"> 301 <a class="wt-initial px-1<?= $show_all === 'yes' ? ' active' : '' ?>" href="<?= e($this->listUrl($tree, ['show_all' => 'yes'] + $params)) ?>"><?= I18N::translate('All') ?></a> 302 </li> 303 <?php endif ?> 304 </ul> 305 306 <!-- Search spiders don't get an option to show/hide the surname sublists, nor does it make sense on the all/unknown/surname views --> 307 <?php if ($show !== 'none' && Session::has('initiated')) : ?> 308 <?php if ($show_marnm === 'yes') : ?> 309 <p> 310 <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'no'] + $params)) ?>"> 311 <?= I18N::translate('Exclude individuals with “%s” as a married name', $legend) ?> 312 </a> 313 </p> 314 <?php else : ?> 315 <p> 316 <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'yes'] + $params)) ?>"> 317 <?= I18N::translate('Include individuals with “%s” as a married name', $legend) ?> 318 </a> 319 </p> 320 <?php endif ?> 321 322 <?php if ($alpha !== '@' && $alpha !== ',' && $surname === '') : ?> 323 <?php if ($show === 'surn') : ?> 324 <p> 325 <a href="<?= e($this->listUrl($tree, ['show' => 'indi'] + $params)) ?>"> 326 <?= I18N::translate('Show the list of individuals') ?> 327 </a> 328 </p> 329 <?php else : ?> 330 <p> 331 <a href="<?= e($this->listUrl($tree, ['show' => 'surn'] + $params)) ?>"> 332 <?= I18N::translate('Show the list of surnames') ?> 333 </a> 334 </p> 335 <?php endif ?> 336 <?php endif ?> 337 <?php endif ?> 338 </div> 339 340 <div class="wt-page-content"> 341 <?php 342 if ($show === 'indi' || $show === 'surn') { 343 switch ($alpha) { 344 case '@': 345 $surns = array_filter($all_surnames, static fn (string $x): bool => $x === Individual::NOMEN_NESCIO, ARRAY_FILTER_USE_KEY); 346 break; 347 case ',': 348 $surns = array_filter($all_surnames, static fn (string $x): bool => $x === '', ARRAY_FILTER_USE_KEY); 349 break; 350 case '': 351 if ($show_all === 'yes') { 352 $surns = array_filter($all_surnames, static fn (string $x): bool => $x !== '' && $x !== Individual::NOMEN_NESCIO, ARRAY_FILTER_USE_KEY); 353 } else { 354 $surns = array_filter($all_surnames, static fn (string $x): bool => $x === $surname, ARRAY_FILTER_USE_KEY); 355 } 356 break; 357 default: 358 if ($surname === '') { 359 $surns = array_filter($all_surnames, static fn (string $x): bool => I18N::language()->initialLetter($x) === $alpha, ARRAY_FILTER_USE_KEY); 360 } else { 361 $surns = array_filter($all_surnames, static fn (string $x): bool => $x === $surname, ARRAY_FILTER_USE_KEY); 362 } 363 break; 364 } 365 366 if ($show === 'surn') { 367 // Show the surname list 368 switch ($tree->getPreference('SURNAME_LIST_STYLE')) { 369 case 'style1': 370 echo view('lists/surnames-column-list', [ 371 'module' => $this, 372 'params' => ['show' => 'indi', 'show_all' => null] + $params, 373 'surnames' => $surns, 374 'totals' => true, 375 'tree' => $tree, 376 ]); 377 break; 378 case 'style3': 379 echo view('lists/surnames-tag-cloud', [ 380 'module' => $this, 381 'params' => ['show' => 'indi', 'show_all' => null] + $params, 382 'surnames' => $surns, 383 'totals' => true, 384 'tree' => $tree, 385 ]); 386 break; 387 case 'style2': 388 default: 389 echo view('lists/surnames-table', [ 390 'families' => $this->families, 391 'module' => $this, 392 'order' => [[0, 'asc']], 393 'params' => ['show' => 'indi', 'show_all' => null] + $params, 394 'surnames' => $surns, 395 'tree' => $tree, 396 ]); 397 break; 398 } 399 } else { 400 // Show the list 401 $count = array_sum(array_map(static fn (array $x): int => array_sum($x), $surns)); 402 403 // Don't sublist short lists. 404 if ($count < $tree->getPreference('SUBLIST_TRIGGER_I')) { 405 $falpha = ''; 406 } else { 407 // Break long lists by initial letter of given name 408 $surns = array_values(array_map(static fn ($x): array => array_keys($x), $surns)); 409 $surns = array_merge(...$surns); 410 $givn_initials = $this->givenNameInitials($tree, $surns, $show_marnm === 'yes', $this->families); 411 412 if ($surname !== '' || $show_all === 'yes') { 413 if ($show_all !== 'yes') { 414 echo '<h2 class="wt-page-title">', I18N::translate('Individuals with surname %s', $legend), '</h2>'; 415 } 416 // Don't show the list until we have some filter criteria 417 $show = $falpha !== '' || $show_all_firstnames === 'yes' ? 'indi' : 'none'; 418 echo '<ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-given-names">'; 419 foreach ($givn_initials as $givn_initial => $given_count) { 420 echo '<li class="wt-initials-list-item d-flex">'; 421 if ($given_count > 0) { 422 if ($show === 'indi' && $givn_initial === $falpha && $show_all_firstnames !== 'yes') { 423 echo '<a class="wt-initial px-1 active" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->displayGivenNameInitial((string) $givn_initial) . '</a>'; 424 } else { 425 echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->displayGivenNameInitial((string) $givn_initial) . '</a>'; 426 } 427 } else { 428 echo '<span class="wt-initial px-1 text-muted">' . $this->displayGivenNameInitial((string) $givn_initial) . '</span>'; 429 } 430 echo '</li>'; 431 } 432 // Search spiders don't get the "show all" option as the other links give them everything. 433 if (Session::has('initiated')) { 434 echo '<li class="wt-initials-list-item d-flex">'; 435 if ($show_all_firstnames === 'yes') { 436 echo '<span class="wt-initial px-1 active">' . I18N::translate('All') . '</span>'; 437 } else { 438 echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['show_all_firstnames' => 'yes'] + $params)) . '" title="' . I18N::number($count) . '">' . I18N::translate('All') . '</a>'; 439 } 440 echo '</li>'; 441 } 442 echo '</ul>'; 443 } 444 } 445 if ($show === 'indi') { 446 if ($this->families) { 447 echo view('lists/families-table', [ 448 'families' => $this->families($tree, $surname, array_keys($all_surnames[$surname] ?? []), $falpha, $show_marnm === 'yes'), 449 'tree' => $tree, 450 ]); 451 } else { 452 echo view('lists/individuals-table', [ 453 'individuals' => $this->individuals($tree, $surname, array_keys($all_surnames[$surname] ?? []), $falpha, $show_marnm === 'yes', false), 454 'sosa' => false, 455 'tree' => $tree, 456 ]); 457 } 458 } 459 } 460 } ?> 461 </div> 462 <?php 463 464 $html = ob_get_clean(); 465 466 return $this->viewResponse('modules/individual-list/page', [ 467 'content' => $html, 468 'title' => $title, 469 'tree' => $tree, 470 ]); 471 } 472 473 /** 474 * Some initial letters have a special meaning 475 * 476 * @param string $initial 477 * 478 * @return string 479 */ 480 protected function displayGivenNameInitial(string $initial): string 481 { 482 if ($initial === '@') { 483 return I18N::translateContext('Unknown given name', '…'); 484 } 485 486 return e($initial); 487 } 488 489 /** 490 * Some initial letters have a special meaning 491 * 492 * @param string $initial 493 * 494 * @return string 495 */ 496 protected function displaySurnameInitial(string $initial): string 497 { 498 if ($initial === '@') { 499 return I18N::translateContext('Unknown surname', '…'); 500 } 501 502 if ($initial === ',') { 503 return I18N::translate('No surname'); 504 } 505 506 return e($initial); 507 } 508 509 /** 510 * Restrict a query to individuals that are a spouse in a family record. 511 * 512 * @param bool $fams 513 * @param Builder $query 514 */ 515 protected function whereFamily(bool $fams, Builder $query): void 516 { 517 if ($fams) { 518 $query->join('link', static function (JoinClause $join): void { 519 $join 520 ->on('l_from', '=', 'n_id') 521 ->on('l_file', '=', 'n_file') 522 ->where('l_type', '=', 'FAMS'); 523 }); 524 } 525 } 526 527 /** 528 * Restrict a query to include/exclude married names. 529 * 530 * @param bool $marnm 531 * @param Builder $query 532 */ 533 protected function whereMarriedName(bool $marnm, Builder $query): void 534 { 535 if (!$marnm) { 536 $query->where('n_type', '<>', '_MARNM'); 537 } 538 } 539 540 /** 541 * Get a count of individuals with each initial letter 542 * 543 * @param Tree $tree 544 * @param array<string> $surns if set, only consider people with this surname 545 * @param bool $marnm if set, include married names 546 * @param bool $fams if set, only consider individuals with FAMS records 547 * 548 * @return array<int> 549 */ 550 public function givenNameInitials(Tree $tree, array $surns, bool $marnm, bool $fams): array 551 { 552 $initials = []; 553 554 // Ensure our own language comes before others. 555 foreach (I18N::language()->alphabet() as $initial) { 556 $initials[$initial] = 0; 557 } 558 559 $query = DB::table('name') 560 ->where('n_file', '=', $tree->id()); 561 562 $this->whereFamily($fams, $query); 563 $this->whereMarriedName($marnm, $query); 564 565 if ($surns !== []) { 566 $query->whereIn('n_surn', $surns); 567 } 568 569 $query 570 ->select([$this->binaryColumn('n_givn', 'n_givn'), new Expression('COUNT(*) AS count')]) 571 ->groupBy([$this->binaryColumn('n_givn')]); 572 573 foreach ($query->get() as $row) { 574 $initial = I18N::strtoupper(I18N::language()->initialLetter($row->n_givn)); 575 $initials[$initial] ??= 0; 576 $initials[$initial] += (int) $row->count; 577 } 578 579 $count_unknown = $initials['@'] ?? 0; 580 581 if ($count_unknown > 0) { 582 unset($initials['@']); 583 $initials['@'] = $count_unknown; 584 } 585 586 return $initials; 587 } 588 589 /** 590 * Get a count of all surnames and variants. 591 * 592 * @param Tree $tree 593 * @param bool $marnm if set, include married names 594 * @param bool $fams if set, only consider individuals with FAMS records 595 * 596 * @return array<array<int>> 597 */ 598 protected function allSurnames(Tree $tree, bool $marnm, bool $fams): array 599 { 600 $query = DB::table('name') 601 ->where('n_file', '=', $tree->id()) 602 ->whereNotNull('n_surn') // Filters old records for sources, repositories, etc. 603 ->whereNotNull('n_surname') 604 ->select([ 605 $this->binaryColumn('n_surn', 'n_surn'), 606 $this->binaryColumn('n_surname', 'n_surname'), 607 new Expression('COUNT(*) AS total'), 608 ]); 609 610 $this->whereFamily($fams, $query); 611 $this->whereMarriedName($marnm, $query); 612 613 $query->groupBy([ 614 $this->binaryColumn('n_surn'), 615 $this->binaryColumn('n_surname'), 616 ]); 617 618 /** @var array<array<int>> $list */ 619 $list = []; 620 621 foreach ($query->get() as $row) { 622 $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn; 623 $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn)); 624 625 $list[$row->n_surn][$row->n_surname] ??= 0; 626 $list[$row->n_surn][$row->n_surname] += (int) $row->total; 627 } 628 629 uksort($list, I18N::comparator()); 630 631 return $list; 632 } 633 634 /** 635 * Extract initial letters and counts for all surnames. 636 * 637 * @param array<array<int>> $all_surnames 638 * 639 * @return array<int> 640 */ 641 protected function surnameInitials(array $all_surnames): array 642 { 643 $initials = []; 644 645 // Ensure our own language comes before others. 646 foreach (I18N::language()->alphabet() as $initial) { 647 $initials[$initial] = 0; 648 } 649 650 foreach ($all_surnames as $surn => $surnames) { 651 $initial = I18N::language()->initialLetter((string) $surn); 652 653 $initials[$initial] ??= 0; 654 $initials[$initial] += array_sum($surnames); 655 } 656 657 // Move specials to the end 658 $count_none = $initials[''] ?? 0; 659 660 if ($count_none > 0) { 661 unset($initials['']); 662 $initials[','] = $count_none; 663 } 664 665 $count_unknown = $initials['@'] ?? 0; 666 667 if ($count_unknown > 0) { 668 unset($initials['@']); 669 $initials['@'] = $count_unknown; 670 } 671 672 return $initials; 673 } 674 675 /** 676 * Fetch a list of individuals with specified names 677 * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@" 678 * To search for names with no surnames, use $salpha="," 679 * 680 * @param Tree $tree 681 * @param string $surname if set, only fetch people with this n_surn 682 * @param array<string> $surnames if set, only fetch people with this n_surname 683 * @param string $galpha if set, only fetch given names starting with this letter 684 * @param bool $marnm if set, include married names 685 * @param bool $fams if set, only fetch individuals with FAMS records 686 * 687 * @return Collection<int,Individual> 688 */ 689 protected function individuals(Tree $tree, string $surname, array $surnames, string $galpha, bool $marnm, bool $fams): Collection 690 { 691 $query = DB::table('individuals') 692 ->join('name', static function (JoinClause $join): void { 693 $join 694 ->on('n_id', '=', 'i_id') 695 ->on('n_file', '=', 'i_file'); 696 }) 697 ->where('i_file', '=', $tree->id()) 698 ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'n_givn', 'n_surn']); 699 700 $this->whereFamily($fams, $query); 701 $this->whereMarriedName($marnm, $query); 702 703 if ($surnames === []) { 704 // SURN, with no surname 705 $query->where('n_surn', '=', $surname); 706 } else { 707 $query->whereIn($this->binaryColumn('n_surname'), $surnames); 708 } 709 710 $query 711 ->orderBy(new Expression("CASE n_surn WHEN '" . Individual::NOMEN_NESCIO . "' THEN 1 ELSE 0 END")) 712 ->orderBy('n_surn') 713 ->orderBy(new Expression("CASE n_givn WHEN '" . Individual::NOMEN_NESCIO . "' THEN 1 ELSE 0 END")) 714 ->orderBy('n_givn'); 715 716 $individuals = new Collection(); 717 718 foreach ($query->get() as $row) { 719 $individual = Registry::individualFactory()->make($row->xref, $tree, $row->gedcom); 720 assert($individual instanceof Individual); 721 722 // The name from the database may be private - check the filtered list... 723 foreach ($individual->getAllNames() as $n => $name) { 724 if ($name['givn'] === $row->n_givn && $name['surn'] === $row->n_surn) { 725 if ($galpha === '' || I18N::strtoupper(I18N::language()->initialLetter($row->n_givn)) === $galpha) { 726 $individual->setPrimaryName($n); 727 // We need to clone $individual, as we may have multiple references to the 728 // same individual in this list, and the "primary name" would otherwise 729 // be shared amongst all of them. 730 $individuals->push(clone $individual); 731 break; 732 } 733 } 734 } 735 } 736 737 return $individuals; 738 } 739 740 /** 741 * Fetch a list of families with specified names 742 * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@" 743 * To search for names with no surnames, use $salpha="," 744 * 745 * @param Tree $tree 746 * @param string $surname if set, only fetch people with this n_surn 747 * @param array<string> $surnames if set, only fetch people with this n_surname 748 * @param string $galpha if set, only fetch given names starting with this letter 749 * @param bool $marnm if set, include married names 750 * 751 * @return Collection<int,Family> 752 */ 753 protected function families(Tree $tree, string $surname, array $surnames, string $galpha, bool $marnm): Collection 754 { 755 $families = new Collection(); 756 757 foreach ($this->individuals($tree, $surname, $surnames, $galpha, $marnm, true) as $indi) { 758 foreach ($indi->spouseFamilies() as $family) { 759 $families->push($family); 760 } 761 } 762 763 return $families->unique(); 764 } 765 766 /** 767 * This module assumes the database will use binary collation on the name columns. 768 * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time. 769 * 770 * @param string $column 771 * @param string|null $alias 772 * 773 * @return Expression 774 */ 775 private function binaryColumn(string $column, string $alias = null): Expression 776 { 777 if (DB::connection()->getDriverName() === 'mysql') { 778 $sql = 'CAST(' . $column . ' AS binary)'; 779 } else { 780 $sql = $column; 781 } 782 783 if ($alias !== null) { 784 $sql .= ' AS ' . $alias; 785 } 786 787 return new Expression($sql); 788 } 789} 790