1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\RouterContainer; 23use Fisharebest\Localization\Locale\LocaleInterface; 24use Fisharebest\Webtrees\Auth; 25use Fisharebest\Webtrees\Contracts\UserInterface; 26use Fisharebest\Webtrees\Factory; 27use Fisharebest\Webtrees\Family; 28use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 29use Fisharebest\Webtrees\GedcomRecord; 30use Fisharebest\Webtrees\I18N; 31use Fisharebest\Webtrees\Individual; 32use Fisharebest\Webtrees\Services\LocalizationService; 33use Fisharebest\Webtrees\Session; 34use Fisharebest\Webtrees\Tree; 35use Illuminate\Database\Capsule\Manager as DB; 36use Illuminate\Database\Query\Builder; 37use Illuminate\Database\Query\Expression; 38use Illuminate\Database\Query\JoinClause; 39use Psr\Http\Message\ResponseInterface; 40use Psr\Http\Message\ServerRequestInterface; 41use Psr\Http\Server\RequestHandlerInterface; 42 43use function app; 44use function array_key_exists; 45use function array_keys; 46use function assert; 47use function e; 48use function implode; 49use function in_array; 50use function ob_get_clean; 51use function ob_start; 52use function redirect; 53use function route; 54use function usort; 55use function view; 56 57/** 58 * Class IndividualListModule 59 */ 60class IndividualListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface 61{ 62 use ModuleListTrait; 63 64 protected const ROUTE_URL = '/tree/{tree}/individual-list'; 65 66 /** @var LocalizationService */ 67 private $localization_service; 68 69 /** 70 * IndividualListModule constructor. 71 * 72 * @param LocalizationService $localization_service 73 */ 74 public function __construct(LocalizationService $localization_service) 75 { 76 $this->localization_service = $localization_service; 77 } 78 79 /** 80 * Initialization. 81 * 82 * @return void 83 */ 84 public function boot(): void 85 { 86 $router_container = app(RouterContainer::class); 87 assert($router_container instanceof RouterContainer); 88 89 $router_container->getMap() 90 ->get(static::class, static::ROUTE_URL, $this); 91 } 92 93 /** 94 * How should this module be identified in the control panel, etc.? 95 * 96 * @return string 97 */ 98 public function title(): string 99 { 100 /* I18N: Name of a module/list */ 101 return I18N::translate('Individuals'); 102 } 103 104 /** 105 * A sentence describing what this module does. 106 * 107 * @return string 108 */ 109 public function description(): string 110 { 111 /* I18N: Description of the “Individuals” module */ 112 return I18N::translate('A list of individuals.'); 113 } 114 115 /** 116 * CSS class for the URL. 117 * 118 * @return string 119 */ 120 public function listMenuClass(): string 121 { 122 return 'menu-list-indi'; 123 } 124 125 /** 126 * @param Tree $tree 127 * @param mixed[] $parameters 128 * 129 * @return string 130 */ 131 public function listUrl(Tree $tree, array $parameters = []): string 132 { 133 $xref = app(ServerRequestInterface::class)->getAttribute('xref', ''); 134 135 if ($xref !== '') { 136 $individual = Factory::individual()->make($xref, $tree); 137 138 if ($individual instanceof Individual && $individual->canShow()) { 139 $parameters['surname'] = $parameters['surname'] ?? $individual->getAllNames()[0]['surn'] ?? null; 140 } 141 } 142 143 $parameters['tree'] = $tree->name(); 144 145 return route(static::class, $parameters); 146 } 147 148 /** 149 * @return string[] 150 */ 151 public function listUrlAttributes(): array 152 { 153 return []; 154 } 155 156 /** 157 * Handle URLs generated by older versions of webtrees 158 * 159 * @param ServerRequestInterface $request 160 * 161 * @return ResponseInterface 162 */ 163 public function getListAction(ServerRequestInterface $request): ResponseInterface 164 { 165 return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams())); 166 } 167 168 /** 169 * @param ServerRequestInterface $request 170 * 171 * @return ResponseInterface 172 */ 173 public function handle(ServerRequestInterface $request): ResponseInterface 174 { 175 $tree = $request->getAttribute('tree'); 176 assert($tree instanceof Tree); 177 178 $user = $request->getAttribute('user'); 179 assert($user instanceof UserInterface); 180 181 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 182 183 return $this->createResponse($tree, $user, $request->getQueryParams(), false); 184 } 185 186 /** 187 * @param Tree $tree 188 * @param UserInterface $user 189 * @param array<string> $params 190 * @param bool $families 191 * 192 * @return ResponseInterface 193 */ 194 protected function createResponse(Tree $tree, UserInterface $user, array $params, bool $families): ResponseInterface 195 { 196 ob_start(); 197 198 // We show three different lists: initials, surnames and individuals 199 200 // All surnames beginning with this letter where "@"=unknown and ","=none 201 $alpha = $params['alpha'] ?? ''; 202 203 // All individuals with this surname 204 $surname = $params['surname'] ?? ''; 205 206 // All individuals 207 $show_all = $params['show_all'] ?? 'no'; 208 209 // Long lists can be broken down by given name 210 $show_all_firstnames = $params['show_all_firstnames'] ?? 'no'; 211 if ($show_all_firstnames === 'yes') { 212 $falpha = ''; 213 } else { 214 // All first names beginning with this letter 215 $falpha = $params['falpha'] ?? ''; 216 } 217 218 $show_marnm = $params['show_marnm'] ?? ''; 219 switch ($show_marnm) { 220 case 'no': 221 case 'yes': 222 $user->setPreference($families ? 'family-list-marnm' : 'individual-list-marnm', $show_marnm); 223 break; 224 default: 225 $show_marnm = $user->getPreference($families ? 'family-list-marnm' : 'individual-list-marnm'); 226 } 227 228 // Make sure selections are consistent. 229 // i.e. can’t specify show_all and surname at the same time. 230 if ($show_all === 'yes') { 231 if ($show_all_firstnames === 'yes') { 232 $alpha = ''; 233 $surname = ''; 234 $legend = I18N::translate('All'); 235 $params = [ 236 'tree' => $tree->name(), 237 'show_all' => 'yes', 238 ]; 239 $show = 'indi'; 240 } elseif ($falpha !== '') { 241 $alpha = ''; 242 $surname = ''; 243 $legend = I18N::translate('All') . ', ' . e($falpha) . '…'; 244 $params = [ 245 'tree' => $tree->name(), 246 'show_all' => 'yes', 247 ]; 248 $show = 'indi'; 249 } else { 250 $alpha = ''; 251 $surname = ''; 252 $legend = I18N::translate('All'); 253 $show = $params['show'] ?? 'surn'; 254 $params = [ 255 'tree' => $tree->name(), 256 'show_all' => 'yes', 257 ]; 258 } 259 } elseif ($surname !== '') { 260 $alpha = $this->localization_service->initialLetter($surname, I18N::locale()); // so we can highlight the initial letter 261 $show_all = 'no'; 262 if ($surname === '@N.N.') { 263 $legend = I18N::translateContext('Unknown surname', '…'); 264 } else { 265 // The surname parameter is a root/canonical form. 266 // Display it as the actual surname 267 $legend = implode('/', array_keys($this->surnames($tree, $surname, $alpha, $show_marnm === 'yes', $families, I18N::locale()))); 268 } 269 $params = [ 270 'tree' => $tree->name(), 271 'surname' => $surname, 272 'falpha' => $falpha, 273 ]; 274 switch ($falpha) { 275 case '': 276 break; 277 case '@': 278 $legend .= ', ' . I18N::translateContext('Unknown given name', '…'); 279 break; 280 default: 281 $legend .= ', ' . e($falpha) . '…'; 282 break; 283 } 284 $show = 'indi'; // SURN list makes no sense here 285 } elseif ($alpha === '@') { 286 $show_all = 'no'; 287 $legend = I18N::translateContext('Unknown surname', '…'); 288 $params = [ 289 'alpha' => $alpha, 290 'tree' => $tree->name(), 291 ]; 292 $show = 'indi'; // SURN list makes no sense here 293 } elseif ($alpha === ',') { 294 $show_all = 'no'; 295 $legend = I18N::translate('None'); 296 $params = [ 297 'alpha' => $alpha, 298 'tree' => $tree->name(), 299 ]; 300 $show = 'indi'; // SURN list makes no sense here 301 } elseif ($alpha !== '') { 302 $show_all = 'no'; 303 $legend = e($alpha) . '…'; 304 $show = $params['show'] ?? 'surn'; 305 $params = [ 306 'alpha' => $alpha, 307 'tree' => $tree->name(), 308 ]; 309 } else { 310 $show_all = 'no'; 311 $legend = '…'; 312 $params = [ 313 'tree' => $tree->name(), 314 ]; 315 $show = 'none'; // Don't show lists until something is chosen 316 } 317 $legend = '<span dir="auto">' . $legend . '</span>'; 318 319 if ($families) { 320 $title = I18N::translate('Families') . ' — ' . $legend; 321 } else { 322 $title = I18N::translate('Individuals') . ' — ' . $legend; 323 } ?> 324 <div class="d-flex flex-column wt-page-options wt-page-options-individual-list d-print-none"> 325 <ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-surname"> 326 327 <?php foreach ($this->surnameAlpha($tree, $show_marnm === 'yes', $families, I18N::locale()) as $letter => $count) : ?> 328 <li class="wt-initials-list-item d-flex"> 329 <?php if ($count > 0) : ?> 330 <a href="<?= e($this->listUrl($tree, ['alpha' => $letter, 'tree' => $tree->name()])) ?>" class="wt-initial px-1<?= $letter === $alpha ? ' active' : '' ?> '" title="<?= I18N::number($count) ?>"><?= $this->surnameInitial((string) $letter) ?></a> 331 <?php else : ?> 332 <span class="wt-initial px-1 text-muted"><?= $this->surnameInitial((string) $letter) ?></span> 333 334 <?php endif ?> 335 </li> 336 <?php endforeach ?> 337 338 <?php if (Session::has('initiated')) : ?> 339 <!-- Search spiders don't get the "show all" option as the other links give them everything. --> 340 <li class="wt-initials-list-item d-flex"> 341 <a class="wt-initial px-1<?= $show_all === 'yes' ? ' active' : '' ?>" href="<?= e($this->listUrl($tree, ['show_all' => 'yes'] + $params)) ?>"><?= I18N::translate('All') ?></a> 342 </li> 343 <?php endif ?> 344 </ul> 345 346 <!-- Search spiders don't get an option to show/hide the surname sublists, nor does it make sense on the all/unknown/surname views --> 347 <?php if ($show !== 'none' && Session::has('initiated')) : ?> 348 <?php if ($show_marnm === 'yes') : ?> 349 <p> 350 <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'no'] + $params)) ?>"> 351 <?= I18N::translate('Exclude individuals with “%s” as a married name', $legend) ?> 352 </a> 353 </p> 354 <?php else : ?> 355 <p> 356 <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'yes'] + $params)) ?>"> 357 <?= I18N::translate('Include individuals with “%s” as a married name', $legend) ?> 358 </a> 359 </p> 360 <?php endif ?> 361 362 <?php if ($alpha !== '@' && $alpha !== ',' && $surname === '') : ?> 363 <?php if ($show === 'surn') : ?> 364 <p> 365 <a href="<?= e($this->listUrl($tree, ['show' => 'indi', 'show_marnm' => 'no'] + $params)) ?>"> 366 <?= I18N::translate('Show the list of individuals') ?> 367 </a> 368 </p> 369 <?php else : ?> 370 <p> 371 <a href="<?= e($this->listUrl($tree, ['show' => 'surn', 'show_marnm' => 'no'] + $params)) ?>"> 372 <?= I18N::translate('Show the list of surnames') ?> 373 </a> 374 </p> 375 <?php endif ?> 376 <?php endif ?> 377 <?php endif ?> 378 </div> 379 380 <div class="wt-page-content"> 381 <?php 382 383 if ($show === 'indi' || $show === 'surn') { 384 $surns = $this->surnames($tree, $surname, $alpha, $show_marnm === 'yes', $families, I18N::locale()); 385 if ($show === 'surn') { 386 // Show the surname list 387 switch ($tree->getPreference('SURNAME_LIST_STYLE')) { 388 case 'style1': 389 echo FunctionsPrintLists::surnameList($surns, 3, true, $this, $tree); 390 break; 391 case 'style3': 392 echo FunctionsPrintLists::surnameTagCloud($surns, $this, true, $tree); 393 break; 394 case 'style2': 395 default: 396 echo view('lists/surnames-table', [ 397 'surnames' => $surns, 398 'families' => $families, 399 'module' => $this, 400 'tree' => $tree, 401 ]); 402 break; 403 } 404 } else { 405 // Show the list 406 $count = 0; 407 foreach ($surns as $surnames) { 408 foreach ($surnames as $total) { 409 $count += $total; 410 } 411 } 412 // Don't sublist short lists. 413 if ($count < $tree->getPreference('SUBLIST_TRIGGER_I')) { 414 $falpha = ''; 415 } else { 416 $givn_initials = $this->givenAlpha($tree, $surname, $alpha, $show_marnm === 'yes', $families, I18N::locale()); 417 // Break long lists by initial letter of given name 418 if ($surname !== '' || $show_all === 'yes') { 419 if ($show_all === 'no') { 420 echo '<h2 class="wt-page-title">', I18N::translate('Individuals with surname %s', $legend), '</h2>'; 421 } 422 // Don't show the list until we have some filter criteria 423 $show = $falpha !== '' || $show_all_firstnames === 'yes' ? 'indi' : 'none'; 424 $list = []; 425 echo '<ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-given-names">'; 426 foreach ($givn_initials as $givn_initial => $given_count) { 427 echo '<li class="wt-initials-list-item d-flex">'; 428 if ($given_count > 0) { 429 if ($show === 'indi' && $givn_initial === $falpha && $show_all_firstnames === 'no') { 430 echo '<a class="wt-initial px-1 active" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->givenNameInitial((string) $givn_initial) . '</a>'; 431 } else { 432 echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->givenNameInitial((string) $givn_initial) . '</a>'; 433 } 434 } else { 435 echo '<span class="wt-initial px-1 text-muted">' . $this->givenNameInitial((string) $givn_initial) . '</span>'; 436 } 437 echo '</li>'; 438 } 439 // Search spiders don't get the "show all" option as the other links give them everything. 440 if (Session::has('initiated')) { 441 echo '<li class="wt-initials-list-item d-flex">'; 442 if ($show_all_firstnames === 'yes') { 443 echo '<span class="wt-initial px-1 warning">' . I18N::translate('All') . '</span>'; 444 } else { 445 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>'; 446 } 447 echo '</li>'; 448 } 449 echo '</ul>'; 450 echo '<p class="text-center alpha_index">', implode(' | ', $list), '</p>'; 451 } 452 } 453 if ($show === 'indi') { 454 if (!$families) { 455 echo view('lists/individuals-table', [ 456 'individuals' => $this->individuals($tree, $surname, $alpha, $falpha, $show_marnm === 'yes', false, I18N::locale()), 457 'sosa' => false, 458 'tree' => $tree, 459 ]); 460 } else { 461 echo view('lists/families-table', [ 462 'families' => $this->families($tree, $surname, $alpha, $falpha, $show_marnm === 'yes', I18N::locale()), 463 'tree' => $tree, 464 ]); 465 } 466 } 467 } 468 } ?> 469 </div> 470 <?php 471 472 $html = ob_get_clean(); 473 474 return $this->viewResponse('modules/individual-list/page', [ 475 'content' => $html, 476 'title' => $title, 477 'tree' => $tree, 478 ]); 479 } 480 481 /** 482 * Some initial letters have a special meaning 483 * 484 * @param string $initial 485 * 486 * @return string 487 */ 488 protected function givenNameInitial(string $initial): string 489 { 490 if ($initial === '@') { 491 return I18N::translateContext('Unknown given name', '…'); 492 } 493 494 return e($initial); 495 } 496 497 /** 498 * Some initial letters have a special meaning 499 * 500 * @param string $initial 501 * 502 * @return string 503 */ 504 protected function surnameInitial(string $initial): string 505 { 506 if ($initial === '@') { 507 return I18N::translateContext('Unknown surname', '…'); 508 } 509 510 if ($initial === ',') { 511 return I18N::translate('None'); 512 } 513 514 return e($initial); 515 } 516 517 /** 518 * Restrict a query to individuals that are a spouse in a family record. 519 * 520 * @param bool $fams 521 * @param Builder $query 522 */ 523 protected function whereFamily(bool $fams, Builder $query): void 524 { 525 if ($fams) { 526 $query->join('link', static function (JoinClause $join): void { 527 $join 528 ->on('l_from', '=', 'n_id') 529 ->on('l_file', '=', 'n_file') 530 ->where('l_type', '=', 'FAMS'); 531 }); 532 } 533 } 534 535 /** 536 * Restrict a query to include/exclude married names. 537 * 538 * @param bool $marnm 539 * @param Builder $query 540 */ 541 protected function whereMarriedName(bool $marnm, Builder $query): void 542 { 543 if (!$marnm) { 544 $query->where('n_type', '<>', '_MARNM'); 545 } 546 } 547 548 /** 549 * Get a list of initial surname letters. 550 * 551 * @param Tree $tree 552 * @param bool $marnm if set, include married names 553 * @param bool $fams if set, only consider individuals with FAMS records 554 * @param LocaleInterface $locale 555 * 556 * @return int[] 557 */ 558 public function surnameAlpha(Tree $tree, bool $marnm, bool $fams, LocaleInterface $locale): array 559 { 560 $collation = $this->localization_service->collation($locale); 561 562 $n_surn = $this->fieldWithCollation('n_surn', $collation); 563 $alphas = []; 564 565 $query = DB::table('name')->where('n_file', '=', $tree->id()); 566 567 $this->whereFamily($fams, $query); 568 $this->whereMarriedName($marnm, $query); 569 570 // Fetch all the letters in our alphabet, whether or not there 571 // are any names beginning with that letter. It looks better to 572 // show the full alphabet, rather than omitting rare letters such as X. 573 foreach ($this->localization_service->alphabet($locale) as $letter) { 574 $query2 = clone $query; 575 576 $this->whereInitial($query2, 'n_surn', $letter, $locale); 577 578 $alphas[$letter] = $query2->count(); 579 } 580 581 // Now fetch initial letters that are not in our alphabet, 582 // including "@" (for "@N.N.") and "" for no surname. 583 foreach ($this->localization_service->alphabet($locale) as $n => $letter) { 584 $query->where($n_surn, 'NOT LIKE', $letter . '%'); 585 } 586 587 $rows = $query 588 ->groupBy(['initial']) 589 ->orderBy('initial') 590 ->pluck(new Expression('COUNT(*) AS aggregate'), new Expression('SUBSTR(n_surn, 1, 1) AS initial')); 591 592 $specials = ['@', '']; 593 594 foreach ($rows as $alpha => $count) { 595 if (!in_array($alpha, $specials, true)) { 596 $alphas[$alpha] = (int) $count; 597 } 598 } 599 600 // Empty surnames have a special code ',' - as we search for SURN.GIVN 601 foreach ($specials as $special) { 602 if ($rows->has($special)) { 603 $alphas[$special ?: ','] = (int) $rows[$special]; 604 } 605 } 606 607 return $alphas; 608 } 609 610 /** 611 * Get a list of initial given name letters for indilist.php and famlist.php 612 * 613 * @param Tree $tree 614 * @param string $surn if set, only consider people with this surname 615 * @param string $salpha if set, only consider surnames starting with this letter 616 * @param bool $marnm if set, include married names 617 * @param bool $fams if set, only consider individuals with FAMS records 618 * @param LocaleInterface $locale 619 * 620 * @return int[] 621 */ 622 public function givenAlpha(Tree $tree, string $surn, string $salpha, bool $marnm, bool $fams, LocaleInterface $locale): array 623 { 624 $collation = $this->localization_service->collation($locale); 625 626 $alphas = []; 627 628 $query = DB::table('name') 629 ->where('n_file', '=', $tree->id()); 630 631 $this->whereFamily($fams, $query); 632 $this->whereMarriedName($marnm, $query); 633 634 if ($surn !== '') { 635 $n_surn = $this->fieldWithCollation('n_surn', $collation); 636 $query->where($n_surn, '=', $surn); 637 } elseif ($salpha === ',') { 638 $query->where('n_surn', '=', ''); 639 } elseif ($salpha === '@') { 640 $query->where('n_surn', '=', '@N.N.'); 641 } elseif ($salpha !== '') { 642 $this->whereInitial($query, 'n_surn', $salpha, $locale); 643 } else { 644 // All surnames 645 $query->whereNotIn('n_surn', ['', '@N.N.']); 646 } 647 648 // Fetch all the letters in our alphabet, whether or not there 649 // are any names beginning with that letter. It looks better to 650 // show the full alphabet, rather than omitting rare letters such as X 651 foreach ($this->localization_service->alphabet($locale) as $letter) { 652 $query2 = clone $query; 653 654 $this->whereInitial($query2, 'n_givn', $letter, $locale); 655 656 $alphas[$letter] = $query2->distinct()->count('n_id'); 657 } 658 659 $rows = $query 660 ->groupBy(['initial']) 661 ->orderBy('initial') 662 ->pluck(new Expression('COUNT(*) AS aggregate'), new Expression('UPPER(SUBSTR(n_givn, 1, 1)) AS initial')); 663 664 $specials = ['@']; 665 666 foreach ($rows as $alpha => $count) { 667 if ($alpha !== '@') { 668 $alphas[$alpha] = (int) $count; 669 } 670 } 671 672 foreach ($specials as $special) { 673 if ($rows->has('@')) { 674 $alphas['@'] = (int) $rows['@']; 675 } 676 } 677 678 679 return $alphas; 680 } 681 682 /** 683 * Get a count of actual surnames and variants, based on a "root" surname. 684 * 685 * @param Tree $tree 686 * @param string $surn if set, only count people with this surname 687 * @param string $salpha if set, only consider surnames starting with this letter 688 * @param bool $marnm if set, include married names 689 * @param bool $fams if set, only consider individuals with FAMS records 690 * @param LocaleInterface $locale 691 * 692 * @return int[][] 693 */ 694 public function surnames( 695 Tree $tree, 696 string $surn, 697 string $salpha, 698 bool $marnm, 699 bool $fams, 700 LocaleInterface $locale 701 ): array { 702 $collation = $this->localization_service->collation($locale); 703 704 $query = DB::table('name') 705 ->where('n_file', '=', $tree->id()) 706 ->select([ 707 new Expression('UPPER(n_surn /*! COLLATE ' . $collation . ' */) AS n_surn'), 708 new Expression('n_surname /*! COLLATE utf8_bin */ AS n_surname'), 709 new Expression('COUNT(*) AS total'), 710 ]); 711 712 $this->whereFamily($fams, $query); 713 $this->whereMarriedName($marnm, $query); 714 715 if ($surn !== '') { 716 $query->where('n_surn', '=', $surn); 717 } elseif ($salpha === ',') { 718 $query->where('n_surn', '=', ''); 719 } elseif ($salpha === '@') { 720 $query->where('n_surn', '=', '@N.N.'); 721 } elseif ($salpha !== '') { 722 $this->whereInitial($query, 'n_surn', $salpha, $locale); 723 } else { 724 // All surnames 725 $query->whereNotIn('n_surn', ['', '@N.N.']); 726 } 727 $query 728 ->groupBy(['n_surn']) 729 ->groupBy(['n_surname']) 730 ->orderBy('n_surname'); 731 732 $list = []; 733 734 foreach ($query->get() as $row) { 735 $list[$row->n_surn][$row->n_surname] = (int) $row->total; 736 } 737 738 return $list; 739 } 740 741 /** 742 * Fetch a list of individuals with specified names 743 * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@" 744 * To search for names with no surnames, use $salpha="," 745 * 746 * @param Tree $tree 747 * @param string $surn if set, only fetch people with this surname 748 * @param string $salpha if set, only fetch surnames starting with this letter 749 * @param string $galpha if set, only fetch given names starting with this letter 750 * @param bool $marnm if set, include married names 751 * @param bool $fams if set, only fetch individuals with FAMS records 752 * @param LocaleInterface $locale 753 * 754 * @return Individual[] 755 */ 756 public function individuals( 757 Tree $tree, 758 string $surn, 759 string $salpha, 760 string $galpha, 761 bool $marnm, 762 bool $fams, 763 LocaleInterface $locale 764 ): array { 765 $collation = $this->localization_service->collation($locale); 766 767 // Use specific collation for name fields. 768 $n_givn = $this->fieldWithCollation('n_givn', $collation); 769 $n_surn = $this->fieldWithCollation('n_surn', $collation); 770 771 $query = DB::table('individuals') 772 ->join('name', static function (JoinClause $join): void { 773 $join 774 ->on('n_id', '=', 'i_id') 775 ->on('n_file', '=', 'i_file'); 776 }) 777 ->where('i_file', '=', $tree->id()) 778 ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'n_givn', 'n_surn']); 779 780 $this->whereFamily($fams, $query); 781 $this->whereMarriedName($marnm, $query); 782 783 if ($surn) { 784 $query->where($n_surn, '=', $surn); 785 } elseif ($salpha === ',') { 786 $query->where($n_surn, '=', ''); 787 } elseif ($salpha === '@') { 788 $query->where($n_surn, '=', '@N.N.'); 789 } elseif ($salpha) { 790 $this->whereInitial($query, 'n_surn', $salpha, $locale); 791 } else { 792 // All surnames 793 $query->whereNotIn($n_surn, ['', '@N.N.']); 794 } 795 if ($galpha) { 796 $this->whereInitial($query, 'n_givn', $galpha, $locale); 797 } 798 799 $query 800 ->orderBy(new Expression("CASE n_surn WHEN '@N.N.' THEN 1 ELSE 0 END")) 801 ->orderBy($n_surn) 802 ->orderBy(new Expression("CASE n_givn WHEN '@N.N.' THEN 1 ELSE 0 END")) 803 ->orderBy($n_givn); 804 805 $list = []; 806 $rows = $query->get(); 807 808 foreach ($rows as $row) { 809 $individual = Factory::individual()->make($row->xref, $tree, $row->gedcom); 810 assert($individual instanceof Individual); 811 812 // The name from the database may be private - check the filtered list... 813 foreach ($individual->getAllNames() as $n => $name) { 814 if ($name['givn'] === $row->n_givn && $name['surn'] === $row->n_surn) { 815 $individual->setPrimaryName($n); 816 // We need to clone $individual, as we may have multiple references to the 817 // same individual in this list, and the "primary name" would otherwise 818 // be shared amongst all of them. 819 $list[] = clone $individual; 820 break; 821 } 822 } 823 } 824 825 return $list; 826 } 827 828 /** 829 * Fetch a list of families with specified names 830 * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@" 831 * To search for names with no surnames, use $salpha="," 832 * 833 * @param Tree $tree 834 * @param string $surn if set, only fetch people with this surname 835 * @param string $salpha if set, only fetch surnames starting with this letter 836 * @param string $galpha if set, only fetch given names starting with this letter 837 * @param bool $marnm if set, include married names 838 * @param LocaleInterface $locale 839 * 840 * @return Family[] 841 */ 842 public function families(Tree $tree, $surn, $salpha, $galpha, $marnm, LocaleInterface $locale): array 843 { 844 $list = []; 845 foreach ($this->individuals($tree, $surn, $salpha, $galpha, $marnm, true, $locale) as $indi) { 846 foreach ($indi->spouseFamilies() as $family) { 847 $list[$family->xref()] = $family; 848 } 849 } 850 usort($list, GedcomRecord::nameComparator()); 851 852 return $list; 853 } 854 855 /** 856 * Use MySQL-specific comments so we can run these queries on other RDBMS. 857 * 858 * @param string $field 859 * @param string $collation 860 * 861 * @return Expression 862 */ 863 protected function fieldWithCollation(string $field, string $collation): Expression 864 { 865 return new Expression($field . ' /*! COLLATE ' . $collation . ' */'); 866 } 867 868 /** 869 * Modify a query to restrict a field to a given initial letter. 870 * Take account of digraphs, equialent letters, etc. 871 * 872 * @param Builder $query 873 * @param string $field 874 * @param string $letter 875 * @param LocaleInterface $locale 876 * 877 * @return void 878 */ 879 protected function whereInitial( 880 Builder $query, 881 string $field, 882 string $letter, 883 LocaleInterface $locale 884 ): void { 885 $collation = $this->localization_service->collation($locale); 886 887 // Use MySQL-specific comments so we can run these queries on other RDBMS. 888 $field_with_collation = $this->fieldWithCollation($field, $collation); 889 890 switch ($locale->languageTag()) { 891 case 'cs': 892 $this->whereInitialCzech($query, $field_with_collation, $letter); 893 break; 894 895 case 'da': 896 case 'nb': 897 case 'nn': 898 $this->whereInitialNorwegian($query, $field_with_collation, $letter); 899 break; 900 901 case 'sv': 902 case 'fi': 903 $this->whereInitialSwedish($query, $field_with_collation, $letter); 904 break; 905 906 case 'hu': 907 $this->whereInitialHungarian($query, $field_with_collation, $letter); 908 break; 909 910 case 'nl': 911 $this->whereInitialDutch($query, $field_with_collation, $letter); 912 break; 913 914 default: 915 $query->where($field_with_collation, 'LIKE', '\\' . $letter . '%'); 916 } 917 } 918 919 /** 920 * @param Builder $query 921 * @param Expression $field 922 * @param string $letter 923 */ 924 protected function whereInitialCzech(Builder $query, Expression $field, string $letter): void 925 { 926 if ($letter === 'C') { 927 $query->where($field, 'LIKE', 'C%')->where($field, 'NOT LIKE', 'CH%'); 928 } else { 929 $query->where($field, 'LIKE', '\\' . $letter . '%'); 930 } 931 } 932 933 /** 934 * @param Builder $query 935 * @param Expression $field 936 * @param string $letter 937 */ 938 protected function whereInitialDutch(Builder $query, Expression $field, string $letter): void 939 { 940 if ($letter === 'I') { 941 $query->where($field, 'LIKE', 'I%')->where($field, 'NOT LIKE', 'IJ%'); 942 } else { 943 $query->where($field, 'LIKE', '\\' . $letter . '%'); 944 } 945 } 946 947 /** 948 * Hungarian has many digraphs and trigraphs, so exclude these from prefixes. 949 * 950 * @param Builder $query 951 * @param Expression $field 952 * @param string $letter 953 */ 954 protected function whereInitialHungarian(Builder $query, Expression $field, string $letter): void 955 { 956 switch ($letter) { 957 case 'C': 958 $query->where($field, 'LIKE', 'C%')->where($field, 'NOT LIKE', 'CS%'); 959 break; 960 961 case 'D': 962 $query->where($field, 'LIKE', 'D%')->where($field, 'NOT LIKE', 'DZ%'); 963 break; 964 965 case 'DZ': 966 $query->where($field, 'LIKE', 'DZ%')->where($field, 'NOT LIKE', 'DZS%'); 967 break; 968 969 case 'G': 970 $query->where($field, 'LIKE', 'G%')->where($field, 'NOT LIKE', 'GY%'); 971 break; 972 973 case 'L': 974 $query->where($field, 'LIKE', 'L%')->where($field, 'NOT LIKE', 'LY%'); 975 break; 976 977 case 'N': 978 $query->where($field, 'LIKE', 'N%')->where($field, 'NOT LIKE', 'NY%'); 979 break; 980 981 case 'S': 982 $query->where($field, 'LIKE', 'S%')->where($field, 'NOT LIKE', 'SZ%'); 983 break; 984 985 case 'T': 986 $query->where($field, 'LIKE', 'T%')->where($field, 'NOT LIKE', 'TY%'); 987 break; 988 989 case 'Z': 990 $query->where($field, 'LIKE', 'Z%')->where($field, 'NOT LIKE', 'ZS%'); 991 break; 992 993 default: 994 $query->where($field, 'LIKE', '\\' . $letter . '%'); 995 break; 996 } 997 } 998 999 /** 1000 * In Norwegian and Danish, AA gets listed under Å, NOT A 1001 * 1002 * @param Builder $query 1003 * @param Expression $field 1004 * @param string $letter 1005 */ 1006 protected function whereInitialNorwegian(Builder $query, Expression $field, string $letter): void 1007 { 1008 switch ($letter) { 1009 case 'A': 1010 $query->where($field, 'LIKE', 'A%')->where($field, 'NOT LIKE', 'AA%'); 1011 break; 1012 1013 case 'Å': 1014 $query->where(static function (Builder $query) use ($field): void { 1015 $query 1016 ->where($field, 'LIKE', 'Å%') 1017 ->orWhere($field, 'LIKE', 'AA%'); 1018 }); 1019 break; 1020 1021 default: 1022 $query->where($field, 'LIKE', '\\' . $letter . '%'); 1023 break; 1024 } 1025 } 1026 1027 /** 1028 * In Swedish and Finnish, AA gets listed under A, NOT Å (even though Swedish collation says they should). 1029 * 1030 * @param Builder $query 1031 * @param Expression $field 1032 * @param string $letter 1033 */ 1034 protected function whereInitialSwedish(Builder $query, Expression $field, string $letter): void 1035 { 1036 if ($letter === 'Å') { 1037 $query->where($field, 'LIKE', 'Å%')->where($field, 'NOT LIKE', 'AA%'); 1038 } else { 1039 $query->where($field, 'LIKE', '\\' . $letter . '%'); 1040 } 1041 } 1042} 1043